# HG changeset patch # User Jittat Fakcharoenphol # Date 2015-01-29 03:09:12 # Node ID 31aa87b1b1e99dd81b875999db78e42a7c7200d4 # Parent c141e39c91184fa8044ad363e5d1423726eeee68 saves submitted output, checks time outs; updated submission front-end flows diff --git a/app/assets/javascripts/toicontest.js b/app/assets/javascripts/toicontest.js --- a/app/assets/javascripts/toicontest.js +++ b/app/assets/javascripts/toicontest.js @@ -1,7 +1,9 @@ var TOIContest = { NO_TIMEOUT: -1, + SUBMISSION_TIMEOUT: 300, timeOuts: {}, + timeStarted: 0, problemSelectClick: function() { $$(".submission-submit-divs").each(function(item) { @@ -14,8 +16,18 @@ $("submission_submit_div_" + problem_id + "_id").show(); }, - confirmDownload: function() { - return confirm("แน่ใจ?"); + confirmDownload: function(pid) { + result = confirm("คุณแน่ใจที่จะส่งข้อนี้หรือไม่?\nเมื่อคุณดาวน์โหลดข้อมูลชุดทดสอบแล้ว คุณจะต้องส่งข้อมูลส่งออกและโปรแกรมภายในเวลา 5 นาที"); + if ( result ) { + if ( TOIContest.timeOuts[ pid ] == TOIContest.NO_TIMEOUT ) { + TOIContest.refreshTimeOuts(); + + TOIContest.timeOuts[ pid ] = TOIContest.SUBMISSION_TIMEOUT; + + TOIContest.refreshTimeOutMessages(); + } + } + return result; }, refreshTimeOutMessages: function() { @@ -26,12 +38,40 @@ var minLeft = parseInt(timeOut / 60); var secLeft = parseInt(timeOut % 60); $('submission_time_left_' + pid + '_id').innerHTML = '| เหลือเวลาอีก ' + minLeft + ':' + secLeft + ' นาที'; + $('submission_form_'+ pid + '_id').show(); } else { $('submission_time_left_' + pid + '_id').innerHTML = '| หมดเวลาส่ง'; $('submission_form_'+ pid + '_id').hide(); } + } else { + $('submission_form_'+ pid + '_id').hide(); } } - } + }, + + refreshTimeOuts: function() { + if ( TOIContest.timeStarted == 0 ) { + TOIContest.timeStarted = (new Date()).getTime(); + } + + var timeElapsed = ((new Date()).getTime() - TOIContest.timeStarted)/1000; + for ( var pid in TOIContest.timeOuts ) { + var timeOut = TOIContest.timeOuts[ pid ]; + if ( timeOut > timeElapsed ) { + TOIContest.timeOuts[ pid ] -= timeElapsed; + } else if ( timeOut > 0 ) { + TOIContest.timeOuts[ pid ] = 0; + } + } + }, + + registerRefreshEvent: function() { + TOIContest.timeStarted = (new Date()).getTime(); + setTimeout(function () { + TOIContest.refreshTimeOuts(); + TOIContest.refreshTimeOutMessages(); + TOIContest.registerRefreshEvent(); + }, 1000); + }, }; diff --git a/app/controllers/main_controller.rb b/app/controllers/main_controller.rb --- a/app/controllers/main_controller.rb +++ b/app/controllers/main_controller.rb @@ -1,3 +1,4 @@ +# coding: utf-8 class MainController < ApplicationController before_filter :authenticate, :except => [:index, :login] @@ -48,6 +49,10 @@ end def list + if session[:current_problem_id] + @current_problem = Problem.find(session[:current_problem_id]) + session[:current_problem_id] = nil + end prepare_list_information end @@ -59,13 +64,35 @@ user = User.find(session[:user_id]) @submission = Submission.new - @submission.problem_id = params[:submission][:problem_id] + @current_problem = Problem.find(params[:submission][:problem_id]) + + if !@current_problem + flash[:notice] = 'Error: คุณยังไม่ได้ระบุข้อที่ต้องการส่ง' + redirect_to :action => 'list' + end + + assignment = user.get_test_pair_assignment_for(@current_problem) + if !assignment + flash[:notice] = 'Error: คุณยังไม่ได้ดาวน์โหลดข้อมูลทดสอบ' + prepare_list_information + render :action => 'list' and return + end + if assignment.expired? + flash[:notice] = 'Error: หมดเวลาส่งสำหรับข้อนี้' + prepare_list_information + render :action => 'list' and return + end + + @submission.problem = @current_problem @submission.user = user @submission.language_id = 0 if (params['file']) and (params['file']!='') @submission.source = params['file'].read @submission.source_filename = params['file'].original_filename end + if (params['output_file']) and (params['output_file']!='') + @submission.output = params['output_file'].read + end @submission.submitted_at = Time.new.gmtime if GraderConfiguration.time_limit_mode? and user.contest_finished? @@ -80,11 +107,17 @@ elsif Task.create(:submission_id => @submission.id, :status => Task::STATUS_INQUEUE) == false flash[:notice] = 'Error adding your submission to task queue' + else + flash[:notice] = 'จัดเก็บคำตอบและโปรแกรมที่คุณส่งเรียบร้อย' end else prepare_list_information render :action => 'list' and return end + + if @current_problem + session[:current_problem_id] = @current_problem.id + end redirect_to :action => 'list' end @@ -225,15 +258,11 @@ test_pair = TestPair.get_for(problem, true) user = User.find(session[:user_id]) - assignent = user.get_test_pair_assignment_for(problem) + assignment = user.get_test_pair_assignment_for(problem) - if !assignent - assignent = TestPairAssignment.new - assignent.user = user - assignent.problem = problem - assignent.test_pair = test_pair - assignent.submitted = false - assignent.save + if !assignment + assignment = TestPairAssignment.create_for(user, problem, test_pair) + assignment.save end send_data(test_pair.input, @@ -252,17 +281,18 @@ redirect_to :action => 'list' and return end + @current_problem = problem test_pair = TestPair.get_for(problem, false) if (params['output_file']) and (params['output_file']!='') output = params['output_file'].read - @current_problem = problem @grading_result = grade(output, test_pair.solution) prepare_list_information render :action => 'list' and return else flash[:notice] = 'Error: output file errors' - redirect_to :action => 'list' + prepare_list_information + render :action => 'list' and return end end diff --git a/app/models/submission.rb b/app/models/submission.rb --- a/app/models/submission.rb +++ b/app/models/submission.rb @@ -10,6 +10,11 @@ validates_presence_of :source validates_length_of :source, :maximum => 100_000, :allow_blank => true, :message => 'too long' validates_length_of :source, :minimum => 1, :allow_blank => true, :message => 'too short' + + validates_presence_of :output + validates_length_of :output, :maximum => 100_000, :allow_blank => true, :message => 'too long' + validates_length_of :output, :minimum => 1, :allow_blank => true, :message => 'too short' + validate :must_have_valid_problem validate :must_specify_language diff --git a/app/models/test_pair_assignment.rb b/app/models/test_pair_assignment.rb --- a/app/models/test_pair_assignment.rb +++ b/app/models/test_pair_assignment.rb @@ -6,4 +6,14 @@ def expired? return created_at + TEST_ASSIGNMENT_EXPIRATION_DURATION < Time.new.gmtime end + + def self.create_for(user, problem, test_pair) + assignment = TestPairAssignment.new + assignment.user = user + assignment.problem = problem + assignment.test_pair = test_pair + assignment.submitted = false + assignment.save + return assignment + end end diff --git a/app/views/main/_submission_box.html.erb b/app/views/main/_submission_box.html.erb --- a/app/views/main/_submission_box.html.erb +++ b/app/views/main/_submission_box.html.erb @@ -1,13 +1,14 @@ +<% selected_problem_id = @current_problem ? @current_problem.id : -1 %>
Problem: <%= select 'submission', 'problem_id', [['กรุณาเลือกข้อที่ต้องการส่งหรือทดสอบ','-1']] + @problems.collect {|p| [p.full_name, p.id]}, - { :selected => '-1' }, + { :selected => selected_problem_id }, { :onchange => 'TOIContest.problemSelectClick()' } %>
<% @problems.each do |problem| %> -
+