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