Description:
fix destroy for user and problem
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r605:1317cbdbc3df - - 2 files changed: 3 inserted, 4 deleted

@@ -1,282 +1,281
1 class ProblemsController < ApplicationController
1 class ProblemsController < ApplicationController
2
2
3 before_filter :authenticate, :authorization
3 before_filter :authenticate, :authorization
4
4
5 in_place_edit_for :problem, :name
5 in_place_edit_for :problem, :name
6 in_place_edit_for :problem, :full_name
6 in_place_edit_for :problem, :full_name
7 in_place_edit_for :problem, :full_score
7 in_place_edit_for :problem, :full_score
8
8
9 def index
9 def index
10 @problems = Problem.find(:all, :order => 'date_added DESC')
10 @problems = Problem.find(:all, :order => 'date_added DESC')
11 end
11 end
12
12
13 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
13 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
14 - verify :method => :post, :only => [ :destroy,
14 + verify :method => :post, :only => [ :create, :quick_create,
15 - :create, :quick_create,
16 :do_manage,
15 :do_manage,
17 :do_import,
16 :do_import,
18 ],
17 ],
19 :redirect_to => { :action => :index }
18 :redirect_to => { :action => :index }
20
19
21 def show
20 def show
22 @problem = Problem.find(params[:id])
21 @problem = Problem.find(params[:id])
23 end
22 end
24
23
25 def new
24 def new
26 @problem = Problem.new
25 @problem = Problem.new
27 @description = nil
26 @description = nil
28 end
27 end
29
28
30 def create
29 def create
31 @problem = Problem.new(params[:problem])
30 @problem = Problem.new(params[:problem])
32 @description = Description.new(params[:description])
31 @description = Description.new(params[:description])
33 if @description.body!=''
32 if @description.body!=''
34 if !@description.save
33 if !@description.save
35 render :action => new and return
34 render :action => new and return
36 end
35 end
37 else
36 else
38 @description = nil
37 @description = nil
39 end
38 end
40 @problem.description = @description
39 @problem.description = @description
41 if @problem.save
40 if @problem.save
42 flash[:notice] = 'Problem was successfully created.'
41 flash[:notice] = 'Problem was successfully created.'
43 redirect_to action: :index
42 redirect_to action: :index
44 else
43 else
45 render :action => 'new'
44 render :action => 'new'
46 end
45 end
47 end
46 end
48
47
49 def quick_create
48 def quick_create
50 @problem = Problem.new(params[:problem])
49 @problem = Problem.new(params[:problem])
51 @problem.full_name = @problem.name if @problem.full_name == ''
50 @problem.full_name = @problem.name if @problem.full_name == ''
52 @problem.full_score = 100
51 @problem.full_score = 100
53 @problem.available = false
52 @problem.available = false
54 @problem.test_allowed = true
53 @problem.test_allowed = true
55 @problem.output_only = false
54 @problem.output_only = false
56 @problem.date_added = Time.new
55 @problem.date_added = Time.new
57 if @problem.save
56 if @problem.save
58 flash[:notice] = 'Problem was successfully created.'
57 flash[:notice] = 'Problem was successfully created.'
59 redirect_to action: :index
58 redirect_to action: :index
60 else
59 else
61 flash[:notice] = 'Error saving problem'
60 flash[:notice] = 'Error saving problem'
62 redirect_to action: :index
61 redirect_to action: :index
63 end
62 end
64 end
63 end
65
64
66 def edit
65 def edit
67 @problem = Problem.find(params[:id])
66 @problem = Problem.find(params[:id])
68 @description = @problem.description
67 @description = @problem.description
69 end
68 end
70
69
71 def update
70 def update
72 @problem = Problem.find(params[:id])
71 @problem = Problem.find(params[:id])
73 @description = @problem.description
72 @description = @problem.description
74 if @description.nil? and params[:description][:body]!=''
73 if @description.nil? and params[:description][:body]!=''
75 @description = Description.new(params[:description])
74 @description = Description.new(params[:description])
76 if !@description.save
75 if !@description.save
77 flash[:notice] = 'Error saving description'
76 flash[:notice] = 'Error saving description'
78 render :action => 'edit' and return
77 render :action => 'edit' and return
79 end
78 end
80 @problem.description = @description
79 @problem.description = @description
81 elsif @description
80 elsif @description
82 if !@description.update_attributes(params[:description])
81 if !@description.update_attributes(params[:description])
83 flash[:notice] = 'Error saving description'
82 flash[:notice] = 'Error saving description'
84 render :action => 'edit' and return
83 render :action => 'edit' and return
85 end
84 end
86 end
85 end
87 if params[:file] and params[:file].content_type != 'application/pdf'
86 if params[:file] and params[:file].content_type != 'application/pdf'
88 flash[:notice] = 'Error: Uploaded file is not PDF'
87 flash[:notice] = 'Error: Uploaded file is not PDF'
89 render :action => 'edit' and return
88 render :action => 'edit' and return
90 end
89 end
91 if @problem.update_attributes(params[:problem])
90 if @problem.update_attributes(params[:problem])
92 flash[:notice] = 'Problem was successfully updated.'
91 flash[:notice] = 'Problem was successfully updated.'
93 unless params[:file] == nil or params[:file] == ''
92 unless params[:file] == nil or params[:file] == ''
94 flash[:notice] = 'Problem was successfully updated and a new PDF file is uploaded.'
93 flash[:notice] = 'Problem was successfully updated and a new PDF file is uploaded.'
95 out_dirname = "#{Problem.download_file_basedir}/#{@problem.id}"
94 out_dirname = "#{Problem.download_file_basedir}/#{@problem.id}"
96 if not FileTest.exists? out_dirname
95 if not FileTest.exists? out_dirname
97 Dir.mkdir out_dirname
96 Dir.mkdir out_dirname
98 end
97 end
99
98
100 out_filename = "#{out_dirname}/#{@problem.name}.pdf"
99 out_filename = "#{out_dirname}/#{@problem.name}.pdf"
101 if FileTest.exists? out_filename
100 if FileTest.exists? out_filename
102 File.delete out_filename
101 File.delete out_filename
103 end
102 end
104
103
105 File.open(out_filename,"wb") do |file|
104 File.open(out_filename,"wb") do |file|
106 file.write(params[:file].read)
105 file.write(params[:file].read)
107 end
106 end
108 @problem.description_filename = "#{@problem.name}.pdf"
107 @problem.description_filename = "#{@problem.name}.pdf"
109 @problem.save
108 @problem.save
110 end
109 end
111 redirect_to :action => 'show', :id => @problem
110 redirect_to :action => 'show', :id => @problem
112 else
111 else
113 render :action => 'edit'
112 render :action => 'edit'
114 end
113 end
115 end
114 end
116
115
117 def destroy
116 def destroy
118 - Problem.find(params[:id]).destroy
117 + p = Problem.find(params[:id]).destroy
119 redirect_to action: :index
118 redirect_to action: :index
120 end
119 end
121
120
122 def toggle
121 def toggle
123 @problem = Problem.find(params[:id])
122 @problem = Problem.find(params[:id])
124 @problem.update_attributes(available: !(@problem.available) )
123 @problem.update_attributes(available: !(@problem.available) )
125 respond_to do |format|
124 respond_to do |format|
126 format.js { }
125 format.js { }
127 end
126 end
128 end
127 end
129
128
130 def toggle_test
129 def toggle_test
131 @problem = Problem.find(params[:id])
130 @problem = Problem.find(params[:id])
132 @problem.update_attributes(test_allowed: !(@problem.test_allowed?) )
131 @problem.update_attributes(test_allowed: !(@problem.test_allowed?) )
133 respond_to do |format|
132 respond_to do |format|
134 format.js { }
133 format.js { }
135 end
134 end
136 end
135 end
137
136
138 def turn_all_off
137 def turn_all_off
139 Problem.find(:all,
138 Problem.find(:all,
140 :conditions => "available = 1").each do |problem|
139 :conditions => "available = 1").each do |problem|
141 problem.available = false
140 problem.available = false
142 problem.save
141 problem.save
143 end
142 end
144 redirect_to action: :index
143 redirect_to action: :index
145 end
144 end
146
145
147 def turn_all_on
146 def turn_all_on
148 Problem.find(:all,
147 Problem.find(:all,
149 :conditions => "available = 0").each do |problem|
148 :conditions => "available = 0").each do |problem|
150 problem.available = true
149 problem.available = true
151 problem.save
150 problem.save
152 end
151 end
153 redirect_to action: :index
152 redirect_to action: :index
154 end
153 end
155
154
156 def stat
155 def stat
157 @problem = Problem.find(params[:id])
156 @problem = Problem.find(params[:id])
158 unless @problem.available or session[:admin]
157 unless @problem.available or session[:admin]
159 redirect_to :controller => 'main', :action => 'list'
158 redirect_to :controller => 'main', :action => 'list'
160 return
159 return
161 end
160 end
162 @submissions = Submission.includes(:user).where(problem_id: params[:id]).order(:user_id,:id)
161 @submissions = Submission.includes(:user).where(problem_id: params[:id]).order(:user_id,:id)
163
162
164 #stat summary
163 #stat summary
165 range =65
164 range =65
166 @histogram = { data: Array.new(range,0), summary: {} }
165 @histogram = { data: Array.new(range,0), summary: {} }
167 user = Hash.new(0)
166 user = Hash.new(0)
168 @submissions.find_each do |sub|
167 @submissions.find_each do |sub|
169 d = (DateTime.now.in_time_zone - sub.submitted_at) / 24 / 60 / 60
168 d = (DateTime.now.in_time_zone - sub.submitted_at) / 24 / 60 / 60
170 @histogram[:data][d.to_i] += 1 if d < range
169 @histogram[:data][d.to_i] += 1 if d < range
171 user[sub.user_id] = [user[sub.user_id], ((sub.try(:points) || 0) >= @problem.full_score) ? 1 : 0].max
170 user[sub.user_id] = [user[sub.user_id], ((sub.try(:points) || 0) >= @problem.full_score) ? 1 : 0].max
172 end
171 end
173 @histogram[:summary][:max] = [@histogram[:data].max,1].max
172 @histogram[:summary][:max] = [@histogram[:data].max,1].max
174
173
175 @summary = { attempt: user.count, solve: 0 }
174 @summary = { attempt: user.count, solve: 0 }
176 user.each_value { |v| @summary[:solve] += 1 if v == 1 }
175 user.each_value { |v| @summary[:solve] += 1 if v == 1 }
177 end
176 end
178
177
179 def manage
178 def manage
180 @problems = Problem.find(:all, :order => 'date_added DESC')
179 @problems = Problem.find(:all, :order => 'date_added DESC')
181 end
180 end
182
181
183 def do_manage
182 def do_manage
184 if params.has_key? 'change_date_added'
183 if params.has_key? 'change_date_added'
185 change_date_added
184 change_date_added
186 elsif params.has_key? 'add_to_contest'
185 elsif params.has_key? 'add_to_contest'
187 add_to_contest
186 add_to_contest
188 elsif params.has_key? 'enable_problem'
187 elsif params.has_key? 'enable_problem'
189 set_available(true)
188 set_available(true)
190 elsif params.has_key? 'disable_problem'
189 elsif params.has_key? 'disable_problem'
191 set_available(false)
190 set_available(false)
192 end
191 end
193 redirect_to :action => 'manage'
192 redirect_to :action => 'manage'
194 end
193 end
195
194
196 def import
195 def import
197 @allow_test_pair_import = allow_test_pair_import?
196 @allow_test_pair_import = allow_test_pair_import?
198 end
197 end
199
198
200 def do_import
199 def do_import
201 old_problem = Problem.find_by_name(params[:name])
200 old_problem = Problem.find_by_name(params[:name])
202 if !allow_test_pair_import? and params.has_key? :import_to_db
201 if !allow_test_pair_import? and params.has_key? :import_to_db
203 params.delete :import_to_db
202 params.delete :import_to_db
204 end
203 end
205 @problem, import_log = Problem.create_from_import_form_params(params,
204 @problem, import_log = Problem.create_from_import_form_params(params,
206 old_problem)
205 old_problem)
207
206
208 if !@problem.errors.empty?
207 if !@problem.errors.empty?
209 render :action => 'import' and return
208 render :action => 'import' and return
210 end
209 end
211
210
212 if old_problem!=nil
211 if old_problem!=nil
213 flash[:notice] = "The test data has been replaced for problem #{@problem.name}"
212 flash[:notice] = "The test data has been replaced for problem #{@problem.name}"
214 end
213 end
215 @log = import_log
214 @log = import_log
216 end
215 end
217
216
218 def remove_contest
217 def remove_contest
219 problem = Problem.find(params[:id])
218 problem = Problem.find(params[:id])
220 contest = Contest.find(params[:contest_id])
219 contest = Contest.find(params[:contest_id])
221 if problem!=nil and contest!=nil
220 if problem!=nil and contest!=nil
222 problem.contests.delete(contest)
221 problem.contests.delete(contest)
223 end
222 end
224 redirect_to :action => 'manage'
223 redirect_to :action => 'manage'
225 end
224 end
226
225
227 ##################################
226 ##################################
228 protected
227 protected
229
228
230 def allow_test_pair_import?
229 def allow_test_pair_import?
231 if defined? ALLOW_TEST_PAIR_IMPORT
230 if defined? ALLOW_TEST_PAIR_IMPORT
232 return ALLOW_TEST_PAIR_IMPORT
231 return ALLOW_TEST_PAIR_IMPORT
233 else
232 else
234 return false
233 return false
235 end
234 end
236 end
235 end
237
236
238 def change_date_added
237 def change_date_added
239 problems = get_problems_from_params
238 problems = get_problems_from_params
240 year = params[:date_added][:year].to_i
239 year = params[:date_added][:year].to_i
241 month = params[:date_added][:month].to_i
240 month = params[:date_added][:month].to_i
242 day = params[:date_added][:day].to_i
241 day = params[:date_added][:day].to_i
243 date = Date.new(year,month,day)
242 date = Date.new(year,month,day)
244 problems.each do |p|
243 problems.each do |p|
245 p.date_added = date
244 p.date_added = date
246 p.save
245 p.save
247 end
246 end
248 end
247 end
249
248
250 def add_to_contest
249 def add_to_contest
251 problems = get_problems_from_params
250 problems = get_problems_from_params
252 contest = Contest.find(params[:contest][:id])
251 contest = Contest.find(params[:contest][:id])
253 if contest!=nil and contest.enabled
252 if contest!=nil and contest.enabled
254 problems.each do |p|
253 problems.each do |p|
255 p.contests << contest
254 p.contests << contest
256 end
255 end
257 end
256 end
258 end
257 end
259
258
260 def set_available(avail)
259 def set_available(avail)
261 problems = get_problems_from_params
260 problems = get_problems_from_params
262 problems.each do |p|
261 problems.each do |p|
263 p.available = avail
262 p.available = avail
264 p.save
263 p.save
265 end
264 end
266 end
265 end
267
266
268 def get_problems_from_params
267 def get_problems_from_params
269 problems = []
268 problems = []
270 params.keys.each do |k|
269 params.keys.each do |k|
271 if k.index('prob-')==0
270 if k.index('prob-')==0
272 name, id, order = k.split('-')
271 name, id, order = k.split('-')
273 problems << Problem.find(id)
272 problems << Problem.find(id)
274 end
273 end
275 end
274 end
276 problems
275 problems
277 end
276 end
278
277
279 def get_problems_stat
278 def get_problems_stat
280 end
279 end
281
280
282 end
281 end
@@ -1,394 +1,394
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 => [
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 @user_count = User.count
19 @user_count = User.count
20 if params[:page] == 'all'
20 if params[:page] == 'all'
21 @users = User.all
21 @users = User.all
22 @paginated = false
22 @paginated = false
23 else
23 else
24 @users = User.paginate :page => params[:page]
24 @users = User.paginate :page => params[:page]
25 @paginated = true
25 @paginated = true
26 end
26 end
27 @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at']
27 @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at']
28 @contests = Contest.enabled
28 @contests = Contest.enabled
29 end
29 end
30
30
31 def active
31 def active
32 sessions = ActiveRecord::SessionStore::Session.find(:all, :conditions => ["updated_at >= ?", 60.minutes.ago])
32 sessions = ActiveRecord::SessionStore::Session.find(:all, :conditions => ["updated_at >= ?", 60.minutes.ago])
33 @users = []
33 @users = []
34 sessions.each do |session|
34 sessions.each do |session|
35 if session.data[:user_id]
35 if session.data[:user_id]
36 @users << User.find(session.data[:user_id])
36 @users << User.find(session.data[:user_id])
37 end
37 end
38 end
38 end
39 end
39 end
40
40
41 def show
41 def show
42 @user = User.find(params[:id])
42 @user = User.find(params[:id])
43 end
43 end
44
44
45 def new
45 def new
46 @user = User.new
46 @user = User.new
47 end
47 end
48
48
49 def create
49 def create
50 @user = User.new(params[:user])
50 @user = User.new(params[:user])
51 @user.activated = true
51 @user.activated = true
52 if @user.save
52 if @user.save
53 flash[:notice] = 'User was successfully created.'
53 flash[:notice] = 'User was successfully created.'
54 redirect_to :action => 'index'
54 redirect_to :action => 'index'
55 else
55 else
56 render :action => 'new'
56 render :action => 'new'
57 end
57 end
58 end
58 end
59
59
60 def clear_last_ip
60 def clear_last_ip
61 @user = User.find(params[:id])
61 @user = User.find(params[:id])
62 @user.last_ip = nil
62 @user.last_ip = nil
63 @user.save
63 @user.save
64 redirect_to action: 'index', page: params[:page]
64 redirect_to action: 'index', page: params[:page]
65 end
65 end
66
66
67 def create_from_list
67 def create_from_list
68 lines = params[:user_list]
68 lines = params[:user_list]
69
69
70 note = []
70 note = []
71
71
72 lines.split("\n").each do |line|
72 lines.split("\n").each do |line|
73 items = line.chomp.split(',')
73 items = line.chomp.split(',')
74 if items.length>=2
74 if items.length>=2
75 login = items[0]
75 login = items[0]
76 full_name = items[1]
76 full_name = items[1]
77
77
78 added_random_password = false
78 added_random_password = false
79 if items.length>=3
79 if items.length>=3
80 password = items[2].chomp(" ")
80 password = items[2].chomp(" ")
81 user_alias = (items.length>=4) ? items[3] : login
81 user_alias = (items.length>=4) ? items[3] : login
82 else
82 else
83 password = random_password
83 password = random_password
84 user_alias = (items.length>=4) ? items[3] : login
84 user_alias = (items.length>=4) ? items[3] : login
85 added_random_password = true
85 added_random_password = true
86 end
86 end
87
87
88 user = User.find_by_login(login)
88 user = User.find_by_login(login)
89 if (user)
89 if (user)
90 user.full_name = full_name
90 user.full_name = full_name
91 user.password = password
91 user.password = password
92 else
92 else
93 user = User.new({:login => login,
93 user = User.new({:login => login,
94 :full_name => full_name,
94 :full_name => full_name,
95 :password => password,
95 :password => password,
96 :password_confirmation => password,
96 :password_confirmation => password,
97 :alias => user_alias})
97 :alias => user_alias})
98 end
98 end
99 user.activated = true
99 user.activated = true
100 user.save
100 user.save
101
101
102 if added_random_password
102 if added_random_password
103 note << "'#{login}' (+)"
103 note << "'#{login}' (+)"
104 else
104 else
105 note << login
105 note << login
106 end
106 end
107 end
107 end
108 end
108 end
109 flash[:notice] = 'User(s) ' + note.join(', ') +
109 flash[:notice] = 'User(s) ' + note.join(', ') +
110 ' were successfully created. ' +
110 ' were successfully created. ' +
111 '( (+) - created with random passwords.)'
111 '( (+) - created with random passwords.)'
112 redirect_to :action => 'index'
112 redirect_to :action => 'index'
113 end
113 end
114
114
115 def edit
115 def edit
116 @user = User.find(params[:id])
116 @user = User.find(params[:id])
117 end
117 end
118
118
119 def update
119 def update
120 @user = User.find(params[:id])
120 @user = User.find(params[:id])
121 if @user.update_attributes(params[:user])
121 if @user.update_attributes(params[:user])
122 flash[:notice] = 'User was successfully updated.'
122 flash[:notice] = 'User was successfully updated.'
123 redirect_to :action => 'show', :id => @user
123 redirect_to :action => 'show', :id => @user
124 else
124 else
125 render :action => 'edit'
125 render :action => 'edit'
126 end
126 end
127 end
127 end
128
128
129 def destroy
129 def destroy
130 User.find(params[:id]).destroy
130 User.find(params[:id]).destroy
131 redirect_to :action => 'index'
131 redirect_to :action => 'index'
132 end
132 end
133
133
134 def user_stat
134 def user_stat
135 if params[:commit] == 'download csv'
135 if params[:commit] == 'download csv'
136 @problems = Problem.all
136 @problems = Problem.all
137 else
137 else
138 @problems = Problem.find_available_problems
138 @problems = Problem.find_available_problems
139 end
139 end
140 @users = User.includes(:contests, :contest_stat).where(enabled: true) #find(:all, :include => [:contests, :contest_stat]).where(enabled: true)
140 @users = User.includes(:contests, :contest_stat).where(enabled: true) #find(:all, :include => [:contests, :contest_stat]).where(enabled: true)
141 @scorearray = Array.new
141 @scorearray = Array.new
142 @users.each do |u|
142 @users.each do |u|
143 ustat = Array.new
143 ustat = Array.new
144 ustat[0] = u
144 ustat[0] = u
145 @problems.each do |p|
145 @problems.each do |p|
146 sub = Submission.find_last_by_user_and_problem(u.id,p.id)
146 sub = Submission.find_last_by_user_and_problem(u.id,p.id)
147 if (sub!=nil) and (sub.points!=nil) and p and p.full_score
147 if (sub!=nil) and (sub.points!=nil) and p and p.full_score
148 ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)]
148 ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)]
149 else
149 else
150 ustat << [0,false]
150 ustat << [0,false]
151 end
151 end
152 end
152 end
153 @scorearray << ustat
153 @scorearray << ustat
154 end
154 end
155 if params[:commit] == 'download csv' then
155 if params[:commit] == 'download csv' then
156 csv = gen_csv_from_scorearray(@scorearray,@problems)
156 csv = gen_csv_from_scorearray(@scorearray,@problems)
157 send_data csv, filename: 'last_score.csv'
157 send_data csv, filename: 'last_score.csv'
158 else
158 else
159 render template: 'user_admin/user_stat'
159 render template: 'user_admin/user_stat'
160 end
160 end
161 end
161 end
162
162
163 def user_stat_max
163 def user_stat_max
164 if params[:commit] == 'download csv'
164 if params[:commit] == 'download csv'
165 @problems = Problem.all
165 @problems = Problem.all
166 else
166 else
167 @problems = Problem.find_available_problems
167 @problems = Problem.find_available_problems
168 end
168 end
169 @users = User.find(:all, :include => [:contests, :contest_stat])
169 @users = User.find(:all, :include => [:contests, :contest_stat])
170 @scorearray = Array.new
170 @scorearray = Array.new
171 #set up range from param
171 #set up range from param
172 since_id = params.fetch(:since_id, 0).to_i
172 since_id = params.fetch(:since_id, 0).to_i
173 until_id = params.fetch(:until_id, 0).to_i
173 until_id = params.fetch(:until_id, 0).to_i
174 @users.each do |u|
174 @users.each do |u|
175 ustat = Array.new
175 ustat = Array.new
176 ustat[0] = u
176 ustat[0] = u
177 @problems.each do |p|
177 @problems.each do |p|
178 max_points = 0
178 max_points = 0
179 Submission.find_in_range_by_user_and_problem(u.id,p.id,since_id,until_id).each do |sub|
179 Submission.find_in_range_by_user_and_problem(u.id,p.id,since_id,until_id).each do |sub|
180 max_points = sub.points if sub and sub.points and (sub.points > max_points)
180 max_points = sub.points if sub and sub.points and (sub.points > max_points)
181 end
181 end
182 ustat << [(max_points.to_f*100/p.full_score).round, (max_points>=p.full_score)]
182 ustat << [(max_points.to_f*100/p.full_score).round, (max_points>=p.full_score)]
183 end
183 end
184 @scorearray << ustat
184 @scorearray << ustat
185 end
185 end
186
186
187 if params[:commit] == 'download csv' then
187 if params[:commit] == 'download csv' then
188 csv = gen_csv_from_scorearray(@scorearray,@problems)
188 csv = gen_csv_from_scorearray(@scorearray,@problems)
189 send_data csv, filename: 'max_score.csv'
189 send_data csv, filename: 'max_score.csv'
190 else
190 else
191 render template: 'user_admin/user_stat'
191 render template: 'user_admin/user_stat'
192 end
192 end
193 end
193 end
194
194
195 def import
195 def import
196 if params[:file]==''
196 if params[:file]==''
197 flash[:notice] = 'Error importing no file'
197 flash[:notice] = 'Error importing no file'
198 redirect_to :action => 'index' and return
198 redirect_to :action => 'index' and return
199 end
199 end
200 import_from_file(params[:file])
200 import_from_file(params[:file])
201 end
201 end
202
202
203 def random_all_passwords
203 def random_all_passwords
204 users = User.find(:all)
204 users = User.find(:all)
205 @prefix = params[:prefix] || ''
205 @prefix = params[:prefix] || ''
206 @non_admin_users = User.find_non_admin_with_prefix(@prefix)
206 @non_admin_users = User.find_non_admin_with_prefix(@prefix)
207 @changed = false
207 @changed = false
208 if request.request_method == 'POST'
208 if request.request_method == 'POST'
209 @non_admin_users.each do |user|
209 @non_admin_users.each do |user|
210 password = random_password
210 password = random_password
211 user.password = password
211 user.password = password
212 user.password_confirmation = password
212 user.password_confirmation = password
213 user.save
213 user.save
214 end
214 end
215 @changed = true
215 @changed = true
216 end
216 end
217 end
217 end
218
218
219 # contest management
219 # contest management
220
220
221 def contests
221 def contests
222 @contest, @users = find_contest_and_user_from_contest_id(params[:id])
222 @contest, @users = find_contest_and_user_from_contest_id(params[:id])
223 @contests = Contest.enabled
223 @contests = Contest.enabled
224 end
224 end
225
225
226 def assign_from_list
226 def assign_from_list
227 contest_id = params[:users_contest_id]
227 contest_id = params[:users_contest_id]
228 org_contest, users = find_contest_and_user_from_contest_id(contest_id)
228 org_contest, users = find_contest_and_user_from_contest_id(contest_id)
229 contest = Contest.find(params[:new_contest][:id])
229 contest = Contest.find(params[:new_contest][:id])
230 if !contest
230 if !contest
231 flash[:notice] = 'Error: no contest'
231 flash[:notice] = 'Error: no contest'
232 redirect_to :action => 'contests', :id =>contest_id
232 redirect_to :action => 'contests', :id =>contest_id
233 end
233 end
234
234
235 note = []
235 note = []
236 users.each do |u|
236 users.each do |u|
237 u.contests = [contest]
237 u.contests = [contest]
238 note << u.login
238 note << u.login
239 end
239 end
240 flash[:notice] = 'User(s) ' + note.join(', ') +
240 flash[:notice] = 'User(s) ' + note.join(', ') +
241 " were successfully reassigned to #{contest.title}."
241 " were successfully reassigned to #{contest.title}."
242 redirect_to :action => 'contests', :id =>contest.id
242 redirect_to :action => 'contests', :id =>contest.id
243 end
243 end
244
244
245 def add_to_contest
245 def add_to_contest
246 user = User.find(params[:id])
246 user = User.find(params[:id])
247 contest = Contest.find(params[:contest_id])
247 contest = Contest.find(params[:contest_id])
248 if user and contest
248 if user and contest
249 user.contests << contest
249 user.contests << contest
250 end
250 end
251 redirect_to :action => 'index'
251 redirect_to :action => 'index'
252 end
252 end
253
253
254 def remove_from_contest
254 def remove_from_contest
255 user = User.find(params[:id])
255 user = User.find(params[:id])
256 contest = Contest.find(params[:contest_id])
256 contest = Contest.find(params[:contest_id])
257 if user and contest
257 if user and contest
258 user.contests.delete(contest)
258 user.contests.delete(contest)
259 end
259 end
260 redirect_to :action => 'index'
260 redirect_to :action => 'index'
261 end
261 end
262
262
263 def contest_management
263 def contest_management
264 end
264 end
265
265
266 def manage_contest
266 def manage_contest
267 contest = Contest.find(params[:contest][:id])
267 contest = Contest.find(params[:contest][:id])
268 if !contest
268 if !contest
269 flash[:notice] = 'You did not choose the contest.'
269 flash[:notice] = 'You did not choose the contest.'
270 redirect_to :action => 'contest_management' and return
270 redirect_to :action => 'contest_management' and return
271 end
271 end
272
272
273 operation = params[:operation]
273 operation = params[:operation]
274
274
275 if not ['add','remove','assign'].include? operation
275 if not ['add','remove','assign'].include? operation
276 flash[:notice] = 'You did not choose the operation to perform.'
276 flash[:notice] = 'You did not choose the operation to perform.'
277 redirect_to :action => 'contest_management' and return
277 redirect_to :action => 'contest_management' and return
278 end
278 end
279
279
280 lines = params[:login_list]
280 lines = params[:login_list]
281 if !lines or lines.blank?
281 if !lines or lines.blank?
282 flash[:notice] = 'You entered an empty list.'
282 flash[:notice] = 'You entered an empty list.'
283 redirect_to :action => 'contest_management' and return
283 redirect_to :action => 'contest_management' and return
284 end
284 end
285
285
286 note = []
286 note = []
287 users = []
287 users = []
288 lines.split("\n").each do |line|
288 lines.split("\n").each do |line|
289 user = User.find_by_login(line.chomp)
289 user = User.find_by_login(line.chomp)
290 if user
290 if user
291 if operation=='add'
291 if operation=='add'
292 if ! user.contests.include? contest
292 if ! user.contests.include? contest
293 user.contests << contest
293 user.contests << contest
294 end
294 end
295 elsif operation=='remove'
295 elsif operation=='remove'
296 user.contests.delete(contest)
296 user.contests.delete(contest)
297 else
297 else
298 user.contests = [contest]
298 user.contests = [contest]
299 end
299 end
300
300
301 if params[:reset_timer]
301 if params[:reset_timer]
302 user.contest_stat.forced_logout = true
302 user.contest_stat.forced_logout = true
303 user.contest_stat.reset_timer_and_save
303 user.contest_stat.reset_timer_and_save
304 end
304 end
305
305
306 if params[:notification_emails]
306 if params[:notification_emails]
307 send_contest_update_notification_email(user, contest)
307 send_contest_update_notification_email(user, contest)
308 end
308 end
309
309
310 note << user.login
310 note << user.login
311 users << user
311 users << user
312 end
312 end
313 end
313 end
314
314
315 if params[:reset_timer]
315 if params[:reset_timer]
316 logout_users(users)
316 logout_users(users)
317 end
317 end
318
318
319 flash[:notice] = 'User(s) ' + note.join(', ') +
319 flash[:notice] = 'User(s) ' + note.join(', ') +
320 ' were successfully modified. '
320 ' were successfully modified. '
321 redirect_to :action => 'contest_management'
321 redirect_to :action => 'contest_management'
322 end
322 end
323
323
324 # admin management
324 # admin management
325
325
326 def admin
326 def admin
327 @admins = User.find(:all).find_all {|user| user.admin? }
327 @admins = User.find(:all).find_all {|user| user.admin? }
328 end
328 end
329
329
330 def grant_admin
330 def grant_admin
331 login = params[:login]
331 login = params[:login]
332 user = User.find_by_login(login)
332 user = User.find_by_login(login)
333 if user!=nil
333 if user!=nil
334 admin_role = Role.find_by_name('admin')
334 admin_role = Role.find_by_name('admin')
335 user.roles << admin_role
335 user.roles << admin_role
336 else
336 else
337 flash[:notice] = 'Unknown user'
337 flash[:notice] = 'Unknown user'
338 end
338 end
339 flash[:notice] = 'User added as admins'
339 flash[:notice] = 'User added as admins'
340 redirect_to :action => 'admin'
340 redirect_to :action => 'admin'
341 end
341 end
342
342
343 def revoke_admin
343 def revoke_admin
344 user = User.find(params[:id])
344 user = User.find(params[:id])
345 if user==nil
345 if user==nil
346 flash[:notice] = 'Unknown user'
346 flash[:notice] = 'Unknown user'
347 redirect_to :action => 'admin' and return
347 redirect_to :action => 'admin' and return
348 elsif user.login == 'root'
348 elsif user.login == 'root'
349 flash[:notice] = 'You cannot revoke admisnistrator permission from root.'
349 flash[:notice] = 'You cannot revoke admisnistrator permission from root.'
350 redirect_to :action => 'admin' and return
350 redirect_to :action => 'admin' and return
351 end
351 end
352
352
353 admin_role = Role.find_by_name('admin')
353 admin_role = Role.find_by_name('admin')
354 user.roles.delete(admin_role)
354 user.roles.delete(admin_role)
355 flash[:notice] = 'User permission revoked'
355 flash[:notice] = 'User permission revoked'
356 redirect_to :action => 'admin'
356 redirect_to :action => 'admin'
357 end
357 end
358
358
359 # mass mailing
359 # mass mailing
360
360
361 def mass_mailing
361 def mass_mailing
362 end
362 end
363
363
364 def bulk_mail
364 def bulk_mail
365 lines = params[:login_list]
365 lines = params[:login_list]
366 if !lines or lines.blank?
366 if !lines or lines.blank?
367 flash[:notice] = 'You entered an empty list.'
367 flash[:notice] = 'You entered an empty list.'
368 redirect_to :action => 'mass_mailing' and return
368 redirect_to :action => 'mass_mailing' and return
369 end
369 end
370
370
371 mail_subject = params[:subject]
371 mail_subject = params[:subject]
372 if !mail_subject or mail_subject.blank?
372 if !mail_subject or mail_subject.blank?
373 flash[:notice] = 'You entered an empty mail subject.'
373 flash[:notice] = 'You entered an empty mail subject.'
374 redirect_to :action => 'mass_mailing' and return
374 redirect_to :action => 'mass_mailing' and return
375 end
375 end
376
376
377 mail_body = params[:email_body]
377 mail_body = params[:email_body]
378 if !mail_body or mail_body.blank?
378 if !mail_body or mail_body.blank?
379 flash[:notice] = 'You entered an empty mail body.'
379 flash[:notice] = 'You entered an empty mail body.'
380 redirect_to :action => 'mass_mailing' and return
380 redirect_to :action => 'mass_mailing' and return
381 end
381 end
382
382
383 note = []
383 note = []
384 users = []
384 users = []
385 lines.split("\n").each do |line|
385 lines.split("\n").each do |line|
386 user = User.find_by_login(line.chomp)
386 user = User.find_by_login(line.chomp)
387 if user
387 if user
388 send_mail(user.email, mail_subject, mail_body)
388 send_mail(user.email, mail_subject, mail_body)
389 note << user.login
389 note << user.login
390 end
390 end
391 end
391 end
392
392
393 flash[:notice] = 'User(s) ' + note.join(', ') +
393 flash[:notice] = 'User(s) ' + note.join(', ') +
394 ' were successfully modified. '
394 ' were successfully modified. '
You need to be logged in to leave comments. Login now