Description:
add submissions to users
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r738:d514585a072a - - 1 file changed: 2 inserted, 0 deleted

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