Description:
Merge branch 'master' into codejom (user stat reset)
Conflicts:
app/models/user.rb
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r248:0f32f3dd1054 - - 2 files changed: 9 inserted, 1 deleted
@@ -1,30 +1,38 | |||
|
1 | 1 | class ContestsController < ApplicationController |
|
2 | 2 | |
|
3 | 3 | before_filter :admin_authorization |
|
4 | 4 | |
|
5 | 5 | def index |
|
6 | 6 | end |
|
7 | 7 | |
|
8 | 8 | def user_stat |
|
9 | 9 | if not Configuration.indv_contest_mode? |
|
10 | 10 | redirect_to :action => 'index' and return |
|
11 | 11 | end |
|
12 | 12 | |
|
13 | 13 | @users = User.find(:all) |
|
14 | 14 | @start_times = {} |
|
15 | 15 | UserContestStat.find(:all).each do |stat| |
|
16 | 16 | @start_times[stat.user_id] = stat.started_at |
|
17 | 17 | end |
|
18 | 18 | end |
|
19 | 19 | |
|
20 | + def clear_stat | |
|
21 | + user = User.find(params[:id]) | |
|
22 | + if user.contest_stat!=nil | |
|
23 | + user.contest_stat.destroy | |
|
24 | + end | |
|
25 | + redirect_to :action => 'user_stat' | |
|
26 | + end | |
|
27 | + | |
|
20 | 28 | def clear_all_stat |
|
21 | 29 | if not Configuration.indv_contest_mode? |
|
22 | 30 | redirect_to :action => 'index' and return |
|
23 | 31 | end |
|
24 | 32 | |
|
25 | 33 | UserContestStat.delete_all() |
|
26 | 34 | flash[:notice] = 'All start time statistic cleared.' |
|
27 | 35 | redirect_to :action => 'index' |
|
28 | 36 | end |
|
29 | 37 | |
|
30 | 38 | end |
@@ -1,315 +1,315 | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | require 'digest/sha1' |
|
3 | 3 | |
|
4 | 4 | class User < ActiveRecord::Base |
|
5 | 5 | |
|
6 | 6 | has_and_belongs_to_many :roles |
|
7 | 7 | |
|
8 | 8 | has_many :test_requests, :order => "submitted_at DESC" |
|
9 | 9 | |
|
10 | 10 | has_many :messages, |
|
11 | 11 | :class_name => "Message", |
|
12 | 12 | :foreign_key => "sender_id", |
|
13 | 13 | :order => 'created_at DESC' |
|
14 | 14 | |
|
15 | 15 | has_many :replied_messages, |
|
16 | 16 | :class_name => "Message", |
|
17 | 17 | :foreign_key => "receiver_id", |
|
18 | 18 | :order => 'created_at DESC' |
|
19 | 19 | |
|
20 | 20 | has_many :test_pair_assignments, :dependent => :delete_all |
|
21 | 21 | has_many :submission_statuses |
|
22 | 22 | |
|
23 | - has_one :contest_stat, :class_name => "UserContestStat" | |
|
23 | + has_one :contest_stat, :class_name => "UserContestStat", :dependent => :destroy | |
|
24 | 24 | |
|
25 | 25 | belongs_to :site |
|
26 | 26 | belongs_to :country |
|
27 | 27 | |
|
28 | 28 | # For Code Jom |
|
29 | 29 | has_one :codejom_status |
|
30 | 30 | |
|
31 | 31 | named_scope :activated_users, :conditions => {:activated => true} |
|
32 | 32 | |
|
33 | 33 | validates_presence_of :login |
|
34 | 34 | validates_uniqueness_of :login |
|
35 | 35 | validates_format_of :login, :with => /^[\_A-Za-z0-9]+$/ |
|
36 | 36 | validates_length_of :login, :within => 3..20 |
|
37 | 37 | |
|
38 | 38 | validates_presence_of :full_name |
|
39 | 39 | validates_length_of :full_name, :minimum => 1 |
|
40 | 40 | |
|
41 | 41 | validates_presence_of :member1_full_name |
|
42 | 42 | validates_length_of :member1_full_name, :minimum => 1 |
|
43 | 43 | |
|
44 | 44 | validates_presence_of :password, :if => :password_required? |
|
45 | 45 | validates_length_of :password, :within => 4..20, :if => :password_required? |
|
46 | 46 | validates_confirmation_of :password, :if => :password_required? |
|
47 | 47 | |
|
48 | 48 | validates_format_of :email, |
|
49 | 49 | :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, |
|
50 | 50 | :if => :email_validation? |
|
51 | 51 | validate :uniqueness_of_email_from_activated_users, |
|
52 | 52 | :if => :email_validation? |
|
53 | 53 | validate :enough_time_interval_between_same_email_registrations, |
|
54 | 54 | :if => :email_validation? |
|
55 | 55 | |
|
56 | 56 | validate :school_names_for_high_school_users |
|
57 | 57 | |
|
58 | 58 | # these are for ytopc |
|
59 | 59 | # disable for now |
|
60 | 60 | #validates_presence_of :province |
|
61 | 61 | |
|
62 | 62 | attr_accessor :password |
|
63 | 63 | |
|
64 | 64 | before_save :encrypt_new_password |
|
65 | 65 | before_save :assign_default_site |
|
66 | 66 | |
|
67 | 67 | def self.authenticate(login, password) |
|
68 | 68 | user = find_by_login(login) |
|
69 | 69 | return user if user && user.authenticated?(password) |
|
70 | 70 | end |
|
71 | 71 | |
|
72 | 72 | def authenticated?(password) |
|
73 | 73 | if self.activated |
|
74 | 74 | hashed_password == User.encrypt(password,self.salt) |
|
75 | 75 | else |
|
76 | 76 | false |
|
77 | 77 | end |
|
78 | 78 | end |
|
79 | 79 | |
|
80 | 80 | def admin? |
|
81 | 81 | self.roles.detect {|r| r.name == 'admin' } |
|
82 | 82 | end |
|
83 | 83 | |
|
84 | 84 | # These are methods related to test pairs |
|
85 | 85 | |
|
86 | 86 | def get_test_pair_assignments_for(problem) |
|
87 | 87 | test_pair_assignments.find_all { |a| a.problem_id == problem.id } |
|
88 | 88 | end |
|
89 | 89 | |
|
90 | 90 | def get_recent_test_pair_assignment_for(problem) |
|
91 | 91 | assignments = get_test_pair_assignments_for problem |
|
92 | 92 | if assignments.length == 0 |
|
93 | 93 | return nil |
|
94 | 94 | else |
|
95 | 95 | recent = assignments[0] |
|
96 | 96 | assignments.each do |a| |
|
97 | 97 | recent = a if a.request_number > recent.request_number |
|
98 | 98 | end |
|
99 | 99 | return recent |
|
100 | 100 | end |
|
101 | 101 | end |
|
102 | 102 | |
|
103 | 103 | def can_request_new_test_pair_for?(problem) |
|
104 | 104 | recent = get_recent_test_pair_assignment_for problem |
|
105 | 105 | return (recent == nil or recent.submitted or recent.expired?) |
|
106 | 106 | end |
|
107 | 107 | |
|
108 | 108 | def get_new_test_pair_assignment_for(problem) |
|
109 | 109 | previous_assignment_numbers = |
|
110 | 110 | get_test_pair_assignments_for(problem).collect {|a| a.test_pair_number } |
|
111 | 111 | test_pair = problem.random_test_pair(previous_assignment_numbers) |
|
112 | 112 | if test_pair |
|
113 | 113 | assignment = TestPairAssignment.new(:user => self, |
|
114 | 114 | :problem => problem, |
|
115 | 115 | :test_pair => test_pair, |
|
116 | 116 | :test_pair_number => test_pair.number, |
|
117 | 117 | :request_number => |
|
118 | 118 | previous_assignment_numbers.length + 1, |
|
119 | 119 | :submitted => false) |
|
120 | 120 | return assignment |
|
121 | 121 | else |
|
122 | 122 | return nil |
|
123 | 123 | end |
|
124 | 124 | end |
|
125 | 125 | |
|
126 | 126 | def get_submission_status_for(problem) |
|
127 | 127 | SubmissionStatus.find(:first, |
|
128 | 128 | :conditions => { |
|
129 | 129 | :user_id => id, |
|
130 | 130 | :problem_id => problem.id |
|
131 | 131 | }) |
|
132 | 132 | end |
|
133 | 133 | |
|
134 | 134 | def email_for_editing |
|
135 | 135 | if self.email==nil |
|
136 | 136 | "(unknown)" |
|
137 | 137 | elsif self.email=='' |
|
138 | 138 | "(blank)" |
|
139 | 139 | else |
|
140 | 140 | self.email |
|
141 | 141 | end |
|
142 | 142 | end |
|
143 | 143 | |
|
144 | 144 | def email_for_editing=(e) |
|
145 | 145 | self.email=e |
|
146 | 146 | end |
|
147 | 147 | |
|
148 | 148 | def alias_for_editing |
|
149 | 149 | if self.alias==nil |
|
150 | 150 | "(unknown)" |
|
151 | 151 | elsif self.alias=='' |
|
152 | 152 | "(blank)" |
|
153 | 153 | else |
|
154 | 154 | self.alias |
|
155 | 155 | end |
|
156 | 156 | end |
|
157 | 157 | |
|
158 | 158 | def alias_for_editing=(e) |
|
159 | 159 | self.alias=e |
|
160 | 160 | end |
|
161 | 161 | |
|
162 | 162 | def activation_key |
|
163 | 163 | if self.hashed_password==nil |
|
164 | 164 | encrypt_new_password |
|
165 | 165 | end |
|
166 | 166 | Digest::SHA1.hexdigest(self.hashed_password)[0..7] |
|
167 | 167 | end |
|
168 | 168 | |
|
169 | 169 | def verify_activation_key(key) |
|
170 | 170 | key == activation_key |
|
171 | 171 | end |
|
172 | 172 | |
|
173 | 173 | def self.random_password(length=5) |
|
174 | 174 | chars = 'abcdefghjkmnopqrstuvwxyz' |
|
175 | 175 | password = '' |
|
176 | 176 | length.times { password << chars[rand(chars.length - 1)] } |
|
177 | 177 | password |
|
178 | 178 | end |
|
179 | 179 | |
|
180 | 180 | def self.find_non_admin_with_prefix(prefix='') |
|
181 | 181 | users = User.find(:all) |
|
182 | 182 | return users.find_all { |u| !(u.admin?) and u.login.index(prefix)==0 } |
|
183 | 183 | end |
|
184 | 184 | |
|
185 | 185 | # Contest information |
|
186 | 186 | |
|
187 | 187 | def contest_time_left |
|
188 | 188 | if Configuration.contest_mode? |
|
189 | 189 | return nil if site==nil |
|
190 | 190 | return site.time_left |
|
191 | 191 | elsif Configuration.indv_contest_mode? |
|
192 | 192 | time_limit = Configuration.contest_time_limit |
|
193 | 193 | if contest_stat==nil |
|
194 | 194 | return (Time.now.gmtime + time_limit) - Time.now.gmtime |
|
195 | 195 | else |
|
196 | 196 | finish_time = contest_stat.started_at + time_limit |
|
197 | 197 | current_time = Time.now.gmtime |
|
198 | 198 | if current_time > finish_time |
|
199 | 199 | return 0 |
|
200 | 200 | else |
|
201 | 201 | return finish_time - current_time |
|
202 | 202 | end |
|
203 | 203 | end |
|
204 | 204 | else |
|
205 | 205 | return nil |
|
206 | 206 | end |
|
207 | 207 | end |
|
208 | 208 | |
|
209 | 209 | def contest_finished? |
|
210 | 210 | if Configuration.contest_mode? |
|
211 | 211 | return false if site==nil |
|
212 | 212 | return site.finished? |
|
213 | 213 | elsif Configuration.indv_contest_mode? |
|
214 | 214 | time_limit = Configuration.contest_time_limit |
|
215 | 215 | |
|
216 | 216 | return false if contest_stat==nil |
|
217 | 217 | |
|
218 | 218 | return contest_time_left == 0 |
|
219 | 219 | else |
|
220 | 220 | return false |
|
221 | 221 | end |
|
222 | 222 | end |
|
223 | 223 | |
|
224 | 224 | def contest_started? |
|
225 | 225 | if Configuration.contest_mode? |
|
226 | 226 | return true if site==nil |
|
227 | 227 | return site.started |
|
228 | 228 | else |
|
229 | 229 | return true |
|
230 | 230 | end |
|
231 | 231 | end |
|
232 | 232 | |
|
233 | 233 | # For Code Jom |
|
234 | 234 | def update_codejom_status |
|
235 | 235 | status = codejom_status || CodejomStatus.new(:user => self) |
|
236 | 236 | problem_count = Problem.available_problem_count |
|
237 | 237 | status.num_problems_passed = (self.submission_statuses.find_all {|s| s.passed and s.problem.available }).length |
|
238 | 238 | status.alive = (problem_count - (status.num_problems_passed)) <= CODEJOM_MAX_ALIVE_LEVEL |
|
239 | 239 | status.save |
|
240 | 240 | end |
|
241 | 241 | |
|
242 | 242 | def codejom_level |
|
243 | 243 | problem_count = Problem.available_problem_count |
|
244 | 244 | if codejom_status!=nil |
|
245 | 245 | return problem_count - codejom_status.num_problems_passed |
|
246 | 246 | else |
|
247 | 247 | return problem_count |
|
248 | 248 | end |
|
249 | 249 | end |
|
250 | 250 | |
|
251 | 251 | protected |
|
252 | 252 | def encrypt_new_password |
|
253 | 253 | return if password.blank? |
|
254 | 254 | self.salt = (10+rand(90)).to_s |
|
255 | 255 | self.hashed_password = User.encrypt(self.password,self.salt) |
|
256 | 256 | end |
|
257 | 257 | |
|
258 | 258 | def assign_default_site |
|
259 | 259 | # have to catch error when migrating (because self.site is not available). |
|
260 | 260 | begin |
|
261 | 261 | if self.site==nil |
|
262 | 262 | self.site = Site.find_by_name('default') |
|
263 | 263 | if self.site==nil |
|
264 | 264 | self.site = Site.find(1) # when 'default has be renamed' |
|
265 | 265 | end |
|
266 | 266 | end |
|
267 | 267 | rescue |
|
268 | 268 | end |
|
269 | 269 | end |
|
270 | 270 | |
|
271 | 271 | def password_required? |
|
272 | 272 | self.hashed_password.blank? || !self.password.blank? |
|
273 | 273 | end |
|
274 | 274 | |
|
275 | 275 | def self.encrypt(string,salt) |
|
276 | 276 | Digest::SHA1.hexdigest(salt + string) |
|
277 | 277 | end |
|
278 | 278 | |
|
279 | 279 | def uniqueness_of_email_from_activated_users |
|
280 | 280 | user = User.activated_users.find_by_email(self.email) |
|
281 | 281 | if user and (user.login != self.login) |
|
282 | 282 | self.errors.add_to_base("Email has already been taken") |
|
283 | 283 | end |
|
284 | 284 | end |
|
285 | 285 | |
|
286 | 286 | def enough_time_interval_between_same_email_registrations |
|
287 | 287 | return if !self.new_record? |
|
288 | 288 | return if self.activated |
|
289 | 289 | open_user = User.find_by_email(self.email, |
|
290 | 290 | :order => 'created_at DESC') |
|
291 | 291 | if open_user and open_user.created_at and |
|
292 | 292 | (open_user.created_at > Time.now.gmtime - 5.minutes) |
|
293 | 293 | self.errors.add_to_base("There are already unactivated registrations with this e-mail address (please wait for 5 minutes)") |
|
294 | 294 | end |
|
295 | 295 | end |
|
296 | 296 | |
|
297 | 297 | def email_validation? |
|
298 | 298 | begin |
|
299 | 299 | return VALIDATE_USER_EMAILS |
|
300 | 300 | rescue |
|
301 | 301 | return false |
|
302 | 302 | end |
|
303 | 303 | end |
|
304 | 304 | |
|
305 | 305 | |
|
306 | 306 | def school_names_for_high_school_users |
|
307 | 307 | if self.high_school |
|
308 | 308 | if (self.member1_school_name=='' or |
|
309 | 309 | (self.member2_full_name!='' and self.member2_school_name=='') or |
|
310 | 310 | (self.member3_full_name!='' and self.member3_school_name=='')) |
|
311 | 311 | self.errors.add_to_base("โปรดระบุชื่อโรงเรียนสำหรับสมาชิกในทีมทุกคน") |
|
312 | 312 | end |
|
313 | 313 | end |
|
314 | 314 | end |
|
315 | 315 | end |
You need to be logged in to leave comments.
Login now