Description:
confusing merge
Commit status:
[Not Reviewed]
References:
merge default
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r483:7cf62e1e74ba - - 1 file changed: 0 inserted, 1 deleted

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