Show More
Commit Description:
Fixed Individual Contest confirmation
Commit Description:
Fixed Individual Contest confirmation
References:
File last commit:
Show/Diff file:
Action:
app/controllers/main_controller.rb
| 381 lines
| 11.4 KiB
| text/x-ruby
| RubyLexer
|
|
r0 | class MainController < ApplicationController | ||
before_filter :authenticate, :except => [:index, :login] | ||||
|
r123 | before_filter :check_viewability, :except => [:index, :login] | ||
|
r0 | |||
|
r302 | append_before_filter :confirm_and_update_start_time, | ||
:except => [:index, | ||||
:login, | ||||
:confirm_contest_start] | ||||
|
r247 | |||
|
r249 | # to prevent log in box to be shown when user logged out of the | ||
# system only in some tab | ||||
|
r302 | prepend_before_filter :reject_announcement_refresh_when_logged_out, | ||
:only => [:announcements] | ||||
|
r249 | |||
|
r160 | # COMMENTED OUT: filter in each action instead | ||
# before_filter :verify_time_limit, :only => [:submit] | ||||
|
r85 | |||
|
r0 | verify :method => :post, :only => [:submit], | ||
:redirect_to => { :action => :index } | ||||
|
r160 | # COMMENT OUT: only need when having high load | ||
# caches_action :index, :login | ||||
|
r15 | |||
|
r160 | # NOTE: This method is not actually needed, 'config/routes.rb' has | ||
# assigned action login as a default action. | ||||
|
r0 | def index | ||
|
r6 | redirect_to :action => 'login' | ||
|
r0 | end | ||
def login | ||||
|
r76 | saved_notice = flash[:notice] | ||
|
r0 | reset_session | ||
|
r229 | flash.now[:notice] = saved_notice | ||
|
r76 | |||
|
r160 | # EXPERIMENT: | ||
# Hide login if in single user mode and the url does not | ||||
# explicitly specify /login | ||||
|
r123 | # | ||
|
r160 | # logger.info "PATH: #{request.path}" | ||
|
r320 | # if GraderConfiguration['system.single_user_mode'] and | ||
|
r160 | # request.path!='/main/login' | ||
# @hidelogin = true | ||||
# end | ||||
|
r151 | @announcements = Announcement.find_for_frontpage | ||
|
r27 | render :action => 'login', :layout => 'empty' | ||
|
r0 | end | ||
def list | ||||
|
r31 | prepare_list_information | ||
|
r0 | end | ||
|
r107 | def help | ||
@user = User.find(session[:user_id]) | ||||
end | ||||
|
r0 | def submit | ||
|
r85 | user = User.find(session[:user_id]) | ||
|
r321 | @submission = Submission.new | ||
@submission.problem_id = params[:submission][:problem_id] | ||||
|
r85 | @submission.user = user | ||
|
r31 | @submission.language_id = 0 | ||
|
r169 | if (params['file']) and (params['file']!='') | ||
|
r99 | @submission.source = params['file'].read | ||
@submission.source_filename = params['file'].original_filename | ||||
end | ||||
|
r121 | @submission.submitted_at = Time.new.gmtime | ||
|
r85 | |||
|
r320 | if GraderConfiguration.time_limit_mode? and user.contest_finished? | ||
r347 | @submission.errors.add(:base,"The contest is over.") | |||
|
r85 | prepare_list_information | ||
render :action => 'list' and return | ||||
end | ||||
|
r31 | if @submission.valid? | ||
if @submission.save == false | ||||
|
r0 | flash[:notice] = 'Error saving your submission' | ||
|
r31 | elsif Task.create(:submission_id => @submission.id, | ||
:status => Task::STATUS_INQUEUE) == false | ||||
|
r0 | flash[:notice] = 'Error adding your submission to task queue' | ||
end | ||||
|
r32 | else | ||
prepare_list_information | ||||
render :action => 'list' and return | ||||
|
r0 | end | ||
|
r32 | redirect_to :action => 'list' | ||
|
r0 | end | ||
|
r70 | def source | ||
|
r0 | submission = Submission.find(params[:id]) | ||
|
r305 | if ((submission.user_id == session[:user_id]) and | ||
(submission.problem != nil) and | ||||
(submission.problem.available)) | ||||
|
r0 | send_data(submission.source, | ||
|
r168 | {:filename => submission.download_filename, | ||
|
r0 | :type => 'text/plain'}) | ||
else | ||||
flash[:notice] = 'Error viewing source' | ||||
|
r44 | redirect_to :action => 'list' | ||
|
r36 | end | ||
end | ||||
|
r70 | def compiler_msg | ||
@submission = Submission.find(params[:id]) | ||||
if @submission.user_id == session[:user_id] | ||||
render :action => 'compiler_msg', :layout => 'empty' | ||||
else | ||||
flash[:notice] = 'Error viewing source' | ||||
redirect_to :action => 'list' | ||||
end | ||||
end | ||||
|
r51 | def submission | ||
@user = User.find(session[:user_id]) | ||||
|
r288 | @problems = @user.available_problems | ||
|
r51 | if params[:id]==nil | ||
@problem = nil | ||||
@submissions = nil | ||||
else | ||||
@problem = Problem.find_by_name(params[:id]) | ||||
|
r153 | if not @problem.available | ||
redirect_to :action => 'list' | ||||
|
r154 | flash[:notice] = 'Error: submissions for that problem are not viewable.' | ||
|
r153 | return | ||
end | ||||
|
r51 | @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id) | ||
end | ||||
end | ||||
|
r134 | def result | ||
|
r320 | if !GraderConfiguration.show_grading_result | ||
|
r134 | redirect_to :action => 'list' and return | ||
end | ||||
@user = User.find(session[:user_id]) | ||||
@submission = Submission.find(params[:id]) | ||||
if @submission.user!=@user | ||||
flash[:notice] = 'You are not allowed to view result of other users.' | ||||
redirect_to :action => 'list' and return | ||||
end | ||||
prepare_grading_result(@submission) | ||||
end | ||||
def load_output | ||||
|
r320 | if !GraderConfiguration.show_grading_result or params[:num]==nil | ||
|
r134 | redirect_to :action => 'list' and return | ||
end | ||||
@user = User.find(session[:user_id]) | ||||
@submission = Submission.find(params[:id]) | ||||
if @submission.user!=@user | ||||
flash[:notice] = 'You are not allowed to view result of other users.' | ||||
redirect_to :action => 'list' and return | ||||
end | ||||
case_num = params[:num].to_i | ||||
out_filename = output_filename(@user.login, | ||||
@submission.problem.name, | ||||
@submission.id, | ||||
case_num) | ||||
if !FileTest.exists?(out_filename) | ||||
flash[:notice] = 'Output not found.' | ||||
redirect_to :action => 'list' and return | ||||
end | ||||
|
r291 | if defined?(USE_APACHE_XSENDFILE) and USE_APACHE_XSENDFILE | ||
response.headers['Content-Type'] = "application/force-download" | ||||
response.headers['Content-Disposition'] = "attachment; filename=\"output-#{case_num}.txt\"" | ||||
response.headers["X-Sendfile"] = out_filename | ||||
response.headers['Content-length'] = File.size(out_filename) | ||||
render :nothing => true | ||||
else | ||||
send_file out_filename, :stream => false, :filename => "output-#{case_num}.txt", :type => "text/plain" | ||||
end | ||||
|
r134 | end | ||
|
r109 | def error | ||
@user = User.find(session[:user_id]) | ||||
end | ||||
|
r192 | # announcement refreshing and hiding methods | ||
def announcements | ||||
if params.has_key? 'recent' | ||||
prepare_announcements(params[:recent]) | ||||
else | ||||
prepare_announcements | ||||
end | ||||
render(:partial => 'announcement', | ||||
:collection => @announcements, | ||||
:locals => {:announcement_effect => true}) | ||||
end | ||||
|
r302 | def confirm_contest_start | ||
user = User.find(session[:user_id]) | ||||
|
r371 | if request.method == 'POST' | ||
|
r302 | user.update_start_time | ||
redirect_to :action => 'list' | ||||
else | ||||
@contests = user.contests | ||||
@user = user | ||||
end | ||||
end | ||||
|
r31 | protected | ||
|
r192 | |||
def prepare_announcements(recent=nil) | ||||
|
r320 | if GraderConfiguration.show_tasks_to?(@user) | ||
|
r192 | @announcements = Announcement.find_published(true) | ||
else | ||||
@announcements = Announcement.find_published | ||||
end | ||||
if recent!=nil | ||||
recent_id = recent.to_i | ||||
@announcements = @announcements.find_all { |a| a.id > recent_id } | ||||
end | ||||
end | ||||
|
r31 | def prepare_list_information | ||
@user = User.find(session[:user_id]) | ||||
|
r320 | if not GraderConfiguration.multicontests? | ||
|
r292 | @problems = @user.available_problems | ||
|
r278 | else | ||
|
r288 | @contest_problems = @user.available_problems_group_by_contests | ||
@problems = @user.available_problems | ||||
|
r278 | end | ||
@prob_submissions = {} | ||||
|
r31 | @problems.each do |p| | ||
|
r35 | sub = Submission.find_last_by_user_and_problem(@user.id,p.id) | ||
if sub!=nil | ||||
|
r278 | @prob_submissions[p.id] = { :count => sub.number, :submission => sub } | ||
|
r35 | else | ||
|
r278 | @prob_submissions[p.id] = { :count => 0, :submission => nil } | ||
|
r35 | end | ||
|
r31 | end | ||
|
r192 | prepare_announcements | ||
|
r31 | end | ||
|
r122 | def check_viewability | ||
|
r124 | @user = User.find(session[:user_id]) | ||
|
r320 | if (!GraderConfiguration.show_tasks_to?(@user)) and | ||
|
r122 | ((action_name=='submission') or (action_name=='submit')) | ||
redirect_to :action => 'list' and return | ||||
end | ||||
end | ||||
|
r134 | def prepare_grading_result(submission) | ||
|
r320 | if GraderConfiguration.task_grading_info.has_key? submission.problem.name | ||
grading_info = GraderConfiguration.task_grading_info[submission.problem.name] | ||||
|
r197 | else | ||
# guess task info from problem.full_score | ||||
cases = submission.problem.full_score / 10 | ||||
grading_info = { | ||||
'testruns' => cases, | ||||
'testcases' => cases | ||||
} | ||||
end | ||||
|
r134 | @test_runs = [] | ||
if grading_info['testruns'].is_a? Integer | ||||
trun_count = grading_info['testruns'] | ||||
trun_count.times do |i| | ||||
@test_runs << [ read_grading_result(@user.login, | ||||
submission.problem.name, | ||||
submission.id, | ||||
i+1) ] | ||||
end | ||||
else | ||||
grading_info['testruns'].keys.sort.each do |num| | ||||
run = [] | ||||
testrun = grading_info['testruns'][num] | ||||
testrun.each do |c| | ||||
run << read_grading_result(@user.login, | ||||
submission.problem.name, | ||||
submission.id, | ||||
c) | ||||
end | ||||
@test_runs << run | ||||
end | ||||
end | ||||
end | ||||
def grading_result_dir(user_name, problem_name, submission_id, case_num) | ||||
return "#{GRADING_RESULT_DIR}/#{user_name}/#{problem_name}/#{submission_id}/test-result/#{case_num}" | ||||
end | ||||
def output_filename(user_name, problem_name, submission_id, case_num) | ||||
dir = grading_result_dir(user_name,problem_name, submission_id, case_num) | ||||
return "#{dir}/output.txt" | ||||
end | ||||
def read_grading_result(user_name, problem_name, submission_id, case_num) | ||||
dir = grading_result_dir(user_name,problem_name, submission_id, case_num) | ||||
result_file_name = "#{dir}/result" | ||||
if !FileTest.exists?(result_file_name) | ||||
return {:num => case_num, :msg => 'program did not run'} | ||||
else | ||||
results = File.open(result_file_name).readlines | ||||
run_stat = extract_running_stat(results) | ||||
output_filename = "#{dir}/output.txt" | ||||
if FileTest.exists?(output_filename) | ||||
output_file = true | ||||
output_size = File.size(output_filename) | ||||
else | ||||
output_file = false | ||||
output_size = 0 | ||||
end | ||||
return { | ||||
:num => case_num, | ||||
:msg => results[0], | ||||
:run_stat => run_stat, | ||||
:output => output_file, | ||||
:output_size => output_size | ||||
} | ||||
end | ||||
end | ||||
# copied from grader/script/lib/test_request_helper.rb | ||||
def extract_running_stat(results) | ||||
running_stat_line = results[-1] | ||||
# extract exit status line | ||||
run_stat = "" | ||||
if !(/[Cc]orrect/.match(results[0])) | ||||
run_stat = results[0].chomp | ||||
else | ||||
run_stat = 'Program exited normally' | ||||
end | ||||
logger.info "Stat line: #{running_stat_line}" | ||||
# extract running time | ||||
if res = /r(.*)u(.*)s/.match(running_stat_line) | ||||
seconds = (res[1].to_f + res[2].to_f) | ||||
time_stat = "Time used: #{seconds} sec." | ||||
else | ||||
seconds = nil | ||||
time_stat = "Time used: n/a sec." | ||||
end | ||||
# extract memory usage | ||||
if res = /s(.*)m/.match(running_stat_line) | ||||
memory_used = res[1].to_i | ||||
else | ||||
memory_used = -1 | ||||
end | ||||
return { | ||||
:msg => "#{run_stat}\n#{time_stat}", | ||||
:running_time => seconds, | ||||
:exit_status => run_stat, | ||||
:memory_usage => memory_used | ||||
} | ||||
end | ||||
|
r302 | def confirm_and_update_start_time | ||
|
r247 | user = User.find(session[:user_id]) | ||
|
r320 | if (GraderConfiguration.indv_contest_mode? and | ||
GraderConfiguration['contest.confirm_indv_contest_start'] and | ||||
|
r302 | !user.contest_started?) | ||
redirect_to :action => 'confirm_contest_start' and return | ||||
end | ||||
|
r320 | if not GraderConfiguration.analysis_mode? | ||
|
r317 | user.update_start_time | ||
end | ||||
|
r247 | end | ||
|
r249 | def reject_announcement_refresh_when_logged_out | ||
if not session[:user_id] | ||||
render :text => 'Access forbidden', :status => 403 | ||||
end | ||||
|
r295 | |||
|
r320 | if GraderConfiguration.multicontests? | ||
|
r295 | user = User.find(session[:user_id]) | ||
if user.contest_stat.forced_logout | ||||
render :text => 'Access forbidden', :status => 403 | ||||
end | ||||
end | ||||
|
r249 | end | ||
|
r0 | end | ||
|
r31 | |||