Description:
added problem identification from source filename git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@364 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

r167:e09937aa9f90 - - 1 file changed: 7 inserted, 2 deleted

@@ -2,151 +2,156
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 before_validation :assign_problem
7 before_validation :assign_problem
8 before_validation :assign_language
8 before_validation :assign_language
9
9
10 validates_presence_of :source
10 validates_presence_of :source
11 validates_length_of :source, :maximum => 100_000, :allow_blank => true, :message => 'too long'
11 validates_length_of :source, :maximum => 100_000, :allow_blank => true, :message => 'too long'
12 validates_length_of :source, :minimum => 1, :allow_blank => true, :message => 'too short'
12 validates_length_of :source, :minimum => 1, :allow_blank => true, :message => 'too short'
13 validate :must_have_valid_problem
13 validate :must_have_valid_problem
14 validate :must_specify_language
14 validate :must_specify_language
15
15
16 before_save :assign_latest_number_if_new_recond
16 before_save :assign_latest_number_if_new_recond
17
17
18 def self.find_last_by_user_and_problem(user_id, problem_id)
18 def self.find_last_by_user_and_problem(user_id, problem_id)
19 last_sub = find(:first,
19 last_sub = find(:first,
20 :conditions => {:user_id => user_id,
20 :conditions => {:user_id => user_id,
21 :problem_id => problem_id},
21 :problem_id => problem_id},
22 :order => 'number DESC')
22 :order => 'number DESC')
23 return last_sub
23 return last_sub
24 end
24 end
25
25
26 def self.find_all_last_by_problem(problem_id)
26 def self.find_all_last_by_problem(problem_id)
27 # need to put in SQL command, maybe there's a better way
27 # need to put in SQL command, maybe there's a better way
28 Submission.find_by_sql("SELECT * FROM submissions " +
28 Submission.find_by_sql("SELECT * FROM submissions " +
29 "WHERE id = " +
29 "WHERE id = " +
30 "(SELECT MAX(id) FROM submissions AS subs " +
30 "(SELECT MAX(id) FROM submissions AS subs " +
31 "WHERE subs.user_id = submissions.user_id AND " +
31 "WHERE subs.user_id = submissions.user_id AND " +
32 "problem_id = " + problem_id.to_s + " " +
32 "problem_id = " + problem_id.to_s + " " +
33 "GROUP BY user_id) " +
33 "GROUP BY user_id) " +
34 "ORDER BY user_id")
34 "ORDER BY user_id")
35 end
35 end
36
36
37 def self.find_last_for_all_available_problems(user_id)
37 def self.find_last_for_all_available_problems(user_id)
38 submissions = Array.new
38 submissions = Array.new
39 problems = Problem.find_available_problems
39 problems = Problem.find_available_problems
40 problems.each do |problem|
40 problems.each do |problem|
41 sub = Submission.find_last_by_user_and_problem(user_id, problem.id)
41 sub = Submission.find_last_by_user_and_problem(user_id, problem.id)
42 submissions << sub if sub!=nil
42 submissions << sub if sub!=nil
43 end
43 end
44 submissions
44 submissions
45 end
45 end
46
46
47 def self.find_by_user_problem_number(user_id, problem_id, number)
47 def self.find_by_user_problem_number(user_id, problem_id, number)
48 Submission.find(:first,
48 Submission.find(:first,
49 :conditions => {
49 :conditions => {
50 :user_id => user_id,
50 :user_id => user_id,
51 :problem_id => problem_id,
51 :problem_id => problem_id,
52 :number => number
52 :number => number
53 })
53 })
54 end
54 end
55
55
56 def self.find_all_by_user_problem(user_id, problem_id)
56 def self.find_all_by_user_problem(user_id, problem_id)
57 Submission.find(:all,
57 Submission.find(:all,
58 :conditions => {
58 :conditions => {
59 :user_id => user_id,
59 :user_id => user_id,
60 :problem_id => problem_id,
60 :problem_id => problem_id,
61 })
61 })
62 end
62 end
63
63
64 protected
64 protected
65
65
66 def self.find_option_in_source(option, source)
66 def self.find_option_in_source(option, source)
67 if source==nil
67 if source==nil
68 return nil
68 return nil
69 end
69 end
70 i = 0
70 i = 0
71 source.each_line do |s|
71 source.each_line do |s|
72 if s =~ option
72 if s =~ option
73 words = s.split
73 words = s.split
74 return words[1]
74 return words[1]
75 end
75 end
76 i = i + 1
76 i = i + 1
77 if i==10
77 if i==10
78 return nil
78 return nil
79 end
79 end
80 end
80 end
81 return nil
81 return nil
82 end
82 end
83
83
84 def self.find_language_in_source(source, source_filename="")
84 def self.find_language_in_source(source, source_filename="")
85 langopt = find_option_in_source(/^LANG:/,source)
85 langopt = find_option_in_source(/^LANG:/,source)
86 if langopt
86 if langopt
87 return (Language.find_by_name(langopt) ||
87 return (Language.find_by_name(langopt) ||
88 Language.find_by_pretty_name(langopt))
88 Language.find_by_pretty_name(langopt))
89 else
89 else
90 if source_filename
90 if source_filename
91 return Language.find_by_extension(source_filename.split('.').last)
91 return Language.find_by_extension(source_filename.split('.').last)
92 else
92 else
93 return nil
93 return nil
94 end
94 end
95 end
95 end
96 end
96 end
97
97
98 - def self.find_problem_in_source(source)
98 + def self.find_problem_in_source(source, source_filename="")
99 prob_opt = find_option_in_source(/^TASK:/,source)
99 prob_opt = find_option_in_source(/^TASK:/,source)
100 if problem = Problem.find_by_name(prob_opt)
100 if problem = Problem.find_by_name(prob_opt)
101 return problem
101 return problem
102 else
102 else
103 + if source_filename
104 + return Problem.find_by_name(source_filename.split('.').first)
105 + else
103 return nil
106 return nil
104 end
107 end
105 end
108 end
109 + end
106
110
107 def assign_problem
111 def assign_problem
108 if self.problem_id!=-1
112 if self.problem_id!=-1
109 begin
113 begin
110 self.problem = Problem.find(self.problem_id)
114 self.problem = Problem.find(self.problem_id)
111 rescue ActiveRecord::RecordNotFound
115 rescue ActiveRecord::RecordNotFound
112 self.problem = nil
116 self.problem = nil
113 end
117 end
114 else
118 else
115 - self.problem = Submission.find_problem_in_source(self.source)
119 + self.problem = Submission.find_problem_in_source(self.source,
120 + self.source_filename)
116 end
121 end
117 end
122 end
118
123
119 def assign_language
124 def assign_language
120 self.language = Submission.find_language_in_source(self.source,
125 self.language = Submission.find_language_in_source(self.source,
121 self.source_filename)
126 self.source_filename)
122 end
127 end
123
128
124 # validation codes
129 # validation codes
125 def must_specify_language
130 def must_specify_language
126 return if self.source==nil
131 return if self.source==nil
127
132
128 # for output_only tasks
133 # for output_only tasks
129 return if self.problem!=nil and self.problem.output_only
134 return if self.problem!=nil and self.problem.output_only
130
135
131 if self.language==nil
136 if self.language==nil
132 errors.add('source',"must specify programming language") unless self.language!=nil
137 errors.add('source',"must specify programming language") unless self.language!=nil
133 end
138 end
134 end
139 end
135
140
136 def must_have_valid_problem
141 def must_have_valid_problem
137 return if self.source==nil
142 return if self.source==nil
138 if self.problem==nil
143 if self.problem==nil
139 errors.add('problem',"must be specified.")
144 errors.add('problem',"must be specified.")
140 elsif (!self.problem.available) and (self.new_record?)
145 elsif (!self.problem.available) and (self.new_record?)
141 errors.add('problem',"must be valid.")
146 errors.add('problem',"must be valid.")
142 end
147 end
143 end
148 end
144
149
145 # callbacks
150 # callbacks
146 def assign_latest_number_if_new_recond
151 def assign_latest_number_if_new_recond
147 return if !self.new_record?
152 return if !self.new_record?
148 latest = Submission.find_last_by_user_and_problem(self.user_id, self.problem_id)
153 latest = Submission.find_last_by_user_and_problem(self.user_id, self.problem_id)
149 self.number = (latest==nil) ? 1 : latest.number + 1;
154 self.number = (latest==nil) ? 1 : latest.number + 1;
150 end
155 end
151
156
152 end
157 end
You need to be logged in to leave comments. Login now