Description:
added number (auto generated when submitting) to submissions
git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@73 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r35:3d41a77d4876 - - 6 files changed: 58 inserted, 14 deleted
@@ -0,0 +1,33 | |||
|
1 | + class AddNumberToSubmissions < ActiveRecord::Migration | |
|
2 | + def self.up | |
|
3 | + add_column :submissions, :number, :integer | |
|
4 | + | |
|
5 | + # add number field for all records | |
|
6 | + Submission.reset_column_information | |
|
7 | + | |
|
8 | + last_user_id = nil | |
|
9 | + last_problem_id = nil | |
|
10 | + current_number = 0 | |
|
11 | + | |
|
12 | + Submission.find(:all, | |
|
13 | + :order => 'user_id, problem_id, submitted_at').each do |submission| | |
|
14 | + if submission.user_id==last_user_id and submission.problem_id==last_problem_id | |
|
15 | + current_number += 1 | |
|
16 | + else | |
|
17 | + current_number = 1 | |
|
18 | + end | |
|
19 | + submission.number = current_number | |
|
20 | + submission.save | |
|
21 | + | |
|
22 | + last_user_id = submission.user_id | |
|
23 | + last_problem_id = submission.problem_id | |
|
24 | + end | |
|
25 | + | |
|
26 | + add_index :submissions, [:user_id, :problem_id, :number], :unique => true | |
|
27 | + end | |
|
28 | + | |
|
29 | + def self.down | |
|
30 | + remove_index :submissions, :column => [:user_id, :problem_id, :number] | |
|
31 | + remove_column :submissions, :number | |
|
32 | + end | |
|
33 | + end |
@@ -57,8 +57,12 | |||
|
57 | 57 | @prob_submissions = Array.new |
|
58 | 58 | @user = User.find(session[:user_id]) |
|
59 | 59 | @problems.each do |p| |
|
60 |
- |
|
|
61 | - @prob_submissions << { :count => c, :submission => sub } | |
|
60 | + sub = Submission.find_last_by_user_and_problem(@user.id,p.id) | |
|
61 | + if sub!=nil | |
|
62 | + @prob_submissions << { :count => sub.number, :submission => sub } | |
|
63 | + else | |
|
64 | + @prob_submissions << { :count => 0, :submission => nil } | |
|
65 | + end | |
|
62 | 66 | end |
|
63 | 67 | end |
|
64 | 68 |
@@ -65,6 +65,6 | |||
|
65 | 65 | |
|
66 | 66 | def stat |
|
67 | 67 | @problem = Problem.find(params[:id]) |
|
68 | - @submissions = Submission.find_last_by_problem(params[:id]) | |
|
68 | + @submissions = Submission.find_all_last_by_problem(params[:id]) | |
|
69 | 69 | end |
|
70 | 70 | end |
@@ -77,8 +77,8 | |||
|
77 | 77 | ustat = Array.new |
|
78 | 78 | ustat[0] = u.login |
|
79 | 79 | @problems.each do |p| |
|
80 |
- |
|
|
81 |
- if ( |
|
|
80 | + sub = Submission.find_last_by_user_and_problem(u.id,p.id) | |
|
81 | + if (sub!=nil) and (sub.points!=nil) | |
|
82 | 82 | ustat << [sub.points, (sub.points>=p.full_score)] |
|
83 | 83 | else |
|
84 | 84 | ustat << [0,false] |
@@ -10,20 +10,17 | |||
|
10 | 10 | validate :must_specify_language |
|
11 | 11 | validate :must_have_valid_problem |
|
12 | 12 | |
|
13 | - def self.find_by_user_and_problem(user_id, problem_id) | |
|
14 | - subcount = count(:conditions => "user_id = #{user_id} AND problem_id = #{problem_id}") | |
|
15 | - if subcount != 0 | |
|
13 | + before_save :assign_latest_number | |
|
14 | + | |
|
15 | + def self.find_last_by_user_and_problem(user_id, problem_id) | |
|
16 | 16 |
|
|
17 | 17 |
|
|
18 | 18 |
|
|
19 | 19 |
|
|
20 | - else | |
|
21 | - last_sub = nil | |
|
22 | - end | |
|
23 | - return subcount, last_sub | |
|
20 | + return last_sub | |
|
24 | 21 | end |
|
25 | 22 | |
|
26 | - def self.find_last_by_problem(problem_id) | |
|
23 | + def self.find_all_last_by_problem(problem_id) | |
|
27 | 24 | # need to put in SQL command, maybe there's a better way |
|
28 | 25 | Submission.find_by_sql("SELECT * FROM submissions " + |
|
29 | 26 | "WHERE id = " + |
@@ -33,6 +30,8 | |||
|
33 | 30 | "GROUP BY user_id)") |
|
34 | 31 | end |
|
35 | 32 | |
|
33 | + protected | |
|
34 | + | |
|
36 | 35 | def self.find_option_in_source(option, source) |
|
37 | 36 | if source==nil |
|
38 | 37 | return nil |
@@ -92,4 +91,10 | |||
|
92 | 91 | end |
|
93 | 92 | end |
|
94 | 93 | |
|
94 | + # callbacks | |
|
95 | + def assign_latest_number | |
|
96 | + latest = Submission.find_last_by_user_and_problem(self.user_id, self.problem_id) | |
|
97 | + self.number = (latest==nil) ? 1 : latest.number + 1; | |
|
95 | 98 | end |
|
99 | + | |
|
100 | + end |
@@ -9,7 +9,7 | |||
|
9 | 9 | # |
|
10 | 10 | # It's strongly recommended to check this file into your version control system. |
|
11 | 11 | |
|
12 |
- ActiveRecord::Schema.define(:version => 1 |
|
|
12 | + ActiveRecord::Schema.define(:version => 18) do | |
|
13 | 13 | |
|
14 | 14 | create_table "grader_processes", :force => true do |t| |
|
15 | 15 | t.string "host", :limit => 20 |
@@ -83,8 +83,10 | |||
|
83 | 83 | t.datetime "graded_at" |
|
84 | 84 | t.integer "points" |
|
85 | 85 | t.text "grader_comment" |
|
86 | + t.integer "number" | |
|
86 | 87 | end |
|
87 | 88 | |
|
89 | + add_index "submissions", ["user_id", "problem_id", "number"], :name => "index_submissions_on_user_id_and_problem_id_and_number", :unique => true | |
|
88 | 90 | add_index "submissions", ["user_id", "problem_id"], :name => "index_submissions_on_user_id_and_problem_id" |
|
89 | 91 | |
|
90 | 92 | create_table "tasks", :force => true do |t| |
You need to be logged in to leave comments.
Login now