Description:
modernize
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r574:6250748f34ac - - 1 file changed: 1 inserted, 1 deleted
@@ -146,193 +146,193 | |||||
|
146 | def alias_for_editing |
|
146 | def alias_for_editing |
|
147 | if self.alias==nil |
|
147 | if self.alias==nil |
|
148 | "(unknown)" |
|
148 | "(unknown)" |
|
149 | elsif self.alias=='' |
|
149 | elsif self.alias=='' |
|
150 | "(blank)" |
|
150 | "(blank)" |
|
151 | else |
|
151 | else |
|
152 | self.alias |
|
152 | self.alias |
|
153 | end |
|
153 | end |
|
154 | end |
|
154 | end |
|
155 |
|
155 | ||
|
156 | def alias_for_editing=(e) |
|
156 | def alias_for_editing=(e) |
|
157 | self.alias=e |
|
157 | self.alias=e |
|
158 | end |
|
158 | end |
|
159 |
|
159 | ||
|
160 | def activation_key |
|
160 | def activation_key |
|
161 | if self.hashed_password==nil |
|
161 | if self.hashed_password==nil |
|
162 | encrypt_new_password |
|
162 | encrypt_new_password |
|
163 | end |
|
163 | end |
|
164 | Digest::SHA1.hexdigest(self.hashed_password)[0..7] |
|
164 | Digest::SHA1.hexdigest(self.hashed_password)[0..7] |
|
165 | end |
|
165 | end |
|
166 |
|
166 | ||
|
167 | def verify_activation_key(key) |
|
167 | def verify_activation_key(key) |
|
168 | key == activation_key |
|
168 | key == activation_key |
|
169 | end |
|
169 | end |
|
170 |
|
170 | ||
|
171 | def self.random_password(length=5) |
|
171 | def self.random_password(length=5) |
|
172 | chars = 'abcdefghjkmnopqrstuvwxyz' |
|
172 | chars = 'abcdefghjkmnopqrstuvwxyz' |
|
173 | password = '' |
|
173 | password = '' |
|
174 | length.times { password << chars[rand(chars.length - 1)] } |
|
174 | length.times { password << chars[rand(chars.length - 1)] } |
|
175 | password |
|
175 | password |
|
176 | end |
|
176 | end |
|
177 |
|
177 | ||
|
178 | def self.find_non_admin_with_prefix(prefix='') |
|
178 | def self.find_non_admin_with_prefix(prefix='') |
|
179 | users = User.find(:all) |
|
179 | users = User.find(:all) |
|
180 | return users.find_all { |u| !(u.admin?) and u.login.index(prefix)==0 } |
|
180 | return users.find_all { |u| !(u.admin?) and u.login.index(prefix)==0 } |
|
181 | end |
|
181 | end |
|
182 |
|
182 | ||
|
183 | # Contest information |
|
183 | # Contest information |
|
184 |
|
184 | ||
|
185 | def self.find_users_with_no_contest() |
|
185 | def self.find_users_with_no_contest() |
|
186 | users = User.find(:all) |
|
186 | users = User.find(:all) |
|
187 | return users.find_all { |u| u.contests.length == 0 } |
|
187 | return users.find_all { |u| u.contests.length == 0 } |
|
188 | end |
|
188 | end |
|
189 |
|
189 | ||
|
190 |
|
190 | ||
|
191 | def contest_time_left |
|
191 | def contest_time_left |
|
192 | if GraderConfiguration.contest_mode? |
|
192 | if GraderConfiguration.contest_mode? |
|
193 | return nil if site==nil |
|
193 | return nil if site==nil |
|
194 | return site.time_left |
|
194 | return site.time_left |
|
195 | elsif GraderConfiguration.indv_contest_mode? |
|
195 | elsif GraderConfiguration.indv_contest_mode? |
|
196 | time_limit = GraderConfiguration.contest_time_limit |
|
196 | time_limit = GraderConfiguration.contest_time_limit |
|
197 | if time_limit == nil |
|
197 | if time_limit == nil |
|
198 | return nil |
|
198 | return nil |
|
199 | end |
|
199 | end |
|
200 | if contest_stat==nil or contest_stat.started_at==nil |
|
200 | if contest_stat==nil or contest_stat.started_at==nil |
|
201 | return (Time.now.gmtime + time_limit) - Time.now.gmtime |
|
201 | return (Time.now.gmtime + time_limit) - Time.now.gmtime |
|
202 | else |
|
202 | else |
|
203 | finish_time = contest_stat.started_at + time_limit |
|
203 | finish_time = contest_stat.started_at + time_limit |
|
204 | current_time = Time.now.gmtime |
|
204 | current_time = Time.now.gmtime |
|
205 | if current_time > finish_time |
|
205 | if current_time > finish_time |
|
206 | return 0 |
|
206 | return 0 |
|
207 | else |
|
207 | else |
|
208 | return finish_time - current_time |
|
208 | return finish_time - current_time |
|
209 | end |
|
209 | end |
|
210 | end |
|
210 | end |
|
211 | else |
|
211 | else |
|
212 | return nil |
|
212 | return nil |
|
213 | end |
|
213 | end |
|
214 | end |
|
214 | end |
|
215 |
|
215 | ||
|
216 | def contest_finished? |
|
216 | def contest_finished? |
|
217 | if GraderConfiguration.contest_mode? |
|
217 | if GraderConfiguration.contest_mode? |
|
218 | return false if site==nil |
|
218 | return false if site==nil |
|
219 | return site.finished? |
|
219 | return site.finished? |
|
220 | elsif GraderConfiguration.indv_contest_mode? |
|
220 | elsif GraderConfiguration.indv_contest_mode? |
|
221 | return false if self.contest_stat(true)==nil |
|
221 | return false if self.contest_stat(true)==nil |
|
222 | return contest_time_left == 0 |
|
222 | return contest_time_left == 0 |
|
223 | else |
|
223 | else |
|
224 | return false |
|
224 | return false |
|
225 | end |
|
225 | end |
|
226 | end |
|
226 | end |
|
227 |
|
227 | ||
|
228 | def contest_started? |
|
228 | def contest_started? |
|
229 | if GraderConfiguration.indv_contest_mode? |
|
229 | if GraderConfiguration.indv_contest_mode? |
|
230 | stat = self.contest_stat |
|
230 | stat = self.contest_stat |
|
231 | return ((stat != nil) and (stat.started_at != nil)) |
|
231 | return ((stat != nil) and (stat.started_at != nil)) |
|
232 | elsif GraderConfiguration.contest_mode? |
|
232 | elsif GraderConfiguration.contest_mode? |
|
233 | return true if site==nil |
|
233 | return true if site==nil |
|
234 | return site.started |
|
234 | return site.started |
|
235 | else |
|
235 | else |
|
236 | return true |
|
236 | return true |
|
237 | end |
|
237 | end |
|
238 | end |
|
238 | end |
|
239 |
|
239 | ||
|
240 | def update_start_time |
|
240 | def update_start_time |
|
241 | stat = self.contest_stat |
|
241 | stat = self.contest_stat |
|
242 |
- if stat |
|
242 | + if (stat.nil?) or (stat.started_at.nil?) |
|
243 | stat ||= UserContestStat.new(:user => self) |
|
243 | stat ||= UserContestStat.new(:user => self) |
|
244 | stat.started_at = Time.now.gmtime |
|
244 | stat.started_at = Time.now.gmtime |
|
245 | stat.save |
|
245 | stat.save |
|
246 | end |
|
246 | end |
|
247 | end |
|
247 | end |
|
248 |
|
248 | ||
|
249 | def problem_in_user_contests?(problem) |
|
249 | def problem_in_user_contests?(problem) |
|
250 | problem_contests = problem.contests.all |
|
250 | problem_contests = problem.contests.all |
|
251 |
|
251 | ||
|
252 | if problem_contests.length == 0 # this is public contest |
|
252 | if problem_contests.length == 0 # this is public contest |
|
253 | return true |
|
253 | return true |
|
254 | end |
|
254 | end |
|
255 |
|
255 | ||
|
256 | contests.each do |contest| |
|
256 | contests.each do |contest| |
|
257 | if problem_contests.find {|c| c.id == contest.id } |
|
257 | if problem_contests.find {|c| c.id == contest.id } |
|
258 | return true |
|
258 | return true |
|
259 | end |
|
259 | end |
|
260 | end |
|
260 | end |
|
261 | return false |
|
261 | return false |
|
262 | end |
|
262 | end |
|
263 |
|
263 | ||
|
264 | def available_problems_group_by_contests |
|
264 | def available_problems_group_by_contests |
|
265 | contest_problems = [] |
|
265 | contest_problems = [] |
|
266 | pin = {} |
|
266 | pin = {} |
|
267 | contests.enabled.each do |contest| |
|
267 | contests.enabled.each do |contest| |
|
268 | available_problems = contest.problems.available |
|
268 | available_problems = contest.problems.available |
|
269 | contest_problems << { |
|
269 | contest_problems << { |
|
270 | :contest => contest, |
|
270 | :contest => contest, |
|
271 | :problems => available_problems |
|
271 | :problems => available_problems |
|
272 | } |
|
272 | } |
|
273 | available_problems.each {|p| pin[p.id] = true} |
|
273 | available_problems.each {|p| pin[p.id] = true} |
|
274 | end |
|
274 | end |
|
275 | other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0} |
|
275 | other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0} |
|
276 | contest_problems << { |
|
276 | contest_problems << { |
|
277 | :contest => nil, |
|
277 | :contest => nil, |
|
278 | :problems => other_avaiable_problems |
|
278 | :problems => other_avaiable_problems |
|
279 | } |
|
279 | } |
|
280 | return contest_problems |
|
280 | return contest_problems |
|
281 | end |
|
281 | end |
|
282 |
|
282 | ||
|
283 | def available_problems |
|
283 | def available_problems |
|
284 | if not GraderConfiguration.multicontests? |
|
284 | if not GraderConfiguration.multicontests? |
|
285 | return Problem.find_available_problems |
|
285 | return Problem.find_available_problems |
|
286 | else |
|
286 | else |
|
287 | contest_problems = [] |
|
287 | contest_problems = [] |
|
288 | pin = {} |
|
288 | pin = {} |
|
289 | contests.enabled.each do |contest| |
|
289 | contests.enabled.each do |contest| |
|
290 | contest.problems.available.each do |problem| |
|
290 | contest.problems.available.each do |problem| |
|
291 | if not pin.has_key? problem.id |
|
291 | if not pin.has_key? problem.id |
|
292 | contest_problems << problem |
|
292 | contest_problems << problem |
|
293 | end |
|
293 | end |
|
294 | pin[problem.id] = true |
|
294 | pin[problem.id] = true |
|
295 | end |
|
295 | end |
|
296 | end |
|
296 | end |
|
297 | other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0} |
|
297 | other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0} |
|
298 | return contest_problems + other_avaiable_problems |
|
298 | return contest_problems + other_avaiable_problems |
|
299 | end |
|
299 | end |
|
300 | end |
|
300 | end |
|
301 |
|
301 | ||
|
302 | def can_view_problem?(problem) |
|
302 | def can_view_problem?(problem) |
|
303 | if not GraderConfiguration.multicontests? |
|
303 | if not GraderConfiguration.multicontests? |
|
304 | return problem.available |
|
304 | return problem.available |
|
305 | else |
|
305 | else |
|
306 | return problem_in_user_contests? problem |
|
306 | return problem_in_user_contests? problem |
|
307 | end |
|
307 | end |
|
308 | end |
|
308 | end |
|
309 |
|
309 | ||
|
310 | def self.clear_last_login |
|
310 | def self.clear_last_login |
|
311 | User.update_all(:last_ip => nil) |
|
311 | User.update_all(:last_ip => nil) |
|
312 | end |
|
312 | end |
|
313 |
|
313 | ||
|
314 | protected |
|
314 | protected |
|
315 | def encrypt_new_password |
|
315 | def encrypt_new_password |
|
316 | return if password.blank? |
|
316 | return if password.blank? |
|
317 | self.salt = (10+rand(90)).to_s |
|
317 | self.salt = (10+rand(90)).to_s |
|
318 | self.hashed_password = User.encrypt(self.password,self.salt) |
|
318 | self.hashed_password = User.encrypt(self.password,self.salt) |
|
319 | end |
|
319 | end |
|
320 |
|
320 | ||
|
321 | def assign_default_site |
|
321 | def assign_default_site |
|
322 | # have to catch error when migrating (because self.site is not available). |
|
322 | # have to catch error when migrating (because self.site is not available). |
|
323 | begin |
|
323 | begin |
|
324 | if self.site==nil |
|
324 | if self.site==nil |
|
325 | self.site = Site.find_by_name('default') |
|
325 | self.site = Site.find_by_name('default') |
|
326 | if self.site==nil |
|
326 | if self.site==nil |
|
327 | self.site = Site.find(1) # when 'default has be renamed' |
|
327 | self.site = Site.find(1) # when 'default has be renamed' |
|
328 | end |
|
328 | end |
|
329 | end |
|
329 | end |
|
330 | rescue |
|
330 | rescue |
|
331 | end |
|
331 | end |
|
332 | end |
|
332 | end |
|
333 |
|
333 | ||
|
334 | def assign_default_contest |
|
334 | def assign_default_contest |
|
335 | # have to catch error when migrating (because self.site is not available). |
|
335 | # have to catch error when migrating (because self.site is not available). |
|
336 | begin |
|
336 | begin |
|
337 | if self.contests.length == 0 |
|
337 | if self.contests.length == 0 |
|
338 | default_contest = Contest.find_by_name(GraderConfiguration['contest.default_contest_name']) |
|
338 | default_contest = Contest.find_by_name(GraderConfiguration['contest.default_contest_name']) |
You need to be logged in to leave comments.
Login now