Description:
shows problems availabe in contests
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r278:a0c2d497b31a - - 9 files changed: 224 inserted, 45 deleted
@@ -4,337 +4,383 | |||||
|
4 | before_filter :check_viewability, :except => [:index, :login] |
|
4 | before_filter :check_viewability, :except => [:index, :login] |
|
5 |
|
5 | ||
|
6 | append_before_filter :update_user_start_time, :except => [:index, :login] |
|
6 | append_before_filter :update_user_start_time, :except => [:index, :login] |
|
7 |
|
7 | ||
|
8 | # to prevent log in box to be shown when user logged out of the |
|
8 | # to prevent log in box to be shown when user logged out of the |
|
9 | # system only in some tab |
|
9 | # system only in some tab |
|
10 | prepend_before_filter :reject_announcement_refresh_when_logged_out, :only => [:announcements] |
|
10 | prepend_before_filter :reject_announcement_refresh_when_logged_out, :only => [:announcements] |
|
11 |
|
11 | ||
|
12 | # COMMENTED OUT: filter in each action instead |
|
12 | # COMMENTED OUT: filter in each action instead |
|
13 | # before_filter :verify_time_limit, :only => [:submit] |
|
13 | # before_filter :verify_time_limit, :only => [:submit] |
|
14 |
|
14 | ||
|
15 | verify :method => :post, :only => [:submit], |
|
15 | verify :method => :post, :only => [:submit], |
|
16 | :redirect_to => { :action => :index } |
|
16 | :redirect_to => { :action => :index } |
|
17 |
|
17 | ||
|
18 | # COMMENT OUT: only need when having high load |
|
18 | # COMMENT OUT: only need when having high load |
|
19 | # caches_action :index, :login |
|
19 | # caches_action :index, :login |
|
20 |
|
20 | ||
|
21 | # NOTE: This method is not actually needed, 'config/routes.rb' has |
|
21 | # NOTE: This method is not actually needed, 'config/routes.rb' has |
|
22 | # assigned action login as a default action. |
|
22 | # assigned action login as a default action. |
|
23 | def index |
|
23 | def index |
|
24 | redirect_to :action => 'login' |
|
24 | redirect_to :action => 'login' |
|
25 | end |
|
25 | end |
|
26 |
|
26 | ||
|
27 | def login |
|
27 | def login |
|
28 | saved_notice = flash[:notice] |
|
28 | saved_notice = flash[:notice] |
|
29 | reset_session |
|
29 | reset_session |
|
30 | flash.now[:notice] = saved_notice |
|
30 | flash.now[:notice] = saved_notice |
|
31 |
|
31 | ||
|
32 | # EXPERIMENT: |
|
32 | # EXPERIMENT: |
|
33 | # Hide login if in single user mode and the url does not |
|
33 | # Hide login if in single user mode and the url does not |
|
34 | # explicitly specify /login |
|
34 | # explicitly specify /login |
|
35 | # |
|
35 | # |
|
36 | # logger.info "PATH: #{request.path}" |
|
36 | # logger.info "PATH: #{request.path}" |
|
37 | # if Configuration['system.single_user_mode'] and |
|
37 | # if Configuration['system.single_user_mode'] and |
|
38 | # request.path!='/main/login' |
|
38 | # request.path!='/main/login' |
|
39 | # @hidelogin = true |
|
39 | # @hidelogin = true |
|
40 | # end |
|
40 | # end |
|
41 |
|
41 | ||
|
42 | @announcements = Announcement.find_for_frontpage |
|
42 | @announcements = Announcement.find_for_frontpage |
|
43 | render :action => 'login', :layout => 'empty' |
|
43 | render :action => 'login', :layout => 'empty' |
|
44 | end |
|
44 | end |
|
45 |
|
45 | ||
|
46 | def list |
|
46 | def list |
|
47 | prepare_list_information |
|
47 | prepare_list_information |
|
48 | end |
|
48 | end |
|
49 |
|
49 | ||
|
50 | def help |
|
50 | def help |
|
51 | @user = User.find(session[:user_id]) |
|
51 | @user = User.find(session[:user_id]) |
|
52 | end |
|
52 | end |
|
53 |
|
53 | ||
|
54 | def submit |
|
54 | def submit |
|
55 | user = User.find(session[:user_id]) |
|
55 | user = User.find(session[:user_id]) |
|
56 |
|
56 | ||
|
57 | @submission = Submission.new(params[:submission]) |
|
57 | @submission = Submission.new(params[:submission]) |
|
58 | @submission.user = user |
|
58 | @submission.user = user |
|
59 | @submission.language_id = 0 |
|
59 | @submission.language_id = 0 |
|
60 | if (params['file']) and (params['file']!='') |
|
60 | if (params['file']) and (params['file']!='') |
|
61 | @submission.source = params['file'].read |
|
61 | @submission.source = params['file'].read |
|
62 | @submission.source_filename = params['file'].original_filename |
|
62 | @submission.source_filename = params['file'].original_filename |
|
63 | end |
|
63 | end |
|
64 | @submission.submitted_at = Time.new.gmtime |
|
64 | @submission.submitted_at = Time.new.gmtime |
|
65 |
|
65 | ||
|
66 | if Configuration.time_limit_mode? and user.contest_finished? |
|
66 | if Configuration.time_limit_mode? and user.contest_finished? |
|
67 | @submission.errors.add_to_base "The contest is over." |
|
67 | @submission.errors.add_to_base "The contest is over." |
|
68 | prepare_list_information |
|
68 | prepare_list_information |
|
69 | render :action => 'list' and return |
|
69 | render :action => 'list' and return |
|
70 | end |
|
70 | end |
|
71 |
|
71 | ||
|
72 | if @submission.valid? |
|
72 | if @submission.valid? |
|
73 | if @submission.save == false |
|
73 | if @submission.save == false |
|
74 | flash[:notice] = 'Error saving your submission' |
|
74 | flash[:notice] = 'Error saving your submission' |
|
75 | elsif Task.create(:submission_id => @submission.id, |
|
75 | elsif Task.create(:submission_id => @submission.id, |
|
76 | :status => Task::STATUS_INQUEUE) == false |
|
76 | :status => Task::STATUS_INQUEUE) == false |
|
77 | flash[:notice] = 'Error adding your submission to task queue' |
|
77 | flash[:notice] = 'Error adding your submission to task queue' |
|
78 | end |
|
78 | end |
|
79 | else |
|
79 | else |
|
80 | prepare_list_information |
|
80 | prepare_list_information |
|
81 | render :action => 'list' and return |
|
81 | render :action => 'list' and return |
|
82 | end |
|
82 | end |
|
83 | redirect_to :action => 'list' |
|
83 | redirect_to :action => 'list' |
|
84 | end |
|
84 | end |
|
85 |
|
85 | ||
|
86 | def source |
|
86 | def source |
|
87 | submission = Submission.find(params[:id]) |
|
87 | submission = Submission.find(params[:id]) |
|
88 | if submission.user_id == session[:user_id] |
|
88 | if submission.user_id == session[:user_id] |
|
89 | send_data(submission.source, |
|
89 | send_data(submission.source, |
|
90 | {:filename => submission.download_filename, |
|
90 | {:filename => submission.download_filename, |
|
91 | :type => 'text/plain'}) |
|
91 | :type => 'text/plain'}) |
|
92 | else |
|
92 | else |
|
93 | flash[:notice] = 'Error viewing source' |
|
93 | flash[:notice] = 'Error viewing source' |
|
94 | redirect_to :action => 'list' |
|
94 | redirect_to :action => 'list' |
|
95 | end |
|
95 | end |
|
96 | end |
|
96 | end |
|
97 |
|
97 | ||
|
98 | def compiler_msg |
|
98 | def compiler_msg |
|
99 | @submission = Submission.find(params[:id]) |
|
99 | @submission = Submission.find(params[:id]) |
|
100 | if @submission.user_id == session[:user_id] |
|
100 | if @submission.user_id == session[:user_id] |
|
101 | render :action => 'compiler_msg', :layout => 'empty' |
|
101 | render :action => 'compiler_msg', :layout => 'empty' |
|
102 | else |
|
102 | else |
|
103 | flash[:notice] = 'Error viewing source' |
|
103 | flash[:notice] = 'Error viewing source' |
|
104 | redirect_to :action => 'list' |
|
104 | redirect_to :action => 'list' |
|
105 | end |
|
105 | end |
|
106 | end |
|
106 | end |
|
107 |
|
107 | ||
|
108 | def submission |
|
108 | def submission |
|
109 | @user = User.find(session[:user_id]) |
|
109 | @user = User.find(session[:user_id]) |
|
110 | @problems = Problem.find_available_problems |
|
110 | @problems = Problem.find_available_problems |
|
111 | if params[:id]==nil |
|
111 | if params[:id]==nil |
|
112 | @problem = nil |
|
112 | @problem = nil |
|
113 | @submissions = nil |
|
113 | @submissions = nil |
|
114 | else |
|
114 | else |
|
115 | @problem = Problem.find_by_name(params[:id]) |
|
115 | @problem = Problem.find_by_name(params[:id]) |
|
116 | if not @problem.available |
|
116 | if not @problem.available |
|
117 | redirect_to :action => 'list' |
|
117 | redirect_to :action => 'list' |
|
118 | flash[:notice] = 'Error: submissions for that problem are not viewable.' |
|
118 | flash[:notice] = 'Error: submissions for that problem are not viewable.' |
|
119 | return |
|
119 | return |
|
120 | end |
|
120 | end |
|
121 | @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id) |
|
121 | @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id) |
|
122 | end |
|
122 | end |
|
123 | end |
|
123 | end |
|
124 |
|
124 | ||
|
125 | def result |
|
125 | def result |
|
126 | if !Configuration.show_grading_result |
|
126 | if !Configuration.show_grading_result |
|
127 | redirect_to :action => 'list' and return |
|
127 | redirect_to :action => 'list' and return |
|
128 | end |
|
128 | end |
|
129 | @user = User.find(session[:user_id]) |
|
129 | @user = User.find(session[:user_id]) |
|
130 | @submission = Submission.find(params[:id]) |
|
130 | @submission = Submission.find(params[:id]) |
|
131 | if @submission.user!=@user |
|
131 | if @submission.user!=@user |
|
132 | flash[:notice] = 'You are not allowed to view result of other users.' |
|
132 | flash[:notice] = 'You are not allowed to view result of other users.' |
|
133 | redirect_to :action => 'list' and return |
|
133 | redirect_to :action => 'list' and return |
|
134 | end |
|
134 | end |
|
135 | prepare_grading_result(@submission) |
|
135 | prepare_grading_result(@submission) |
|
136 | end |
|
136 | end |
|
137 |
|
137 | ||
|
138 | def load_output |
|
138 | def load_output |
|
139 | if !Configuration.show_grading_result or params[:num]==nil |
|
139 | if !Configuration.show_grading_result or params[:num]==nil |
|
140 | redirect_to :action => 'list' and return |
|
140 | redirect_to :action => 'list' and return |
|
141 | end |
|
141 | end |
|
142 | @user = User.find(session[:user_id]) |
|
142 | @user = User.find(session[:user_id]) |
|
143 | @submission = Submission.find(params[:id]) |
|
143 | @submission = Submission.find(params[:id]) |
|
144 | if @submission.user!=@user |
|
144 | if @submission.user!=@user |
|
145 | flash[:notice] = 'You are not allowed to view result of other users.' |
|
145 | flash[:notice] = 'You are not allowed to view result of other users.' |
|
146 | redirect_to :action => 'list' and return |
|
146 | redirect_to :action => 'list' and return |
|
147 | end |
|
147 | end |
|
148 | case_num = params[:num].to_i |
|
148 | case_num = params[:num].to_i |
|
149 | out_filename = output_filename(@user.login, |
|
149 | out_filename = output_filename(@user.login, |
|
150 | @submission.problem.name, |
|
150 | @submission.problem.name, |
|
151 | @submission.id, |
|
151 | @submission.id, |
|
152 | case_num) |
|
152 | case_num) |
|
153 | if !FileTest.exists?(out_filename) |
|
153 | if !FileTest.exists?(out_filename) |
|
154 | flash[:notice] = 'Output not found.' |
|
154 | flash[:notice] = 'Output not found.' |
|
155 | redirect_to :action => 'list' and return |
|
155 | redirect_to :action => 'list' and return |
|
156 | end |
|
156 | end |
|
157 |
|
157 | ||
|
158 | response.headers['Content-Type'] = "application/force-download" |
|
158 | response.headers['Content-Type'] = "application/force-download" |
|
159 | response.headers['Content-Disposition'] = "attachment; filename=\"output-#{case_num}.txt\"" |
|
159 | response.headers['Content-Disposition'] = "attachment; filename=\"output-#{case_num}.txt\"" |
|
160 | response.headers["X-Sendfile"] = out_filename |
|
160 | response.headers["X-Sendfile"] = out_filename |
|
161 | response.headers['Content-length'] = File.size(out_filename) |
|
161 | response.headers['Content-length'] = File.size(out_filename) |
|
162 | render :nothing => true |
|
162 | render :nothing => true |
|
163 | end |
|
163 | end |
|
164 |
|
164 | ||
|
165 | def error |
|
165 | def error |
|
166 | @user = User.find(session[:user_id]) |
|
166 | @user = User.find(session[:user_id]) |
|
167 | end |
|
167 | end |
|
168 |
|
168 | ||
|
169 | # announcement refreshing and hiding methods |
|
169 | # announcement refreshing and hiding methods |
|
170 |
|
170 | ||
|
171 | def announcements |
|
171 | def announcements |
|
172 | if params.has_key? 'recent' |
|
172 | if params.has_key? 'recent' |
|
173 | prepare_announcements(params[:recent]) |
|
173 | prepare_announcements(params[:recent]) |
|
174 | else |
|
174 | else |
|
175 | prepare_announcements |
|
175 | prepare_announcements |
|
176 | end |
|
176 | end |
|
177 | render(:partial => 'announcement', |
|
177 | render(:partial => 'announcement', |
|
178 | :collection => @announcements, |
|
178 | :collection => @announcements, |
|
179 | :locals => {:announcement_effect => true}) |
|
179 | :locals => {:announcement_effect => true}) |
|
180 | end |
|
180 | end |
|
181 |
|
181 | ||
|
182 | protected |
|
182 | protected |
|
183 |
|
183 | ||
|
184 | def prepare_announcements(recent=nil) |
|
184 | def prepare_announcements(recent=nil) |
|
185 | if Configuration.show_tasks_to?(@user) |
|
185 | if Configuration.show_tasks_to?(@user) |
|
186 | @announcements = Announcement.find_published(true) |
|
186 | @announcements = Announcement.find_published(true) |
|
187 | else |
|
187 | else |
|
188 | @announcements = Announcement.find_published |
|
188 | @announcements = Announcement.find_published |
|
189 | end |
|
189 | end |
|
190 | if recent!=nil |
|
190 | if recent!=nil |
|
191 | recent_id = recent.to_i |
|
191 | recent_id = recent.to_i |
|
192 | @announcements = @announcements.find_all { |a| a.id > recent_id } |
|
192 | @announcements = @announcements.find_all { |a| a.id > recent_id } |
|
193 | end |
|
193 | end |
|
194 | end |
|
194 | end |
|
195 |
|
195 | ||
|
|
196 | + def problem_list_by_user_contests(user) | ||
|
|
197 | + contest_problems = [] | ||
|
|
198 | + pin = {} | ||
|
|
199 | + user.contests.each do |contest| | ||
|
|
200 | + available_problems = contest.problems.available | ||
|
|
201 | + contest_problems << { | ||
|
|
202 | + :contest => contest, | ||
|
|
203 | + :problems => available_problems | ||
|
|
204 | + } | ||
|
|
205 | + available_problems.each {|p| pin[p.id] = true} | ||
|
|
206 | + end | ||
|
|
207 | + other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0} | ||
|
|
208 | + contest_problems << { | ||
|
|
209 | + :contest => nil, | ||
|
|
210 | + :problems => other_avaiable_problems | ||
|
|
211 | + } | ||
|
|
212 | + return contest_problems | ||
|
|
213 | + end | ||
|
|
214 | + | ||
|
|
215 | + def problem_list_for_user(user, contest_problems=nil) | ||
|
|
216 | + if not Configuration.multicontests? | ||
|
|
217 | + return Problem.find_available_problems | ||
|
|
218 | + else | ||
|
|
219 | + if contest_problems==nil | ||
|
|
220 | + contest_problems = problem_list_by_user_contests(user) | ||
|
|
221 | + end | ||
|
|
222 | + | ||
|
|
223 | + problems = [] | ||
|
|
224 | + collected = {} | ||
|
|
225 | + contest_problems.each do |cp| | ||
|
|
226 | + cp[:problems].each do |problem| | ||
|
|
227 | + if not collected[problem.id] | ||
|
|
228 | + problems << problem | ||
|
|
229 | + collected[problem.id] = true | ||
|
|
230 | + end | ||
|
|
231 | + end | ||
|
|
232 | + end | ||
|
|
233 | + return problems | ||
|
|
234 | + end | ||
|
|
235 | + end | ||
|
|
236 | + | ||
|
196 | def prepare_list_information |
|
237 | def prepare_list_information |
|
197 | - @problems = Problem.find_available_problems |
|
||
|
198 | - @prob_submissions = Array.new |
|
||
|
199 | @user = User.find(session[:user_id]) |
|
238 | @user = User.find(session[:user_id]) |
|
|
239 | + if not Configuration.multicontests? | ||
|
|
240 | + @problems = problem_list_for_user(@user) | ||
|
|
241 | + else | ||
|
|
242 | + @contest_problems = problem_list_by_user_contests(@user) | ||
|
|
243 | + @problems = problem_list_for_user(@user, @contest_problems) | ||
|
|
244 | + end | ||
|
|
245 | + @prob_submissions = {} | ||
|
200 | @problems.each do |p| |
|
246 | @problems.each do |p| |
|
201 | sub = Submission.find_last_by_user_and_problem(@user.id,p.id) |
|
247 | sub = Submission.find_last_by_user_and_problem(@user.id,p.id) |
|
202 | if sub!=nil |
|
248 | if sub!=nil |
|
203 |
- @prob_submissions |
|
249 | + @prob_submissions[p.id] = { :count => sub.number, :submission => sub } |
|
204 | else |
|
250 | else |
|
205 |
- @prob_submissions |
|
251 | + @prob_submissions[p.id] = { :count => 0, :submission => nil } |
|
206 | end |
|
252 | end |
|
207 | end |
|
253 | end |
|
208 | prepare_announcements |
|
254 | prepare_announcements |
|
209 | end |
|
255 | end |
|
210 |
|
256 | ||
|
211 | def check_viewability |
|
257 | def check_viewability |
|
212 | @user = User.find(session[:user_id]) |
|
258 | @user = User.find(session[:user_id]) |
|
213 | if (!Configuration.show_tasks_to?(@user)) and |
|
259 | if (!Configuration.show_tasks_to?(@user)) and |
|
214 | ((action_name=='submission') or (action_name=='submit')) |
|
260 | ((action_name=='submission') or (action_name=='submit')) |
|
215 | redirect_to :action => 'list' and return |
|
261 | redirect_to :action => 'list' and return |
|
216 | end |
|
262 | end |
|
217 | end |
|
263 | end |
|
218 |
|
264 | ||
|
219 | def prepare_grading_result(submission) |
|
265 | def prepare_grading_result(submission) |
|
220 | if Configuration.task_grading_info.has_key? submission.problem.name |
|
266 | if Configuration.task_grading_info.has_key? submission.problem.name |
|
221 | grading_info = Configuration.task_grading_info[submission.problem.name] |
|
267 | grading_info = Configuration.task_grading_info[submission.problem.name] |
|
222 | else |
|
268 | else |
|
223 | # guess task info from problem.full_score |
|
269 | # guess task info from problem.full_score |
|
224 | cases = submission.problem.full_score / 10 |
|
270 | cases = submission.problem.full_score / 10 |
|
225 | grading_info = { |
|
271 | grading_info = { |
|
226 | 'testruns' => cases, |
|
272 | 'testruns' => cases, |
|
227 | 'testcases' => cases |
|
273 | 'testcases' => cases |
|
228 | } |
|
274 | } |
|
229 | end |
|
275 | end |
|
230 | @test_runs = [] |
|
276 | @test_runs = [] |
|
231 | if grading_info['testruns'].is_a? Integer |
|
277 | if grading_info['testruns'].is_a? Integer |
|
232 | trun_count = grading_info['testruns'] |
|
278 | trun_count = grading_info['testruns'] |
|
233 | trun_count.times do |i| |
|
279 | trun_count.times do |i| |
|
234 | @test_runs << [ read_grading_result(@user.login, |
|
280 | @test_runs << [ read_grading_result(@user.login, |
|
235 | submission.problem.name, |
|
281 | submission.problem.name, |
|
236 | submission.id, |
|
282 | submission.id, |
|
237 | i+1) ] |
|
283 | i+1) ] |
|
238 | end |
|
284 | end |
|
239 | else |
|
285 | else |
|
240 | grading_info['testruns'].keys.sort.each do |num| |
|
286 | grading_info['testruns'].keys.sort.each do |num| |
|
241 | run = [] |
|
287 | run = [] |
|
242 | testrun = grading_info['testruns'][num] |
|
288 | testrun = grading_info['testruns'][num] |
|
243 | testrun.each do |c| |
|
289 | testrun.each do |c| |
|
244 | run << read_grading_result(@user.login, |
|
290 | run << read_grading_result(@user.login, |
|
245 | submission.problem.name, |
|
291 | submission.problem.name, |
|
246 | submission.id, |
|
292 | submission.id, |
|
247 | c) |
|
293 | c) |
|
248 | end |
|
294 | end |
|
249 | @test_runs << run |
|
295 | @test_runs << run |
|
250 | end |
|
296 | end |
|
251 | end |
|
297 | end |
|
252 | end |
|
298 | end |
|
253 |
|
299 | ||
|
254 | def grading_result_dir(user_name, problem_name, submission_id, case_num) |
|
300 | def grading_result_dir(user_name, problem_name, submission_id, case_num) |
|
255 | return "#{GRADING_RESULT_DIR}/#{user_name}/#{problem_name}/#{submission_id}/test-result/#{case_num}" |
|
301 | return "#{GRADING_RESULT_DIR}/#{user_name}/#{problem_name}/#{submission_id}/test-result/#{case_num}" |
|
256 | end |
|
302 | end |
|
257 |
|
303 | ||
|
258 | def output_filename(user_name, problem_name, submission_id, case_num) |
|
304 | def output_filename(user_name, problem_name, submission_id, case_num) |
|
259 | dir = grading_result_dir(user_name,problem_name, submission_id, case_num) |
|
305 | dir = grading_result_dir(user_name,problem_name, submission_id, case_num) |
|
260 | return "#{dir}/output.txt" |
|
306 | return "#{dir}/output.txt" |
|
261 | end |
|
307 | end |
|
262 |
|
308 | ||
|
263 | def read_grading_result(user_name, problem_name, submission_id, case_num) |
|
309 | def read_grading_result(user_name, problem_name, submission_id, case_num) |
|
264 | dir = grading_result_dir(user_name,problem_name, submission_id, case_num) |
|
310 | dir = grading_result_dir(user_name,problem_name, submission_id, case_num) |
|
265 | result_file_name = "#{dir}/result" |
|
311 | result_file_name = "#{dir}/result" |
|
266 | if !FileTest.exists?(result_file_name) |
|
312 | if !FileTest.exists?(result_file_name) |
|
267 | return {:num => case_num, :msg => 'program did not run'} |
|
313 | return {:num => case_num, :msg => 'program did not run'} |
|
268 | else |
|
314 | else |
|
269 | results = File.open(result_file_name).readlines |
|
315 | results = File.open(result_file_name).readlines |
|
270 | run_stat = extract_running_stat(results) |
|
316 | run_stat = extract_running_stat(results) |
|
271 | output_filename = "#{dir}/output.txt" |
|
317 | output_filename = "#{dir}/output.txt" |
|
272 | if FileTest.exists?(output_filename) |
|
318 | if FileTest.exists?(output_filename) |
|
273 | output_file = true |
|
319 | output_file = true |
|
274 | output_size = File.size(output_filename) |
|
320 | output_size = File.size(output_filename) |
|
275 | else |
|
321 | else |
|
276 | output_file = false |
|
322 | output_file = false |
|
277 | output_size = 0 |
|
323 | output_size = 0 |
|
278 | end |
|
324 | end |
|
279 |
|
325 | ||
|
280 | return { |
|
326 | return { |
|
281 | :num => case_num, |
|
327 | :num => case_num, |
|
282 | :msg => results[0], |
|
328 | :msg => results[0], |
|
283 | :run_stat => run_stat, |
|
329 | :run_stat => run_stat, |
|
284 | :output => output_file, |
|
330 | :output => output_file, |
|
285 | :output_size => output_size |
|
331 | :output_size => output_size |
|
286 | } |
|
332 | } |
|
287 | end |
|
333 | end |
|
288 | end |
|
334 | end |
|
289 |
|
335 | ||
|
290 | # copied from grader/script/lib/test_request_helper.rb |
|
336 | # copied from grader/script/lib/test_request_helper.rb |
|
291 | def extract_running_stat(results) |
|
337 | def extract_running_stat(results) |
|
292 | running_stat_line = results[-1] |
|
338 | running_stat_line = results[-1] |
|
293 |
|
339 | ||
|
294 | # extract exit status line |
|
340 | # extract exit status line |
|
295 | run_stat = "" |
|
341 | run_stat = "" |
|
296 | if !(/[Cc]orrect/.match(results[0])) |
|
342 | if !(/[Cc]orrect/.match(results[0])) |
|
297 | run_stat = results[0].chomp |
|
343 | run_stat = results[0].chomp |
|
298 | else |
|
344 | else |
|
299 | run_stat = 'Program exited normally' |
|
345 | run_stat = 'Program exited normally' |
|
300 | end |
|
346 | end |
|
301 |
|
347 | ||
|
302 | logger.info "Stat line: #{running_stat_line}" |
|
348 | logger.info "Stat line: #{running_stat_line}" |
|
303 |
|
349 | ||
|
304 | # extract running time |
|
350 | # extract running time |
|
305 | if res = /r(.*)u(.*)s/.match(running_stat_line) |
|
351 | if res = /r(.*)u(.*)s/.match(running_stat_line) |
|
306 | seconds = (res[1].to_f + res[2].to_f) |
|
352 | seconds = (res[1].to_f + res[2].to_f) |
|
307 | time_stat = "Time used: #{seconds} sec." |
|
353 | time_stat = "Time used: #{seconds} sec." |
|
308 | else |
|
354 | else |
|
309 | seconds = nil |
|
355 | seconds = nil |
|
310 | time_stat = "Time used: n/a sec." |
|
356 | time_stat = "Time used: n/a sec." |
|
311 | end |
|
357 | end |
|
312 |
|
358 | ||
|
313 | # extract memory usage |
|
359 | # extract memory usage |
|
314 | if res = /s(.*)m/.match(running_stat_line) |
|
360 | if res = /s(.*)m/.match(running_stat_line) |
|
315 | memory_used = res[1].to_i |
|
361 | memory_used = res[1].to_i |
|
316 | else |
|
362 | else |
|
317 | memory_used = -1 |
|
363 | memory_used = -1 |
|
318 | end |
|
364 | end |
|
319 |
|
365 | ||
|
320 | return { |
|
366 | return { |
|
321 | :msg => "#{run_stat}\n#{time_stat}", |
|
367 | :msg => "#{run_stat}\n#{time_stat}", |
|
322 | :running_time => seconds, |
|
368 | :running_time => seconds, |
|
323 | :exit_status => run_stat, |
|
369 | :exit_status => run_stat, |
|
324 | :memory_usage => memory_used |
|
370 | :memory_usage => memory_used |
|
325 | } |
|
371 | } |
|
326 | end |
|
372 | end |
|
327 |
|
373 | ||
|
328 | def update_user_start_time |
|
374 | def update_user_start_time |
|
329 | user = User.find(session[:user_id]) |
|
375 | user = User.find(session[:user_id]) |
|
330 | user.update_start_time |
|
376 | user.update_start_time |
|
331 | end |
|
377 | end |
|
332 |
|
378 | ||
|
333 | def reject_announcement_refresh_when_logged_out |
|
379 | def reject_announcement_refresh_when_logged_out |
|
334 | if not session[:user_id] |
|
380 | if not session[:user_id] |
|
335 | render :text => 'Access forbidden', :status => 403 |
|
381 | render :text => 'Access forbidden', :status => 403 |
|
336 | end |
|
382 | end |
|
337 | end |
|
383 | end |
|
338 |
|
384 | ||
|
339 | end |
|
385 | end |
|
340 |
|
386 |
@@ -1,178 +1,182 | |||||
|
1 | require 'yaml' |
|
1 | require 'yaml' |
|
2 |
|
2 | ||
|
3 | # |
|
3 | # |
|
4 | # This class also contains various login of the system. |
|
4 | # This class also contains various login of the system. |
|
5 | # |
|
5 | # |
|
6 | class Configuration < ActiveRecord::Base |
|
6 | class Configuration < ActiveRecord::Base |
|
7 |
|
7 | ||
|
8 | SYSTEM_MODE_CONF_KEY = 'system.mode' |
|
8 | SYSTEM_MODE_CONF_KEY = 'system.mode' |
|
9 | TEST_REQUEST_EARLY_TIMEOUT_KEY = 'contest.test_request.early_timeout' |
|
9 | TEST_REQUEST_EARLY_TIMEOUT_KEY = 'contest.test_request.early_timeout' |
|
10 |
|
10 | ||
|
11 | cattr_accessor :cache |
|
11 | cattr_accessor :cache |
|
12 | cattr_accessor :config_cache |
|
12 | cattr_accessor :config_cache |
|
13 | cattr_accessor :task_grading_info |
|
13 | cattr_accessor :task_grading_info |
|
14 | cattr_accessor :contest_time_str |
|
14 | cattr_accessor :contest_time_str |
|
15 | cattr_accessor :contest_time |
|
15 | cattr_accessor :contest_time |
|
16 |
|
16 | ||
|
17 | # set @@cache = true to only reload once. |
|
17 | # set @@cache = true to only reload once. |
|
18 | Configuration.cache = false |
|
18 | Configuration.cache = false |
|
19 |
|
19 | ||
|
20 | Configuration.config_cache = nil |
|
20 | Configuration.config_cache = nil |
|
21 | Configuration.task_grading_info = nil |
|
21 | Configuration.task_grading_info = nil |
|
22 |
|
22 | ||
|
23 | def self.get(key) |
|
23 | def self.get(key) |
|
24 | if Configuration.cache |
|
24 | if Configuration.cache |
|
25 | if Configuration.config_cache == nil |
|
25 | if Configuration.config_cache == nil |
|
26 | self.read_config |
|
26 | self.read_config |
|
27 | end |
|
27 | end |
|
28 | return Configuration.config_cache[key] |
|
28 | return Configuration.config_cache[key] |
|
29 | else |
|
29 | else |
|
30 | return Configuration.read_one_key(key) |
|
30 | return Configuration.read_one_key(key) |
|
31 | end |
|
31 | end |
|
32 | end |
|
32 | end |
|
33 |
|
33 | ||
|
34 | def self.[](key) |
|
34 | def self.[](key) |
|
35 | self.get(key) |
|
35 | self.get(key) |
|
36 | end |
|
36 | end |
|
37 |
|
37 | ||
|
38 | def self.reload |
|
38 | def self.reload |
|
39 | self.read_config |
|
39 | self.read_config |
|
40 | end |
|
40 | end |
|
41 |
|
41 | ||
|
42 | def self.clear |
|
42 | def self.clear |
|
43 | Configuration.config_cache = nil |
|
43 | Configuration.config_cache = nil |
|
44 | end |
|
44 | end |
|
45 |
|
45 | ||
|
46 | def self.cache? |
|
46 | def self.cache? |
|
47 | Configuration.cache |
|
47 | Configuration.cache |
|
48 | end |
|
48 | end |
|
49 |
|
49 | ||
|
50 | def self.enable_caching |
|
50 | def self.enable_caching |
|
51 | Configuration.cache = true |
|
51 | Configuration.cache = true |
|
52 | end |
|
52 | end |
|
53 |
|
53 | ||
|
54 | # |
|
54 | # |
|
55 | # View decision |
|
55 | # View decision |
|
56 | # |
|
56 | # |
|
57 | def self.show_submitbox_to?(user) |
|
57 | def self.show_submitbox_to?(user) |
|
58 | mode = get(SYSTEM_MODE_CONF_KEY) |
|
58 | mode = get(SYSTEM_MODE_CONF_KEY) |
|
59 | return false if mode=='analysis' |
|
59 | return false if mode=='analysis' |
|
60 | if (mode=='contest') |
|
60 | if (mode=='contest') |
|
61 | return false if (user.site!=nil) and |
|
61 | return false if (user.site!=nil) and |
|
62 | ((user.site.started!=true) or (user.site.finished?)) |
|
62 | ((user.site.started!=true) or (user.site.finished?)) |
|
63 | end |
|
63 | end |
|
64 | return true |
|
64 | return true |
|
65 | end |
|
65 | end |
|
66 |
|
66 | ||
|
67 | def self.show_tasks_to?(user) |
|
67 | def self.show_tasks_to?(user) |
|
68 | if time_limit_mode? |
|
68 | if time_limit_mode? |
|
69 | return false if not user.contest_started? |
|
69 | return false if not user.contest_started? |
|
70 | end |
|
70 | end |
|
71 | return true |
|
71 | return true |
|
72 | end |
|
72 | end |
|
73 |
|
73 | ||
|
74 | def self.show_grading_result |
|
74 | def self.show_grading_result |
|
75 | return (get(SYSTEM_MODE_CONF_KEY)=='analysis') |
|
75 | return (get(SYSTEM_MODE_CONF_KEY)=='analysis') |
|
76 | end |
|
76 | end |
|
77 |
|
77 | ||
|
78 | def self.allow_test_request(user) |
|
78 | def self.allow_test_request(user) |
|
79 | mode = get(SYSTEM_MODE_CONF_KEY) |
|
79 | mode = get(SYSTEM_MODE_CONF_KEY) |
|
80 | early_timeout = get(TEST_REQUEST_EARLY_TIMEOUT_KEY) |
|
80 | early_timeout = get(TEST_REQUEST_EARLY_TIMEOUT_KEY) |
|
81 | if (mode=='contest') |
|
81 | if (mode=='contest') |
|
82 | return false if ((user.site!=nil) and |
|
82 | return false if ((user.site!=nil) and |
|
83 | ((user.site.started!=true) or |
|
83 | ((user.site.started!=true) or |
|
84 | (early_timeout and (user.site.time_left < 30.minutes)))) |
|
84 | (early_timeout and (user.site.time_left < 30.minutes)))) |
|
85 | end |
|
85 | end |
|
86 | return false if mode=='analysis' |
|
86 | return false if mode=='analysis' |
|
87 | return true |
|
87 | return true |
|
88 | end |
|
88 | end |
|
89 |
|
89 | ||
|
90 | def self.task_grading_info |
|
90 | def self.task_grading_info |
|
91 | if Configuration.task_grading_info==nil |
|
91 | if Configuration.task_grading_info==nil |
|
92 | read_grading_info |
|
92 | read_grading_info |
|
93 | end |
|
93 | end |
|
94 | return Configuration.task_grading_info |
|
94 | return Configuration.task_grading_info |
|
95 | end |
|
95 | end |
|
96 |
|
96 | ||
|
97 | def self.standard_mode? |
|
97 | def self.standard_mode? |
|
98 | return get(SYSTEM_MODE_CONF_KEY) == 'standard' |
|
98 | return get(SYSTEM_MODE_CONF_KEY) == 'standard' |
|
99 | end |
|
99 | end |
|
100 |
|
100 | ||
|
101 | def self.contest_mode? |
|
101 | def self.contest_mode? |
|
102 | return get(SYSTEM_MODE_CONF_KEY) == 'contest' |
|
102 | return get(SYSTEM_MODE_CONF_KEY) == 'contest' |
|
103 | end |
|
103 | end |
|
104 |
|
104 | ||
|
105 | def self.indv_contest_mode? |
|
105 | def self.indv_contest_mode? |
|
106 | return get(SYSTEM_MODE_CONF_KEY) == 'indv-contest' |
|
106 | return get(SYSTEM_MODE_CONF_KEY) == 'indv-contest' |
|
107 | end |
|
107 | end |
|
108 |
|
108 | ||
|
|
109 | + def self.multicontests? | ||
|
|
110 | + return get('system.multicontests') == true | ||
|
|
111 | + end | ||
|
|
112 | + | ||
|
109 | def self.time_limit_mode? |
|
113 | def self.time_limit_mode? |
|
110 | mode = get(SYSTEM_MODE_CONF_KEY) |
|
114 | mode = get(SYSTEM_MODE_CONF_KEY) |
|
111 | return ((mode == 'contest') or (mode == 'indv-contest')) |
|
115 | return ((mode == 'contest') or (mode == 'indv-contest')) |
|
112 | end |
|
116 | end |
|
113 |
|
117 | ||
|
114 | def self.analysis_mode? |
|
118 | def self.analysis_mode? |
|
115 | return get(SYSTEM_MODE_CONF_KEY) == 'analysis' |
|
119 | return get(SYSTEM_MODE_CONF_KEY) == 'analysis' |
|
116 | end |
|
120 | end |
|
117 |
|
121 | ||
|
118 | def self.contest_time_limit |
|
122 | def self.contest_time_limit |
|
119 | contest_time_str = Configuration['contest.time_limit'] |
|
123 | contest_time_str = Configuration['contest.time_limit'] |
|
120 |
|
124 | ||
|
121 | if not defined? Configuration.contest_time_str |
|
125 | if not defined? Configuration.contest_time_str |
|
122 | Configuration.contest_time_str = nil |
|
126 | Configuration.contest_time_str = nil |
|
123 | end |
|
127 | end |
|
124 |
|
128 | ||
|
125 | if Configuration.contest_time_str != contest_time_str |
|
129 | if Configuration.contest_time_str != contest_time_str |
|
126 | Configuration.contest_time_str = contest_time_str |
|
130 | Configuration.contest_time_str = contest_time_str |
|
127 | if tmatch = /(\d+):(\d+)/.match(contest_time_str) |
|
131 | if tmatch = /(\d+):(\d+)/.match(contest_time_str) |
|
128 | h = tmatch[1].to_i |
|
132 | h = tmatch[1].to_i |
|
129 | m = tmatch[2].to_i |
|
133 | m = tmatch[2].to_i |
|
130 |
|
134 | ||
|
131 | Configuration.contest_time = h.hour + m.minute |
|
135 | Configuration.contest_time = h.hour + m.minute |
|
132 | else |
|
136 | else |
|
133 | Configuration.contest_time = nil |
|
137 | Configuration.contest_time = nil |
|
134 | end |
|
138 | end |
|
135 | end |
|
139 | end |
|
136 | return Configuration.contest_time |
|
140 | return Configuration.contest_time |
|
137 | end |
|
141 | end |
|
138 |
|
142 | ||
|
139 | protected |
|
143 | protected |
|
140 |
|
144 | ||
|
141 | def self.convert_type(val,type) |
|
145 | def self.convert_type(val,type) |
|
142 | case type |
|
146 | case type |
|
143 | when 'string' |
|
147 | when 'string' |
|
144 | return val |
|
148 | return val |
|
145 |
|
149 | ||
|
146 | when 'integer' |
|
150 | when 'integer' |
|
147 | return val.to_i |
|
151 | return val.to_i |
|
148 |
|
152 | ||
|
149 | when 'boolean' |
|
153 | when 'boolean' |
|
150 | return (val=='true') |
|
154 | return (val=='true') |
|
151 | end |
|
155 | end |
|
152 | end |
|
156 | end |
|
153 |
|
157 | ||
|
154 | def self.read_config |
|
158 | def self.read_config |
|
155 | Configuration.config_cache = {} |
|
159 | Configuration.config_cache = {} |
|
156 | Configuration.find(:all).each do |conf| |
|
160 | Configuration.find(:all).each do |conf| |
|
157 | key = conf.key |
|
161 | key = conf.key |
|
158 | val = conf.value |
|
162 | val = conf.value |
|
159 | Configuration.config_cache[key] = Configuration.convert_type(val,conf.value_type) |
|
163 | Configuration.config_cache[key] = Configuration.convert_type(val,conf.value_type) |
|
160 | end |
|
164 | end |
|
161 | end |
|
165 | end |
|
162 |
|
166 | ||
|
163 | def self.read_one_key(key) |
|
167 | def self.read_one_key(key) |
|
164 | conf = Configuration.find_by_key(key) |
|
168 | conf = Configuration.find_by_key(key) |
|
165 | if conf |
|
169 | if conf |
|
166 | return Configuration.convert_type(conf.value,conf.value_type) |
|
170 | return Configuration.convert_type(conf.value,conf.value_type) |
|
167 | else |
|
171 | else |
|
168 | return nil |
|
172 | return nil |
|
169 | end |
|
173 | end |
|
170 | end |
|
174 | end |
|
171 |
|
175 | ||
|
172 | def self.read_grading_info |
|
176 | def self.read_grading_info |
|
173 | f = File.open(TASK_GRADING_INFO_FILENAME) |
|
177 | f = File.open(TASK_GRADING_INFO_FILENAME) |
|
174 | Configuration.task_grading_info = YAML.load(f) |
|
178 | Configuration.task_grading_info = YAML.load(f) |
|
175 | f.close |
|
179 | f.close |
|
176 | end |
|
180 | end |
|
177 |
|
181 | ||
|
178 | end |
|
182 | end |
@@ -1,106 +1,108 | |||||
|
1 | class Problem < ActiveRecord::Base |
|
1 | class Problem < ActiveRecord::Base |
|
2 |
|
2 | ||
|
3 | belongs_to :description |
|
3 | belongs_to :description |
|
4 | has_and_belongs_to_many :contests |
|
4 | has_and_belongs_to_many :contests |
|
5 | has_many :test_pairs, :dependent => :delete_all |
|
5 | has_many :test_pairs, :dependent => :delete_all |
|
6 |
|
6 | ||
|
7 | validates_presence_of :name |
|
7 | validates_presence_of :name |
|
8 | validates_format_of :name, :with => /^\w+$/ |
|
8 | validates_format_of :name, :with => /^\w+$/ |
|
9 | validates_presence_of :full_name |
|
9 | validates_presence_of :full_name |
|
10 |
|
10 | ||
|
|
11 | + named_scope :available, :conditions => {:available => true} | ||
|
|
12 | + | ||
|
11 | DEFAULT_TIME_LIMIT = 1 |
|
13 | DEFAULT_TIME_LIMIT = 1 |
|
12 | DEFAULT_MEMORY_LIMIT = 32 |
|
14 | DEFAULT_MEMORY_LIMIT = 32 |
|
13 |
|
15 | ||
|
14 | def self.find_available_problems |
|
16 | def self.find_available_problems |
|
15 |
- |
|
17 | + Problem.available.all(:order => "date_added DESC") |
|
16 | end |
|
18 | end |
|
17 |
|
19 | ||
|
18 | def self.create_from_import_form_params(params, old_problem=nil) |
|
20 | def self.create_from_import_form_params(params, old_problem=nil) |
|
19 | problem = old_problem || Problem.new |
|
21 | problem = old_problem || Problem.new |
|
20 | import_params = Problem.extract_params_and_check(params, problem) |
|
22 | import_params = Problem.extract_params_and_check(params, problem) |
|
21 |
|
23 | ||
|
22 | if not problem.valid? |
|
24 | if not problem.valid? |
|
23 | return problem, 'Error importing' |
|
25 | return problem, 'Error importing' |
|
24 | end |
|
26 | end |
|
25 |
|
27 | ||
|
26 | problem.full_score = 100 |
|
28 | problem.full_score = 100 |
|
27 | problem.date_added = Time.new |
|
29 | problem.date_added = Time.new |
|
28 | problem.test_allowed = true |
|
30 | problem.test_allowed = true |
|
29 | problem.output_only = false |
|
31 | problem.output_only = false |
|
30 | problem.available = false |
|
32 | problem.available = false |
|
31 |
|
33 | ||
|
32 | if not problem.save |
|
34 | if not problem.save |
|
33 | return problem, 'Error importing' |
|
35 | return problem, 'Error importing' |
|
34 | end |
|
36 | end |
|
35 |
|
37 | ||
|
36 | import_to_db = params.has_key? :import_to_db |
|
38 | import_to_db = params.has_key? :import_to_db |
|
37 |
|
39 | ||
|
38 | importer = TestdataImporter.new(problem) |
|
40 | importer = TestdataImporter.new(problem) |
|
39 |
|
41 | ||
|
40 | if not importer.import_from_file(import_params[:file], |
|
42 | if not importer.import_from_file(import_params[:file], |
|
41 | import_params[:time_limit], |
|
43 | import_params[:time_limit], |
|
42 | import_params[:memory_limit], |
|
44 | import_params[:memory_limit], |
|
43 | import_to_db) |
|
45 | import_to_db) |
|
44 | problem.errors.add_to_base('Import error.') |
|
46 | problem.errors.add_to_base('Import error.') |
|
45 | end |
|
47 | end |
|
46 |
|
48 | ||
|
47 | return problem, importer.log_msg |
|
49 | return problem, importer.log_msg |
|
48 | end |
|
50 | end |
|
49 |
|
51 | ||
|
50 | def self.download_file_basedir |
|
52 | def self.download_file_basedir |
|
51 | return "#{RAILS_ROOT}/data/tasks" |
|
53 | return "#{RAILS_ROOT}/data/tasks" |
|
52 | end |
|
54 | end |
|
53 |
|
55 | ||
|
54 | protected |
|
56 | protected |
|
55 |
|
57 | ||
|
56 | def self.to_i_or_default(st, default) |
|
58 | def self.to_i_or_default(st, default) |
|
57 | if st!='' |
|
59 | if st!='' |
|
58 | st.to_i |
|
60 | st.to_i |
|
59 | else |
|
61 | else |
|
60 | default |
|
62 | default |
|
61 | end |
|
63 | end |
|
62 | end |
|
64 | end |
|
63 |
|
65 | ||
|
64 | def self.extract_params_and_check(params, problem) |
|
66 | def self.extract_params_and_check(params, problem) |
|
65 | time_limit = Problem.to_i_or_default(params[:time_limit], |
|
67 | time_limit = Problem.to_i_or_default(params[:time_limit], |
|
66 | DEFAULT_TIME_LIMIT) |
|
68 | DEFAULT_TIME_LIMIT) |
|
67 | memory_limit = Problem.to_i_or_default(params[:memory_limit], |
|
69 | memory_limit = Problem.to_i_or_default(params[:memory_limit], |
|
68 | DEFAULT_MEMORY_LIMIT) |
|
70 | DEFAULT_MEMORY_LIMIT) |
|
69 |
|
71 | ||
|
70 | if time_limit==0 and time_limit_s!='0' |
|
72 | if time_limit==0 and time_limit_s!='0' |
|
71 | problem.errors.add_to_base('Time limit format errors.') |
|
73 | problem.errors.add_to_base('Time limit format errors.') |
|
72 | elsif time_limit<=0 or time_limit >60 |
|
74 | elsif time_limit<=0 or time_limit >60 |
|
73 | problem.errors.add_to_base('Time limit out of range.') |
|
75 | problem.errors.add_to_base('Time limit out of range.') |
|
74 | end |
|
76 | end |
|
75 |
|
77 | ||
|
76 | if memory_limit==0 and memory_limit_s!='0' |
|
78 | if memory_limit==0 and memory_limit_s!='0' |
|
77 | problem.errors.add_to_base('Memory limit format errors.') |
|
79 | problem.errors.add_to_base('Memory limit format errors.') |
|
78 | elsif memory_limit<=0 or memory_limit >512 |
|
80 | elsif memory_limit<=0 or memory_limit >512 |
|
79 | problem.errors.add_to_base('Memory limit out of range.') |
|
81 | problem.errors.add_to_base('Memory limit out of range.') |
|
80 | end |
|
82 | end |
|
81 |
|
83 | ||
|
82 | if params[:file]==nil or params[:file]=='' |
|
84 | if params[:file]==nil or params[:file]=='' |
|
83 | problem.errors.add_to_base('No testdata file.') |
|
85 | problem.errors.add_to_base('No testdata file.') |
|
84 | end |
|
86 | end |
|
85 |
|
87 | ||
|
86 | file = params[:file] |
|
88 | file = params[:file] |
|
87 |
|
89 | ||
|
88 | if problem.errors.length!=0 |
|
90 | if problem.errors.length!=0 |
|
89 | return problem |
|
91 | return problem |
|
90 | end |
|
92 | end |
|
91 |
|
93 | ||
|
92 | problem.name = params[:name] |
|
94 | problem.name = params[:name] |
|
93 | if params[:full_name]!='' |
|
95 | if params[:full_name]!='' |
|
94 | problem.full_name = params[:full_name] |
|
96 | problem.full_name = params[:full_name] |
|
95 | else |
|
97 | else |
|
96 | problem.full_name = params[:name] |
|
98 | problem.full_name = params[:name] |
|
97 | end |
|
99 | end |
|
98 |
|
100 | ||
|
99 | return { |
|
101 | return { |
|
100 | :time_limit => time_limit, |
|
102 | :time_limit => time_limit, |
|
101 | :memory_limit => memory_limit, |
|
103 | :memory_limit => memory_limit, |
|
102 | :file => file |
|
104 | :file => file |
|
103 | } |
|
105 | } |
|
104 | end |
|
106 | end |
|
105 |
|
107 | ||
|
106 | end |
|
108 | end |
@@ -1,18 +1,18 | |||||
|
1 | <tr class="info-<%= (problem_counter%2==0) ? "even" : "odd" %>"> |
|
1 | <tr class="info-<%= (problem_counter%2==0) ? "even" : "odd" %>"> |
|
2 | <td> |
|
2 | <td> |
|
3 | <%= "#{problem_counter+1}" %> |
|
3 | <%= "#{problem_counter+1}" %> |
|
4 | </td> |
|
4 | </td> |
|
5 | <td> |
|
5 | <td> |
|
6 | <%= "#{problem.full_name} (#{problem.name})" %> |
|
6 | <%= "#{problem.full_name} (#{problem.name})" %> |
|
7 | <%= link_to_description_if_any "[#{t 'main.problem_desc'}]", problem %> |
|
7 | <%= link_to_description_if_any "[#{t 'main.problem_desc'}]", problem %> |
|
8 | </td> |
|
8 | </td> |
|
9 | <td align="center"> |
|
9 | <td align="center"> |
|
10 |
- <%= @prob_submissions[problem |
|
10 | + <%= @prob_submissions[problem.id][:count] %> |
|
11 | </td> |
|
11 | </td> |
|
12 | <td> |
|
12 | <td> |
|
13 | <%= render :partial => 'submission_short', |
|
13 | <%= render :partial => 'submission_short', |
|
14 | :locals => { |
|
14 | :locals => { |
|
15 |
- :submission => @prob_submissions[problem |
|
15 | + :submission => @prob_submissions[problem.id][:submission], |
|
16 | :problem_name => problem.name }%> |
|
16 | :problem_name => problem.name }%> |
|
17 | </td> |
|
17 | </td> |
|
18 | </tr> |
|
18 | </tr> |
@@ -1,38 +1,51 | |||||
|
1 | - content_for :head do |
|
1 | - content_for :head do |
|
2 | = javascript_include_tag :defaults |
|
2 | = javascript_include_tag :defaults |
|
3 | = javascript_include_tag 'announcement_refresh.js' |
|
3 | = javascript_include_tag 'announcement_refresh.js' |
|
4 |
|
4 | ||
|
5 | = user_title_bar(@user) |
|
5 | = user_title_bar(@user) |
|
6 |
|
6 | ||
|
7 | .announcementbox{:style => (@announcements.length==0 ? "display:none" : "")} |
|
7 | .announcementbox{:style => (@announcements.length==0 ? "display:none" : "")} |
|
8 | %span{:class => 'title'} |
|
8 | %span{:class => 'title'} |
|
9 | Announcements |
|
9 | Announcements |
|
10 | #announcementbox-body |
|
10 | #announcementbox-body |
|
11 | = render :partial => 'announcement', :collection => @announcements |
|
11 | = render :partial => 'announcement', :collection => @announcements |
|
12 |
|
12 | ||
|
13 | - if Configuration.show_submitbox_to?(@user) |
|
13 | - if Configuration.show_submitbox_to?(@user) |
|
14 | .submitbox |
|
14 | .submitbox |
|
15 | = error_messages_for 'submission' |
|
15 | = error_messages_for 'submission' |
|
16 | = render :partial => 'submission_box' |
|
16 | = render :partial => 'submission_box' |
|
17 |
|
17 | ||
|
18 |
|
18 | ||
|
19 | %hr/ |
|
19 | %hr/ |
|
20 |
|
20 | ||
|
21 | - if (Configuration.contest_mode?) and (@user.site!=nil) and (@user.site.started!=true) |
|
21 | - if (Configuration.contest_mode?) and (@user.site!=nil) and (@user.site.started!=true) |
|
22 | %p=t 'main.start_soon' |
|
22 | %p=t 'main.start_soon' |
|
23 |
|
23 | ||
|
24 | - if Configuration.show_tasks_to?(@user) |
|
24 | - if Configuration.show_tasks_to?(@user) |
|
25 | - %table.info |
|
25 | + - if not Configuration.multicontests? |
|
26 |
- %t |
|
26 | + %table.info |
|
27 | - %th |
|
27 | + %tr.info-head |
|
28 |
- %th |
|
28 | + %th |
|
29 |
- %th |
|
29 | + %th Tasks |
|
30 |
- %th |
|
30 | + %th # of sub(s) |
|
31 | - = render :partial => 'problem', :collection => @problems |
|
31 | + %th Results |
|
|
32 | + = render :partial => 'problem', :collection => @problems | ||
|
|
33 | + - else | ||
|
|
34 | + - @contest_problems.each do |cp| | ||
|
|
35 | + %h2{:class =>'contest-title'} | ||
|
|
36 | + = "#{cp[:contest] ? cp[:contest].title : 'Public problems'}" | ||
|
|
37 | + %table.info | ||
|
|
38 | + %tr.info-head | ||
|
|
39 | + %th | ||
|
|
40 | + %th Tasks | ||
|
|
41 | + %th # of sub(s) | ||
|
|
42 | + %th Results | ||
|
|
43 | + = render :partial => 'problem', :collection => cp[:problems] | ||
|
|
44 | + | ||
|
32 |
|
45 | ||
|
33 | %hr/ |
|
46 | %hr/ |
|
34 |
|
47 | ||
|
35 | %script{:type => 'text/javascript'} |
|
48 | %script{:type => 'text/javascript'} |
|
36 | = "Announcement.refreshUrl = '#{url_for :controller => 'main', :action => 'announcements'}';" |
|
49 | = "Announcement.refreshUrl = '#{url_for :controller => 'main', :action => 'announcements'}';" |
|
37 | Announcement.registerRefreshEventTimer(); |
|
50 | Announcement.registerRefreshEventTimer(); |
|
38 |
|
51 |
@@ -1,56 +1,159 | |||||
|
1 | - |
|
||
|
2 |
|
|
1 | require File.dirname(__FILE__) + '/../spec_helper' |
|
3 |
|
2 | ||
|
4 | - describe MainController do |
|
3 | + module ConfigHelperMethods |
|
|
4 | + def enable_multicontest | ||
|
|
5 | + c = Configuration.new(:key => 'system.multicontests', | ||
|
|
6 | + :value_type => 'boolean', | ||
|
|
7 | + :value => 'true') | ||
|
|
8 | + c.save | ||
|
|
9 | + end | ||
|
|
10 | + | ||
|
|
11 | + def disable_multicontest | ||
|
|
12 | + c = Configuration.new(:key => 'system.multicontests', | ||
|
|
13 | + :value_type => 'boolean', | ||
|
|
14 | + :value => 'false') | ||
|
|
15 | + c.save | ||
|
|
16 | + end | ||
|
|
17 | + end | ||
|
|
18 | + | ||
|
|
19 | + describe MainController, "when a user comes to list page" do | ||
|
|
20 | + | ||
|
|
21 | + it "should redirect user to login page when unlogged-in user try to access main/list" do | ||
|
|
22 | + get 'list' | ||
|
|
23 | + response.should redirect_to(:action => 'login') | ||
|
|
24 | + end | ||
|
|
25 | + | ||
|
|
26 | + end | ||
|
|
27 | + | ||
|
|
28 | + describe MainController, "when a logged in user comes to list page, with multicontests off" do | ||
|
|
29 | + integrate_views | ||
|
|
30 | + | ||
|
|
31 | + include ConfigHelperMethods | ||
|
|
32 | + | ||
|
|
33 | + fixtures :users | ||
|
|
34 | + fixtures :problems | ||
|
|
35 | + fixtures :contests | ||
|
|
36 | + | ||
|
|
37 | + before(:each) do | ||
|
|
38 | + disable_multicontest | ||
|
|
39 | + end | ||
|
|
40 | + | ||
|
|
41 | + it "should list available problems" do | ||
|
|
42 | + john = users(:john) | ||
|
|
43 | + get "list", {}, {:user_id => john.id} | ||
|
|
44 | + | ||
|
|
45 | + response.should render_template 'main/list' | ||
|
|
46 | + response.should have_text(/add/) | ||
|
|
47 | + response.should have_text(/easy_problem/) | ||
|
|
48 | + response.should have_text(/hard_problem/) | ||
|
|
49 | + end | ||
|
|
50 | + | ||
|
|
51 | + end | ||
|
|
52 | + | ||
|
|
53 | + describe MainController, "when a logged in user comes to list page, with multicontests on" do | ||
|
|
54 | + integrate_views | ||
|
|
55 | + | ||
|
|
56 | + include ConfigHelperMethods | ||
|
|
57 | + | ||
|
|
58 | + fixtures :users | ||
|
|
59 | + fixtures :problems | ||
|
|
60 | + fixtures :contests | ||
|
|
61 | + | ||
|
|
62 | + before(:each) do | ||
|
|
63 | + enable_multicontest | ||
|
|
64 | + end | ||
|
|
65 | + | ||
|
|
66 | + it "should list only available public problems to users with no contest assigned" do | ||
|
|
67 | + john = users(:john) | ||
|
|
68 | + get "list", {}, {:user_id => john.id} | ||
|
|
69 | + | ||
|
|
70 | + response.should render_template('main/list') | ||
|
|
71 | + response.should have_text(/add/) | ||
|
|
72 | + response.should_not have_text(/easy_problem/) | ||
|
|
73 | + response.should_not have_text(/hard_problem/) | ||
|
|
74 | + end | ||
|
|
75 | + | ||
|
|
76 | + it "should list available problems on a specific contest" do | ||
|
|
77 | + james = users(:james) | ||
|
|
78 | + get "list", {}, {:user_id => james.id} | ||
|
|
79 | + | ||
|
|
80 | + response.should render_template('main/list') | ||
|
|
81 | + response.should have_text(/add/) | ||
|
|
82 | + response.should have_text(/easy_problem/) | ||
|
|
83 | + response.should_not have_text(/hard_problem/) | ||
|
|
84 | + end | ||
|
|
85 | + | ||
|
|
86 | + it "should shows available problems by contests" do | ||
|
|
87 | + james = users(:james) | ||
|
|
88 | + get "list", {}, {:user_id => james.id} | ||
|
|
89 | + | ||
|
|
90 | + response.should render_template('main/list') | ||
|
|
91 | + response.should have_text(Regexp.new('Contest A.*easy_problem', Regexp::MULTILINE)) | ||
|
|
92 | + end | ||
|
|
93 | + | ||
|
|
94 | + it "should shows available problems by contests; problems belonging to more the one contest should appear many times" do | ||
|
|
95 | + jack = users(:jack) | ||
|
|
96 | + get "list", {}, {:user_id => jack.id} | ||
|
|
97 | + | ||
|
|
98 | + response.should render_template('main/list') | ||
|
|
99 | + response.should have_text(Regexp.new('Contest A.*easy_problem.*Contest B.*easy_problem', Regexp::MULTILINE)) | ||
|
|
100 | + response.should have_text(Regexp.new('Contest B.*hard_problem', Regexp::MULTILINE)) | ||
|
|
101 | + end | ||
|
|
102 | + end | ||
|
|
103 | + | ||
|
|
104 | + describe MainController, "when a user loads sources and compiler messages" do | ||
|
5 |
|
105 | ||
|
6 | before(:each) do |
|
106 | before(:each) do |
|
7 | @problem = mock(Problem, :name => 'test', :output_only => false) |
|
107 | @problem = mock(Problem, :name => 'test', :output_only => false) |
|
8 | @language = mock(Language, :name => 'cpp', :ext => 'cpp') |
|
108 | @language = mock(Language, :name => 'cpp', :ext => 'cpp') |
|
9 | @submission = mock(Submission, |
|
109 | @submission = mock(Submission, |
|
10 | :id => 1, |
|
110 | :id => 1, |
|
11 | :user_id => 1, |
|
111 | :user_id => 1, |
|
12 | :problem => @problem, |
|
112 | :problem => @problem, |
|
13 | :language => @language, |
|
113 | :language => @language, |
|
14 | :source => 'sample source', |
|
114 | :source => 'sample source', |
|
15 | :compiler_message => 'none') |
|
115 | :compiler_message => 'none') |
|
|
116 | + | ||
|
16 | @user = mock(User, :id => 1, :login => 'john') |
|
117 | @user = mock(User, :id => 1, :login => 'john') |
|
|
118 | + @user.should_receive(:update_start_time).at_most(:once) | ||
|
|
119 | + | ||
|
17 | @another_user = mock(User, :id => 2, :login => 'mary') |
|
120 | @another_user = mock(User, :id => 2, :login => 'mary') |
|
18 | - end |
|
121 | + @another_user.should_receive(:update_start_time).at_most(:once) |
|
19 |
|
122 | ||
|
20 | - it "should redirect user to login page when unlogged-in user try to access main/list" do |
|
123 | + User.should_receive(:find). |
|
21 | - get 'list' |
|
124 | + with(1).any_number_of_times. |
|
22 | - response.should redirect_to(:action => 'login') |
|
125 | + and_return(@user) |
|
|
126 | + User.should_receive(:find). | ||
|
|
127 | + with(2).any_number_of_times. | ||
|
|
128 | + and_return(@another_user) | ||
|
|
129 | + Submission.should_receive(:find). | ||
|
|
130 | + any_number_of_times.with(@submission.id.to_s). | ||
|
|
131 | + and_return(@submission) | ||
|
23 | end |
|
132 | end |
|
24 |
|
133 | ||
|
25 | it "should let user sees her own source" do |
|
134 | it "should let user sees her own source" do |
|
26 |
- |
|
135 | + @submission.should_receive(:download_filename).and_return("foo.c") |
|
27 | - User.should_receive(:find).with(1).and_return(@user) |
|
||
|
28 | - @user.should_receive(:update_start_time) |
|
||
|
29 | get 'source', {:id => @submission.id}, {:user_id => 1} |
|
136 | get 'source', {:id => @submission.id}, {:user_id => 1} |
|
30 | response.should be_success |
|
137 | response.should be_success |
|
31 | end |
|
138 | end |
|
32 |
|
139 | ||
|
33 | it "should let user sees her own compiler message" do |
|
140 | it "should let user sees her own compiler message" do |
|
34 | - Submission.should_receive(:find).with(@submission.id.to_s).and_return(@submission) |
|
||
|
35 | - User.should_receive(:find).with(1).and_return(@user) |
|
||
|
36 | get 'compiler_msg', {:id => @submission.id}, {:user_id => 1} |
|
141 | get 'compiler_msg', {:id => @submission.id}, {:user_id => 1} |
|
37 | response.should be_success |
|
142 | response.should be_success |
|
38 | end |
|
143 | end |
|
39 |
|
144 | ||
|
40 | it "should not let user sees other user's source" do |
|
145 | it "should not let user sees other user's source" do |
|
41 | - Submission.should_receive(:find).with(@submission.id.to_s).and_return(@submission) |
|
||
|
42 | - User.should_receive(:find).with(2).and_return(@another_user) |
|
||
|
43 | get 'source', {:id => @submission.id}, {:user_id => 2} |
|
146 | get 'source', {:id => @submission.id}, {:user_id => 2} |
|
44 | flash[:notice].should =~ /[Ee]rror/ |
|
147 | flash[:notice].should =~ /[Ee]rror/ |
|
45 | response.should redirect_to(:action => 'list') |
|
148 | response.should redirect_to(:action => 'list') |
|
46 | end |
|
149 | end |
|
47 |
|
150 | ||
|
48 | it "should not let user sees other user's compiler message" do |
|
151 | it "should not let user sees other user's compiler message" do |
|
49 | - Submission.should_receive(:find).with(@submission.id.to_s).and_return(@submission) |
|
||
|
50 | - User.should_receive(:find).with(2).and_return(@another_user) |
|
||
|
51 | get 'compiler_msg', {:id => @submission.id}, {:user_id => 2} |
|
152 | get 'compiler_msg', {:id => @submission.id}, {:user_id => 2} |
|
52 | flash[:notice].should =~ /[Ee]rror/ |
|
153 | flash[:notice].should =~ /[Ee]rror/ |
|
53 | response.should redirect_to(:action => 'list') |
|
154 | response.should redirect_to(:action => 'list') |
|
54 | end |
|
155 | end |
|
55 |
|
156 | ||
|
56 | end |
|
157 | end |
|
|
158 | + | ||
|
|
159 | + |
@@ -1,11 +1,24 | |||||
|
1 | # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html |
|
1 | # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html |
|
2 | one: |
|
2 | one: |
|
3 | id: 1 |
|
3 | id: 1 |
|
4 | name: add |
|
4 | name: add |
|
5 | full_name: add_full_name |
|
5 | full_name: add_full_name |
|
6 | available: true |
|
6 | available: true |
|
|
7 | + | ||
|
7 | two: |
|
8 | two: |
|
8 | id: 2 |
|
9 | id: 2 |
|
9 | name: subtract |
|
10 | name: subtract |
|
10 | full_name: subtract_full_name |
|
11 | full_name: subtract_full_name |
|
11 | available: false |
|
12 | available: false |
|
|
13 | + | ||
|
|
14 | + easy: | ||
|
|
15 | + name: easy_problem | ||
|
|
16 | + full_name: Easy Problem | ||
|
|
17 | + available: true | ||
|
|
18 | + contests: contest_a, contest_b | ||
|
|
19 | + | ||
|
|
20 | + hard: | ||
|
|
21 | + name: hard_problem | ||
|
|
22 | + full_name: Hard Problem | ||
|
|
23 | + available: true | ||
|
|
24 | + contests: contest_b |
@@ -1,20 +1,34 | |||||
|
1 | # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html |
|
1 | # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html |
|
2 |
|
2 | ||
|
3 | <% |
|
3 | <% |
|
4 | User.public_class_method :encrypt |
|
4 | User.public_class_method :encrypt |
|
5 |
|
5 | ||
|
6 | salt = "abc" |
|
6 | salt = "abc" |
|
7 | %> |
|
7 | %> |
|
8 |
|
8 | ||
|
9 | john: |
|
9 | john: |
|
10 | login: john |
|
10 | login: john |
|
11 | full_name: john |
|
11 | full_name: john |
|
12 | hashed_password: <%= User.encrypt("hello",salt) %> |
|
12 | hashed_password: <%= User.encrypt("hello",salt) %> |
|
13 | salt: <%= salt %> |
|
13 | salt: <%= salt %> |
|
|
14 | + | ||
|
14 | mary: |
|
15 | mary: |
|
15 | login: mary |
|
16 | login: mary |
|
16 | full_name: mary |
|
17 | full_name: mary |
|
17 | hashed_password: <%= User.encrypt("goodbye",salt) %> |
|
18 | hashed_password: <%= User.encrypt("goodbye",salt) %> |
|
18 | salt: <%= salt %> |
|
19 | salt: <%= salt %> |
|
19 | roles: admin |
|
20 | roles: admin |
|
20 |
|
21 | ||
|
|
22 | + james: | ||
|
|
23 | + login: james | ||
|
|
24 | + full_name: James | ||
|
|
25 | + hashed_password: <%= User.encrypt("morning",salt) %> | ||
|
|
26 | + salt: <%= salt %> | ||
|
|
27 | + contests: contest_a | ||
|
|
28 | + | ||
|
|
29 | + jack: | ||
|
|
30 | + login: jack | ||
|
|
31 | + full_name: Jack | ||
|
|
32 | + hashed_password: <%= User.encrypt("morning",salt) %> | ||
|
|
33 | + salt: <%= salt %> | ||
|
|
34 | + contests: contest_a, contest_b |
@@ -1,20 +1,4 | |||||
|
1 | require File.dirname(__FILE__) + '/../test_helper' |
|
1 | require File.dirname(__FILE__) + '/../test_helper' |
|
2 |
|
2 | ||
|
3 | class MainControllerTest < ActionController::TestCase |
|
3 | class MainControllerTest < ActionController::TestCase |
|
4 | - fixtures :users |
|
||
|
5 | - fixtures :problems |
|
||
|
6 | - |
|
||
|
7 | - def test_should_redirect_new_user_to_login |
|
||
|
8 | - get :list |
|
||
|
9 | - assert_redirected_to :controller => 'main', :action => 'login' |
|
||
|
10 | - end |
|
||
|
11 | - |
|
||
|
12 | - def test_should_list_available_problems_if_logged_in |
|
||
|
13 | - john = users(:john) |
|
||
|
14 | - get :list, {}, {:user_id => john.id} |
|
||
|
15 | - |
|
||
|
16 | - assert_template 'main/list' |
|
||
|
17 | - assert_select "table tr:nth-child(2)", :text => /\(add\)/ |
|
||
|
18 | - end |
|
||
|
19 | - |
|
||
|
20 | end |
|
4 | end |
You need to be logged in to leave comments.
Login now