Description:
[web] fixed submission numbering bugs: should sort by number instead of submitted_at because submitted_at can be equal git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@149 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r72:4bf745b7ef5b - - 1 file changed: 1 inserted, 1 deleted

@@ -1,67 +1,67
1 class Submission < ActiveRecord::Base
1 class Submission < ActiveRecord::Base
2
2
3 belongs_to :language
3 belongs_to :language
4 belongs_to :problem
4 belongs_to :problem
5 belongs_to :user
5 belongs_to :user
6
6
7 validates_presence_of :source
7 validates_presence_of :source
8 validates_length_of :source, :maximum => 100_000, :allow_blank => true, :message => 'too long'
8 validates_length_of :source, :maximum => 100_000, :allow_blank => true, :message => 'too long'
9 validates_length_of :source, :minimum => 1, :allow_blank => true, :message => 'too short'
9 validates_length_of :source, :minimum => 1, :allow_blank => true, :message => 'too short'
10 validate :must_specify_language
10 validate :must_specify_language
11 validate :must_have_valid_problem
11 validate :must_have_valid_problem
12
12
13 before_save :assign_latest_number_if_new_recond
13 before_save :assign_latest_number_if_new_recond
14
14
15 def self.find_last_by_user_and_problem(user_id, problem_id)
15 def self.find_last_by_user_and_problem(user_id, problem_id)
16 last_sub = find(:first,
16 last_sub = find(:first,
17 :conditions => {:user_id => user_id,
17 :conditions => {:user_id => user_id,
18 :problem_id => problem_id},
18 :problem_id => problem_id},
19 - :order => 'submitted_at DESC')
19 + :order => 'number DESC')
20 return last_sub
20 return last_sub
21 end
21 end
22
22
23 def self.find_all_last_by_problem(problem_id)
23 def self.find_all_last_by_problem(problem_id)
24 # need to put in SQL command, maybe there's a better way
24 # need to put in SQL command, maybe there's a better way
25 Submission.find_by_sql("SELECT * FROM submissions " +
25 Submission.find_by_sql("SELECT * FROM submissions " +
26 "WHERE id = " +
26 "WHERE id = " +
27 "(SELECT MAX(id) FROM submissions AS subs " +
27 "(SELECT MAX(id) FROM submissions AS subs " +
28 "WHERE subs.user_id = submissions.user_id AND " +
28 "WHERE subs.user_id = submissions.user_id AND " +
29 "problem_id = " + problem_id.to_s + " " +
29 "problem_id = " + problem_id.to_s + " " +
30 "GROUP BY user_id)")
30 "GROUP BY user_id)")
31 end
31 end
32
32
33 def self.find_last_for_all_available_problems(user_id)
33 def self.find_last_for_all_available_problems(user_id)
34 submissions = Array.new
34 submissions = Array.new
35 problems = Problem.find_available_problems
35 problems = Problem.find_available_problems
36 problems.each do |problem|
36 problems.each do |problem|
37 sub = Submission.find_last_by_user_and_problem(user_id, problem.id)
37 sub = Submission.find_last_by_user_and_problem(user_id, problem.id)
38 submissions << sub if sub!=nil
38 submissions << sub if sub!=nil
39 end
39 end
40 submissions
40 submissions
41 end
41 end
42
42
43 def self.find_by_user_problem_number(user_id, problem_id, number)
43 def self.find_by_user_problem_number(user_id, problem_id, number)
44 Submission.find(:first,
44 Submission.find(:first,
45 :conditions => {
45 :conditions => {
46 :user_id => user_id,
46 :user_id => user_id,
47 :problem_id => problem_id,
47 :problem_id => problem_id,
48 :number => number
48 :number => number
49 })
49 })
50 end
50 end
51
51
52 def self.find_all_by_user_problem(user_id, problem_id)
52 def self.find_all_by_user_problem(user_id, problem_id)
53 Submission.find(:all,
53 Submission.find(:all,
54 :conditions => {
54 :conditions => {
55 :user_id => user_id,
55 :user_id => user_id,
56 :problem_id => problem_id,
56 :problem_id => problem_id,
57 })
57 })
58 end
58 end
59
59
60 protected
60 protected
61
61
62 def self.find_option_in_source(option, source)
62 def self.find_option_in_source(option, source)
63 if source==nil
63 if source==nil
64 return nil
64 return nil
65 end
65 end
66 i = 0
66 i = 0
67 source.each_line do |s|
67 source.each_line do |s|
You need to be logged in to leave comments. Login now