diff --git a/app/controllers/problems_controller.rb b/app/controllers/problems_controller.rb --- a/app/controllers/problems_controller.rb +++ b/app/controllers/problems_controller.rb @@ -15,6 +15,7 @@ verify :method => :post, :only => [ :destroy, :create, :quick_create, :do_manage, + :do_import, :update ], :redirect_to => { :action => :list } @@ -145,6 +146,20 @@ redirect_to :action => 'manage' end + def import + end + + def do_import + @problem, import_log = Problem.new_from_import_form_params(params) + + if @problem.errors.length != 0 + render :action => 'import' and return + end + + @problem.save + @log = import_log + end + ################################## protected diff --git a/app/models/problem.rb b/app/models/problem.rb --- a/app/models/problem.rb +++ b/app/models/problem.rb @@ -9,4 +9,68 @@ find(:all, :conditions => {:available => true}, :order => "date_added DESC") end + def self.new_from_import_form_params(params) + problem = Problem.new + + # form error checking + + time_limit_s = params[:time_limit] + memory_limit_s = params[:memory_limit] + + time_limit_s = '1' if time_limit_s=='' + memory_limit_s = '32' if memory_limit_s=='' + + time_limit = time_limit_s.to_i + memory_limit = memory_limit_s.to_i + + if time_limit==0 and time_limit_s!='0' + problem.errors.add_to_base('Time limit format errors.') + elsif time_limit<=0 or time_limit >60 + problem.errors.add_to_base('Time limit out of range.') + end + + if memory_limit==0 and memory_limit_s!='0' + problem.errors.add_to_base('Memory limit format errors.') + elsif memory_limit<=0 or memory_limit >512 + problem.errors.add_to_base('Memory limit out of range.') + end + + if params[:file]==nil or params[:file]=='' + problem.errors.add_to_base('No testdata file.') + end + + file = params[:file] + + if problem.errors.length!=0 + return problem + end + + problem.name = params[:name] + if params[:full_name]!='' + problem.full_name = params[:full_name] + else + problem.full_name = params[:name] + end + + if not problem.valid? + return problem + end + + importer = TestdataImporter.new + + if not importer.import_from_file(problem.name, + file, + time_limit, + memory_limit) + problem.errors.add_to_base('Import error.') + end + + problem.full_score = 100 + problem.date_added = Time.new + problem.test_allowed = true + problem.output_only = false + problem.available = false + return problem, importer.log_msg + end + end diff --git a/app/views/problems/do_import.html.haml b/app/views/problems/do_import.html.haml new file mode 100644 --- /dev/null +++ b/app/views/problems/do_import.html.haml @@ -0,0 +1,20 @@ +- content_for :head do + = stylesheet_link_tag 'problems' + = javascript_include_tag :defaults + +%h1 Import problems: successful + +%p + %b Problem: + = "#{@problem.full_name} (#{@problem.name})" + %br/ + Note that the full score has be assigned to the default value, 100. + You should change it to the correct full score. + +%p + = link_to '[Back to problem list]', :action => 'list' + = link_to '[Import other problems]', :action => 'import' + +%h3 Import log +%pre.import-log + = @log diff --git a/app/views/problems/import.html.haml b/app/views/problems/import.html.haml new file mode 100644 --- /dev/null +++ b/app/views/problems/import.html.haml @@ -0,0 +1,43 @@ +- content_for :head do + = stylesheet_link_tag 'problems' + = javascript_include_tag :defaults + +%h1 Import problems + +%p= link_to '[Back to problem list]', :action => 'list' + +- if @problem and @problem.errors + =error_messages_for 'problem' + +- form_tag({:action => 'do_import'}, :multipart => true) do + .submitbox + %table + %tr + %td Name: + %td= text_field_tag 'name' + %tr + %td Full name: + %td + = text_field_tag 'full_name' + %span{:class => 'help'} Leave blank to use the same value as the name above. + %tr + %td Testdata file: + %td= file_field_tag 'file' + %tr + %td Time limit: + %td + = text_field_tag 'time_limit' + %span{:class => 'help'} In seconds. Leave blank to use 1 sec. + %tr + %td Memory limit: + %td + = text_field_tag 'memory_limit' + %span{:class => 'help'} In MB. Leave blank to use 32MB. + %tr + %td + %td= submit_tag 'Import problem' + +- if @log + %h3 Import log + %pre.import-log + = @log diff --git a/app/views/problems/list.rhtml b/app/views/problems/list.rhtml --- a/app/views/problems/list.rhtml +++ b/app/views/problems/list.rhtml @@ -8,6 +8,7 @@
<%= link_to '[New problem]', :action => 'new' %> <%= link_to '[Manage problems]', :action => 'manage' %> +<%= link_to '[Import problems]', :action => 'import' %> <%= link_to '[Turn off all problems]', :action => 'turn_all_off' %> <%= link_to '[Turn on all problems]', :action => 'turn_all_on' %>
@@ -54,4 +55,4 @@