Description:
fixed bug: increament of number on old records in submission git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@98 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

r46:c9ffe114dd77 - - 1 file changed: 3 inserted, 2 deleted

@@ -1,61 +1,61
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
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 => 'submitted_at 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 protected
52 protected
53
53
54 def self.find_option_in_source(option, source)
54 def self.find_option_in_source(option, source)
55 if source==nil
55 if source==nil
56 return nil
56 return nil
57 end
57 end
58 i = 0
58 i = 0
59 source.each_line do |s|
59 source.each_line do |s|
60 if s =~ option
60 if s =~ option
61 words = s.split
61 words = s.split
@@ -66,54 +66,55
66 return nil
66 return nil
67 end
67 end
68 end
68 end
69 return nil
69 return nil
70 end
70 end
71
71
72 def self.find_language_in_source(source)
72 def self.find_language_in_source(source)
73 langopt = find_option_in_source(/^LANG:/,source)
73 langopt = find_option_in_source(/^LANG:/,source)
74 if language = Language.find_by_name(langopt)
74 if language = Language.find_by_name(langopt)
75 return language
75 return language
76 elsif language = Language.find_by_pretty_name(langopt)
76 elsif language = Language.find_by_pretty_name(langopt)
77 return language
77 return language
78 else
78 else
79 return nil
79 return nil
80 end
80 end
81 end
81 end
82
82
83 def self.find_problem_in_source(source)
83 def self.find_problem_in_source(source)
84 prob_opt = find_option_in_source(/^TASK:/,source)
84 prob_opt = find_option_in_source(/^TASK:/,source)
85 if problem = Problem.find_by_name(prob_opt)
85 if problem = Problem.find_by_name(prob_opt)
86 return problem
86 return problem
87 else
87 else
88 return nil
88 return nil
89 end
89 end
90 end
90 end
91
91
92 # validation codes
92 # validation codes
93 def must_specify_language
93 def must_specify_language
94 return if self.source==nil
94 return if self.source==nil
95 self.language = Submission.find_language_in_source(self.source)
95 self.language = Submission.find_language_in_source(self.source)
96 errors.add_to_base("must specify programming language") unless self.language!=nil
96 errors.add_to_base("must specify programming language") unless self.language!=nil
97 end
97 end
98
98
99 def must_have_valid_problem
99 def must_have_valid_problem
100 return if self.source==nil
100 return if self.source==nil
101 if self.problem_id!=-1
101 if self.problem_id!=-1
102 problem = Problem.find(self.problem_id)
102 problem = Problem.find(self.problem_id)
103 else
103 else
104 problem = Submission.find_problem_in_source(self.source)
104 problem = Submission.find_problem_in_source(self.source)
105 end
105 end
106 if problem==nil
106 if problem==nil
107 errors.add_to_base("must specify problem")
107 errors.add_to_base("must specify problem")
108 elsif !problem.available
108 elsif !problem.available
109 errors.add_to_base("must specify valid problem")
109 errors.add_to_base("must specify valid problem")
110 end
110 end
111 end
111 end
112
112
113 # callbacks
113 # callbacks
114 - def assign_latest_number
114 + def assign_latest_number_if_new_recond
115 + return if !self.new_record?
115 latest = Submission.find_last_by_user_and_problem(self.user_id, self.problem_id)
116 latest = Submission.find_last_by_user_and_problem(self.user_id, self.problem_id)
116 self.number = (latest==nil) ? 1 : latest.number + 1;
117 self.number = (latest==nil) ? 1 : latest.number + 1;
117 end
118 end
118
119
119 end
120 end
You need to be logged in to leave comments. Login now