Description:
fix bug when score is nil
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r492:9941b31fd734 - - 1 file changed: 1 inserted, 1 deleted
@@ -70,193 +70,193 | |||||
|
70 |
|
70 | ||
|
71 | def edit |
|
71 | def edit |
|
72 | @problem = Problem.find(params[:id]) |
|
72 | @problem = Problem.find(params[:id]) |
|
73 | @description = @problem.description |
|
73 | @description = @problem.description |
|
74 | end |
|
74 | end |
|
75 |
|
75 | ||
|
76 | def update |
|
76 | def update |
|
77 | @problem = Problem.find(params[:id]) |
|
77 | @problem = Problem.find(params[:id]) |
|
78 | @description = @problem.description |
|
78 | @description = @problem.description |
|
79 | if @description == nil and params[:description][:body]!='' |
|
79 | if @description == nil and params[:description][:body]!='' |
|
80 | @description = Description.new(params[:description]) |
|
80 | @description = Description.new(params[:description]) |
|
81 | if !@description.save |
|
81 | if !@description.save |
|
82 | flash[:notice] = 'Error saving description' |
|
82 | flash[:notice] = 'Error saving description' |
|
83 | render :action => 'edit' and return |
|
83 | render :action => 'edit' and return |
|
84 | end |
|
84 | end |
|
85 | @problem.description = @description |
|
85 | @problem.description = @description |
|
86 | elsif @description!=nil |
|
86 | elsif @description!=nil |
|
87 | if !@description.update_attributes(params[:description]) |
|
87 | if !@description.update_attributes(params[:description]) |
|
88 | flash[:notice] = 'Error saving description' |
|
88 | flash[:notice] = 'Error saving description' |
|
89 | render :action => 'edit' and return |
|
89 | render :action => 'edit' and return |
|
90 | end |
|
90 | end |
|
91 | end |
|
91 | end |
|
92 | if params[:file] and params[:file].content_type != 'application/pdf' |
|
92 | if params[:file] and params[:file].content_type != 'application/pdf' |
|
93 | flash[:notice] = 'Error: Uploaded file is not PDF' |
|
93 | flash[:notice] = 'Error: Uploaded file is not PDF' |
|
94 | render :action => 'edit' and return |
|
94 | render :action => 'edit' and return |
|
95 | end |
|
95 | end |
|
96 | if @problem.update_attributes(params[:problem]) |
|
96 | if @problem.update_attributes(params[:problem]) |
|
97 | flash[:notice] = 'Problem was successfully updated.' |
|
97 | flash[:notice] = 'Problem was successfully updated.' |
|
98 | unless params[:file] == nil or params[:file] == '' |
|
98 | unless params[:file] == nil or params[:file] == '' |
|
99 | flash[:notice] = 'Problem was successfully updated and a new PDF file is uploaded.' |
|
99 | flash[:notice] = 'Problem was successfully updated and a new PDF file is uploaded.' |
|
100 | out_dirname = "#{Problem.download_file_basedir}/#{@problem.id}" |
|
100 | out_dirname = "#{Problem.download_file_basedir}/#{@problem.id}" |
|
101 | if not FileTest.exists? out_dirname |
|
101 | if not FileTest.exists? out_dirname |
|
102 | Dir.mkdir out_dirname |
|
102 | Dir.mkdir out_dirname |
|
103 | end |
|
103 | end |
|
104 |
|
104 | ||
|
105 | out_filename = "#{out_dirname}/#{@problem.name}.pdf" |
|
105 | out_filename = "#{out_dirname}/#{@problem.name}.pdf" |
|
106 | if FileTest.exists? out_filename |
|
106 | if FileTest.exists? out_filename |
|
107 | File.delete out_filename |
|
107 | File.delete out_filename |
|
108 | end |
|
108 | end |
|
109 |
|
109 | ||
|
110 | File.open(out_filename,"wb") do |file| |
|
110 | File.open(out_filename,"wb") do |file| |
|
111 | file.write(params[:file].read) |
|
111 | file.write(params[:file].read) |
|
112 | end |
|
112 | end |
|
113 | @problem.description_filename = "#{@problem.name}.pdf" |
|
113 | @problem.description_filename = "#{@problem.name}.pdf" |
|
114 | @problem.save |
|
114 | @problem.save |
|
115 | end |
|
115 | end |
|
116 | redirect_to :action => 'show', :id => @problem |
|
116 | redirect_to :action => 'show', :id => @problem |
|
117 | else |
|
117 | else |
|
118 | render :action => 'edit' |
|
118 | render :action => 'edit' |
|
119 | end |
|
119 | end |
|
120 | end |
|
120 | end |
|
121 |
|
121 | ||
|
122 | def destroy |
|
122 | def destroy |
|
123 | Problem.find(params[:id]).destroy |
|
123 | Problem.find(params[:id]).destroy |
|
124 | redirect_to :action => 'list' |
|
124 | redirect_to :action => 'list' |
|
125 | end |
|
125 | end |
|
126 |
|
126 | ||
|
127 | def toggle |
|
127 | def toggle |
|
128 | @problem = Problem.find(params[:id]) |
|
128 | @problem = Problem.find(params[:id]) |
|
129 | @problem.available = !(@problem.available) |
|
129 | @problem.available = !(@problem.available) |
|
130 | @problem.save |
|
130 | @problem.save |
|
131 | end |
|
131 | end |
|
132 |
|
132 | ||
|
133 | def turn_all_off |
|
133 | def turn_all_off |
|
134 | Problem.find(:all, |
|
134 | Problem.find(:all, |
|
135 | :conditions => "available = 1").each do |problem| |
|
135 | :conditions => "available = 1").each do |problem| |
|
136 | problem.available = false |
|
136 | problem.available = false |
|
137 | problem.save |
|
137 | problem.save |
|
138 | end |
|
138 | end |
|
139 | redirect_to :action => 'list' |
|
139 | redirect_to :action => 'list' |
|
140 | end |
|
140 | end |
|
141 |
|
141 | ||
|
142 | def turn_all_on |
|
142 | def turn_all_on |
|
143 | Problem.find(:all, |
|
143 | Problem.find(:all, |
|
144 | :conditions => "available = 0").each do |problem| |
|
144 | :conditions => "available = 0").each do |problem| |
|
145 | problem.available = true |
|
145 | problem.available = true |
|
146 | problem.save |
|
146 | problem.save |
|
147 | end |
|
147 | end |
|
148 | redirect_to :action => 'list' |
|
148 | redirect_to :action => 'list' |
|
149 | end |
|
149 | end |
|
150 |
|
150 | ||
|
151 | def stat |
|
151 | def stat |
|
152 | @problem = Problem.find(params[:id]) |
|
152 | @problem = Problem.find(params[:id]) |
|
153 | unless @problem.available or session[:admin] |
|
153 | unless @problem.available or session[:admin] |
|
154 | redirect_to :controller => 'main', :action => 'list' |
|
154 | redirect_to :controller => 'main', :action => 'list' |
|
155 | return |
|
155 | return |
|
156 | end |
|
156 | end |
|
157 | @submissions = Submission.includes(:user).where(problem_id: params[:id]).order(:user_id,:id) |
|
157 | @submissions = Submission.includes(:user).where(problem_id: params[:id]).order(:user_id,:id) |
|
158 |
|
158 | ||
|
159 | #stat summary |
|
159 | #stat summary |
|
160 | range =65 |
|
160 | range =65 |
|
161 | @histogram = { data: Array.new(range,0), summary: {} } |
|
161 | @histogram = { data: Array.new(range,0), summary: {} } |
|
162 | user = Hash.new(0) |
|
162 | user = Hash.new(0) |
|
163 | @submissions.find_each do |sub| |
|
163 | @submissions.find_each do |sub| |
|
164 | d = (DateTime.now.in_time_zone - sub.submitted_at) / 24 / 60 / 60 |
|
164 | d = (DateTime.now.in_time_zone - sub.submitted_at) / 24 / 60 / 60 |
|
165 | @histogram[:data][d.to_i] += 1 if d < range |
|
165 | @histogram[:data][d.to_i] += 1 if d < range |
|
166 | - user[sub.user_id] = [user[sub.user_id], (sub.points >= @problem.full_score) ? 1 : 0].max |
|
166 | + user[sub.user_id] = [user[sub.user_id], ( sub.try(:points) >= @problem.full_score) ? 1 : 0].max |
|
167 | end |
|
167 | end |
|
168 | @histogram[:summary][:max] = [@histogram[:data].max,1].max |
|
168 | @histogram[:summary][:max] = [@histogram[:data].max,1].max |
|
169 |
|
169 | ||
|
170 | @summary = { attempt: user.count, solve: 0 } |
|
170 | @summary = { attempt: user.count, solve: 0 } |
|
171 | user.each_value { |v| @summary[:solve] += 1 if v == 1 } |
|
171 | user.each_value { |v| @summary[:solve] += 1 if v == 1 } |
|
172 | end |
|
172 | end |
|
173 |
|
173 | ||
|
174 | def manage |
|
174 | def manage |
|
175 | @problems = Problem.find(:all, :order => 'date_added DESC') |
|
175 | @problems = Problem.find(:all, :order => 'date_added DESC') |
|
176 | end |
|
176 | end |
|
177 |
|
177 | ||
|
178 | def do_manage |
|
178 | def do_manage |
|
179 | if params.has_key? 'change_date_added' |
|
179 | if params.has_key? 'change_date_added' |
|
180 | change_date_added |
|
180 | change_date_added |
|
181 | elsif params.has_key? 'add_to_contest' |
|
181 | elsif params.has_key? 'add_to_contest' |
|
182 | add_to_contest |
|
182 | add_to_contest |
|
183 | elsif params.has_key? 'enable_problem' |
|
183 | elsif params.has_key? 'enable_problem' |
|
184 | set_available(true) |
|
184 | set_available(true) |
|
185 | elsif params.has_key? 'disable_problem' |
|
185 | elsif params.has_key? 'disable_problem' |
|
186 | set_available(false) |
|
186 | set_available(false) |
|
187 | end |
|
187 | end |
|
188 | redirect_to :action => 'manage' |
|
188 | redirect_to :action => 'manage' |
|
189 | end |
|
189 | end |
|
190 |
|
190 | ||
|
191 | def import |
|
191 | def import |
|
192 | @allow_test_pair_import = allow_test_pair_import? |
|
192 | @allow_test_pair_import = allow_test_pair_import? |
|
193 | end |
|
193 | end |
|
194 |
|
194 | ||
|
195 | def do_import |
|
195 | def do_import |
|
196 | old_problem = Problem.find_by_name(params[:name]) |
|
196 | old_problem = Problem.find_by_name(params[:name]) |
|
197 | if !allow_test_pair_import? and params.has_key? :import_to_db |
|
197 | if !allow_test_pair_import? and params.has_key? :import_to_db |
|
198 | params.delete :import_to_db |
|
198 | params.delete :import_to_db |
|
199 | end |
|
199 | end |
|
200 | @problem, import_log = Problem.create_from_import_form_params(params, |
|
200 | @problem, import_log = Problem.create_from_import_form_params(params, |
|
201 | old_problem) |
|
201 | old_problem) |
|
202 |
|
202 | ||
|
203 | if !@problem.errors.empty? |
|
203 | if !@problem.errors.empty? |
|
204 | render :action => 'import' and return |
|
204 | render :action => 'import' and return |
|
205 | end |
|
205 | end |
|
206 |
|
206 | ||
|
207 | if old_problem!=nil |
|
207 | if old_problem!=nil |
|
208 | flash[:notice] = "The test data has been replaced for problem #{@problem.name}" |
|
208 | flash[:notice] = "The test data has been replaced for problem #{@problem.name}" |
|
209 | end |
|
209 | end |
|
210 | @log = import_log |
|
210 | @log = import_log |
|
211 | end |
|
211 | end |
|
212 |
|
212 | ||
|
213 | def remove_contest |
|
213 | def remove_contest |
|
214 | problem = Problem.find(params[:id]) |
|
214 | problem = Problem.find(params[:id]) |
|
215 | contest = Contest.find(params[:contest_id]) |
|
215 | contest = Contest.find(params[:contest_id]) |
|
216 | if problem!=nil and contest!=nil |
|
216 | if problem!=nil and contest!=nil |
|
217 | problem.contests.delete(contest) |
|
217 | problem.contests.delete(contest) |
|
218 | end |
|
218 | end |
|
219 | redirect_to :action => 'manage' |
|
219 | redirect_to :action => 'manage' |
|
220 | end |
|
220 | end |
|
221 |
|
221 | ||
|
222 | ################################## |
|
222 | ################################## |
|
223 | protected |
|
223 | protected |
|
224 |
|
224 | ||
|
225 | def allow_test_pair_import? |
|
225 | def allow_test_pair_import? |
|
226 | if defined? ALLOW_TEST_PAIR_IMPORT |
|
226 | if defined? ALLOW_TEST_PAIR_IMPORT |
|
227 | return ALLOW_TEST_PAIR_IMPORT |
|
227 | return ALLOW_TEST_PAIR_IMPORT |
|
228 | else |
|
228 | else |
|
229 | return false |
|
229 | return false |
|
230 | end |
|
230 | end |
|
231 | end |
|
231 | end |
|
232 |
|
232 | ||
|
233 | def change_date_added |
|
233 | def change_date_added |
|
234 | problems = get_problems_from_params |
|
234 | problems = get_problems_from_params |
|
235 | year = params[:date_added][:year].to_i |
|
235 | year = params[:date_added][:year].to_i |
|
236 | month = params[:date_added][:month].to_i |
|
236 | month = params[:date_added][:month].to_i |
|
237 | day = params[:date_added][:day].to_i |
|
237 | day = params[:date_added][:day].to_i |
|
238 | date = Date.new(year,month,day) |
|
238 | date = Date.new(year,month,day) |
|
239 | problems.each do |p| |
|
239 | problems.each do |p| |
|
240 | p.date_added = date |
|
240 | p.date_added = date |
|
241 | p.save |
|
241 | p.save |
|
242 | end |
|
242 | end |
|
243 | end |
|
243 | end |
|
244 |
|
244 | ||
|
245 | def add_to_contest |
|
245 | def add_to_contest |
|
246 | problems = get_problems_from_params |
|
246 | problems = get_problems_from_params |
|
247 | contest = Contest.find(params[:contest][:id]) |
|
247 | contest = Contest.find(params[:contest][:id]) |
|
248 | if contest!=nil and contest.enabled |
|
248 | if contest!=nil and contest.enabled |
|
249 | problems.each do |p| |
|
249 | problems.each do |p| |
|
250 | p.contests << contest |
|
250 | p.contests << contest |
|
251 | end |
|
251 | end |
|
252 | end |
|
252 | end |
|
253 | end |
|
253 | end |
|
254 |
|
254 | ||
|
255 | def set_available(avail) |
|
255 | def set_available(avail) |
|
256 | problems = get_problems_from_params |
|
256 | problems = get_problems_from_params |
|
257 | problems.each do |p| |
|
257 | problems.each do |p| |
|
258 | p.available = avail |
|
258 | p.available = avail |
|
259 | p.save |
|
259 | p.save |
|
260 | end |
|
260 | end |
|
261 | end |
|
261 | end |
|
262 |
|
262 |
You need to be logged in to leave comments.
Login now