Description:
remove junk from ealier merge
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r484:a0c51d75908c - - 1 file changed: 0 inserted, 28 deleted

@@ -1,558 +1,530
1 require 'csv'
1 require 'csv'
2
2
3 class UserAdminController < ApplicationController
3 class UserAdminController < ApplicationController
4
4
5 include MailHelperMethods
5 include MailHelperMethods
6
6
7 before_filter :admin_authorization
7 before_filter :admin_authorization
8
8
9 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
9 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
10 verify :method => :post, :only => [ :destroy,
10 verify :method => :post, :only => [ :destroy,
11 :create, :create_from_list,
11 :create, :create_from_list,
12 :update,
12 :update,
13 :manage_contest,
13 :manage_contest,
14 :bulk_mail
14 :bulk_mail
15 ],
15 ],
16 :redirect_to => { :action => :list }
16 :redirect_to => { :action => :list }
17
17
18 def index
18 def index
19 list
19 list
20 render :action => 'list'
20 render :action => 'list'
21 end
21 end
22
22
23 def list
23 def list
24 @user_count = User.count
24 @user_count = User.count
25 if params[:page] == 'all'
25 if params[:page] == 'all'
26 @users = User.all
26 @users = User.all
27 @paginated = false
27 @paginated = false
28 else
28 else
29 @users = User.paginate :page => params[:page]
29 @users = User.paginate :page => params[:page]
30 @paginated = true
30 @paginated = true
31 end
31 end
32 @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at']
32 @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at']
33 @contests = Contest.enabled
33 @contests = Contest.enabled
34 end
34 end
35
35
36 def active
36 def active
37 sessions = ActiveRecord::SessionStore::Session.find(:all, :conditions => ["updated_at >= ?", 60.minutes.ago])
37 sessions = ActiveRecord::SessionStore::Session.find(:all, :conditions => ["updated_at >= ?", 60.minutes.ago])
38 @users = []
38 @users = []
39 sessions.each do |session|
39 sessions.each do |session|
40 if session.data[:user_id]
40 if session.data[:user_id]
41 @users << User.find(session.data[:user_id])
41 @users << User.find(session.data[:user_id])
42 end
42 end
43 end
43 end
44 end
44 end
45
45
46 def show
46 def show
47 @user = User.find(params[:id])
47 @user = User.find(params[:id])
48 end
48 end
49
49
50 def new
50 def new
51 @user = User.new
51 @user = User.new
52 end
52 end
53
53
54 def create
54 def create
55 @user = User.new(params[:user])
55 @user = User.new(params[:user])
56 @user.activated = true
56 @user.activated = true
57 if @user.save
57 if @user.save
58 flash[:notice] = 'User was successfully created.'
58 flash[:notice] = 'User was successfully created.'
59 redirect_to :action => 'list'
59 redirect_to :action => 'list'
60 else
60 else
61 render :action => 'new'
61 render :action => 'new'
62 end
62 end
63 end
63 end
64
64
65 def create_from_list
65 def create_from_list
66 lines = params[:user_list]
66 lines = params[:user_list]
67
67
68 note = []
68 note = []
69
69
70 lines.split("\n").each do |line|
70 lines.split("\n").each do |line|
71 items = line.chomp.split(',')
71 items = line.chomp.split(',')
72 if items.length>=2
72 if items.length>=2
73 login = items[0]
73 login = items[0]
74 full_name = items[1]
74 full_name = items[1]
75
75
76 added_random_password = false
76 added_random_password = false
77 if items.length>=3
77 if items.length>=3
78 password = items[2].chomp(" ")
78 password = items[2].chomp(" ")
79 user_alias = (items.length>=4) ? items[3] : login
79 user_alias = (items.length>=4) ? items[3] : login
80 else
80 else
81 password = random_password
81 password = random_password
82 user_alias = (items.length>=4) ? items[3] : login
82 user_alias = (items.length>=4) ? items[3] : login
83 added_random_password = true
83 added_random_password = true
84 end
84 end
85
85
86 user = User.find_by_login(login)
86 user = User.find_by_login(login)
87 if (user)
87 if (user)
88 user.full_name = full_name
88 user.full_name = full_name
89 user.password = password
89 user.password = password
90 else
90 else
91 user = User.new({:login => login,
91 user = User.new({:login => login,
92 :full_name => full_name,
92 :full_name => full_name,
93 :password => password,
93 :password => password,
94 :password_confirmation => password,
94 :password_confirmation => password,
95 :alias => user_alias})
95 :alias => user_alias})
96 end
96 end
97 user.activated = true
97 user.activated = true
98 user.save
98 user.save
99
99
100 if added_random_password
100 if added_random_password
101 note << "'#{login}' (+)"
101 note << "'#{login}' (+)"
102 else
102 else
103 note << login
103 note << login
104 end
104 end
105 end
105 end
106 end
106 end
107 flash[:notice] = 'User(s) ' + note.join(', ') +
107 flash[:notice] = 'User(s) ' + note.join(', ') +
108 ' were successfully created. ' +
108 ' were successfully created. ' +
109 '( (+) - created with random passwords.)'
109 '( (+) - created with random passwords.)'
110 redirect_to :action => 'list'
110 redirect_to :action => 'list'
111 end
111 end
112
112
113 def edit
113 def edit
114 @user = User.find(params[:id])
114 @user = User.find(params[:id])
115 end
115 end
116
116
117 def update
117 def update
118 @user = User.find(params[:id])
118 @user = User.find(params[:id])
119 if @user.update_attributes(params[:user])
119 if @user.update_attributes(params[:user])
120 flash[:notice] = 'User was successfully updated.'
120 flash[:notice] = 'User was successfully updated.'
121 redirect_to :action => 'show', :id => @user
121 redirect_to :action => 'show', :id => @user
122 else
122 else
123 render :action => 'edit'
123 render :action => 'edit'
124 end
124 end
125 end
125 end
126
126
127 def destroy
127 def destroy
128 User.find(params[:id]).destroy
128 User.find(params[:id]).destroy
129 redirect_to :action => 'list'
129 redirect_to :action => 'list'
130 end
130 end
131
131
132 def user_stat
132 def user_stat
133 if params[:commit] == 'download csv'
133 if params[:commit] == 'download csv'
134 @problems = Problem.all
134 @problems = Problem.all
135 else
135 else
136 @problems = Problem.find_available_problems
136 @problems = Problem.find_available_problems
137 end
137 end
138 @users = User.find(:all, :include => [:contests, :contest_stat])
138 @users = User.find(:all, :include => [:contests, :contest_stat])
139 @scorearray = Array.new
139 @scorearray = Array.new
140 @users.each do |u|
140 @users.each do |u|
141 ustat = Array.new
141 ustat = Array.new
142 ustat[0] = u
142 ustat[0] = u
143 @problems.each do |p|
143 @problems.each do |p|
144 sub = Submission.find_last_by_user_and_problem(u.id,p.id)
144 sub = Submission.find_last_by_user_and_problem(u.id,p.id)
145 if (sub!=nil) and (sub.points!=nil)
145 if (sub!=nil) and (sub.points!=nil)
146 ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)]
146 ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)]
147 else
147 else
148 ustat << [0,false]
148 ustat << [0,false]
149 end
149 end
150 end
150 end
151 @scorearray << ustat
151 @scorearray << ustat
152 end
152 end
153 end
153 end
154
154
155 def user_stat_max
155 def user_stat_max
156 - @problems = Problem.find_available_problems
157 - @users = User.find(:all, :include => [:contests, :contest_stat])
158 - @scorearray = Array.new
159 - #set up range from param
160 - since_id = params.fetch(:since_id, 0).to_i
161 - until_id = params.fetch(:until_id, 0).to_i
162 - @users.each do |u|
163 - ustat = Array.new
164 - ustat[0] = u
165 - @problems.each do |p|
166 - max_points = 0
167 - Submission.find_in_range_by_user_and_problem(u.id,p.id,since_id,until_id).each do |sub|
168 - max_points = sub.points if sub and sub.points and (sub.points > max_points)
169 - end
170 - ustat << [(max_points.to_f*100/p.full_score).round, (max_points>=p.full_score)]
171 - end
172 - @scorearray << ustat
173 - end
174 -
175 - if params[:commit] == 'download csv' then
176 - csv = gen_csv_from_scorearray(@scorearray,@problems)
177 - send_data csv, filename: 'last_score.csv'
178 - else
179 - render template: 'user_admin/user_stat'
180 - end
181 - end
182 -
183 - def user_stat_max
184 if params[:commit] == 'download csv'
156 if params[:commit] == 'download csv'
185 @problems = Problem.all
157 @problems = Problem.all
186 else
158 else
187 @problems = Problem.find_available_problems
159 @problems = Problem.find_available_problems
188 end
160 end
189 @users = User.find(:all, :include => [:contests, :contest_stat])
161 @users = User.find(:all, :include => [:contests, :contest_stat])
190 @scorearray = Array.new
162 @scorearray = Array.new
191 #set up range from param
163 #set up range from param
192 since_id = params.fetch(:since_id, 0).to_i
164 since_id = params.fetch(:since_id, 0).to_i
193 until_id = params.fetch(:until_id, 0).to_i
165 until_id = params.fetch(:until_id, 0).to_i
194 @users.each do |u|
166 @users.each do |u|
195 ustat = Array.new
167 ustat = Array.new
196 ustat[0] = u
168 ustat[0] = u
197 @problems.each do |p|
169 @problems.each do |p|
198 max_points = 0
170 max_points = 0
199 Submission.find_in_range_by_user_and_problem(u.id,p.id,since_id,until_id).each do |sub|
171 Submission.find_in_range_by_user_and_problem(u.id,p.id,since_id,until_id).each do |sub|
200 max_points = sub.points if sub and sub.points and (sub.points > max_points)
172 max_points = sub.points if sub and sub.points and (sub.points > max_points)
201 end
173 end
202 ustat << [(max_points.to_f*100/p.full_score).round, (max_points>=p.full_score)]
174 ustat << [(max_points.to_f*100/p.full_score).round, (max_points>=p.full_score)]
203 end
175 end
204 @scorearray << ustat
176 @scorearray << ustat
205 end
177 end
206
178
207 if params[:commit] == 'download csv' then
179 if params[:commit] == 'download csv' then
208 csv = gen_csv_from_scorearray(@scorearray,@problems)
180 csv = gen_csv_from_scorearray(@scorearray,@problems)
209 send_data csv, filename: 'max_score.csv'
181 send_data csv, filename: 'max_score.csv'
210 else
182 else
211 render template: 'user_admin/user_stat'
183 render template: 'user_admin/user_stat'
212 end
184 end
213 end
185 end
214
186
215 def import
187 def import
216 if params[:file]==''
188 if params[:file]==''
217 flash[:notice] = 'Error importing no file'
189 flash[:notice] = 'Error importing no file'
218 redirect_to :action => 'list' and return
190 redirect_to :action => 'list' and return
219 end
191 end
220 import_from_file(params[:file])
192 import_from_file(params[:file])
221 end
193 end
222
194
223 def random_all_passwords
195 def random_all_passwords
224 users = User.find(:all)
196 users = User.find(:all)
225 @prefix = params[:prefix] || ''
197 @prefix = params[:prefix] || ''
226 @non_admin_users = User.find_non_admin_with_prefix(@prefix)
198 @non_admin_users = User.find_non_admin_with_prefix(@prefix)
227 @changed = false
199 @changed = false
228 if request.request_method == 'POST'
200 if request.request_method == 'POST'
229 @non_admin_users.each do |user|
201 @non_admin_users.each do |user|
230 password = random_password
202 password = random_password
231 user.password = password
203 user.password = password
232 user.password_confirmation = password
204 user.password_confirmation = password
233 user.save
205 user.save
234 end
206 end
235 @changed = true
207 @changed = true
236 end
208 end
237 end
209 end
238
210
239 # contest management
211 # contest management
240
212
241 def contests
213 def contests
242 @contest, @users = find_contest_and_user_from_contest_id(params[:id])
214 @contest, @users = find_contest_and_user_from_contest_id(params[:id])
243 @contests = Contest.enabled
215 @contests = Contest.enabled
244 end
216 end
245
217
246 def assign_from_list
218 def assign_from_list
247 contest_id = params[:users_contest_id]
219 contest_id = params[:users_contest_id]
248 org_contest, users = find_contest_and_user_from_contest_id(contest_id)
220 org_contest, users = find_contest_and_user_from_contest_id(contest_id)
249 contest = Contest.find(params[:new_contest][:id])
221 contest = Contest.find(params[:new_contest][:id])
250 if !contest
222 if !contest
251 flash[:notice] = 'Error: no contest'
223 flash[:notice] = 'Error: no contest'
252 redirect_to :action => 'contests', :id =>contest_id
224 redirect_to :action => 'contests', :id =>contest_id
253 end
225 end
254
226
255 note = []
227 note = []
256 users.each do |u|
228 users.each do |u|
257 u.contests = [contest]
229 u.contests = [contest]
258 note << u.login
230 note << u.login
259 end
231 end
260 flash[:notice] = 'User(s) ' + note.join(', ') +
232 flash[:notice] = 'User(s) ' + note.join(', ') +
261 " were successfully reassigned to #{contest.title}."
233 " were successfully reassigned to #{contest.title}."
262 redirect_to :action => 'contests', :id =>contest.id
234 redirect_to :action => 'contests', :id =>contest.id
263 end
235 end
264
236
265 def add_to_contest
237 def add_to_contest
266 user = User.find(params[:id])
238 user = User.find(params[:id])
267 contest = Contest.find(params[:contest_id])
239 contest = Contest.find(params[:contest_id])
268 if user and contest
240 if user and contest
269 user.contests << contest
241 user.contests << contest
270 end
242 end
271 redirect_to :action => 'list'
243 redirect_to :action => 'list'
272 end
244 end
273
245
274 def remove_from_contest
246 def remove_from_contest
275 user = User.find(params[:id])
247 user = User.find(params[:id])
276 contest = Contest.find(params[:contest_id])
248 contest = Contest.find(params[:contest_id])
277 if user and contest
249 if user and contest
278 user.contests.delete(contest)
250 user.contests.delete(contest)
279 end
251 end
280 redirect_to :action => 'list'
252 redirect_to :action => 'list'
281 end
253 end
282
254
283 def contest_management
255 def contest_management
284 end
256 end
285
257
286 def manage_contest
258 def manage_contest
287 contest = Contest.find(params[:contest][:id])
259 contest = Contest.find(params[:contest][:id])
288 if !contest
260 if !contest
289 flash[:notice] = 'You did not choose the contest.'
261 flash[:notice] = 'You did not choose the contest.'
290 redirect_to :action => 'contest_management' and return
262 redirect_to :action => 'contest_management' and return
291 end
263 end
292
264
293 operation = params[:operation]
265 operation = params[:operation]
294
266
295 if not ['add','remove','assign'].include? operation
267 if not ['add','remove','assign'].include? operation
296 flash[:notice] = 'You did not choose the operation to perform.'
268 flash[:notice] = 'You did not choose the operation to perform.'
297 redirect_to :action => 'contest_management' and return
269 redirect_to :action => 'contest_management' and return
298 end
270 end
299
271
300 lines = params[:login_list]
272 lines = params[:login_list]
301 if !lines or lines.blank?
273 if !lines or lines.blank?
302 flash[:notice] = 'You entered an empty list.'
274 flash[:notice] = 'You entered an empty list.'
303 redirect_to :action => 'contest_management' and return
275 redirect_to :action => 'contest_management' and return
304 end
276 end
305
277
306 note = []
278 note = []
307 users = []
279 users = []
308 lines.split("\n").each do |line|
280 lines.split("\n").each do |line|
309 user = User.find_by_login(line.chomp)
281 user = User.find_by_login(line.chomp)
310 if user
282 if user
311 if operation=='add'
283 if operation=='add'
312 if ! user.contests.include? contest
284 if ! user.contests.include? contest
313 user.contests << contest
285 user.contests << contest
314 end
286 end
315 elsif operation=='remove'
287 elsif operation=='remove'
316 user.contests.delete(contest)
288 user.contests.delete(contest)
317 else
289 else
318 user.contests = [contest]
290 user.contests = [contest]
319 end
291 end
320
292
321 if params[:reset_timer]
293 if params[:reset_timer]
322 user.contest_stat.forced_logout = true
294 user.contest_stat.forced_logout = true
323 user.contest_stat.reset_timer_and_save
295 user.contest_stat.reset_timer_and_save
324 end
296 end
325
297
326 if params[:notification_emails]
298 if params[:notification_emails]
327 send_contest_update_notification_email(user, contest)
299 send_contest_update_notification_email(user, contest)
328 end
300 end
329
301
330 note << user.login
302 note << user.login
331 users << user
303 users << user
332 end
304 end
333 end
305 end
334
306
335 if params[:reset_timer]
307 if params[:reset_timer]
336 logout_users(users)
308 logout_users(users)
337 end
309 end
338
310
339 flash[:notice] = 'User(s) ' + note.join(', ') +
311 flash[:notice] = 'User(s) ' + note.join(', ') +
340 ' were successfully modified. '
312 ' were successfully modified. '
341 redirect_to :action => 'contest_management'
313 redirect_to :action => 'contest_management'
342 end
314 end
343
315
344 # admin management
316 # admin management
345
317
346 def admin
318 def admin
347 @admins = User.find(:all).find_all {|user| user.admin? }
319 @admins = User.find(:all).find_all {|user| user.admin? }
348 end
320 end
349
321
350 def grant_admin
322 def grant_admin
351 login = params[:login]
323 login = params[:login]
352 user = User.find_by_login(login)
324 user = User.find_by_login(login)
353 if user!=nil
325 if user!=nil
354 admin_role = Role.find_by_name('admin')
326 admin_role = Role.find_by_name('admin')
355 user.roles << admin_role
327 user.roles << admin_role
356 else
328 else
357 flash[:notice] = 'Unknown user'
329 flash[:notice] = 'Unknown user'
358 end
330 end
359 flash[:notice] = 'User added as admins'
331 flash[:notice] = 'User added as admins'
360 redirect_to :action => 'admin'
332 redirect_to :action => 'admin'
361 end
333 end
362
334
363 def revoke_admin
335 def revoke_admin
364 user = User.find(params[:id])
336 user = User.find(params[:id])
365 if user==nil
337 if user==nil
366 flash[:notice] = 'Unknown user'
338 flash[:notice] = 'Unknown user'
367 redirect_to :action => 'admin' and return
339 redirect_to :action => 'admin' and return
368 elsif user.login == 'root'
340 elsif user.login == 'root'
369 flash[:notice] = 'You cannot revoke admisnistrator permission from root.'
341 flash[:notice] = 'You cannot revoke admisnistrator permission from root.'
370 redirect_to :action => 'admin' and return
342 redirect_to :action => 'admin' and return
371 end
343 end
372
344
373 admin_role = Role.find_by_name('admin')
345 admin_role = Role.find_by_name('admin')
374 user.roles.delete(admin_role)
346 user.roles.delete(admin_role)
375 flash[:notice] = 'User permission revoked'
347 flash[:notice] = 'User permission revoked'
376 redirect_to :action => 'admin'
348 redirect_to :action => 'admin'
377 end
349 end
378
350
379 # mass mailing
351 # mass mailing
380
352
381 def mass_mailing
353 def mass_mailing
382 end
354 end
383
355
384 def bulk_mail
356 def bulk_mail
385 lines = params[:login_list]
357 lines = params[:login_list]
386 if !lines or lines.blank?
358 if !lines or lines.blank?
387 flash[:notice] = 'You entered an empty list.'
359 flash[:notice] = 'You entered an empty list.'
388 redirect_to :action => 'mass_mailing' and return
360 redirect_to :action => 'mass_mailing' and return
389 end
361 end
390
362
391 mail_subject = params[:subject]
363 mail_subject = params[:subject]
392 if !mail_subject or mail_subject.blank?
364 if !mail_subject or mail_subject.blank?
393 flash[:notice] = 'You entered an empty mail subject.'
365 flash[:notice] = 'You entered an empty mail subject.'
394 redirect_to :action => 'mass_mailing' and return
366 redirect_to :action => 'mass_mailing' and return
395 end
367 end
396
368
397 mail_body = params[:email_body]
369 mail_body = params[:email_body]
398 if !mail_body or mail_body.blank?
370 if !mail_body or mail_body.blank?
399 flash[:notice] = 'You entered an empty mail body.'
371 flash[:notice] = 'You entered an empty mail body.'
400 redirect_to :action => 'mass_mailing' and return
372 redirect_to :action => 'mass_mailing' and return
401 end
373 end
402
374
403 note = []
375 note = []
404 users = []
376 users = []
405 lines.split("\n").each do |line|
377 lines.split("\n").each do |line|
406 user = User.find_by_login(line.chomp)
378 user = User.find_by_login(line.chomp)
407 if user
379 if user
408 send_mail(user.email, mail_subject, mail_body)
380 send_mail(user.email, mail_subject, mail_body)
409 note << user.login
381 note << user.login
410 end
382 end
411 end
383 end
412
384
413 flash[:notice] = 'User(s) ' + note.join(', ') +
385 flash[:notice] = 'User(s) ' + note.join(', ') +
414 ' were successfully modified. '
386 ' were successfully modified. '
415 redirect_to :action => 'mass_mailing'
387 redirect_to :action => 'mass_mailing'
416 end
388 end
417
389
418 protected
390 protected
419
391
420 def random_password(length=5)
392 def random_password(length=5)
421 chars = 'abcdefghijkmnopqrstuvwxyz23456789'
393 chars = 'abcdefghijkmnopqrstuvwxyz23456789'
422 newpass = ""
394 newpass = ""
423 length.times { newpass << chars[rand(chars.size-1)] }
395 length.times { newpass << chars[rand(chars.size-1)] }
424 return newpass
396 return newpass
425 end
397 end
426
398
427 def import_from_file(f)
399 def import_from_file(f)
428 data_hash = YAML.load(f)
400 data_hash = YAML.load(f)
429 @import_log = ""
401 @import_log = ""
430
402
431 country_data = data_hash[:countries]
403 country_data = data_hash[:countries]
432 site_data = data_hash[:sites]
404 site_data = data_hash[:sites]
433 user_data = data_hash[:users]
405 user_data = data_hash[:users]
434
406
435 # import country
407 # import country
436 countries = {}
408 countries = {}
437 country_data.each_pair do |id,country|
409 country_data.each_pair do |id,country|
438 c = Country.find_by_name(country[:name])
410 c = Country.find_by_name(country[:name])
439 if c!=nil
411 if c!=nil
440 countries[id] = c
412 countries[id] = c
441 @import_log << "Found #{country[:name]}\n"
413 @import_log << "Found #{country[:name]}\n"
442 else
414 else
443 countries[id] = Country.new(:name => country[:name])
415 countries[id] = Country.new(:name => country[:name])
444 countries[id].save
416 countries[id].save
445 @import_log << "Created #{country[:name]}\n"
417 @import_log << "Created #{country[:name]}\n"
446 end
418 end
447 end
419 end
448
420
449 # import sites
421 # import sites
450 sites = {}
422 sites = {}
451 site_data.each_pair do |id,site|
423 site_data.each_pair do |id,site|
452 s = Site.find_by_name(site[:name])
424 s = Site.find_by_name(site[:name])
453 if s!=nil
425 if s!=nil
454 @import_log << "Found #{site[:name]}\n"
426 @import_log << "Found #{site[:name]}\n"
455 else
427 else
456 s = Site.new(:name => site[:name])
428 s = Site.new(:name => site[:name])
457 @import_log << "Created #{site[:name]}\n"
429 @import_log << "Created #{site[:name]}\n"
458 end
430 end
459 s.password = site[:password]
431 s.password = site[:password]
460 s.country = countries[site[:country_id]]
432 s.country = countries[site[:country_id]]
461 s.save
433 s.save
462 sites[id] = s
434 sites[id] = s
463 end
435 end
464
436
465 # import users
437 # import users
466 user_data.each_pair do |id,user|
438 user_data.each_pair do |id,user|
467 u = User.find_by_login(user[:login])
439 u = User.find_by_login(user[:login])
468 if u!=nil
440 if u!=nil
469 @import_log << "Found #{user[:login]}\n"
441 @import_log << "Found #{user[:login]}\n"
470 else
442 else
471 u = User.new(:login => user[:login])
443 u = User.new(:login => user[:login])
472 @import_log << "Created #{user[:login]}\n"
444 @import_log << "Created #{user[:login]}\n"
473 end
445 end
474 u.full_name = user[:name]
446 u.full_name = user[:name]
475 u.password = user[:password]
447 u.password = user[:password]
476 u.country = countries[user[:country_id]]
448 u.country = countries[user[:country_id]]
477 u.site = sites[user[:site_id]]
449 u.site = sites[user[:site_id]]
478 u.activated = true
450 u.activated = true
479 u.email = "empty-#{u.login}@none.com"
451 u.email = "empty-#{u.login}@none.com"
480 if not u.save
452 if not u.save
481 @import_log << "Errors\n"
453 @import_log << "Errors\n"
482 u.errors.each { |attr,msg| @import_log << "#{attr} - #{msg}\n" }
454 u.errors.each { |attr,msg| @import_log << "#{attr} - #{msg}\n" }
483 end
455 end
484 end
456 end
485
457
486 end
458 end
487
459
488 def logout_users(users)
460 def logout_users(users)
489 users.each do |user|
461 users.each do |user|
490 contest_stat = user.contest_stat(true)
462 contest_stat = user.contest_stat(true)
491 if contest_stat and !contest_stat.forced_logout
463 if contest_stat and !contest_stat.forced_logout
492 contest_stat.forced_logout = true
464 contest_stat.forced_logout = true
493 contest_stat.save
465 contest_stat.save
494 end
466 end
495 end
467 end
496 end
468 end
497
469
498 def send_contest_update_notification_email(user, contest)
470 def send_contest_update_notification_email(user, contest)
499 contest_title_name = GraderConfiguration['contest.name']
471 contest_title_name = GraderConfiguration['contest.name']
500 contest_name = contest.name
472 contest_name = contest.name
501 mail_subject = t('contest.notification.email_subject', {
473 mail_subject = t('contest.notification.email_subject', {
502 :contest_title_name => contest_title_name,
474 :contest_title_name => contest_title_name,
503 :contest_name => contest_name })
475 :contest_name => contest_name })
504 mail_body = t('contest.notification.email_body', {
476 mail_body = t('contest.notification.email_body', {
505 :full_name => user.full_name,
477 :full_name => user.full_name,
506 :contest_title_name => contest_title_name,
478 :contest_title_name => contest_title_name,
507 :contest_name => contest.name,
479 :contest_name => contest.name,
508 })
480 })
509
481
510 logger.info mail_body
482 logger.info mail_body
511 send_mail(user.email, mail_subject, mail_body)
483 send_mail(user.email, mail_subject, mail_body)
512 end
484 end
513
485
514 def find_contest_and_user_from_contest_id(id)
486 def find_contest_and_user_from_contest_id(id)
515 if id!='none'
487 if id!='none'
516 @contest = Contest.find(id)
488 @contest = Contest.find(id)
517 else
489 else
518 @contest = nil
490 @contest = nil
519 end
491 end
520 if @contest
492 if @contest
521 @users = @contest.users
493 @users = @contest.users
522 else
494 else
523 @users = User.find_users_with_no_contest
495 @users = User.find_users_with_no_contest
524 end
496 end
525 return [@contest, @users]
497 return [@contest, @users]
526 end
498 end
527
499
528 def gen_csv_from_scorearray(scorearray,problem)
500 def gen_csv_from_scorearray(scorearray,problem)
529 CSV.generate do |csv|
501 CSV.generate do |csv|
530 #add header
502 #add header
531 header = ['User','Name', 'Activated?', 'Logged in', 'Contest']
503 header = ['User','Name', 'Activated?', 'Logged in', 'Contest']
532 problem.each { |p| header << p.name }
504 problem.each { |p| header << p.name }
533 header += ['Total','Passed']
505 header += ['Total','Passed']
534 csv << header
506 csv << header
535 #add data
507 #add data
536 scorearray.each do |sc|
508 scorearray.each do |sc|
537 total = num_passed = 0
509 total = num_passed = 0
538 row = Array.new
510 row = Array.new
539 sc.each_index do |i|
511 sc.each_index do |i|
540 if i == 0
512 if i == 0
541 row << sc[i].login
513 row << sc[i].login
542 row << sc[i].full_name
514 row << sc[i].full_name
543 row << sc[i].activated
515 row << sc[i].activated
544 row << (sc[i].try(:contest_stat).try(:started_at)!=nil ? 'yes' : 'no')
516 row << (sc[i].try(:contest_stat).try(:started_at)!=nil ? 'yes' : 'no')
545 row << sc[i].contests.collect {|c| c.name}.join(', ')
517 row << sc[i].contests.collect {|c| c.name}.join(', ')
546 else
518 else
547 row << sc[i][0]
519 row << sc[i][0]
548 total += sc[i][0]
520 total += sc[i][0]
549 num_passed += 1 if sc[i][1]
521 num_passed += 1 if sc[i][1]
550 end
522 end
551 end
523 end
552 row << total
524 row << total
553 row << num_passed
525 row << num_passed
554 csv << row
526 csv << row
555 end
527 end
556 end
528 end
557 end
529 end
558 end
530 end
You need to be logged in to leave comments. Login now