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,59 +1,59 | |||
|
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 |
@@ -142,112 +142,144 | |||
|
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 = { |
@@ -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