diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 5d57457..416f901 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -170,6 +170,44 @@ def confirm_rsvps @rsvps = @event.rsvps.joins(:user).where("users.id in (?)", users).sort_by { |rsvp| rsvp.user.full_name } end + def leaderboard + @semester = params[:semester] ? MemberSemester.find_by_id(params[:semester]) : MemberSemester.current + @users = Role.members.semester_filter(@semester).all_users + @users_array = [] + #moar_people = [ 'eunjian' ].collect{|u|Person.find_by_username(u)} # TODO remove when we have leaderboard opt-in + @users.each do |user| + # Makeshift data structure + events = user.rsvps.where(confirmed: 't').joins(:event).where("events.start_time > ? AND events.start_time < ?", @semester.start_time, @semester.end_time) + @users_array << { + :user => user, + :total => events.count, + :events => events + } + end + + @users_array.each do |entry| + entry[:big_fun] = entry[:events].where("events.event_type = ? ", "Big Fun").count + entry[:fun] = entry[:events].where("events.event_type = ? ", "Fun").count + entry[:service] = entry[:events].where("events.event_type = ? ", "Service").count + entry[:score] = 2*entry[:big_fun] + entry[:fun] + 3*entry[:service] + end + + @users_array.sort!{|a,b| a[:score] <=> b[:score]} + @users_array.reverse! + rank = 0 + last_num = -1 + incr = 1 + @users_array.each do |entry| + if last_num != entry[:score] + rank += incr + last_num = entry[:score] + incr = 0 + end + entry[:rank] = rank + incr += 1 + end + end + private def event_authorize @event_auth = comm_authorize diff --git a/app/controllers/rsvps_controller.rb b/app/controllers/rsvps_controller.rb index 8f6078c..b414315 100644 --- a/app/controllers/rsvps_controller.rb +++ b/app/controllers/rsvps_controller.rb @@ -92,10 +92,10 @@ def destroy def confirm - role = params[:role] || :candidate + role = params[:role] || :candidates respond_to do |format| - if @rsvp.update_attribute :confirmed, Rsvp::Confirmed + if @rsvp.update_attributes(confirmed: Rsvp::Confirmed, confirmed_by: current_user.id, confirmed_at: Time.now) format.html { redirect_to(confirm_rsvps_path(@rsvp.event_id, :role => role), :notice => 'Rsvp was confirmed.') } format.xml { render :xml => @rsvp } else @@ -106,7 +106,7 @@ def confirm end def unconfirm - @rsvp.update_attribute :confirmed, Rsvp::Unconfirmed + @rsvp.update_attributes(confirmed: Rsvp::Unconfirmed, confirmed_by: current_user.id, confirmed_at: Time.now) role = params[:role] || "candidates" respond_to do |format| @@ -116,7 +116,7 @@ def unconfirm end def reject - @rsvp.update_attribute :confirmed, Rsvp::Rejected + @rsvp.update_attributes(confirmed: Rsvp::Rejected, confirmed_by: current_user.id, confirmed_at: Time.now) role = params[:role] || "candidates" respond_to do |format| diff --git a/app/models/member_semester.rb b/app/models/member_semester.rb index e01e9ed..23871d6 100644 --- a/app/models/member_semester.rb +++ b/app/models/member_semester.rb @@ -57,4 +57,20 @@ def prev MemberSemester.find_by_year_and_season(year, "Spring") end end + + def start_time + if season == "Spring" + Date.civil(year) + else + Date.civil(year, 8) + end + end + + def end_time + if season == "Spring" + Date.civil(year, 6) + else + Date.civil(year, 12, 30) + end + end end diff --git a/app/views/events/leaderboard.html.erb b/app/views/events/leaderboard.html.erb new file mode 100644 index 0000000..09b5e62 --- /dev/null +++ b/app/views/events/leaderboard.html.erb @@ -0,0 +1,29 @@ +

Scoring system: Big fun = 2 pts; Fun = 1 pt; Service = 3 pts +

+ + +<%= link_to "←prev".html_safe, event_leaderboard_path(@semester.prev.id) unless @semester.prev.nil? %> + <%= @semester.name %>  +<%= link_to "next→".html_safe, event_leaderboard_path(@semester.next.id) unless @semester.next.nil? %> + + + + + + + + + + +<% @users_array.each do |entry| %> + + + + + + + + +<% end %> +
Rank + NameScoreBig FunFunService
<%= entry[:rank] %><%= entry[:user].full_name %><%= entry[:score] %><%= entry[:big_fun] %><%= entry[:fun] %><%= entry[:service] %>
\ No newline at end of file diff --git a/app/views/layouts/_officer_submenu.html.erb b/app/views/layouts/_officer_submenu.html.erb index 2d24219..712a97f 100644 --- a/app/views/layouts/_officer_submenu.html.erb +++ b/app/views/layouts/_officer_submenu.html.erb @@ -51,7 +51,7 @@
  • <%= link_to "My RSVPs", my_rsvps_path %>
  • <% if user_signed_in? && active_member_authorize %>
  • <%= link_to "New Event", new_event_path %>
  • -
  • <%= link_to "Leaderboard", '#' %>
  • +
  • <%= link_to "Leaderboard", event_leaderboard_path %>
  • <% end %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index 14a45d3..3dabc9a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -38,6 +38,7 @@ resources :users, except: [:new, :create, :index] scope "events" do + match "leaderboard(/:semester)", to: "events#leaderboard", via: :get, as: :event_leaderboard match "rsvps", to: "rsvps#my_rsvps", via: :get, as: :my_rsvps match "calendar", to: "events#calendar", via: :get, as: "events_calendar" match ":category", to: "events#index", via: :get, as: :events_category, constraints: {:category => /(future|past)/}