# HG changeset patch # User jittat # Date 2008-04-22 00:25:26 # Node ID ab383e0b489e563fc93f9a353a31bec6ef706cf7 # Parent f4a25aa9faf499a353a5391e15533012cdad2db9 [web] added support for output only problems git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@188 6386c4cd-e34a-4fa8-8920-d93eb39b512e 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 @@ -33,7 +33,10 @@ @submission = Submission.new(params[:submission]) @submission.user = user @submission.language_id = 0 - @submission.source = params['file'].read if params['file']!='' + if params['file']!='' + @submission.source = params['file'].read + @submission.source_filename = params['file'].original_filename + end @submission.submitted_at = Time.new if user.site!=nil and user.site.finished? diff --git a/app/models/submission.rb b/app/models/submission.rb --- a/app/models/submission.rb +++ b/app/models/submission.rb @@ -4,11 +4,14 @@ belongs_to :problem belongs_to :user + before_validation :assign_problem + before_validation :assign_language + 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' + validate :must_have_valid_problem validate :must_specify_language - validate :must_have_valid_problem before_save :assign_latest_number_if_new_recond @@ -97,30 +100,40 @@ end end + def assign_problem + if self.problem_id!=-1 + begin + self.problem = Problem.find(self.problem_id) + rescue ActiveRecord::RecordNotFound + self.problem = nil + end + else + self.problem = Submission.find_problem_in_source(self.source) + end + end + + def assign_language + self.language = Submission.find_language_in_source(self.source) + end + # validation codes def must_specify_language return if self.source==nil - self.language = Submission.find_language_in_source(self.source) - errors.add('source',"must specify programming language") unless self.language!=nil + + # for output_only tasks + return if self.problem!=nil and self.problem.output_only + + if self.language==nil + errors.add('source',"must specify programming language") unless self.language!=nil + end end def must_have_valid_problem return if self.source==nil - if self.problem_id!=-1 - begin - problem = Problem.find(self.problem_id) - rescue ActiveRecord::RecordNotFound - problem = nil - end - else - problem = Submission.find_problem_in_source(self.source) - end - if problem==nil + if self.problem==nil errors.add('problem',"must be specified.") - elsif (!problem.available) and (self.new_record?) + elsif (!self.problem.available) and (self.new_record?) errors.add('problem',"must be valid.") - else - self.problem = problem end end diff --git a/app/views/problems/_form.rhtml b/app/views/problems/_form.rhtml --- a/app/views/problems/_form.rhtml +++ b/app/views/problems/_form.rhtml @@ -13,11 +13,25 @@


<%= date_select 'problem', 'date_added' %>

-

-<%= select("problem","available",[['True',true],['False',false]]) %>

+<% +# TODO: these should be put in model Problem, but I can't think of +# nice default values for them. These values look fine only +# in this case (of lazily adding new problems). +@problem.available = true if @problem!=nil and @problem.available==nil +@problem.test_allowed = true if @problem!=nil and @problem.test_allowed==nil +@problem.output_only = false if @problem!=nil and @problem.output_only==nil +%> -

-<%= select("problem","test_allowed",[['True',true],['False',false]]) %>

+

+ +<%= check_box :problem, :available %> + + +<%= check_box :problem, :test_allowed %> + + +<%= check_box :problem, :output_only %> +

<%= error_messages_for 'description' %> diff --git a/db/migrate/031_add_supports_for_output_only_problems.rb b/db/migrate/031_add_supports_for_output_only_problems.rb new file mode 100644 --- /dev/null +++ b/db/migrate/031_add_supports_for_output_only_problems.rb @@ -0,0 +1,11 @@ +class AddSupportsForOutputOnlyProblems < ActiveRecord::Migration + def self.up + add_column :submissions, :source_filename, :string + add_column :problems, :output_only, :boolean + end + + def self.down + remove_column :submissions, :source_filename + add_column :problems, :output_only, :boolean + end +end diff --git a/db/schema.rb b/db/schema.rb --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 30) do +ActiveRecord::Schema.define(:version => 31) do create_table "announcements", :force => true do |t| t.string "author" @@ -61,6 +61,7 @@ t.string "url" t.integer "description_id" t.boolean "test_allowed" + t.boolean "output_only" end create_table "rights", :force => true do |t| @@ -117,6 +118,7 @@ t.integer "points" t.text "grader_comment" t.integer "number" + t.string "source_filename" end add_index "submissions", ["user_id", "problem_id", "number"], :name => "index_submissions_on_user_id_and_problem_id_and_number", :unique => true