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: 52 inserted, 7 deleted
@@ -5,13 +5,13 | |||
|
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 |
@@ -190,22 +190,54 | |||
|
190 | 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 |
@@ -3,7 +3,20 | |||
|
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] | |
|
9 | 16 | end |
|
17 | + return true | |
|
18 | + else | |
|
19 | + return false | |
|
20 | + end | |
|
21 | + end | |
|
22 | + end |
@@ -8,19 +8,19 | |||
|
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