Skip to content

Commit

Permalink
Merge pull request alaashafaee#546 from alaashafaee/C5_Mohab_468_Cont…
Browse files Browse the repository at this point in the history
…est_Standings_5.25

Pull Request: C5_Mohab_468_Contest_Standings_5.25
  • Loading branch information
mohamedsaeed93 committed May 18, 2014
2 parents 551df6c + 065d9b5 commit a7ef398
Show file tree
Hide file tree
Showing 16 changed files with 405 additions and 24 deletions.
2 changes: 2 additions & 0 deletions tutor/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ gem 'json_builder', '3.1.0'
# whenever gem for scheduling tasks
gem 'whenever', :require => false

gem 'table_print'

#pagination
gem 'will_paginate', '~> 3.0.5'

Expand Down
2 changes: 2 additions & 0 deletions tutor/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ GEM
activesupport (>= 3.0)
sprockets (~> 2.8)
sqlite3 (1.3.9)
table_print (1.5.1)
thor (0.19.1)
thread_safe (0.3.3)
tilt (1.4.1)
Expand Down Expand Up @@ -236,6 +237,7 @@ DEPENDENCIES
sass-rails (~> 4.0.2)
sdoc
sqlite3
table_print
tire
uglifier (>= 1.3.0)
unicorn
Expand Down
3 changes: 3 additions & 0 deletions tutor/app/assets/javascripts/contests.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
4 changes: 0 additions & 4 deletions tutor/app/assets/stylesheets/contests.css

This file was deleted.

66 changes: 66 additions & 0 deletions tutor/app/assets/stylesheets/contests.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#countdown {
width: 240px;
height: 50px;
}

.solution-accepted {
background: #A9F5A9;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
text-align: center
}

.wrong-answer {
background: #F08080;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
text-align : center;
}

.contest_table {
background: #f9f9f9;
text-align: center;
margin-top: 20px;
}

.contest_information h2 {
width: 30%;
margin: auto;
}

.contest_information {
text-align : center !important;
}

.contest_information ul {
list-style: none;
border-bottom: 1px solid #A0A4A6;
}

.contest_information ul li {
display: inline;
padding: 20px;
}

.contest_note{
font-weight: bold;
color: #75706B;
}

.box-example {
width :50px;
height: 20px;
background-color: #BABABA;
text-align: center;
border-radius: 5px;
float: left;
font-weight: bold;
}

.box-note {
float: left;
margin-left: 5px;
color: #75706B;
}
9 changes: 9 additions & 0 deletions tutor/app/controllers/contests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ class ContestsController < ApplicationController
before_action :validate_timer, :except => ['new', 'create', 'edit', 'update',
'destroy', 'add_problems', 'add']

# [Contest Standings - Story 5.25]
# Returns the contest object whose id is params[:id]
# Parameters: none
# Returns: none
# Author: Mohab Ghanim
def standings
@contest = Contest.find_by_id(params[:id])
end

# [Edit Contest - Story 5.17]
# Passes instance variable of the contest to be edited to the edit
# view and renders the edit view
Expand Down
2 changes: 2 additions & 0 deletions tutor/app/helpers/contest_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ContestHelper
end
30 changes: 29 additions & 1 deletion tutor/app/models/contest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,34 @@ class Contest < ActiveRecord::Base

#Methods

# [Contest Standings - Story 5.25]
# Sorts students in contest according to the number of correct problems
# they solved and the total time of submission
# Parameters: none
# Returns: Sorted Hash containing ContestProgress of students
# Author: Mohab Ghanim
def get_contest_standings
contest_standing_records = ContestProgress.where(contest_id: self.id)
contest_standings_unique = Hash.new
contest_standing_records.each do |record|
wrong_answers = ContestProgress.where(contest_id: self.id,
student_id: record.student.id,
status: false).count
time_spent = 0
ContestProgress.where(contest_id: self.id,
student_id: record.student.id).each do |progress|
updated_at = ContestProgress.where(contest_id: self.id, cproblem_id:
progress.cproblem_id, student_id: record.student.id).take.updated_at
start_time = Contest.find(self.id).start_time
time_spent = time_spent + ((updated_at - start_time)/60).to_i
end
contest_standings_unique[record.student_id] = {wrong_answers: wrong_answers,
record: record, time_spent: time_spent}
end
return Hash[contest_standings_unique.sort_by {|key, value| [value[:wrong_answers],
value[:time_spent]]}.to_a]
end

# [Create Contest - Story 5.16]
# Validates that the contest end date/time is after its
# start date/time
Expand Down Expand Up @@ -52,4 +80,4 @@ def validate_future_start_time
end
end

end
end
2 changes: 1 addition & 1 deletion tutor/app/models/contest_progress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ContestProgress < ActiveRecord::Base
#Relations
belongs_to :contest
belongs_to :student
belongs_to :problem, class_name:"Cproblem"
belongs_to :problem, class_name: "Cproblem", foreign_key: :problem_id

#Scoops

Expand Down
2 changes: 1 addition & 1 deletion tutor/app/views/contests/_show_timer.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<center>
<%= content_tag "div", id: "temp", data: {timer: @timer, del: @del} do %>
<% end %>
<h2><%= @message %></h2>
<h4><%= @message %></h4>
<div id="countdown" />
</center>
79 changes: 79 additions & 0 deletions tutor/app/views/contests/standings.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<!-- Author : Mohab Ghanim -->
<div class="contest_information">
<h2> Contest: <%= @contest.title %></h2>
<h3><%= @contest.description %></h3>
<h4><% if Time.now < @contest.end_time %>
<% if Time.now < @contest.start_time %>
Not started yet
<% else %>
<br>
<%= render "contests/show_timer" %>
<br>
<%end%>
<% else %>
expired
<p> Any new progress will not be saved </p>
<% end %>
</h4>
<h5><% if Time.now < @contest.start_time %>
Starts at
<% else %>
Started at
<%end%>
<%= @contest.start_time %>
</h5>
<h5>Ends at <%= @contest.end_time %></h5>
<ul>
<li><a href ="/contests/<%= @contest.id %>/registrants">Registrants</a></li>
</ul>
</div>
<table class="table table-bordered contest_table">
<% rank = 0 %>
<tr>
<td> Rank </td>
<td> Contestant </td>
<% @contest.problems.each do |problem| %>
<td> <a href ="/cproblems/<%= problem.id %>"><%= problem.title %></a></td>
<% end %>
</tr>
<% if @contest.get_contest_standings.count != 0 %>
<% @contest.get_contest_standings.each do |key, value| %>
<tr>
<td>
<%= rank += 1 %>
</td>
<td>
<%= link_to value[:record].student.name,
controller: "/students",
action: "show",
id: value[:record].student.id %>
</td>
<% @contest.problems.each do |problem| %>
<% trials = ContestProgress.where(contest_id:
@contest.id, cproblem_id: problem.id,
student_id: value[:record].student.id).take.trials %>
<% if ContestProgress.where(contest_id: @contest.id , cproblem_id: problem.id,
student_id: value[:record].student.id).take.status %>
<% time_taken = ((ContestProgress.where(contest_id: @contest.id,
cproblem_id: problem.id, student_id: value[:record].student.id).
take.updated_at - Contest.find(@contest.id).
start_time)/60).to_i %>
<td class="solution-accepted"> <%= trials %>/<%=time_taken %> </td>
<% else %>
<td class="wrong-answer"> <%= trials %> /-- </td>
<% end %>
<% end %>
</tr>
<% end %>
<% else %>
<tr><td colspan=<%= @contest.problems.count + 2 %> ><h5> No solved problems yet </h5></td></tr>
<% end %>
</table>
<div class="contest_note">
<div class="box-example">
X/Y
</div>
<div class="box-note">
X : number of trials until the first success, Y : submission time - contest start time
</div>
</div>
1 change: 1 addition & 0 deletions tutor/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
post '/posts/:id' => 'posts#update'
post 'tracks/insert_recommendation' => 'tracks#insert_recommendation'
post 'debuggers/:id' => 'debuggers#start'
get 'contests/:id/standings' => 'contests#standings'
post 'contests/add/:id' => 'contests#add'

get 'problems/edit'
Expand Down
58 changes: 41 additions & 17 deletions tutor/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -606,19 +606,27 @@

puts("# ----------------------- Contests ----------------------- ")
Contest.create(title: "Iteration", description: "If you can solve this you will get a level up",
incomplete: false, start_time: Time.now + 2.days, end_time: Time.now + 7.days)
incomplete: false, start_time: Time.now + 1.second, end_time: Time.now + 7.days)
Contest.create(title: "Recursion", description: "If you can solve this you will get 2 level up",
incomplete: false, start_time: Time.now + 3.days, end_time: Time.now + 10.days)
incomplete: false, start_time: Time.now + 1.days, end_time: Time.now + 10.days)
Contest.create(title: "DB", description: "If you can solve this you will get 4 level up",
incomplete: false, start_time: Time.now + 5.days, end_time: Time.now + 12.days)
Contest.create(title: "DFS", description: "If you can solve this you will get 28 level up",
incomplete: false, start_time: Time.now + 6.days, end_time: Time.now + 19.days)

puts("# ----------------------- Sleeping ----------------------- ")
sleep 3
puts("# ----------------------- Finished ----------------------- ")
puts("# ----------------------- Contests ----------------------- ")
ContestProgress.create!(status:true)
ContestProgress.create!(status:true)
ContestProgress.create!(status:true)
ContestProgress.create!(status:false)
ContestProgress.create!(status:true, trials: 1)
ContestProgress.create!(status:false, trials: 2)
ContestProgress.create!(status:true, trials: 3)
ContestProgress.create!(status:false, trials: 4)
ContestProgress.create!(status:true, trials: 5)
ContestProgress.create!(status:false, trials: 6)
ContestProgress.create!(status:false, trials: 7)
ContestProgress.create!(status:true, trials: 8)
ContestProgress.create!(status:true, trials: 9)

puts("# ----------------------- Assignments ----------------------- ")
Assignment.create(title:"DSD Assignment_1", publish: true, due_date: Date.new(2009,6,13),
Expand Down Expand Up @@ -736,13 +744,19 @@
## Contests
Student.first.contests << Contest.first
Student.find_by_id(2).contests << Contest.first
Student.find_by_id(3).contests << Contest.find_by_id(2)
Student.find_by_id(4).contests << Contest.find_by_id(3)
Student.find_by_id(3).contests << Contest.first
Student.find_by_id(4).contests << Contest.first
## Contests progress
Student.first.contest_progresses << ContestProgress.first
Student.first.contest_progresses << ContestProgress.find_by_id(2)
Student.find_by_id(2).contest_progresses << ContestProgress.find_by_id(3)
Student.first.contest_progresses << ContestProgress.find_by_id(3)
Student.find_by_id(2).contest_progresses << ContestProgress.find_by_id(4)
Student.find_by_id(2).contest_progresses << ContestProgress.find_by_id(5)
Student.find_by_id(2).contest_progresses << ContestProgress.find_by_id(6)
Student.find_by_id(3).contest_progresses << ContestProgress.find_by_id(7)
Student.find_by_id(3).contest_progresses << ContestProgress.find_by_id(8)
Student.find_by_id(3).contest_progresses << ContestProgress.find_by_id(9)

## Grades
Student.first.grades << Grade.first
Student.first.grades << Grade.find_by_id(2)
Expand Down Expand Up @@ -789,6 +803,7 @@
AssignmentProblem.first.solutions << Solution.find_by_id(4)
Problem.find_by_id(2).solutions << Solution.find_by_id(2)
Problem.find_by_id(3).solutions << Solution.find_by_id(3)

## Attempts
Problem.first.attempts << Attempt.first
Problem.first.attempts << Attempt.find_by_id(2)
Expand All @@ -805,10 +820,15 @@
Problem.first.attempts << Attempt.find_by_id(13)
## Contest Progress
Cproblem.find_by_id(1).contests_progresses << ContestProgress.first
Cproblem.find_by_id(1).contests_progresses << ContestProgress.find_by_id(2)
Cproblem.find_by_id(2).contests_progresses << ContestProgress.find_by_id(3)
Cproblem.find_by_id(2).contests_progresses << ContestProgress.find_by_id(2)
Cproblem.find_by_id(3).contests_progresses << ContestProgress.find_by_id(3)
Cproblem.find_by_id(4).contests_progresses << ContestProgress.find_by_id(4)
Cproblem.find_by_id(1).contests_progresses << ContestProgress.find_by_id(4)
Cproblem.find_by_id(2).contests_progresses << ContestProgress.find_by_id(5)
Cproblem.find_by_id(3).contests_progresses << ContestProgress.find_by_id(6)
Cproblem.find_by_id(1).contests_progresses << ContestProgress.find_by_id(7)
Cproblem.find_by_id(2).contests_progresses << ContestProgress.find_by_id(8)
Cproblem.find_by_id(3).contests_progresses << ContestProgress.find_by_id(9)

## Hints
Problem.first.model_answers.first.hints << Hint.first
Problem.first.model_answers.first.hints << Hint.all.second
Expand Down Expand Up @@ -865,13 +885,17 @@
Contest.first.problems << Cproblem.find_by_id(1)
Contest.first.problems << Cproblem.find_by_id(2)
Contest.first.problems << Cproblem.find_by_id(3)
Contest.find_by_id(2).problems << Cproblem.find_by_id(4)
Contest.find_by_id(3).problems << Cproblem.find_by_id(5)

## Contests Progress
Contest.first.progress << ContestProgress.first
Contest.find_by_id(2).progress << ContestProgress.find_by_id(2)
Contest.find_by_id(2).progress << ContestProgress.find_by_id(3)
Contest.find_by_id(3).progress << ContestProgress.find_by_id(4)
Contest.first.progress << ContestProgress.find_by_id(2)
Contest.first.progress << ContestProgress.find_by_id(3)
Contest.first.progress << ContestProgress.find_by_id(4)
Contest.first.progress << ContestProgress.find_by_id(5)
Contest.first.progress << ContestProgress.find_by_id(6)
Contest.first.progress << ContestProgress.find_by_id(7)
Contest.first.progress << ContestProgress.find_by_id(8)
Contest.first.progress << ContestProgress.find_by_id(9)

puts("# ----------------------- Assignment ----------------------- ")
## Problems
Expand Down
Loading

0 comments on commit a7ef398

Please sign in to comment.