diff --git a/app/controllers/test_controller.rb b/app/controllers/test_controller.rb new file mode 100644 --- /dev/null +++ b/app/controllers/test_controller.rb @@ -0,0 +1,49 @@ +class TestController < ApplicationController + + before_filter :authenticate + + verify :method => :post, :only => [:test_submit], + :redirect_to => { :action => :index } + + def index + @user = User.find(session[:user_id]) + prepare_index_information + end + + def submit + @user = User.find(session[:user_id]) + test_request = TestRequest.new_from_form_params(@user,params[:test_request]) + if test_request.save + redirect_to :action => 'index' + else + flash[:notice] = 'Error saving your test submission' + redirect_to :action => 'index' + end + end + + def read + user = User.find(session[:user_id]) + test_request = TestRequest.find(params[:id]) + if test_request.user_id != user.id + flash[:notice] = 'Invalid output' + redirect_to :action => 'index' + return + end + if test_request.output_file_name!=nil + data = File.open(test_request.output_file_name).read(2048) + send_data(data, + {:filename => 'output.txt', + :type => 'text/plain'}) + return + end + redirect_to :action => 'index' + end + + protected + + def prepare_index_information + @submissions = Submission.find_last_for_all_available_problems(@user.id) + @problems = @submissions.collect { |submission| submission.problem } + end + +end