Description:
fixed msg grammar git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@293 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

r154:3580f3e3f91b - - 2 files changed: 2 inserted, 2 deleted

@@ -1,304 +1,304
1 class MainController < ApplicationController
1 class MainController < ApplicationController
2
2
3 SYSTEM_MODE_CONF_KEY = 'system.mode'
3 SYSTEM_MODE_CONF_KEY = 'system.mode'
4
4
5 before_filter :authenticate, :except => [:index, :login]
5 before_filter :authenticate, :except => [:index, :login]
6 before_filter :check_viewability, :except => [:index, :login]
6 before_filter :check_viewability, :except => [:index, :login]
7
7
8 #
8 #
9 # COMMENT OUT: filter in each action instead
9 # COMMENT OUT: filter in each action instead
10 #
10 #
11 # before_filter :verify_time_limit, :only => [:submit]
11 # before_filter :verify_time_limit, :only => [:submit]
12
12
13 verify :method => :post, :only => [:submit],
13 verify :method => :post, :only => [:submit],
14 :redirect_to => { :action => :index }
14 :redirect_to => { :action => :index }
15
15
16 # COMMENT OUT, only need when having high load
16 # COMMENT OUT, only need when having high load
17 # caches_action :index, :login
17 # caches_action :index, :login
18
18
19 def index
19 def index
20 redirect_to :action => 'login'
20 redirect_to :action => 'login'
21 end
21 end
22
22
23 def login
23 def login
24 saved_notice = flash[:notice]
24 saved_notice = flash[:notice]
25 reset_session
25 reset_session
26 flash[:notice] = saved_notice
26 flash[:notice] = saved_notice
27
27
28 #
28 #
29 # These are for site administrator login
29 # These are for site administrator login
30 #
30 #
31 @countries = Country.find(:all, :include => :sites)
31 @countries = Country.find(:all, :include => :sites)
32 @country_select = @countries.collect { |c| [c.name, c.id] }
32 @country_select = @countries.collect { |c| [c.name, c.id] }
33
33
34 @country_select_with_all = [['Any',0]]
34 @country_select_with_all = [['Any',0]]
35 @countries.each do |country|
35 @countries.each do |country|
36 @country_select_with_all << [country.name, country.id]
36 @country_select_with_all << [country.name, country.id]
37 end
37 end
38
38
39 @site_select = []
39 @site_select = []
40 @countries.each do |country|
40 @countries.each do |country|
41 country.sites.each do |site|
41 country.sites.each do |site|
42 @site_select << ["#{site.name}, #{country.name}", site.id]
42 @site_select << ["#{site.name}, #{country.name}", site.id]
43 end
43 end
44 end
44 end
45
45
46 @announcements = Announcement.find_for_frontpage
46 @announcements = Announcement.find_for_frontpage
47 render :action => 'login', :layout => 'empty'
47 render :action => 'login', :layout => 'empty'
48 end
48 end
49
49
50 def list
50 def list
51 prepare_list_information
51 prepare_list_information
52 end
52 end
53
53
54 def help
54 def help
55 @user = User.find(session[:user_id])
55 @user = User.find(session[:user_id])
56 end
56 end
57
57
58 def submit
58 def submit
59 user = User.find(session[:user_id])
59 user = User.find(session[:user_id])
60
60
61 @submission = Submission.new(params[:submission])
61 @submission = Submission.new(params[:submission])
62 @submission.user = user
62 @submission.user = user
63 @submission.language_id = 0
63 @submission.language_id = 0
64 if params['file']!=''
64 if params['file']!=''
65 @submission.source = params['file'].read
65 @submission.source = params['file'].read
66 @submission.source_filename = params['file'].original_filename
66 @submission.source_filename = params['file'].original_filename
67 end
67 end
68 @submission.submitted_at = Time.new.gmtime
68 @submission.submitted_at = Time.new.gmtime
69
69
70 if Configuration[SYSTEM_MODE_CONF_KEY]=='contest' and
70 if Configuration[SYSTEM_MODE_CONF_KEY]=='contest' and
71 user.site!=nil and user.site.finished?
71 user.site!=nil and user.site.finished?
72 @submission.errors.add_to_base "The contest is over."
72 @submission.errors.add_to_base "The contest is over."
73 prepare_list_information
73 prepare_list_information
74 render :action => 'list' and return
74 render :action => 'list' and return
75 end
75 end
76
76
77 if @submission.valid?
77 if @submission.valid?
78 if @submission.save == false
78 if @submission.save == false
79 flash[:notice] = 'Error saving your submission'
79 flash[:notice] = 'Error saving your submission'
80 elsif Task.create(:submission_id => @submission.id,
80 elsif Task.create(:submission_id => @submission.id,
81 :status => Task::STATUS_INQUEUE) == false
81 :status => Task::STATUS_INQUEUE) == false
82 flash[:notice] = 'Error adding your submission to task queue'
82 flash[:notice] = 'Error adding your submission to task queue'
83 end
83 end
84 else
84 else
85 prepare_list_information
85 prepare_list_information
86 render :action => 'list' and return
86 render :action => 'list' and return
87 end
87 end
88 redirect_to :action => 'list'
88 redirect_to :action => 'list'
89 end
89 end
90
90
91 def source
91 def source
92 submission = Submission.find(params[:id])
92 submission = Submission.find(params[:id])
93 if submission.user_id == session[:user_id]
93 if submission.user_id == session[:user_id]
94 if submission.problem.output_only
94 if submission.problem.output_only
95 fname = submission.source_filename
95 fname = submission.source_filename
96 else
96 else
97 fname = submission.problem.name + '.' + submission.language.ext
97 fname = submission.problem.name + '.' + submission.language.ext
98 end
98 end
99 send_data(submission.source,
99 send_data(submission.source,
100 {:filename => fname,
100 {:filename => fname,
101 :type => 'text/plain'})
101 :type => 'text/plain'})
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 compiler_msg
108 def compiler_msg
109 @submission = Submission.find(params[:id])
109 @submission = Submission.find(params[:id])
110 if @submission.user_id == session[:user_id]
110 if @submission.user_id == session[:user_id]
111 render :action => 'compiler_msg', :layout => 'empty'
111 render :action => 'compiler_msg', :layout => 'empty'
112 else
112 else
113 flash[:notice] = 'Error viewing source'
113 flash[:notice] = 'Error viewing source'
114 redirect_to :action => 'list'
114 redirect_to :action => 'list'
115 end
115 end
116 end
116 end
117
117
118 def submission
118 def submission
119 @user = User.find(session[:user_id])
119 @user = User.find(session[:user_id])
120 @problems = Problem.find_available_problems
120 @problems = Problem.find_available_problems
121 if params[:id]==nil
121 if params[:id]==nil
122 @problem = nil
122 @problem = nil
123 @submissions = nil
123 @submissions = nil
124 else
124 else
125 @problem = Problem.find_by_name(params[:id])
125 @problem = Problem.find_by_name(params[:id])
126 if not @problem.available
126 if not @problem.available
127 redirect_to :action => 'list'
127 redirect_to :action => 'list'
128 - flash[:notice] = 'Error: submissions for that problem is not available'
128 + flash[:notice] = 'Error: submissions for that problem are not viewable.'
129 return
129 return
130 end
130 end
131 @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id)
131 @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id)
132 end
132 end
133 end
133 end
134
134
135 def result
135 def result
136 if !Configuration.show_grading_result
136 if !Configuration.show_grading_result
137 redirect_to :action => 'list' and return
137 redirect_to :action => 'list' and return
138 end
138 end
139 @user = User.find(session[:user_id])
139 @user = User.find(session[:user_id])
140 @submission = Submission.find(params[:id])
140 @submission = Submission.find(params[:id])
141 if @submission.user!=@user
141 if @submission.user!=@user
142 flash[:notice] = 'You are not allowed to view result of other users.'
142 flash[:notice] = 'You are not allowed to view result of other users.'
143 redirect_to :action => 'list' and return
143 redirect_to :action => 'list' and return
144 end
144 end
145 prepare_grading_result(@submission)
145 prepare_grading_result(@submission)
146 end
146 end
147
147
148 def load_output
148 def load_output
149 if !Configuration.show_grading_result or params[:num]==nil
149 if !Configuration.show_grading_result or params[:num]==nil
150 redirect_to :action => 'list' and return
150 redirect_to :action => 'list' and return
151 end
151 end
152 @user = User.find(session[:user_id])
152 @user = User.find(session[:user_id])
153 @submission = Submission.find(params[:id])
153 @submission = Submission.find(params[:id])
154 if @submission.user!=@user
154 if @submission.user!=@user
155 flash[:notice] = 'You are not allowed to view result of other users.'
155 flash[:notice] = 'You are not allowed to view result of other users.'
156 redirect_to :action => 'list' and return
156 redirect_to :action => 'list' and return
157 end
157 end
158 case_num = params[:num].to_i
158 case_num = params[:num].to_i
159 out_filename = output_filename(@user.login,
159 out_filename = output_filename(@user.login,
160 @submission.problem.name,
160 @submission.problem.name,
161 @submission.id,
161 @submission.id,
162 case_num)
162 case_num)
163 if !FileTest.exists?(out_filename)
163 if !FileTest.exists?(out_filename)
164 flash[:notice] = 'Output not found.'
164 flash[:notice] = 'Output not found.'
165 redirect_to :action => 'list' and return
165 redirect_to :action => 'list' and return
166 end
166 end
167
167
168 response.headers['Content-Type'] = "application/force-download"
168 response.headers['Content-Type'] = "application/force-download"
169 response.headers['Content-Disposition'] = "attachment; filename=\"output-#{case_num}.txt\""
169 response.headers['Content-Disposition'] = "attachment; filename=\"output-#{case_num}.txt\""
170 response.headers["X-Sendfile"] = out_filename
170 response.headers["X-Sendfile"] = out_filename
171 response.headers['Content-length'] = File.size(out_filename)
171 response.headers['Content-length'] = File.size(out_filename)
172 render :nothing => true
172 render :nothing => true
173 end
173 end
174
174
175 def error
175 def error
176 @user = User.find(session[:user_id])
176 @user = User.find(session[:user_id])
177 end
177 end
178
178
179 protected
179 protected
180 def prepare_list_information
180 def prepare_list_information
181 @problems = Problem.find_available_problems
181 @problems = Problem.find_available_problems
182 @prob_submissions = Array.new
182 @prob_submissions = Array.new
183 @user = User.find(session[:user_id])
183 @user = User.find(session[:user_id])
184 @problems.each do |p|
184 @problems.each do |p|
185 sub = Submission.find_last_by_user_and_problem(@user.id,p.id)
185 sub = Submission.find_last_by_user_and_problem(@user.id,p.id)
186 if sub!=nil
186 if sub!=nil
187 @prob_submissions << { :count => sub.number, :submission => sub }
187 @prob_submissions << { :count => sub.number, :submission => sub }
188 else
188 else
189 @prob_submissions << { :count => 0, :submission => nil }
189 @prob_submissions << { :count => 0, :submission => nil }
190 end
190 end
191 end
191 end
192 @announcements = Announcement.find_published
192 @announcements = Announcement.find_published
193 end
193 end
194
194
195 def check_viewability
195 def check_viewability
196 @user = User.find(session[:user_id])
196 @user = User.find(session[:user_id])
197 if (!Configuration.show_tasks_to?(@user)) and
197 if (!Configuration.show_tasks_to?(@user)) and
198 ((action_name=='submission') or (action_name=='submit'))
198 ((action_name=='submission') or (action_name=='submit'))
199 redirect_to :action => 'list' and return
199 redirect_to :action => 'list' and return
200 end
200 end
201 end
201 end
202
202
203 def prepare_grading_result(submission)
203 def prepare_grading_result(submission)
204 grading_info = Configuration.task_grading_info[submission.problem.name]
204 grading_info = Configuration.task_grading_info[submission.problem.name]
205 @test_runs = []
205 @test_runs = []
206 if grading_info['testruns'].is_a? Integer
206 if grading_info['testruns'].is_a? Integer
207 trun_count = grading_info['testruns']
207 trun_count = grading_info['testruns']
208 trun_count.times do |i|
208 trun_count.times do |i|
209 @test_runs << [ read_grading_result(@user.login,
209 @test_runs << [ read_grading_result(@user.login,
210 submission.problem.name,
210 submission.problem.name,
211 submission.id,
211 submission.id,
212 i+1) ]
212 i+1) ]
213 end
213 end
214 else
214 else
215 grading_info['testruns'].keys.sort.each do |num|
215 grading_info['testruns'].keys.sort.each do |num|
216 run = []
216 run = []
217 testrun = grading_info['testruns'][num]
217 testrun = grading_info['testruns'][num]
218 testrun.each do |c|
218 testrun.each do |c|
219 run << read_grading_result(@user.login,
219 run << read_grading_result(@user.login,
220 submission.problem.name,
220 submission.problem.name,
221 submission.id,
221 submission.id,
222 c)
222 c)
223 end
223 end
224 @test_runs << run
224 @test_runs << run
225 end
225 end
226 end
226 end
227 end
227 end
228
228
229 def grading_result_dir(user_name, problem_name, submission_id, case_num)
229 def grading_result_dir(user_name, problem_name, submission_id, case_num)
230 return "#{GRADING_RESULT_DIR}/#{user_name}/#{problem_name}/#{submission_id}/test-result/#{case_num}"
230 return "#{GRADING_RESULT_DIR}/#{user_name}/#{problem_name}/#{submission_id}/test-result/#{case_num}"
231 end
231 end
232
232
233 def output_filename(user_name, problem_name, submission_id, case_num)
233 def output_filename(user_name, problem_name, submission_id, case_num)
234 dir = grading_result_dir(user_name,problem_name, submission_id, case_num)
234 dir = grading_result_dir(user_name,problem_name, submission_id, case_num)
235 return "#{dir}/output.txt"
235 return "#{dir}/output.txt"
236 end
236 end
237
237
238 def read_grading_result(user_name, problem_name, submission_id, case_num)
238 def read_grading_result(user_name, problem_name, submission_id, case_num)
239 dir = grading_result_dir(user_name,problem_name, submission_id, case_num)
239 dir = grading_result_dir(user_name,problem_name, submission_id, case_num)
240 result_file_name = "#{dir}/result"
240 result_file_name = "#{dir}/result"
241 if !FileTest.exists?(result_file_name)
241 if !FileTest.exists?(result_file_name)
242 return {:num => case_num, :msg => 'program did not run'}
242 return {:num => case_num, :msg => 'program did not run'}
243 else
243 else
244 results = File.open(result_file_name).readlines
244 results = File.open(result_file_name).readlines
245 run_stat = extract_running_stat(results)
245 run_stat = extract_running_stat(results)
246 output_filename = "#{dir}/output.txt"
246 output_filename = "#{dir}/output.txt"
247 if FileTest.exists?(output_filename)
247 if FileTest.exists?(output_filename)
248 output_file = true
248 output_file = true
249 output_size = File.size(output_filename)
249 output_size = File.size(output_filename)
250 else
250 else
251 output_file = false
251 output_file = false
252 output_size = 0
252 output_size = 0
253 end
253 end
254
254
255 return {
255 return {
256 :num => case_num,
256 :num => case_num,
257 :msg => results[0],
257 :msg => results[0],
258 :run_stat => run_stat,
258 :run_stat => run_stat,
259 :output => output_file,
259 :output => output_file,
260 :output_size => output_size
260 :output_size => output_size
261 }
261 }
262 end
262 end
263 end
263 end
264
264
265 # copied from grader/script/lib/test_request_helper.rb
265 # copied from grader/script/lib/test_request_helper.rb
266 def extract_running_stat(results)
266 def extract_running_stat(results)
267 running_stat_line = results[-1]
267 running_stat_line = results[-1]
268
268
269 # extract exit status line
269 # extract exit status line
270 run_stat = ""
270 run_stat = ""
271 if !(/[Cc]orrect/.match(results[0]))
271 if !(/[Cc]orrect/.match(results[0]))
272 run_stat = results[0].chomp
272 run_stat = results[0].chomp
273 else
273 else
274 run_stat = 'Program exited normally'
274 run_stat = 'Program exited normally'
275 end
275 end
276
276
277 logger.info "Stat line: #{running_stat_line}"
277 logger.info "Stat line: #{running_stat_line}"
278
278
279 # extract running time
279 # extract running time
280 if res = /r(.*)u(.*)s/.match(running_stat_line)
280 if res = /r(.*)u(.*)s/.match(running_stat_line)
281 seconds = (res[1].to_f + res[2].to_f)
281 seconds = (res[1].to_f + res[2].to_f)
282 time_stat = "Time used: #{seconds} sec."
282 time_stat = "Time used: #{seconds} sec."
283 else
283 else
284 seconds = nil
284 seconds = nil
285 time_stat = "Time used: n/a sec."
285 time_stat = "Time used: n/a sec."
286 end
286 end
287
287
288 # extract memory usage
288 # extract memory usage
289 if res = /s(.*)m/.match(running_stat_line)
289 if res = /s(.*)m/.match(running_stat_line)
290 memory_used = res[1].to_i
290 memory_used = res[1].to_i
291 else
291 else
292 memory_used = -1
292 memory_used = -1
293 end
293 end
294
294
295 return {
295 return {
296 :msg => "#{run_stat}\n#{time_stat}",
296 :msg => "#{run_stat}\n#{time_stat}",
297 :running_time => seconds,
297 :running_time => seconds,
298 :exit_status => run_stat,
298 :exit_status => run_stat,
299 :memory_usage => memory_used
299 :memory_usage => memory_used
300 }
300 }
301 end
301 end
302
302
303 end
303 end
304
304
@@ -1,9 +1,9
1 class Problem < ActiveRecord::Base
1 class Problem < ActiveRecord::Base
2
2
3 belongs_to :description
3 belongs_to :description
4
4
5 def self.find_available_problems
5 def self.find_available_problems
6 - find(:all, :conditions => {:available => true})
6 + find(:all, :conditions => {:available => true}, :order => "date_added DESC")
7 end
7 end
8
8
9 end
9 end
You need to be logged in to leave comments. Login now