Description:
added solution submission with grading
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r214:cdf8c2c3dce8 - - 3 files changed: 53 inserted, 8 deleted
@@ -1,301 +1,333 | |||
|
1 | 1 | class MainController < ApplicationController |
|
2 | 2 | |
|
3 | 3 | SYSTEM_MODE_CONF_KEY = 'system.mode' |
|
4 | 4 | |
|
5 | 5 | before_filter :authenticate, :except => [:index, :login] |
|
6 | 6 | before_filter :check_viewability, :except => [:index, :login] |
|
7 | 7 | |
|
8 | 8 | # COMMENTED OUT: filter in each action instead |
|
9 | 9 | # before_filter :verify_time_limit, :only => [:submit] |
|
10 | 10 | |
|
11 | - verify :method => :post, :only => [:submit], | |
|
11 | + verify :method => :post, :only => [:submit, :new_input, :download_input, :submit_solution], | |
|
12 | 12 | :redirect_to => { :action => :index } |
|
13 | 13 | |
|
14 | 14 | # COMMENT OUT: only need when having high load |
|
15 | 15 | # caches_action :index, :login |
|
16 | 16 | |
|
17 | 17 | # NOTE: This method is not actually needed, 'config/routes.rb' has |
|
18 | 18 | # assigned action login as a default action. |
|
19 | 19 | def index |
|
20 | 20 | redirect_to :action => 'login' |
|
21 | 21 | end |
|
22 | 22 | |
|
23 | 23 | def login |
|
24 | 24 | saved_notice = flash[:notice] |
|
25 | 25 | reset_session |
|
26 | 26 | flash[:notice] = saved_notice |
|
27 | 27 | |
|
28 | 28 | # EXPERIMENT: |
|
29 | 29 | # Hide login if in single user mode and the url does not |
|
30 | 30 | # explicitly specify /login |
|
31 | 31 | # |
|
32 | 32 | # logger.info "PATH: #{request.path}" |
|
33 | 33 | # if Configuration['system.single_user_mode'] and |
|
34 | 34 | # request.path!='/main/login' |
|
35 | 35 | # @hidelogin = true |
|
36 | 36 | # end |
|
37 | 37 | |
|
38 | 38 | @announcements = Announcement.find_for_frontpage |
|
39 | 39 | render :action => 'login', :layout => 'empty' |
|
40 | 40 | end |
|
41 | 41 | |
|
42 | 42 | def list |
|
43 | 43 | prepare_list_information |
|
44 | 44 | end |
|
45 | 45 | |
|
46 | 46 | def help |
|
47 | 47 | @user = User.find(session[:user_id]) |
|
48 | 48 | end |
|
49 | 49 | |
|
50 | 50 | def submit |
|
51 | 51 | user = User.find(session[:user_id]) |
|
52 | 52 | |
|
53 | 53 | @submission = Submission.new(params[:submission]) |
|
54 | 54 | @submission.user = user |
|
55 | 55 | @submission.language_id = 0 |
|
56 | 56 | if (params['file']) and (params['file']!='') |
|
57 | 57 | @submission.source = params['file'].read |
|
58 | 58 | @submission.source_filename = params['file'].original_filename |
|
59 | 59 | end |
|
60 | 60 | @submission.submitted_at = Time.new.gmtime |
|
61 | 61 | |
|
62 | 62 | if Configuration[SYSTEM_MODE_CONF_KEY]=='contest' and |
|
63 | 63 | user.site!=nil and user.site.finished? |
|
64 | 64 | @submission.errors.add_to_base "The contest is over." |
|
65 | 65 | prepare_list_information |
|
66 | 66 | render :action => 'list' and return |
|
67 | 67 | end |
|
68 | 68 | |
|
69 | 69 | if @submission.valid? |
|
70 | 70 | if @submission.save == false |
|
71 | 71 | flash[:notice] = 'Error saving your submission' |
|
72 | 72 | elsif Task.create(:submission_id => @submission.id, |
|
73 | 73 | :status => Task::STATUS_INQUEUE) == false |
|
74 | 74 | flash[:notice] = 'Error adding your submission to task queue' |
|
75 | 75 | end |
|
76 | 76 | else |
|
77 | 77 | prepare_list_information |
|
78 | 78 | render :action => 'list' and return |
|
79 | 79 | end |
|
80 | 80 | redirect_to :action => 'list' |
|
81 | 81 | end |
|
82 | 82 | |
|
83 | 83 | def source |
|
84 | 84 | submission = Submission.find(params[:id]) |
|
85 | 85 | if submission.user_id == session[:user_id] |
|
86 | 86 | send_data(submission.source, |
|
87 | 87 | {:filename => submission.download_filename, |
|
88 | 88 | :type => 'text/plain'}) |
|
89 | 89 | else |
|
90 | 90 | flash[:notice] = 'Error viewing source' |
|
91 | 91 | redirect_to :action => 'list' |
|
92 | 92 | end |
|
93 | 93 | end |
|
94 | 94 | |
|
95 | 95 | def compiler_msg |
|
96 | 96 | @submission = Submission.find(params[:id]) |
|
97 | 97 | if @submission.user_id == session[:user_id] |
|
98 | 98 | render :action => 'compiler_msg', :layout => 'empty' |
|
99 | 99 | else |
|
100 | 100 | flash[:notice] = 'Error viewing source' |
|
101 | 101 | redirect_to :action => 'list' |
|
102 | 102 | end |
|
103 | 103 | end |
|
104 | 104 | |
|
105 | 105 | def submission |
|
106 | 106 | @user = User.find(session[:user_id]) |
|
107 | 107 | @problems = Problem.find_available_problems |
|
108 | 108 | if params[:id]==nil |
|
109 | 109 | @problem = nil |
|
110 | 110 | @submissions = nil |
|
111 | 111 | else |
|
112 | 112 | @problem = Problem.find_by_name(params[:id]) |
|
113 | 113 | if not @problem.available |
|
114 | 114 | redirect_to :action => 'list' |
|
115 | 115 | flash[:notice] = 'Error: submissions for that problem are not viewable.' |
|
116 | 116 | return |
|
117 | 117 | end |
|
118 | 118 | @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id) |
|
119 | 119 | end |
|
120 | 120 | end |
|
121 | 121 | |
|
122 | 122 | def result |
|
123 | 123 | if !Configuration.show_grading_result |
|
124 | 124 | redirect_to :action => 'list' and return |
|
125 | 125 | end |
|
126 | 126 | @user = User.find(session[:user_id]) |
|
127 | 127 | @submission = Submission.find(params[:id]) |
|
128 | 128 | if @submission.user!=@user |
|
129 | 129 | flash[:notice] = 'You are not allowed to view result of other users.' |
|
130 | 130 | redirect_to :action => 'list' and return |
|
131 | 131 | end |
|
132 | 132 | prepare_grading_result(@submission) |
|
133 | 133 | end |
|
134 | 134 | |
|
135 | 135 | def load_output |
|
136 | 136 | if !Configuration.show_grading_result or params[:num]==nil |
|
137 | 137 | redirect_to :action => 'list' and return |
|
138 | 138 | end |
|
139 | 139 | @user = User.find(session[:user_id]) |
|
140 | 140 | @submission = Submission.find(params[:id]) |
|
141 | 141 | if @submission.user!=@user |
|
142 | 142 | flash[:notice] = 'You are not allowed to view result of other users.' |
|
143 | 143 | redirect_to :action => 'list' and return |
|
144 | 144 | end |
|
145 | 145 | case_num = params[:num].to_i |
|
146 | 146 | out_filename = output_filename(@user.login, |
|
147 | 147 | @submission.problem.name, |
|
148 | 148 | @submission.id, |
|
149 | 149 | case_num) |
|
150 | 150 | if !FileTest.exists?(out_filename) |
|
151 | 151 | flash[:notice] = 'Output not found.' |
|
152 | 152 | redirect_to :action => 'list' and return |
|
153 | 153 | end |
|
154 | 154 | |
|
155 | 155 | response.headers['Content-Type'] = "application/force-download" |
|
156 | 156 | response.headers['Content-Disposition'] = "attachment; filename=\"output-#{case_num}.txt\"" |
|
157 | 157 | response.headers["X-Sendfile"] = out_filename |
|
158 | 158 | response.headers['Content-length'] = File.size(out_filename) |
|
159 | 159 | render :nothing => true |
|
160 | 160 | end |
|
161 | 161 | |
|
162 | 162 | def error |
|
163 | 163 | @user = User.find(session[:user_id]) |
|
164 | 164 | end |
|
165 | 165 | |
|
166 | 166 | # announcement refreshing and hiding methods |
|
167 | 167 | |
|
168 | 168 | def announcements |
|
169 | 169 | if params.has_key? 'recent' |
|
170 | 170 | prepare_announcements(params[:recent]) |
|
171 | 171 | else |
|
172 | 172 | prepare_announcements |
|
173 | 173 | end |
|
174 | 174 | render(:partial => 'announcement', |
|
175 | 175 | :collection => @announcements, |
|
176 | 176 | :locals => {:announcement_effect => true}) |
|
177 | 177 | end |
|
178 | 178 | |
|
179 | 179 | # actions for Code Jom |
|
180 | 180 | def new_input |
|
181 | 181 | problem = Problem.find(params[:id]) |
|
182 | 182 | user = User.find(session[:user_id]) |
|
183 | 183 | if user.can_request_new_test_pair_for? problem |
|
184 | 184 | assignment = user.get_new_test_pair_assignment_for problem |
|
185 | 185 | assignment.save |
|
186 | 186 | |
|
187 | 187 | send_data(assignment.test_pair.input, |
|
188 | 188 | { :filename => "#{problem.name}-#{assignment.request_number}.in", |
|
189 | 189 | :type => 'text/plain' }) |
|
190 | - else | |
|
190 | + else | |
|
191 | 191 | flash[:notice] = 'You cannot request new input now.' |
|
192 | 192 | redirect_to :action => 'list' |
|
193 | 193 | end |
|
194 | 194 | end |
|
195 | 195 | |
|
196 | - def download | |
|
196 | + def download_input | |
|
197 | 197 | problem = Problem.find(params[:id]) |
|
198 | 198 | user = User.find(session[:user_id]) |
|
199 | 199 | recent_assignment = user.get_recent_test_pair_assignment_for problem |
|
200 | 200 | if recent_assignment != nil |
|
201 | 201 | send_data(recent_assignment.test_pair.input, |
|
202 | 202 | { :filename => "#{problem.name}-#{recent_assignment.request_number}.in", |
|
203 | 203 | :type => 'text/plain' }) |
|
204 | 204 | else |
|
205 | - flash[:notice] = 'You have not request for any input data for this problem.' | |
|
205 | + flash[:notice] = 'You have not requested for any input data for this problem.' | |
|
206 | + redirect_to :action => 'list' | |
|
207 | + end | |
|
208 | + end | |
|
209 | + | |
|
210 | + def submit_solution | |
|
211 | + problem = Problem.find(params[:id]) | |
|
212 | + user = User.find(session[:user_id]) | |
|
213 | + recent_assignment = user.get_recent_test_pair_assignment_for problem | |
|
214 | + if recent_assignment != nil | |
|
215 | + submitted_solution = params[:file].read | |
|
216 | + test_pair = recent_assignment.test_pair | |
|
217 | + passed = test_pair.grade(submitted_solution) | |
|
218 | + points = passed ? 100 : 0 | |
|
219 | + submission = Submission.new(:user => user, | |
|
220 | + :problem => problem, | |
|
221 | + :source => params[:file].read, | |
|
222 | + :source_filename => params['file'].original_filename, | |
|
223 | + :language_id => 0, | |
|
224 | + :submitted_at => Time.new.gmtime, | |
|
225 | + :graded_at => Time.new.gmtime, | |
|
226 | + :points => points) | |
|
227 | + submission.save | |
|
228 | + recent_assignment.submitted = true | |
|
229 | + recent_assignment.save | |
|
230 | + if passed | |
|
231 | + flash[:notice] = 'Correct solution' | |
|
232 | + else | |
|
233 | + flash[:notice] = 'Incorrect solution' | |
|
234 | + end | |
|
235 | + redirect_to :action => 'list' | |
|
236 | + else | |
|
237 | + flash[:notice] = 'You have not requested for any input data for this problem.' | |
|
206 | 238 | redirect_to :action => 'list' |
|
207 | 239 | end |
|
208 | 240 | end |
|
209 | 241 | |
|
210 | 242 | protected |
|
211 | 243 | |
|
212 | 244 | def prepare_announcements(recent=nil) |
|
213 | 245 | if Configuration.show_tasks_to?(@user) |
|
214 | 246 | @announcements = Announcement.find_published(true) |
|
215 | 247 | else |
|
216 | 248 | @announcements = Announcement.find_published |
|
217 | 249 | end |
|
218 | 250 | if recent!=nil |
|
219 | 251 | recent_id = recent.to_i |
|
220 | 252 | @announcements = @announcements.find_all { |a| a.id > recent_id } |
|
221 | 253 | end |
|
222 | 254 | end |
|
223 | 255 | |
|
224 | 256 | def prepare_list_information |
|
225 | 257 | @problems = Problem.find_available_problems |
|
226 | 258 | @prob_submissions = Array.new |
|
227 | 259 | @user = User.find(session[:user_id]) |
|
228 | 260 | @problems.each do |p| |
|
229 | 261 | sub = Submission.find_last_by_user_and_problem(@user.id,p.id) |
|
230 | 262 | if sub!=nil |
|
231 | 263 | @prob_submissions << { :count => sub.number, :submission => sub } |
|
232 | 264 | else |
|
233 | 265 | @prob_submissions << { :count => 0, :submission => nil } |
|
234 | 266 | end |
|
235 | 267 | end |
|
236 | 268 | prepare_announcements |
|
237 | 269 | end |
|
238 | 270 | |
|
239 | 271 | def check_viewability |
|
240 | 272 | @user = User.find(session[:user_id]) |
|
241 | 273 | if (!Configuration.show_tasks_to?(@user)) and |
|
242 | 274 | ((action_name=='submission') or (action_name=='submit')) |
|
243 | 275 | redirect_to :action => 'list' and return |
|
244 | 276 | end |
|
245 | 277 | end |
|
246 | 278 | |
|
247 | 279 | def prepare_grading_result(submission) |
|
248 | 280 | if Configuration.task_grading_info.has_key? submission.problem.name |
|
249 | 281 | grading_info = Configuration.task_grading_info[submission.problem.name] |
|
250 | 282 | else |
|
251 | 283 | # guess task info from problem.full_score |
|
252 | 284 | cases = submission.problem.full_score / 10 |
|
253 | 285 | grading_info = { |
|
254 | 286 | 'testruns' => cases, |
|
255 | 287 | 'testcases' => cases |
|
256 | 288 | } |
|
257 | 289 | end |
|
258 | 290 | @test_runs = [] |
|
259 | 291 | if grading_info['testruns'].is_a? Integer |
|
260 | 292 | trun_count = grading_info['testruns'] |
|
261 | 293 | trun_count.times do |i| |
|
262 | 294 | @test_runs << [ read_grading_result(@user.login, |
|
263 | 295 | submission.problem.name, |
|
264 | 296 | submission.id, |
|
265 | 297 | i+1) ] |
|
266 | 298 | end |
|
267 | 299 | else |
|
268 | 300 | grading_info['testruns'].keys.sort.each do |num| |
|
269 | 301 | run = [] |
|
270 | 302 | testrun = grading_info['testruns'][num] |
|
271 | 303 | testrun.each do |c| |
|
272 | 304 | run << read_grading_result(@user.login, |
|
273 | 305 | submission.problem.name, |
|
274 | 306 | submission.id, |
|
275 | 307 | c) |
|
276 | 308 | end |
|
277 | 309 | @test_runs << run |
|
278 | 310 | end |
|
279 | 311 | end |
|
280 | 312 | end |
|
281 | 313 | |
|
282 | 314 | def grading_result_dir(user_name, problem_name, submission_id, case_num) |
|
283 | 315 | return "#{GRADING_RESULT_DIR}/#{user_name}/#{problem_name}/#{submission_id}/test-result/#{case_num}" |
|
284 | 316 | end |
|
285 | 317 | |
|
286 | 318 | def output_filename(user_name, problem_name, submission_id, case_num) |
|
287 | 319 | dir = grading_result_dir(user_name,problem_name, submission_id, case_num) |
|
288 | 320 | return "#{dir}/output.txt" |
|
289 | 321 | end |
|
290 | 322 | |
|
291 | 323 | def read_grading_result(user_name, problem_name, submission_id, case_num) |
|
292 | 324 | dir = grading_result_dir(user_name,problem_name, submission_id, case_num) |
|
293 | 325 | result_file_name = "#{dir}/result" |
|
294 | 326 | if !FileTest.exists?(result_file_name) |
|
295 | 327 | return {:num => case_num, :msg => 'program did not run'} |
|
296 | 328 | else |
|
297 | 329 | results = File.open(result_file_name).readlines |
|
298 | 330 | run_stat = extract_running_stat(results) |
|
299 | 331 | output_filename = "#{dir}/output.txt" |
|
300 | 332 | if FileTest.exists?(output_filename) |
|
301 | 333 | output_file = true |
@@ -1,9 +1,22 | |||
|
1 | 1 | # TestPair stores an input-solution pair for a problem. This is used |
|
2 | 2 | # in a certain "test-pair"-type problem for the CodeJom competition |
|
3 | 3 | # which follows the Google Code Jam format, i.e., a participant only |
|
4 | 4 | # submits a solution to a single random input that the participant |
|
5 | 5 | # requested. This input-solution pair is a TestPair. |
|
6 | 6 | |
|
7 | 7 | class TestPair < ActiveRecord::Base |
|
8 | 8 | belongs_to :problem |
|
9 | + | |
|
10 | + def grade(submitted_solution) | |
|
11 | + sols = solution.split | |
|
12 | + subs = submitted_solution.split | |
|
13 | + if sols.length == subs.length | |
|
14 | + subs.length.times do |i| | |
|
15 | + return false if subs[i]!=sols[i] | |
|
16 | + end | |
|
17 | + return true | |
|
18 | + else | |
|
19 | + return false | |
|
20 | + end | |
|
21 | + end | |
|
9 | 22 | end |
@@ -1,26 +1,26 | |||
|
1 | 1 | <tr class="info-<%= (problem_counter%2==0) ? "even" : "odd" %>"> |
|
2 | 2 | <td> |
|
3 | 3 | <%= "#{problem_counter+1}" %> |
|
4 | 4 | </td> |
|
5 | 5 | <td> |
|
6 | 6 | <%= "#{problem.full_name} (#{problem.name})" %> |
|
7 | 7 | <%= link_to "[#{t 'main.problem_desc'}]", problem.url, :popup => true if (problem.url!=nil) and (problem.url!='') %> |
|
8 | 8 | </td> |
|
9 | 9 | <td align="center"> |
|
10 | 10 | <%= @prob_submissions[problem_counter][:count] %> |
|
11 | 11 | </td> |
|
12 | 12 | <td> |
|
13 | 13 | <span id="problem-form-<%= problem.id %>"> |
|
14 |
- <% form_tag |
|
|
14 | + <% form_tag({:action => 'new_input', :id => problem.id}, :method => :post) do -%> | |
|
15 | 15 | <input type="submit" value="New input"/> |
|
16 | 16 | <% end -%> |
|
17 |
- <% form_tag |
|
|
17 | + <% form_tag({ :action => 'download_input', :id => problem.id }, :method => :post) do -%> | |
|
18 | 18 | <input type="submit" value="Download input"/> |
|
19 | 19 | <% end -%> |
|
20 |
- <% form_tag |
|
|
21 | - <input type="file"> | |
|
20 | + <% form_tag({ :action => 'submit_solution', :id => problem.id }, :method => :post, :multipart => true) do -%> | |
|
21 | + <input type="file" name="file"> | |
|
22 | 22 | <input type="submit" value="Submit solution"/> |
|
23 | 23 | <% end -%> |
|
24 | 24 | </span> |
|
25 | 25 | </td> |
|
26 | 26 | </tr> |
You need to be logged in to leave comments.
Login now