# HG changeset patch # User Jittat Fakcharoenphol # Date 2010-01-28 02:05:35 # Node ID 9e664dd718889dcc0adcea5dee55170ccd383922 # Parent eda4ffd09089b70acbdb2b8fa0c69f88fd3c796e added timeout related javascripts 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 @@ -272,6 +272,24 @@ end end + def prepare_timeout_information(problems) + @submission_timeouts = {} + problems.each do |problem| + assignment = @user.get_recent_test_pair_assignment_for(problem) + if assignment == nil + timeout = nil + else + if assignment.expired? + timeout = 0 + else + timeout = assignment.created_at + TEST_ASSIGNMENT_EXPIRATION_DURATION - Time.new.gmtime + end + end + @submission_timeouts[problem.id] = timeout + end + @submission_timeouts.each_pair {|k,v| puts "#{k} => #{v}"} + end + def prepare_list_information @user = User.find(session[:user_id]) @@ -295,6 +313,8 @@ @problems = all_problems.reject { |problem| passed.has_key? problem.id } + prepare_timeout_information(@problems) + @prob_submissions = Array.new @problems.each do |p| if sub_count.has_key? p.id diff --git a/app/views/main/_problem.html.haml b/app/views/main/_problem.html.haml --- a/app/views/main/_problem.html.haml +++ b/app/views/main/_problem.html.haml @@ -2,7 +2,7 @@ .problem-form{:id => "problem-form-#{problem.id}"} - form_tag({ :action => 'download_input', :id => problem.id }, :method => :post) do %b Input: - %input{:type => "submit", :value => "Download input"} + %input{:type => "submit", :value => "Download input", :onclick => "CodejomTimeout.updateTimeoutAfterDownloadClick(#{problem.id}); return true;"} %span{:id => "problem-timing-message-#{problem.id}"} = "After downloading, you have #{TEST_ASSIGNMENT_EXPIRATION_DURATION/60} minutes to submit." %div{:id => "problem-submission-form-#{problem.id}"} diff --git a/app/views/main/_submission_timeouts.html.haml b/app/views/main/_submission_timeouts.html.haml new file mode 100644 --- /dev/null +++ b/app/views/main/_submission_timeouts.html.haml @@ -0,0 +1,7 @@ +CodejomTimeout.timeouts = [ +- @submission_timeouts.each_pair do |id, t| + - if t!=nil + = "{problem: #{id}, timeout: #{t}}," + - else + = "{problem: #{id}, timeout: null}," +]; diff --git a/app/views/main/list.html.haml b/app/views/main/list.html.haml --- a/app/views/main/list.html.haml +++ b/app/views/main/list.html.haml @@ -1,6 +1,7 @@ - content_for :head do = javascript_include_tag :defaults = javascript_include_tag 'announcement_refresh.js' + = javascript_include_tag 'codejom_timeout.js' = user_title_bar(@user) @@ -28,6 +29,9 @@ %br{:clear=>'both'}/ %hr/ -:javascript +%script{:type => "text/javascript"} Announcement.registerRefreshEventTimer(); + = render :partial => 'submission_timeouts' + CodejomTimeout.updateProblemMessages(); + CodejomTimeout.registerRefreshEvent(); diff --git a/public/javascripts/codejom_timeout.js b/public/javascripts/codejom_timeout.js new file mode 100644 --- /dev/null +++ b/public/javascripts/codejom_timeout.js @@ -0,0 +1,58 @@ +var CodejomTimeout = { + + timeStarted: null, + + inputDataDuration: 5, // 5 minutes + + timeouts: [], + + updateProblemMessages: function() { + CodejomTimeout.timeouts.each(function(data) { + if(data.timeout==null) { + $("problem-submission-form-" + data.problem).hide(); + } else if(data.timeout==0) { + $("problem-timing-message-" + data.problem).innerHTML = + "The recent input data is expired. Please download a new one. You'll have 5 minute to submit."; + $("problem-submission-form-" + data.problem).hide(); + } else { + $("problem-timing-message-" + data.problem).innerHTML = + "You have about " + parseInt(data.timeout/60) + " minute(s) and " + parseInt(data.timeout % 60) + " second(s) to submit."; + $("problem-submission-form-" + data.problem).show(); + } + }); + }, + + refreshProblemMessages: function() { + var timeElapsed = ((new Date()).getTime() - CodejomTimeout.timeStarted)/1000; + // update timeout info + CodejomTimeout.timeouts.each(function(data) { + if(data.timeout > timeElapsed) { + data.timeout -= timeElapsed; + } else if(data.timeout > 0) { + data.timeout = 0; + } + }); + + CodejomTimeout.updateProblemMessages(); + CodejomTimeout.registerRefreshEvent(); + }, + + registerRefreshEvent: function() { + CodejomTimeout.timeStarted = (new Date()).getTime(), + setTimeout(function () { + CodejomTimeout.refreshProblemMessages(); + }, 2700); + }, + + updateTimeoutAfterDownloadClick: function(problem) { + CodejomTimeout.timeouts + .filter(function(data) { return data.problem==problem; }) + .each(function(data) { + if(data.timeout==0 || data.timeout==null) { + // TODO: use value from rails app. + data.timeout = CodejomTimeout.inputDataDuration * 60; + } + }); + CodejomTimeout.updateProblemMessages(); + }, +};