Description:
Merge pull request #6 from nattee/master update mail functionality. Update syntax for mail gem (we might need mor...
Commit status:
[Not Reviewed]
References:
merge default
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r361:fae81d234a8a - - 8 files changed: 116 inserted, 10 deleted

@@ -0,0 +1,18
1 +
2 +
3 + = form_tag({action: :user_stat_max }) do
4 + .submitbox
5 + %table
6 + %tr
7 + %td{colspan: 6, style: 'font-weight: bold'} Query maximum score in submission range
8 + %tr
9 + %td{style: 'width: 120px; font-weight: bold'} Submission ID:
10 + %td{align: 'right'} From:
11 + %td= text_field_tag 'since_id', params[:since_id], style: 'width:40px'
12 + %td To:
13 + %td= text_field_tag 'until_id', params[:until_id], style: 'width:40px'
14 + %td= submit_tag 'query'
15 + %tr
16 + %td
17 + %td{colspan: 5} Leave blank for uncondition
18 +
@@ -0,0 +1,44
1 + %h1 User grading results
2 + %h2 Show max scores in submission range
3 +
4 + - if @problem and @problem.errors
5 + =error_messages_for 'problem'
6 +
7 + = render partial: 'submission_range'
8 +
9 + - if @log
10 + %h3 Import log
11 + %pre.import-log
12 + = @log
13 +
14 + %p= link_to '[Show only latest submissions]', controller: :user_admin, action: :user_stat
15 +
16 + %table.info
17 + %thead
18 + %tr.info-head
19 + %th User
20 + %th Name
21 + %th Activated?
22 + %th Logged in
23 + %th Contest(s)
24 + - @problems.each do |p|
25 + %th= p.name
26 + %th Total
27 + %th Passed
28 + %tbody
29 + - @scorearray.each do |sc|
30 + %tr{class: cycle('info-even','info-odd')}
31 + - total,num_passed = 0,0
32 + - sc.each_index do |i|
33 + - if i == 0
34 + %td= sc[i].login
35 + %td= sc[i].full_name
36 + %td= sc[i].activated
37 + %td= sc[i].try(:contest_stat).try(:started_at)!=nil ? 'yes' : 'no'
38 + %td= sc[i].contests.collect {|c| c.name}.join(', ')
39 + - else
40 + %td= sc[i][0]
41 + - total += sc[i][0]
42 + - num_passed += 1 if sc[i][1]
43 + %td= total
44 + %td= num_passed
@@ -1,455 +1,476
1 class UserAdminController < ApplicationController
1 class UserAdminController < ApplicationController
2
2
3 include MailHelperMethods
3 include MailHelperMethods
4
4
5 before_filter :admin_authorization
5 before_filter :admin_authorization
6
6
7 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
7 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
8 verify :method => :post, :only => [ :destroy,
8 verify :method => :post, :only => [ :destroy,
9 :create, :create_from_list,
9 :create, :create_from_list,
10 :update,
10 :update,
11 :manage_contest,
11 :manage_contest,
12 :bulk_mail
12 :bulk_mail
13 ],
13 ],
14 :redirect_to => { :action => :list }
14 :redirect_to => { :action => :list }
15
15
16 def index
16 def index
17 list
17 list
18 render :action => 'list'
18 render :action => 'list'
19 end
19 end
20
20
21 def list
21 def list
22 @user_count = User.count
22 @user_count = User.count
23 if params[:page] == 'all'
23 if params[:page] == 'all'
24 @users = User.all
24 @users = User.all
25 @paginated = false
25 @paginated = false
26 else
26 else
27 @users = User.paginate :page => params[:page]
27 @users = User.paginate :page => params[:page]
28 @paginated = true
28 @paginated = true
29 end
29 end
30 @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at']
30 @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at']
31 @contests = Contest.enabled
31 @contests = Contest.enabled
32 end
32 end
33
33
34 def active
34 def active
35 sessions = ActiveRecord::SessionStore::Session.find(:all, :conditions => ["updated_at >= ?", 60.minutes.ago])
35 sessions = ActiveRecord::SessionStore::Session.find(:all, :conditions => ["updated_at >= ?", 60.minutes.ago])
36 @users = []
36 @users = []
37 sessions.each do |session|
37 sessions.each do |session|
38 if session.data[:user_id]
38 if session.data[:user_id]
39 @users << User.find(session.data[:user_id])
39 @users << User.find(session.data[:user_id])
40 end
40 end
41 end
41 end
42 end
42 end
43
43
44 def show
44 def show
45 @user = User.find(params[:id])
45 @user = User.find(params[:id])
46 end
46 end
47
47
48 def new
48 def new
49 @user = User.new
49 @user = User.new
50 end
50 end
51
51
52 def create
52 def create
53 @user = User.new(params[:user])
53 @user = User.new(params[:user])
54 @user.activated = true
54 @user.activated = true
55 if @user.save
55 if @user.save
56 flash[:notice] = 'User was successfully created.'
56 flash[:notice] = 'User was successfully created.'
57 redirect_to :action => 'list'
57 redirect_to :action => 'list'
58 else
58 else
59 render :action => 'new'
59 render :action => 'new'
60 end
60 end
61 end
61 end
62
62
63 def create_from_list
63 def create_from_list
64 lines = params[:user_list]
64 lines = params[:user_list]
65
65
66 note = []
66 note = []
67
67
68 lines.split("\n").each do |line|
68 lines.split("\n").each do |line|
69 items = line.chomp.split(',')
69 items = line.chomp.split(',')
70 if items.length>=2
70 if items.length>=2
71 login = items[0]
71 login = items[0]
72 full_name = items[1]
72 full_name = items[1]
73
73
74 added_random_password = false
74 added_random_password = false
75 if items.length>=3
75 if items.length>=3
76 password = items[2].chomp(" ")
76 password = items[2].chomp(" ")
77 user_alias = (items.length>=4) ? items[3] : login
77 user_alias = (items.length>=4) ? items[3] : login
78 else
78 else
79 password = random_password
79 password = random_password
80 user_alias = (items.length>=4) ? items[3] : login
80 user_alias = (items.length>=4) ? items[3] : login
81 added_random_password = true
81 added_random_password = true
82 end
82 end
83
83
84 user = User.new({:login => login,
84 user = User.new({:login => login,
85 :full_name => full_name,
85 :full_name => full_name,
86 :password => password,
86 :password => password,
87 :password_confirmation => password,
87 :password_confirmation => password,
88 :alias => user_alias})
88 :alias => user_alias})
89 user.activated = true
89 user.activated = true
90 user.save
90 user.save
91
91
92 if added_random_password
92 if added_random_password
93 note << "'#{login}' (+)"
93 note << "'#{login}' (+)"
94 else
94 else
95 note << login
95 note << login
96 end
96 end
97 end
97 end
98 end
98 end
99 flash[:notice] = 'User(s) ' + note.join(', ') +
99 flash[:notice] = 'User(s) ' + note.join(', ') +
100 ' were successfully created. ' +
100 ' were successfully created. ' +
101 '( (+) - created with random passwords.)'
101 '( (+) - created with random passwords.)'
102 redirect_to :action => 'list'
102 redirect_to :action => 'list'
103 end
103 end
104
104
105 def edit
105 def edit
106 @user = User.find(params[:id])
106 @user = User.find(params[:id])
107 end
107 end
108
108
109 def update
109 def update
110 @user = User.find(params[:id])
110 @user = User.find(params[:id])
111 if @user.update_attributes(params[:user])
111 if @user.update_attributes(params[:user])
112 flash[:notice] = 'User was successfully updated.'
112 flash[:notice] = 'User was successfully updated.'
113 redirect_to :action => 'show', :id => @user
113 redirect_to :action => 'show', :id => @user
114 else
114 else
115 render :action => 'edit'
115 render :action => 'edit'
116 end
116 end
117 end
117 end
118
118
119 def destroy
119 def destroy
120 User.find(params[:id]).destroy
120 User.find(params[:id]).destroy
121 redirect_to :action => 'list'
121 redirect_to :action => 'list'
122 end
122 end
123
123
124 def user_stat
124 def user_stat
125 @problems = Problem.find_available_problems
125 @problems = Problem.find_available_problems
126 @users = User.find(:all, :include => [:contests, :contest_stat])
126 @users = User.find(:all, :include => [:contests, :contest_stat])
127 @scorearray = Array.new
127 @scorearray = Array.new
128 @users.each do |u|
128 @users.each do |u|
129 ustat = Array.new
129 ustat = Array.new
130 ustat[0] = u
130 ustat[0] = u
131 @problems.each do |p|
131 @problems.each do |p|
132 - sub = Submission.find_last_by_user_and_problem(u.id,p.id)
132 + sub = Submission.find_last_by_user_and_problem(u.id,p.id)
133 - if (sub!=nil) and (sub.points!=nil)
133 + if (sub!=nil) and (sub.points!=nil)
134 - ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)]
134 + ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)]
135 - else
135 + else
136 - ustat << [0,false]
136 + ustat << [0,false]
137 - end
137 + end
138 + end
139 + @scorearray << ustat
140 + end
141 + end
142 +
143 + def user_stat_max
144 + @problems = Problem.find_available_problems
145 + @users = User.find(:all, :include => [:contests, :contest_stat])
146 + @scorearray = Array.new
147 + #set up range from param
148 + since_id = params.fetch(:since_id, 0).to_i
149 + until_id = params.fetch(:until_id, 0).to_i
150 + @users.each do |u|
151 + ustat = Array.new
152 + ustat[0] = u
153 + @problems.each do |p|
154 + max_points = 0
155 + Submission.find_in_range_by_user_and_problem(u.id,p.id,since_id,until_id).each do |sub|
156 + max_points = sub.points if sub and sub.points and (sub.points > max_points)
157 + end
158 + ustat << [(max_points.to_f*100/p.full_score).round, (max_points>=p.full_score)]
138 end
159 end
139 @scorearray << ustat
160 @scorearray << ustat
140 end
161 end
141 end
162 end
142
163
143 def import
164 def import
144 if params[:file]==''
165 if params[:file]==''
145 flash[:notice] = 'Error importing no file'
166 flash[:notice] = 'Error importing no file'
146 redirect_to :action => 'list' and return
167 redirect_to :action => 'list' and return
147 end
168 end
148 import_from_file(params[:file])
169 import_from_file(params[:file])
149 end
170 end
150
171
151 def random_all_passwords
172 def random_all_passwords
152 users = User.find(:all)
173 users = User.find(:all)
153 @prefix = params[:prefix] || ''
174 @prefix = params[:prefix] || ''
154 @non_admin_users = User.find_non_admin_with_prefix(@prefix)
175 @non_admin_users = User.find_non_admin_with_prefix(@prefix)
155 @changed = false
176 @changed = false
156 if request.request_method == 'POST'
177 if request.request_method == 'POST'
157 @non_admin_users.each do |user|
178 @non_admin_users.each do |user|
158 password = random_password
179 password = random_password
159 user.password = password
180 user.password = password
160 user.password_confirmation = password
181 user.password_confirmation = password
161 user.save
182 user.save
162 end
183 end
163 @changed = true
184 @changed = true
164 end
185 end
165 end
186 end
166
187
167 # contest management
188 # contest management
168
189
169 def contests
190 def contests
170 @contest, @users = find_contest_and_user_from_contest_id(params[:id])
191 @contest, @users = find_contest_and_user_from_contest_id(params[:id])
171 @contests = Contest.enabled
192 @contests = Contest.enabled
172 end
193 end
173
194
174 def assign_from_list
195 def assign_from_list
175 contest_id = params[:users_contest_id]
196 contest_id = params[:users_contest_id]
176 org_contest, users = find_contest_and_user_from_contest_id(contest_id)
197 org_contest, users = find_contest_and_user_from_contest_id(contest_id)
177 contest = Contest.find(params[:new_contest][:id])
198 contest = Contest.find(params[:new_contest][:id])
178 if !contest
199 if !contest
179 flash[:notice] = 'Error: no contest'
200 flash[:notice] = 'Error: no contest'
180 redirect_to :action => 'contests', :id =>contest_id
201 redirect_to :action => 'contests', :id =>contest_id
181 end
202 end
182
203
183 note = []
204 note = []
184 users.each do |u|
205 users.each do |u|
185 u.contests = [contest]
206 u.contests = [contest]
186 note << u.login
207 note << u.login
187 end
208 end
188 flash[:notice] = 'User(s) ' + note.join(', ') +
209 flash[:notice] = 'User(s) ' + note.join(', ') +
189 " were successfully reassigned to #{contest.title}."
210 " were successfully reassigned to #{contest.title}."
190 redirect_to :action => 'contests', :id =>contest.id
211 redirect_to :action => 'contests', :id =>contest.id
191 end
212 end
192
213
193 def add_to_contest
214 def add_to_contest
194 user = User.find(params[:id])
215 user = User.find(params[:id])
195 contest = Contest.find(params[:contest_id])
216 contest = Contest.find(params[:contest_id])
196 if user and contest
217 if user and contest
197 user.contests << contest
218 user.contests << contest
198 end
219 end
199 redirect_to :action => 'list'
220 redirect_to :action => 'list'
200 end
221 end
201
222
202 def remove_from_contest
223 def remove_from_contest
203 user = User.find(params[:id])
224 user = User.find(params[:id])
204 contest = Contest.find(params[:contest_id])
225 contest = Contest.find(params[:contest_id])
205 if user and contest
226 if user and contest
206 user.contests.delete(contest)
227 user.contests.delete(contest)
207 end
228 end
208 redirect_to :action => 'list'
229 redirect_to :action => 'list'
209 end
230 end
210
231
211 def contest_management
232 def contest_management
212 end
233 end
213
234
214 def manage_contest
235 def manage_contest
215 contest = Contest.find(params[:contest][:id])
236 contest = Contest.find(params[:contest][:id])
216 if !contest
237 if !contest
217 flash[:notice] = 'You did not choose the contest.'
238 flash[:notice] = 'You did not choose the contest.'
218 redirect_to :action => 'contest_management' and return
239 redirect_to :action => 'contest_management' and return
219 end
240 end
220
241
221 operation = params[:operation]
242 operation = params[:operation]
222
243
223 if not ['add','remove','assign'].include? operation
244 if not ['add','remove','assign'].include? operation
224 flash[:notice] = 'You did not choose the operation to perform.'
245 flash[:notice] = 'You did not choose the operation to perform.'
225 redirect_to :action => 'contest_management' and return
246 redirect_to :action => 'contest_management' and return
226 end
247 end
227
248
228 lines = params[:login_list]
249 lines = params[:login_list]
229 if !lines or lines.blank?
250 if !lines or lines.blank?
230 flash[:notice] = 'You entered an empty list.'
251 flash[:notice] = 'You entered an empty list.'
231 redirect_to :action => 'contest_management' and return
252 redirect_to :action => 'contest_management' and return
232 end
253 end
233
254
234 note = []
255 note = []
235 users = []
256 users = []
236 lines.split("\n").each do |line|
257 lines.split("\n").each do |line|
237 user = User.find_by_login(line.chomp)
258 user = User.find_by_login(line.chomp)
238 if user
259 if user
239 if operation=='add'
260 if operation=='add'
240 if ! user.contests.include? contest
261 if ! user.contests.include? contest
241 user.contests << contest
262 user.contests << contest
242 end
263 end
243 elsif operation=='remove'
264 elsif operation=='remove'
244 user.contests.delete(contest)
265 user.contests.delete(contest)
245 else
266 else
246 user.contests = [contest]
267 user.contests = [contest]
247 end
268 end
248
269
249 if params[:reset_timer]
270 if params[:reset_timer]
250 user.contest_stat.forced_logout = true
271 user.contest_stat.forced_logout = true
251 user.contest_stat.reset_timer_and_save
272 user.contest_stat.reset_timer_and_save
252 end
273 end
253
274
254 if params[:notification_emails]
275 if params[:notification_emails]
255 send_contest_update_notification_email(user, contest)
276 send_contest_update_notification_email(user, contest)
256 end
277 end
257
278
258 note << user.login
279 note << user.login
259 users << user
280 users << user
260 end
281 end
261 end
282 end
262
283
263 if params[:reset_timer]
284 if params[:reset_timer]
264 logout_users(users)
285 logout_users(users)
265 end
286 end
266
287
267 flash[:notice] = 'User(s) ' + note.join(', ') +
288 flash[:notice] = 'User(s) ' + note.join(', ') +
268 ' were successfully modified. '
289 ' were successfully modified. '
269 redirect_to :action => 'contest_management'
290 redirect_to :action => 'contest_management'
270 end
291 end
271
292
272 # admin management
293 # admin management
273
294
274 def admin
295 def admin
275 @admins = User.find(:all).find_all {|user| user.admin? }
296 @admins = User.find(:all).find_all {|user| user.admin? }
276 end
297 end
277
298
278 def grant_admin
299 def grant_admin
279 login = params[:login]
300 login = params[:login]
280 user = User.find_by_login(login)
301 user = User.find_by_login(login)
281 if user!=nil
302 if user!=nil
282 admin_role = Role.find_by_name('admin')
303 admin_role = Role.find_by_name('admin')
283 user.roles << admin_role
304 user.roles << admin_role
284 else
305 else
285 flash[:notice] = 'Unknown user'
306 flash[:notice] = 'Unknown user'
286 end
307 end
287 flash[:notice] = 'User added as admins'
308 flash[:notice] = 'User added as admins'
288 redirect_to :action => 'admin'
309 redirect_to :action => 'admin'
289 end
310 end
290
311
291 def revoke_admin
312 def revoke_admin
292 user = User.find(params[:id])
313 user = User.find(params[:id])
293 if user==nil
314 if user==nil
294 flash[:notice] = 'Unknown user'
315 flash[:notice] = 'Unknown user'
295 redirect_to :action => 'admin' and return
316 redirect_to :action => 'admin' and return
296 elsif user.login == 'root'
317 elsif user.login == 'root'
297 flash[:notice] = 'You cannot revoke admisnistrator permission from root.'
318 flash[:notice] = 'You cannot revoke admisnistrator permission from root.'
298 redirect_to :action => 'admin' and return
319 redirect_to :action => 'admin' and return
299 end
320 end
300
321
301 admin_role = Role.find_by_name('admin')
322 admin_role = Role.find_by_name('admin')
302 user.roles.delete(admin_role)
323 user.roles.delete(admin_role)
303 flash[:notice] = 'User permission revoked'
324 flash[:notice] = 'User permission revoked'
304 redirect_to :action => 'admin'
325 redirect_to :action => 'admin'
305 end
326 end
306
327
307 # mass mailing
328 # mass mailing
308
329
309 def mass_mailing
330 def mass_mailing
310 end
331 end
311
332
312 def bulk_mail
333 def bulk_mail
313 lines = params[:login_list]
334 lines = params[:login_list]
314 if !lines or lines.blank?
335 if !lines or lines.blank?
315 flash[:notice] = 'You entered an empty list.'
336 flash[:notice] = 'You entered an empty list.'
316 redirect_to :action => 'mass_mailing' and return
337 redirect_to :action => 'mass_mailing' and return
317 end
338 end
318
339
319 mail_subject = params[:subject]
340 mail_subject = params[:subject]
320 if !mail_subject or mail_subject.blank?
341 if !mail_subject or mail_subject.blank?
321 flash[:notice] = 'You entered an empty mail subject.'
342 flash[:notice] = 'You entered an empty mail subject.'
322 redirect_to :action => 'mass_mailing' and return
343 redirect_to :action => 'mass_mailing' and return
323 end
344 end
324
345
325 mail_body = params[:email_body]
346 mail_body = params[:email_body]
326 if !mail_body or mail_body.blank?
347 if !mail_body or mail_body.blank?
327 flash[:notice] = 'You entered an empty mail body.'
348 flash[:notice] = 'You entered an empty mail body.'
328 redirect_to :action => 'mass_mailing' and return
349 redirect_to :action => 'mass_mailing' and return
329 end
350 end
330
351
331 note = []
352 note = []
332 users = []
353 users = []
333 lines.split("\n").each do |line|
354 lines.split("\n").each do |line|
334 user = User.find_by_login(line.chomp)
355 user = User.find_by_login(line.chomp)
335 if user
356 if user
336 send_mail(user.email, mail_subject, mail_body)
357 send_mail(user.email, mail_subject, mail_body)
337 note << user.login
358 note << user.login
338 end
359 end
339 end
360 end
340
361
341 flash[:notice] = 'User(s) ' + note.join(', ') +
362 flash[:notice] = 'User(s) ' + note.join(', ') +
342 ' were successfully modified. '
363 ' were successfully modified. '
343 redirect_to :action => 'mass_mailing'
364 redirect_to :action => 'mass_mailing'
344 end
365 end
345
366
346 protected
367 protected
347
368
348 def random_password(length=5)
369 def random_password(length=5)
349 chars = 'abcdefghijkmnopqrstuvwxyz23456789'
370 chars = 'abcdefghijkmnopqrstuvwxyz23456789'
350 newpass = ""
371 newpass = ""
351 length.times { newpass << chars[rand(chars.size-1)] }
372 length.times { newpass << chars[rand(chars.size-1)] }
352 return newpass
373 return newpass
353 end
374 end
354
375
355 def import_from_file(f)
376 def import_from_file(f)
356 data_hash = YAML.load(f)
377 data_hash = YAML.load(f)
357 @import_log = ""
378 @import_log = ""
358
379
359 country_data = data_hash[:countries]
380 country_data = data_hash[:countries]
360 site_data = data_hash[:sites]
381 site_data = data_hash[:sites]
361 user_data = data_hash[:users]
382 user_data = data_hash[:users]
362
383
363 # import country
384 # import country
364 countries = {}
385 countries = {}
365 country_data.each_pair do |id,country|
386 country_data.each_pair do |id,country|
366 c = Country.find_by_name(country[:name])
387 c = Country.find_by_name(country[:name])
367 if c!=nil
388 if c!=nil
368 countries[id] = c
389 countries[id] = c
369 @import_log << "Found #{country[:name]}\n"
390 @import_log << "Found #{country[:name]}\n"
370 else
391 else
371 countries[id] = Country.new(:name => country[:name])
392 countries[id] = Country.new(:name => country[:name])
372 countries[id].save
393 countries[id].save
373 @import_log << "Created #{country[:name]}\n"
394 @import_log << "Created #{country[:name]}\n"
374 end
395 end
375 end
396 end
376
397
377 # import sites
398 # import sites
378 sites = {}
399 sites = {}
379 site_data.each_pair do |id,site|
400 site_data.each_pair do |id,site|
380 s = Site.find_by_name(site[:name])
401 s = Site.find_by_name(site[:name])
381 if s!=nil
402 if s!=nil
382 @import_log << "Found #{site[:name]}\n"
403 @import_log << "Found #{site[:name]}\n"
383 else
404 else
384 s = Site.new(:name => site[:name])
405 s = Site.new(:name => site[:name])
385 @import_log << "Created #{site[:name]}\n"
406 @import_log << "Created #{site[:name]}\n"
386 end
407 end
387 s.password = site[:password]
408 s.password = site[:password]
388 s.country = countries[site[:country_id]]
409 s.country = countries[site[:country_id]]
389 s.save
410 s.save
390 sites[id] = s
411 sites[id] = s
391 end
412 end
392
413
393 # import users
414 # import users
394 user_data.each_pair do |id,user|
415 user_data.each_pair do |id,user|
395 u = User.find_by_login(user[:login])
416 u = User.find_by_login(user[:login])
396 if u!=nil
417 if u!=nil
397 @import_log << "Found #{user[:login]}\n"
418 @import_log << "Found #{user[:login]}\n"
398 else
419 else
399 u = User.new(:login => user[:login])
420 u = User.new(:login => user[:login])
400 @import_log << "Created #{user[:login]}\n"
421 @import_log << "Created #{user[:login]}\n"
401 end
422 end
402 u.full_name = user[:name]
423 u.full_name = user[:name]
403 u.password = user[:password]
424 u.password = user[:password]
404 u.country = countries[user[:country_id]]
425 u.country = countries[user[:country_id]]
405 u.site = sites[user[:site_id]]
426 u.site = sites[user[:site_id]]
406 u.activated = true
427 u.activated = true
407 u.email = "empty-#{u.login}@none.com"
428 u.email = "empty-#{u.login}@none.com"
408 if not u.save
429 if not u.save
409 @import_log << "Errors\n"
430 @import_log << "Errors\n"
410 u.errors.each { |attr,msg| @import_log << "#{attr} - #{msg}\n" }
431 u.errors.each { |attr,msg| @import_log << "#{attr} - #{msg}\n" }
411 end
432 end
412 end
433 end
413
434
414 end
435 end
415
436
416 def logout_users(users)
437 def logout_users(users)
417 users.each do |user|
438 users.each do |user|
418 contest_stat = user.contest_stat(true)
439 contest_stat = user.contest_stat(true)
419 if contest_stat and !contest_stat.forced_logout
440 if contest_stat and !contest_stat.forced_logout
420 contest_stat.forced_logout = true
441 contest_stat.forced_logout = true
421 contest_stat.save
442 contest_stat.save
422 end
443 end
423 end
444 end
424 end
445 end
425
446
426 def send_contest_update_notification_email(user, contest)
447 def send_contest_update_notification_email(user, contest)
427 contest_title_name = GraderConfiguration['contest.name']
448 contest_title_name = GraderConfiguration['contest.name']
428 contest_name = contest.name
449 contest_name = contest.name
429 mail_subject = t('contest.notification.email_subject', {
450 mail_subject = t('contest.notification.email_subject', {
430 :contest_title_name => contest_title_name,
451 :contest_title_name => contest_title_name,
431 :contest_name => contest_name })
452 :contest_name => contest_name })
432 mail_body = t('contest.notification.email_body', {
453 mail_body = t('contest.notification.email_body', {
433 :full_name => user.full_name,
454 :full_name => user.full_name,
434 :contest_title_name => contest_title_name,
455 :contest_title_name => contest_title_name,
435 :contest_name => contest.name,
456 :contest_name => contest.name,
436 })
457 })
437
458
438 logger.info mail_body
459 logger.info mail_body
439 send_mail(user.email, mail_subject, mail_body)
460 send_mail(user.email, mail_subject, mail_body)
440 end
461 end
441
462
442 def find_contest_and_user_from_contest_id(id)
463 def find_contest_and_user_from_contest_id(id)
443 if id!='none'
464 if id!='none'
444 @contest = Contest.find(id)
465 @contest = Contest.find(id)
445 else
466 else
446 @contest = nil
467 @contest = nil
447 end
468 end
448 if @contest
469 if @contest
449 @users = @contest.users
470 @users = @contest.users
450 else
471 else
451 @users = User.find_users_with_no_contest
472 @users = User.find_users_with_no_contest
452 end
473 end
453 return [@contest, @users]
474 return [@contest, @users]
454 end
475 end
455 end
476 end
@@ -1,156 +1,156
1 require 'net/smtp'
1 require 'net/smtp'
2
2
3 class UsersController < ApplicationController
3 class UsersController < ApplicationController
4
4
5 include MailHelperMethods
5 include MailHelperMethods
6
6
7 before_filter :authenticate, :except => [:new,
7 before_filter :authenticate, :except => [:new,
8 :register,
8 :register,
9 :confirm,
9 :confirm,
10 :forget,
10 :forget,
11 :retrieve_password]
11 :retrieve_password]
12
12
13 before_filter :verify_online_registration, :only => [:new,
13 before_filter :verify_online_registration, :only => [:new,
14 :register,
14 :register,
15 :forget,
15 :forget,
16 :retrieve_password]
16 :retrieve_password]
17
17
18 verify :method => :post, :only => [:chg_passwd],
18 verify :method => :post, :only => [:chg_passwd],
19 :redirect_to => { :action => :index }
19 :redirect_to => { :action => :index }
20
20
21 #in_place_edit_for :user, :alias_for_editing
21 #in_place_edit_for :user, :alias_for_editing
22 #in_place_edit_for :user, :email_for_editing
22 #in_place_edit_for :user, :email_for_editing
23
23
24 def index
24 def index
25 if !GraderConfiguration['system.user_setting_enabled']
25 if !GraderConfiguration['system.user_setting_enabled']
26 redirect_to :controller => 'main', :action => 'list'
26 redirect_to :controller => 'main', :action => 'list'
27 else
27 else
28 @user = User.find(session[:user_id])
28 @user = User.find(session[:user_id])
29 end
29 end
30 end
30 end
31
31
32 def chg_passwd
32 def chg_passwd
33 user = User.find(session[:user_id])
33 user = User.find(session[:user_id])
34 user.password = params[:passwd]
34 user.password = params[:passwd]
35 user.password_confirmation = params[:passwd_verify]
35 user.password_confirmation = params[:passwd_verify]
36 if user.save
36 if user.save
37 flash[:notice] = 'password changed'
37 flash[:notice] = 'password changed'
38 else
38 else
39 flash[:notice] = 'Error: password changing failed'
39 flash[:notice] = 'Error: password changing failed'
40 end
40 end
41 redirect_to :action => 'index'
41 redirect_to :action => 'index'
42 end
42 end
43
43
44 def new
44 def new
45 @user = User.new
45 @user = User.new
46 render :action => 'new', :layout => 'empty'
46 render :action => 'new', :layout => 'empty'
47 end
47 end
48
48
49 def register
49 def register
50 if(params[:cancel])
50 if(params[:cancel])
51 redirect_to :controller => 'main', :action => 'login'
51 redirect_to :controller => 'main', :action => 'login'
52 return
52 return
53 end
53 end
54 @user = User.new(params[:user])
54 @user = User.new(params[:user])
55 @user.password_confirmation = @user.password = User.random_password
55 @user.password_confirmation = @user.password = User.random_password
56 @user.activated = false
56 @user.activated = false
57 if (@user.valid?) and (@user.save)
57 if (@user.valid?) and (@user.save)
58 if send_confirmation_email(@user)
58 if send_confirmation_email(@user)
59 render :action => 'new_splash', :layout => 'empty'
59 render :action => 'new_splash', :layout => 'empty'
60 else
60 else
61 @admin_email = GraderConfiguration['system.admin_email']
61 @admin_email = GraderConfiguration['system.admin_email']
62 render :action => 'email_error', :layout => 'empty'
62 render :action => 'email_error', :layout => 'empty'
63 end
63 end
64 else
64 else
65 @user.errors.add(:base,"Email cannot be blank") if @user.email==''
65 @user.errors.add(:base,"Email cannot be blank") if @user.email==''
66 render :action => 'new', :layout => 'empty'
66 render :action => 'new', :layout => 'empty'
67 end
67 end
68 end
68 end
69
69
70 def confirm
70 def confirm
71 login = params[:login]
71 login = params[:login]
72 key = params[:activation]
72 key = params[:activation]
73 @user = User.find_by_login(login)
73 @user = User.find_by_login(login)
74 if (@user) and (@user.verify_activation_key(key))
74 if (@user) and (@user.verify_activation_key(key))
75 if @user.valid? # check uniquenss of email
75 if @user.valid? # check uniquenss of email
76 @user.activated = true
76 @user.activated = true
77 @user.save
77 @user.save
78 @result = :successful
78 @result = :successful
79 else
79 else
80 @result = :email_used
80 @result = :email_used
81 end
81 end
82 else
82 else
83 @result = :failed
83 @result = :failed
84 end
84 end
85 render :action => 'confirm', :layout => 'empty'
85 render :action => 'confirm', :layout => 'empty'
86 end
86 end
87
87
88 def forget
88 def forget
89 render :action => 'forget', :layout => 'empty'
89 render :action => 'forget', :layout => 'empty'
90 end
90 end
91
91
92 def retrieve_password
92 def retrieve_password
93 email = params[:email]
93 email = params[:email]
94 user = User.find_by_email(email)
94 user = User.find_by_email(email)
95 if user
95 if user
96 last_updated_time = user.updated_at || user.created_at || (Time.now.gmtime - 1.hour)
96 last_updated_time = user.updated_at || user.created_at || (Time.now.gmtime - 1.hour)
97 if last_updated_time > Time.now.gmtime - 5.minutes
97 if last_updated_time > Time.now.gmtime - 5.minutes
98 flash[:notice] = 'The account has recently created or new password has recently been requested. Please wait for 5 minutes'
98 flash[:notice] = 'The account has recently created or new password has recently been requested. Please wait for 5 minutes'
99 else
99 else
100 user.password = user.password_confirmation = User.random_password
100 user.password = user.password_confirmation = User.random_password
101 user.save
101 user.save
102 send_new_password_email(user)
102 send_new_password_email(user)
103 flash[:notice] = 'New password has been mailed to you.'
103 flash[:notice] = 'New password has been mailed to you.'
104 end
104 end
105 else
105 else
106 flash[:notice] = I18n.t 'registration.password_retrieval.no_email'
106 flash[:notice] = I18n.t 'registration.password_retrieval.no_email'
107 end
107 end
108 redirect_to :action => 'forget'
108 redirect_to :action => 'forget'
109 end
109 end
110
110
111 protected
111 protected
112
112
113 def verify_online_registration
113 def verify_online_registration
114 if !GraderConfiguration['system.online_registration']
114 if !GraderConfiguration['system.online_registration']
115 redirect_to :controller => 'main', :action => 'login'
115 redirect_to :controller => 'main', :action => 'login'
116 end
116 end
117 end
117 end
118
118
119 def send_confirmation_email(user)
119 def send_confirmation_email(user)
120 contest_name = GraderConfiguration['contest.name']
120 contest_name = GraderConfiguration['contest.name']
121 activation_url = url_for(:action => 'confirm',
121 activation_url = url_for(:action => 'confirm',
122 :login => user.login,
122 :login => user.login,
123 :activation => user.activation_key)
123 :activation => user.activation_key)
124 home_url = url_for(:controller => 'main', :action => 'index')
124 home_url = url_for(:controller => 'main', :action => 'index')
125 mail_subject = "[#{contest_name}] Confirmation"
125 mail_subject = "[#{contest_name}] Confirmation"
126 mail_body = t('registration.email_body', {
126 mail_body = t('registration.email_body', {
127 :full_name => user.full_name,
127 :full_name => user.full_name,
128 :contest_name => contest_name,
128 :contest_name => contest_name,
129 :login => user.login,
129 :login => user.login,
130 :password => user.password,
130 :password => user.password,
131 :activation_url => activation_url,
131 :activation_url => activation_url,
132 - :admin_email => admin_email
132 + :admin_email => GraderConfiguration['system.admin_email']
133 })
133 })
134
134
135 logger.info mail_body
135 logger.info mail_body
136
136
137 send_mail(user.email, mail_subject, mail_body)
137 send_mail(user.email, mail_subject, mail_body)
138 end
138 end
139
139
140 def send_new_password_email(user)
140 def send_new_password_email(user)
141 contest_name = GraderConfiguration['contest.name']
141 contest_name = GraderConfiguration['contest.name']
142 mail_subject = "[#{contest_name}] Password recovery"
142 mail_subject = "[#{contest_name}] Password recovery"
143 mail_body = t('registration.password_retrieval.email_body', {
143 mail_body = t('registration.password_retrieval.email_body', {
144 :full_name => user.full_name,
144 :full_name => user.full_name,
145 :contest_name => contest_name,
145 :contest_name => contest_name,
146 :login => user.login,
146 :login => user.login,
147 :password => user.password,
147 :password => user.password,
148 - :admin_email => admin_email
148 + :admin_email => GraderConfiguration['system.admin_email']
149 })
149 })
150
150
151 logger.info mail_body
151 logger.info mail_body
152
152
153 send_mail(user.email, mail_subject, mail_body)
153 send_mail(user.email, mail_subject, mail_body)
154 end
154 end
155
155
156 end
156 end
@@ -1,133 +1,133
1 # Methods added to this helper will be available to all templates in the application.
1 # Methods added to this helper will be available to all templates in the application.
2 module ApplicationHelper
2 module ApplicationHelper
3
3
4 def user_header
4 def user_header
5 menu_items = ''
5 menu_items = ''
6 user = User.find(session[:user_id])
6 user = User.find(session[:user_id])
7
7
8 if (user!=nil) and (session[:admin])
8 if (user!=nil) and (session[:admin])
9 # admin menu
9 # admin menu
10 menu_items << "<b>Administrative task:</b> "
10 menu_items << "<b>Administrative task:</b> "
11 append_to menu_items, '[Announcements]', 'announcements', 'index'
11 append_to menu_items, '[Announcements]', 'announcements', 'index'
12 append_to menu_items, '[Msg console]', 'messages', 'console'
12 append_to menu_items, '[Msg console]', 'messages', 'console'
13 append_to menu_items, '[Problems]', 'problems', 'index'
13 append_to menu_items, '[Problems]', 'problems', 'index'
14 append_to menu_items, '[Users]', 'user_admin', 'index'
14 append_to menu_items, '[Users]', 'user_admin', 'index'
15 append_to menu_items, '[Results]', 'user_admin', 'user_stat'
15 append_to menu_items, '[Results]', 'user_admin', 'user_stat'
16 append_to menu_items, '[Graders]', 'graders', 'list'
16 append_to menu_items, '[Graders]', 'graders', 'list'
17 append_to menu_items, '[Contests]', 'contest_management', 'index'
17 append_to menu_items, '[Contests]', 'contest_management', 'index'
18 append_to menu_items, '[Sites]', 'sites', 'index'
18 append_to menu_items, '[Sites]', 'sites', 'index'
19 append_to menu_items, '[System config]', 'configurations', 'index'
19 append_to menu_items, '[System config]', 'configurations', 'index'
20 menu_items << "<br/>"
20 menu_items << "<br/>"
21 end
21 end
22
22
23 # main page
23 # main page
24 append_to menu_items, "[#{I18n.t 'menu.main'}]", 'main', 'list'
24 append_to menu_items, "[#{I18n.t 'menu.main'}]", 'main', 'list'
25 append_to menu_items, "[#{I18n.t 'menu.messages'}]", 'messages', 'list'
25 append_to menu_items, "[#{I18n.t 'menu.messages'}]", 'messages', 'list'
26
26
27 if (user!=nil) and (GraderConfiguration.show_tasks_to?(user))
27 if (user!=nil) and (GraderConfiguration.show_tasks_to?(user))
28 append_to menu_items, "[#{I18n.t 'menu.tasks'}]", 'tasks', 'list'
28 append_to menu_items, "[#{I18n.t 'menu.tasks'}]", 'tasks', 'list'
29 append_to menu_items, "[#{I18n.t 'menu.submissions'}]", 'main', 'submission'
29 append_to menu_items, "[#{I18n.t 'menu.submissions'}]", 'main', 'submission'
30 append_to menu_items, "[#{I18n.t 'menu.test'}]", 'test', 'index'
30 append_to menu_items, "[#{I18n.t 'menu.test'}]", 'test', 'index'
31 end
31 end
32 append_to menu_items, "[#{I18n.t 'menu.help'}]", 'main', 'help'
32 append_to menu_items, "[#{I18n.t 'menu.help'}]", 'main', 'help'
33
33
34 if GraderConfiguration['system.user_setting_enabled']
34 if GraderConfiguration['system.user_setting_enabled']
35 append_to menu_items, "[#{I18n.t 'menu.settings'}]", 'users', 'index'
35 append_to menu_items, "[#{I18n.t 'menu.settings'}]", 'users', 'index'
36 end
36 end
37 append_to menu_items, "[#{I18n.t 'menu.log_out'}]", 'main', 'login'
37 append_to menu_items, "[#{I18n.t 'menu.log_out'}]", 'main', 'login'
38
38
39 menu_items.html_safe
39 menu_items.html_safe
40 end
40 end
41
41
42 def append_to(option,label, controller, action)
42 def append_to(option,label, controller, action)
43 option << ' ' if option!=''
43 option << ' ' if option!=''
44 option << link_to_unless_current(label,
44 option << link_to_unless_current(label,
45 :controller => controller,
45 :controller => controller,
46 :action => action)
46 :action => action)
47 end
47 end
48
48
49 def format_short_time(time)
49 def format_short_time(time)
50 now = Time.now.gmtime
50 now = Time.now.gmtime
51 st = ''
51 st = ''
52 if (time.yday != now.yday) or
52 if (time.yday != now.yday) or
53 (time.year != now.year)
53 (time.year != now.year)
54 st = time.strftime("%x ")
54 st = time.strftime("%x ")
55 end
55 end
56 st + time.strftime("%X")
56 st + time.strftime("%X")
57 end
57 end
58
58
59 def format_short_duration(duration)
59 def format_short_duration(duration)
60 return '' if duration==nil
60 return '' if duration==nil
61 d = duration.to_f
61 d = duration.to_f
62 return Time.at(d).gmtime.strftime("%X")
62 return Time.at(d).gmtime.strftime("%X")
63 end
63 end
64
64
65 def read_textfile(fname,max_size=2048)
65 def read_textfile(fname,max_size=2048)
66 begin
66 begin
67 File.open(fname).read(max_size)
67 File.open(fname).read(max_size)
68 rescue
68 rescue
69 nil
69 nil
70 end
70 end
71 end
71 end
72
72
73 def user_title_bar(user)
73 def user_title_bar(user)
74 header = ''
74 header = ''
75 time_left = ''
75 time_left = ''
76
76
77 #
77 #
78 # if the contest is over
78 # if the contest is over
79 if GraderConfiguration.time_limit_mode?
79 if GraderConfiguration.time_limit_mode?
80 if user.contest_finished?
80 if user.contest_finished?
81 header = <<CONTEST_OVER
81 header = <<CONTEST_OVER
82 <tr><td colspan="2" align="center">
82 <tr><td colspan="2" align="center">
83 <span class="contest-over-msg">THE CONTEST IS OVER</span>
83 <span class="contest-over-msg">THE CONTEST IS OVER</span>
84 </td></tr>
84 </td></tr>
85 CONTEST_OVER
85 CONTEST_OVER
86 end
86 end
87 if !user.contest_started?
87 if !user.contest_started?
88 time_left = "&nbsp;&nbsp;" + (t 'title_bar.contest_not_started')
88 time_left = "&nbsp;&nbsp;" + (t 'title_bar.contest_not_started')
89 else
89 else
90 time_left = "&nbsp;&nbsp;" + (t 'title_bar.remaining_time') +
90 time_left = "&nbsp;&nbsp;" + (t 'title_bar.remaining_time') +
91 " #{format_short_duration(user.contest_time_left)}"
91 " #{format_short_duration(user.contest_time_left)}"
92 end
92 end
93 end
93 end
94
94
95 #
95 #
96 # if the contest is in the anaysis mode
96 # if the contest is in the anaysis mode
97 if GraderConfiguration.analysis_mode?
97 if GraderConfiguration.analysis_mode?
98 header = <<ANALYSISMODE
98 header = <<ANALYSISMODE
99 <tr><td colspan="2" align="center">
99 <tr><td colspan="2" align="center">
100 <span class="contest-over-msg">ANALYSIS MODE</span>
100 <span class="contest-over-msg">ANALYSIS MODE</span>
101 </td></tr>
101 </td></tr>
102 ANALYSISMODE
102 ANALYSISMODE
103 end
103 end
104
104
105 contest_name = GraderConfiguration['contest.name']
105 contest_name = GraderConfiguration['contest.name']
106
106
107 #
107 #
108 # build real title bar
108 # build real title bar
109 result = <<TITLEBAR
109 result = <<TITLEBAR
110 <div class="title">
110 <div class="title">
111 <table>
111 <table>
112 #{header}
112 #{header}
113 <tr>
113 <tr>
114 <td class="left-col">
114 <td class="left-col">
115 #{user.full_name}<br/>
115 #{user.full_name}<br/>
116 - #{t 'title_bar.current_time'} #{format_short_time(Time.new)}
116 + #{t 'title_bar.current_time'} #{format_short_time(Time.zone.now)}
117 #{time_left}
117 #{time_left}
118 <br/>
118 <br/>
119 </td>
119 </td>
120 <td class="right-col">#{contest_name}</td>
120 <td class="right-col">#{contest_name}</td>
121 </tr>
121 </tr>
122 </table>
122 </table>
123 </div>
123 </div>
124 TITLEBAR
124 TITLEBAR
125 result.html_safe
125 result.html_safe
126 end
126 end
127
127
128 def markdown(text)
128 def markdown(text)
129 markdown = RDiscount.new(text)
129 markdown = RDiscount.new(text)
130 markdown.to_html.html_safe
130 markdown.to_html.html_safe
131 end
131 end
132
132
133 end
133 end
@@ -1,166 +1,173
1 class Submission < ActiveRecord::Base
1 class Submission < ActiveRecord::Base
2
2
3 belongs_to :language
3 belongs_to :language
4 belongs_to :problem
4 belongs_to :problem
5 belongs_to :user
5 belongs_to :user
6
6
7 before_validation :assign_problem
7 before_validation :assign_problem
8 before_validation :assign_language
8 before_validation :assign_language
9
9
10 validates_presence_of :source
10 validates_presence_of :source
11 validates_length_of :source, :maximum => 100_000, :allow_blank => true, :message => 'too long'
11 validates_length_of :source, :maximum => 100_000, :allow_blank => true, :message => 'too long'
12 validates_length_of :source, :minimum => 1, :allow_blank => true, :message => 'too short'
12 validates_length_of :source, :minimum => 1, :allow_blank => true, :message => 'too short'
13 validate :must_have_valid_problem
13 validate :must_have_valid_problem
14 validate :must_specify_language
14 validate :must_specify_language
15
15
16 before_save :assign_latest_number_if_new_recond
16 before_save :assign_latest_number_if_new_recond
17
17
18 def self.find_last_by_user_and_problem(user_id, problem_id)
18 def self.find_last_by_user_and_problem(user_id, problem_id)
19 last_sub = find(:first,
19 last_sub = find(:first,
20 :conditions => {:user_id => user_id,
20 :conditions => {:user_id => user_id,
21 :problem_id => problem_id},
21 :problem_id => problem_id},
22 :order => 'number DESC')
22 :order => 'number DESC')
23 return last_sub
23 return last_sub
24 end
24 end
25
25
26 def self.find_all_last_by_problem(problem_id)
26 def self.find_all_last_by_problem(problem_id)
27 # need to put in SQL command, maybe there's a better way
27 # need to put in SQL command, maybe there's a better way
28 Submission.find_by_sql("SELECT * FROM submissions " +
28 Submission.find_by_sql("SELECT * FROM submissions " +
29 "WHERE id = " +
29 "WHERE id = " +
30 "(SELECT MAX(id) FROM submissions AS subs " +
30 "(SELECT MAX(id) FROM submissions AS subs " +
31 "WHERE subs.user_id = submissions.user_id AND " +
31 "WHERE subs.user_id = submissions.user_id AND " +
32 "problem_id = " + problem_id.to_s + " " +
32 "problem_id = " + problem_id.to_s + " " +
33 "GROUP BY user_id) " +
33 "GROUP BY user_id) " +
34 "ORDER BY user_id")
34 "ORDER BY user_id")
35 end
35 end
36
36
37 + def self.find_in_range_by_user_and_problem(user_id, problem_id,since_id,until_id)
38 + records = Submission.where(problem_id: problem_id,user_id: user_id)
39 + records = records.where('id >= ?',since_id) if since_id > 0
40 + records = records.where('id <= ?',until_id) if until_id > 0
41 + records.all
42 + end
43 +
37 def self.find_last_for_all_available_problems(user_id)
44 def self.find_last_for_all_available_problems(user_id)
38 submissions = Array.new
45 submissions = Array.new
39 problems = Problem.find_available_problems
46 problems = Problem.find_available_problems
40 problems.each do |problem|
47 problems.each do |problem|
41 sub = Submission.find_last_by_user_and_problem(user_id, problem.id)
48 sub = Submission.find_last_by_user_and_problem(user_id, problem.id)
42 submissions << sub if sub!=nil
49 submissions << sub if sub!=nil
43 end
50 end
44 submissions
51 submissions
45 end
52 end
46
53
47 def self.find_by_user_problem_number(user_id, problem_id, number)
54 def self.find_by_user_problem_number(user_id, problem_id, number)
48 Submission.find(:first,
55 Submission.find(:first,
49 :conditions => {
56 :conditions => {
50 :user_id => user_id,
57 :user_id => user_id,
51 :problem_id => problem_id,
58 :problem_id => problem_id,
52 :number => number
59 :number => number
53 })
60 })
54 end
61 end
55
62
56 def self.find_all_by_user_problem(user_id, problem_id)
63 def self.find_all_by_user_problem(user_id, problem_id)
57 Submission.find(:all,
64 Submission.find(:all,
58 :conditions => {
65 :conditions => {
59 :user_id => user_id,
66 :user_id => user_id,
60 :problem_id => problem_id,
67 :problem_id => problem_id,
61 })
68 })
62 end
69 end
63
70
64 def download_filename
71 def download_filename
65 if self.problem.output_only
72 if self.problem.output_only
66 return self.source_filename
73 return self.source_filename
67 else
74 else
68 timestamp = self.submitted_at.localtime.strftime("%H%M%S")
75 timestamp = self.submitted_at.localtime.strftime("%H%M%S")
69 return "#{self.problem.name}-#{timestamp}.#{self.language.ext}"
76 return "#{self.problem.name}-#{timestamp}.#{self.language.ext}"
70 end
77 end
71 end
78 end
72
79
73 protected
80 protected
74
81
75 def self.find_option_in_source(option, source)
82 def self.find_option_in_source(option, source)
76 if source==nil
83 if source==nil
77 return nil
84 return nil
78 end
85 end
79 i = 0
86 i = 0
80 source.each_line do |s|
87 source.each_line do |s|
81 if s =~ option
88 if s =~ option
82 words = s.split
89 words = s.split
83 return words[1]
90 return words[1]
84 end
91 end
85 i = i + 1
92 i = i + 1
86 if i==10
93 if i==10
87 return nil
94 return nil
88 end
95 end
89 end
96 end
90 return nil
97 return nil
91 end
98 end
92
99
93 def self.find_language_in_source(source, source_filename="")
100 def self.find_language_in_source(source, source_filename="")
94 langopt = find_option_in_source(/^LANG:/,source)
101 langopt = find_option_in_source(/^LANG:/,source)
95 if langopt
102 if langopt
96 return (Language.find_by_name(langopt) ||
103 return (Language.find_by_name(langopt) ||
97 Language.find_by_pretty_name(langopt))
104 Language.find_by_pretty_name(langopt))
98 else
105 else
99 if source_filename
106 if source_filename
100 return Language.find_by_extension(source_filename.split('.').last)
107 return Language.find_by_extension(source_filename.split('.').last)
101 else
108 else
102 return nil
109 return nil
103 end
110 end
104 end
111 end
105 end
112 end
106
113
107 def self.find_problem_in_source(source, source_filename="")
114 def self.find_problem_in_source(source, source_filename="")
108 prob_opt = find_option_in_source(/^TASK:/,source)
115 prob_opt = find_option_in_source(/^TASK:/,source)
109 if problem = Problem.find_by_name(prob_opt)
116 if problem = Problem.find_by_name(prob_opt)
110 return problem
117 return problem
111 else
118 else
112 if source_filename
119 if source_filename
113 return Problem.find_by_name(source_filename.split('.').first)
120 return Problem.find_by_name(source_filename.split('.').first)
114 else
121 else
115 return nil
122 return nil
116 end
123 end
117 end
124 end
118 end
125 end
119
126
120 def assign_problem
127 def assign_problem
121 if self.problem_id!=-1
128 if self.problem_id!=-1
122 begin
129 begin
123 self.problem = Problem.find(self.problem_id)
130 self.problem = Problem.find(self.problem_id)
124 rescue ActiveRecord::RecordNotFound
131 rescue ActiveRecord::RecordNotFound
125 self.problem = nil
132 self.problem = nil
126 end
133 end
127 else
134 else
128 self.problem = Submission.find_problem_in_source(self.source,
135 self.problem = Submission.find_problem_in_source(self.source,
129 self.source_filename)
136 self.source_filename)
130 end
137 end
131 end
138 end
132
139
133 def assign_language
140 def assign_language
134 self.language = Submission.find_language_in_source(self.source,
141 self.language = Submission.find_language_in_source(self.source,
135 self.source_filename)
142 self.source_filename)
136 end
143 end
137
144
138 # validation codes
145 # validation codes
139 def must_specify_language
146 def must_specify_language
140 return if self.source==nil
147 return if self.source==nil
141
148
142 # for output_only tasks
149 # for output_only tasks
143 return if self.problem!=nil and self.problem.output_only
150 return if self.problem!=nil and self.problem.output_only
144
151
145 if self.language==nil
152 if self.language==nil
146 errors.add('source',"must specify programming language") unless self.language!=nil
153 errors.add('source',"must specify programming language") unless self.language!=nil
147 end
154 end
148 end
155 end
149
156
150 def must_have_valid_problem
157 def must_have_valid_problem
151 return if self.source==nil
158 return if self.source==nil
152 if self.problem==nil
159 if self.problem==nil
153 errors.add('problem',"must be specified.")
160 errors.add('problem',"must be specified.")
154 elsif (!self.problem.available) and (self.new_record?)
161 elsif (!self.problem.available) and (self.new_record?)
155 errors.add('problem',"must be valid.")
162 errors.add('problem',"must be valid.")
156 end
163 end
157 end
164 end
158
165
159 # callbacks
166 # callbacks
160 def assign_latest_number_if_new_recond
167 def assign_latest_number_if_new_recond
161 return if !self.new_record?
168 return if !self.new_record?
162 latest = Submission.find_last_by_user_and_problem(self.user_id, self.problem_id)
169 latest = Submission.find_last_by_user_and_problem(self.user_id, self.problem_id)
163 self.number = (latest==nil) ? 1 : latest.number + 1;
170 self.number = (latest==nil) ? 1 : latest.number + 1;
164 end
171 end
165
172
166 end
173 end
@@ -1,43 +1,48
1 <h1>User grading results</h1>
1 <h1>User grading results</h1>
2 + <h2>Show scores from latest submission</h2>
3 +
4 + <%= render 'submission_range' %>
5 +
6 + <p>Latest scores</p>
2
7
3 <table class="info">
8 <table class="info">
4 <tr class="info-head">
9 <tr class="info-head">
5 <th>User</th>
10 <th>User</th>
6 <th>Name</th>
11 <th>Name</th>
7 <th>Activated?</th>
12 <th>Activated?</th>
8 <th>Logged in</th>
13 <th>Logged in</th>
9 <th>Contest(s)</th>
14 <th>Contest(s)</th>
10 <% @problems.each do |p| %>
15 <% @problems.each do |p| %>
11 <th><%= p.name %></th>
16 <th><%= p.name %></th>
12 <% end %>
17 <% end %>
13 <th>Total</th>
18 <th>Total</th>
14 <th>Passed</th>
19 <th>Passed</th>
15 </tr>
20 </tr>
16 <% counter = 0 %>
21 <% counter = 0 %>
17 <% @scorearray.each do |sc| %>
22 <% @scorearray.each do |sc| %>
18 <tr class="<%= (counter %2 ==0) ? "info-even" : "info-odd" %>">
23 <tr class="<%= (counter %2 ==0) ? "info-even" : "info-odd" %>">
19 <% total = 0 %>
24 <% total = 0 %>
20 <% num_passed = 0 %>
25 <% num_passed = 0 %>
21 <% sc.each_index do |i| %>
26 <% sc.each_index do |i| %>
22 <% if i==0 %>
27 <% if i==0 %>
23 <td><%= sc[i].login %></td>
28 <td><%= sc[i].login %></td>
24 <td><%= sc[i].full_name %></td>
29 <td><%= sc[i].full_name %></td>
25 <td><%= sc[i].activated %></td>
30 <td><%= sc[i].activated %></td>
26 <td>
31 <td>
27 <%= sc[i].try(:contest_stat).try(:started_at)!=nil ? 'yes' : 'no' %>
32 <%= sc[i].try(:contest_stat).try(:started_at)!=nil ? 'yes' : 'no' %>
28 </td>
33 </td>
29 <td>
34 <td>
30 <%= sc[i].contests.collect {|c| c.name}.join(', ') %>
35 <%= sc[i].contests.collect {|c| c.name}.join(', ') %>
31 </td>
36 </td>
32 <% else %>
37 <% else %>
33 <td><%= sc[i][0] %></td>
38 <td><%= sc[i][0] %></td>
34 <% total += sc[i][0] %>
39 <% total += sc[i][0] %>
35 <% num_passed += 1 if sc[i][1] %>
40 <% num_passed += 1 if sc[i][1] %>
36 <% end %>
41 <% end %>
37 <% end %>
42 <% end %>
38 <td><%= total %></td>
43 <td><%= total %></td>
39 <td><%= num_passed %></td>
44 <td><%= num_passed %></td>
40 </tr>
45 </tr>
41 <% counter += 1 %>
46 <% counter += 1 %>
42 <% end %>
47 <% end %>
43 </table>
48 </table>
@@ -1,30 +1,41
1 module MailHelperMethods
1 module MailHelperMethods
2
2
3 def send_mail(mail_to, mail_subject, mail_body)
3 def send_mail(mail_to, mail_subject, mail_body)
4 mail_from = GraderConfiguration['system.online_registration.from']
4 mail_from = GraderConfiguration['system.online_registration.from']
5 smtp_server = GraderConfiguration['system.online_registration.smtp']
5 smtp_server = GraderConfiguration['system.online_registration.smtp']
6
6
7 if ['fake', 'debug'].include? smtp_server
7 if ['fake', 'debug'].include? smtp_server
8 puts "-------------------------
8 puts "-------------------------
9 To: #{mail_to}
9 To: #{mail_to}
10 From: #{mail_from}
10 From: #{mail_from}
11 Subject: #{mail_subject}
11 Subject: #{mail_subject}
12 #{mail_body}
12 #{mail_body}
13 --------------------------
13 --------------------------
14 "
14 "
15 return true
15 return true
16 end
16 end
17
17
18 mail = Mail.new do
18 mail = Mail.new do
19 from mail_from
19 from mail_from
20 to mail_to
20 to mail_to
21 subject mail_subject
21 subject mail_subject
22 body mail_body
22 body mail_body
23 end
23 end
24
24
25 - mail.delivery_settings = { :address => smtp_server }
25 + mail_option = {
26 + :address => smtp_server,
27 + # :domain => nil,
28 + # :port => 25,
29 + # :user_name => nil,
30 + # :password => nil,
31 + # :authentication=>'plain',
32 + # :enable_starttls_auto => true
33 + }
34 +
35 + mail.delivery_method :smtp, mail_option
36 +
26 mail.deliver
37 mail.deliver
27 end
38 end
28
39
29 end
40 end
30
41
You need to be logged in to leave comments. Login now