Skip to content
This repository has been archived by the owner on Dec 10, 2017. It is now read-only.

Commit

Permalink
leaderboard port
Browse files Browse the repository at this point in the history
  • Loading branch information
kacasey8 committed Jan 17, 2014
1 parent 3c12f42 commit b6f259e
Showing 6 changed files with 89 additions and 5 deletions.
38 changes: 38 additions & 0 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions app/controllers/rsvps_controller.rb
Original file line number Diff line number Diff line change
@@ -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|
16 changes: 16 additions & 0 deletions app/models/member_semester.rb
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions app/views/events/leaderboard.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<p> Scoring system: Big fun = 2 pts; Fun = 1 pt; Service = 3 pts
</p>

<span id="semester-nav">
<%= link_to "&larr;prev".html_safe, event_leaderboard_path(@semester.prev.id) unless @semester.prev.nil? %>
&nbsp;<%= @semester.name %>&nbsp;
<%= link_to "next&rarr;".html_safe, event_leaderboard_path(@semester.next.id) unless @semester.next.nil? %>
</span>

<table>
<tr>
<th>Rank</td>
<th>Name</th>
<th>Score</th>
<th>Big Fun</th>
<th>Fun</th>
<th>Service</th>
</tr>
<% @users_array.each do |entry| %>
<tr>
<td><%= entry[:rank] %></td>
<td><%= entry[:user].full_name %></td>
<td><%= entry[:score] %></td>
<td><%= entry[:big_fun] %></td>
<td><%= entry[:fun] %></td>
<td><%= entry[:service] %></td>
</tr>
<% end %>
</table>
2 changes: 1 addition & 1 deletion app/views/layouts/_officer_submenu.html.erb
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@
<li><%= link_to "My RSVPs", my_rsvps_path %></li>
<% if user_signed_in? && active_member_authorize %>
<li><%= link_to "New Event", new_event_path %></li>
<li><%= link_to "Leaderboard", '#' %></li>
<li><%= link_to "Leaderboard", event_leaderboard_path %></li>
<% end %>
</ul>
<% end %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -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)/}

0 comments on commit b6f259e

Please sign in to comment.