Description:
add submissions to users
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r738:d514585a072a - - 1 file changed: 2 inserted, 0 deleted
@@ -1,111 +1,113 | |||||
|
1 | class Problem < ActiveRecord::Base |
|
1 | class Problem < ActiveRecord::Base |
|
2 |
|
2 | ||
|
3 | belongs_to :description |
|
3 | belongs_to :description |
|
4 | has_and_belongs_to_many :contests, :uniq => true |
|
4 | has_and_belongs_to_many :contests, :uniq => true |
|
5 |
|
5 | ||
|
6 | #has_and_belongs_to_many :groups |
|
6 | #has_and_belongs_to_many :groups |
|
7 | has_many :groups_problems, class_name: GroupProblem |
|
7 | has_many :groups_problems, class_name: GroupProblem |
|
8 | has_many :groups, :through => :groups_problems |
|
8 | has_many :groups, :through => :groups_problems |
|
9 |
|
9 | ||
|
10 | has_many :problems_tags, class_name: ProblemTag |
|
10 | has_many :problems_tags, class_name: ProblemTag |
|
11 | has_many :tags, through: :problems_tags |
|
11 | has_many :tags, through: :problems_tags |
|
12 |
|
12 | ||
|
13 | has_many :test_pairs, :dependent => :delete_all |
|
13 | has_many :test_pairs, :dependent => :delete_all |
|
14 | has_many :testcases, :dependent => :destroy |
|
14 | has_many :testcases, :dependent => :destroy |
|
15 |
|
15 | ||
|
|
16 | + has_many :submissions | ||
|
|
17 | + | ||
|
16 | validates_presence_of :name |
|
18 | validates_presence_of :name |
|
17 | validates_format_of :name, :with => /\A\w+\z/ |
|
19 | validates_format_of :name, :with => /\A\w+\z/ |
|
18 | validates_presence_of :full_name |
|
20 | validates_presence_of :full_name |
|
19 |
|
21 | ||
|
20 | scope :available, -> { where(available: true) } |
|
22 | scope :available, -> { where(available: true) } |
|
21 |
|
23 | ||
|
22 | DEFAULT_TIME_LIMIT = 1 |
|
24 | DEFAULT_TIME_LIMIT = 1 |
|
23 | DEFAULT_MEMORY_LIMIT = 32 |
|
25 | DEFAULT_MEMORY_LIMIT = 32 |
|
24 |
|
26 | ||
|
25 | def self.available_problems |
|
27 | def self.available_problems |
|
26 | available.order(date_added: :desc).order(:name) |
|
28 | available.order(date_added: :desc).order(:name) |
|
27 | #Problem.available.all(:order => "date_added DESC, name ASC") |
|
29 | #Problem.available.all(:order => "date_added DESC, name ASC") |
|
28 | end |
|
30 | end |
|
29 |
|
31 | ||
|
30 | def self.create_from_import_form_params(params, old_problem=nil) |
|
32 | def self.create_from_import_form_params(params, old_problem=nil) |
|
31 | org_problem = old_problem || Problem.new |
|
33 | org_problem = old_problem || Problem.new |
|
32 | import_params, problem = Problem.extract_params_and_check(params, |
|
34 | import_params, problem = Problem.extract_params_and_check(params, |
|
33 | org_problem) |
|
35 | org_problem) |
|
34 |
|
36 | ||
|
35 | if !problem.errors.empty? |
|
37 | if !problem.errors.empty? |
|
36 | return problem, 'Error importing' |
|
38 | return problem, 'Error importing' |
|
37 | end |
|
39 | end |
|
38 |
|
40 | ||
|
39 | problem.full_score = 100 |
|
41 | problem.full_score = 100 |
|
40 | problem.date_added = Time.new |
|
42 | problem.date_added = Time.new |
|
41 | problem.test_allowed = true |
|
43 | problem.test_allowed = true |
|
42 | problem.output_only = false |
|
44 | problem.output_only = false |
|
43 | problem.available = false |
|
45 | problem.available = false |
|
44 |
|
46 | ||
|
45 | if not problem.save |
|
47 | if not problem.save |
|
46 | return problem, 'Error importing' |
|
48 | return problem, 'Error importing' |
|
47 | end |
|
49 | end |
|
48 |
|
50 | ||
|
49 | import_to_db = params.has_key? :import_to_db |
|
51 | import_to_db = params.has_key? :import_to_db |
|
50 |
|
52 | ||
|
51 | importer = TestdataImporter.new(problem) |
|
53 | importer = TestdataImporter.new(problem) |
|
52 |
|
54 | ||
|
53 | if not importer.import_from_file(import_params[:file], |
|
55 | if not importer.import_from_file(import_params[:file], |
|
54 | import_params[:time_limit], |
|
56 | import_params[:time_limit], |
|
55 | import_params[:memory_limit], |
|
57 | import_params[:memory_limit], |
|
56 | import_params[:checker_name], |
|
58 | import_params[:checker_name], |
|
57 | import_to_db) |
|
59 | import_to_db) |
|
58 | problem.errors.add(:base,'Import error.') |
|
60 | problem.errors.add(:base,'Import error.') |
|
59 | end |
|
61 | end |
|
60 |
|
62 | ||
|
61 | return problem, importer.log_msg |
|
63 | return problem, importer.log_msg |
|
62 | end |
|
64 | end |
|
63 |
|
65 | ||
|
64 | def self.download_file_basedir |
|
66 | def self.download_file_basedir |
|
65 | return "#{Rails.root}/data/tasks" |
|
67 | return "#{Rails.root}/data/tasks" |
|
66 | end |
|
68 | end |
|
67 |
|
69 | ||
|
68 | def get_submission_stat |
|
70 | def get_submission_stat |
|
69 | result = Hash.new |
|
71 | result = Hash.new |
|
70 | #total number of submission |
|
72 | #total number of submission |
|
71 | result[:total_sub] = Submission.where(problem_id: self.id).count |
|
73 | result[:total_sub] = Submission.where(problem_id: self.id).count |
|
72 | result[:attempted_user] = Submission.where(problem_id: self.id).group(:user_id) |
|
74 | result[:attempted_user] = Submission.where(problem_id: self.id).group(:user_id) |
|
73 | result[:pass] = Submission.where(problem_id: self.id).where("points >= ?",self.full_score).count |
|
75 | result[:pass] = Submission.where(problem_id: self.id).where("points >= ?",self.full_score).count |
|
74 | return result |
|
76 | return result |
|
75 | end |
|
77 | end |
|
76 |
|
78 | ||
|
77 | def long_name |
|
79 | def long_name |
|
78 | "[#{name}] #{full_name}" |
|
80 | "[#{name}] #{full_name}" |
|
79 | end |
|
81 | end |
|
80 |
|
82 | ||
|
81 | protected |
|
83 | protected |
|
82 |
|
84 | ||
|
83 | def self.to_i_or_default(st, default) |
|
85 | def self.to_i_or_default(st, default) |
|
84 | if st!='' |
|
86 | if st!='' |
|
85 | result = st.to_i |
|
87 | result = st.to_i |
|
86 | end |
|
88 | end |
|
87 | result ||= default |
|
89 | result ||= default |
|
88 | end |
|
90 | end |
|
89 |
|
91 | ||
|
90 | def self.to_f_or_default(st, default) |
|
92 | def self.to_f_or_default(st, default) |
|
91 | if st!='' |
|
93 | if st!='' |
|
92 | result = st.to_f |
|
94 | result = st.to_f |
|
93 | end |
|
95 | end |
|
94 | result ||= default |
|
96 | result ||= default |
|
95 | end |
|
97 | end |
|
96 |
|
98 | ||
|
97 | def self.extract_params_and_check(params, problem) |
|
99 | def self.extract_params_and_check(params, problem) |
|
98 | time_limit = Problem.to_f_or_default(params[:time_limit], |
|
100 | time_limit = Problem.to_f_or_default(params[:time_limit], |
|
99 | DEFAULT_TIME_LIMIT) |
|
101 | DEFAULT_TIME_LIMIT) |
|
100 | memory_limit = Problem.to_i_or_default(params[:memory_limit], |
|
102 | memory_limit = Problem.to_i_or_default(params[:memory_limit], |
|
101 | DEFAULT_MEMORY_LIMIT) |
|
103 | DEFAULT_MEMORY_LIMIT) |
|
102 |
|
104 | ||
|
103 | if time_limit<=0 or time_limit >60 |
|
105 | if time_limit<=0 or time_limit >60 |
|
104 | problem.errors.add(:base,'Time limit out of range.') |
|
106 | problem.errors.add(:base,'Time limit out of range.') |
|
105 | end |
|
107 | end |
|
106 |
|
108 | ||
|
107 | if memory_limit==0 and params[:memory_limit]!='0' |
|
109 | if memory_limit==0 and params[:memory_limit]!='0' |
|
108 | problem.errors.add(:base,'Memory limit format errors.') |
|
110 | problem.errors.add(:base,'Memory limit format errors.') |
|
109 | elsif memory_limit<=0 or memory_limit >512 |
|
111 | elsif memory_limit<=0 or memory_limit >512 |
|
110 | problem.errors.add(:base,'Memory limit out of range.') |
|
112 | problem.errors.add(:base,'Memory limit out of range.') |
|
111 | end |
|
113 | end |
You need to be logged in to leave comments.
Login now