Show More
Commit Description:
manages users in contests
Commit Description:
manages users in contests
References:
File last commit:
Show/Diff file:
Action:
app/models/problem.rb
| 108 lines
| 2.8 KiB
| text/x-ruby
| RubyLexer
|
|
r0 | class Problem < ActiveRecord::Base | ||
|
r92 | belongs_to :description | ||
|
r280 | has_and_belongs_to_many :contests, :uniq => true | ||
|
r210 | has_many :test_pairs, :dependent => :delete_all | ||
|
r92 | |||
|
r171 | validates_presence_of :name | ||
|
r205 | validates_format_of :name, :with => /^\w+$/ | ||
|
r171 | validates_presence_of :full_name | ||
|
r208 | |||
|
r278 | named_scope :available, :conditions => {:available => true} | ||
|
r208 | DEFAULT_TIME_LIMIT = 1 | ||
DEFAULT_MEMORY_LIMIT = 32 | ||||
|
r271 | |||
|
r0 | def self.find_available_problems | ||
|
r278 | Problem.available.all(:order => "date_added DESC") | ||
|
r0 | end | ||
|
r210 | def self.create_from_import_form_params(params, old_problem=nil) | ||
problem = old_problem || Problem.new | ||||
|
r208 | import_params = Problem.extract_params_and_check(params, problem) | ||
|
r204 | |||
|
r208 | if not problem.valid? | ||
|
r210 | return problem, 'Error importing' | ||
|
r208 | end | ||
|
r204 | |||
|
r208 | problem.full_score = 100 | ||
problem.date_added = Time.new | ||||
problem.test_allowed = true | ||||
problem.output_only = false | ||||
problem.available = false | ||||
|
r210 | |||
if not problem.save | ||||
return problem, 'Error importing' | ||||
end | ||||
import_to_db = params.has_key? :import_to_db | ||||
importer = TestdataImporter.new(problem) | ||||
if not importer.import_from_file(import_params[:file], | ||||
import_params[:time_limit], | ||||
import_params[:memory_limit], | ||||
import_to_db) | ||||
problem.errors.add_to_base('Import error.') | ||||
end | ||||
|
r208 | return problem, importer.log_msg | ||
end | ||||
|
r271 | def self.download_file_basedir | ||
return "#{RAILS_ROOT}/data/tasks" | ||||
end | ||||
|
r208 | protected | ||
|
r204 | |||
|
r208 | def self.to_i_or_default(st, default) | ||
if st!='' | ||||
st.to_i | ||||
else | ||||
default | ||||
end | ||||
end | ||||
def self.extract_params_and_check(params, problem) | ||||
time_limit = Problem.to_i_or_default(params[:time_limit], | ||||
DEFAULT_TIME_LIMIT) | ||||
memory_limit = Problem.to_i_or_default(params[:memory_limit], | ||||
DEFAULT_MEMORY_LIMIT) | ||||
|
r204 | |||
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 | ||||
|
r208 | return { | ||
:time_limit => time_limit, | ||||
:memory_limit => memory_limit, | ||||
:file => file | ||||
} | ||||
|
r204 | end | ||
|
r0 | end | ||