Description:
+ new list of user with remark
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r633:49e05926711f - - 1 file changed: 17 inserted, 5 deleted

@@ -1,289 +1,301
1 1 require 'csv'
2 2
3 3 class UserAdminController < ApplicationController
4 4
5 5 include MailHelperMethods
6 6
7 7 before_filter :admin_authorization
8 8
9 9 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
10 10 verify :method => :post, :only => [
11 11 :create, :create_from_list,
12 12 :update,
13 13 :manage_contest,
14 14 :bulk_mail
15 15 ],
16 16 :redirect_to => { :action => :list }
17 17
18 18 def index
19 19 @user_count = User.count
20 20 if params[:page] == 'all'
21 21 @users = User.all
22 22 @paginated = false
23 23 else
24 24 @users = User.paginate :page => params[:page]
25 25 @paginated = true
26 26 end
27 27 @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at']
28 28 @contests = Contest.enabled
29 29 end
30 30
31 31 def active
32 32 sessions = ActiveRecord::SessionStore::Session.where("updated_at >= ?", 60.minutes.ago)
33 33 @users = []
34 34 sessions.each do |session|
35 35 if session.data[:user_id]
36 36 @users << User.find(session.data[:user_id])
37 37 end
38 38 end
39 39 end
40 40
41 41 def show
42 42 @user = User.find(params[:id])
43 43 end
44 44
45 45 def new
46 46 @user = User.new
47 47 end
48 48
49 49 def create
50 50 @user = User.new(params[:user])
51 51 @user.activated = true
52 52 if @user.save
53 53 flash[:notice] = 'User was successfully created.'
54 54 redirect_to :action => 'index'
55 55 else
56 56 render :action => 'new'
57 57 end
58 58 end
59 59
60 60 def clear_last_ip
61 61 @user = User.find(params[:id])
62 62 @user.last_ip = nil
63 63 @user.save
64 64 redirect_to action: 'index', page: params[:page]
65 65 end
66 66
67 67 def create_from_list
68 68 lines = params[:user_list]
69 69
70 70 note = []
71 71
72 72 lines.split("\n").each do |line|
73 73 items = line.chomp.split(',')
74 74 if items.length>=2
75 75 login = items[0]
76 76 full_name = items[1]
77 + remark =''
78 + user_alias = ''
77 79
78 80 added_random_password = false
79 - if items.length>=3
81 + if items.length >= 3 and items[2].chomp(" ").length > 0;
80 82 password = items[2].chomp(" ")
81 - user_alias = (items.length>=4) ? items[3] : login
82 83 else
83 84 password = random_password
84 - user_alias = (items.length>=4) ? items[3] : login
85 - added_random_password = true
85 + add_random_password=true;
86 + end
87 +
88 + if items.length>= 4 and items[3].chomp(" ").length > 0;
89 + user_alias = items[3].chomp(" ")
90 + else
91 + user_alias = login
92 + end
93 +
94 + if items.length>=5
95 + remark = items[4].strip;
86 96 end
87 97
88 98 user = User.find_by_login(login)
89 99 if (user)
90 100 user.full_name = full_name
91 101 user.password = password
102 + user.remark = remark
92 103 else
93 104 user = User.new({:login => login,
94 105 :full_name => full_name,
95 106 :password => password,
96 107 :password_confirmation => password,
97 - :alias => user_alias})
108 + :alias => user_alias,
109 + :remark => remark})
98 110 end
99 111 user.activated = true
100 112 user.save
101 113
102 114 if added_random_password
103 115 note << "'#{login}' (+)"
104 116 else
105 117 note << login
106 118 end
107 119 end
108 120 end
109 121 flash[:notice] = 'User(s) ' + note.join(', ') +
110 122 ' were successfully created. ' +
111 123 '( (+) - created with random passwords.)'
112 124 redirect_to :action => 'index'
113 125 end
114 126
115 127 def edit
116 128 @user = User.find(params[:id])
117 129 end
118 130
119 131 def update
120 132 @user = User.find(params[:id])
121 133 if @user.update_attributes(user_params)
122 134 flash[:notice] = 'User was successfully updated.'
123 135 redirect_to :action => 'show', :id => @user
124 136 else
125 137 render :action => 'edit'
126 138 end
127 139 end
128 140
129 141 def destroy
130 142 User.find(params[:id]).destroy
131 143 redirect_to :action => 'index'
132 144 end
133 145
134 146 def user_stat
135 147 if params[:commit] == 'download csv'
136 148 @problems = Problem.all
137 149 else
138 150 @problems = Problem.available_problems
139 151 end
140 152 @users = User.includes(:contests, :contest_stat).where(enabled: true)
141 153 @scorearray = Array.new
142 154 @users.each do |u|
143 155 ustat = Array.new
144 156 ustat[0] = u
145 157 @problems.each do |p|
146 158 sub = Submission.find_last_by_user_and_problem(u.id,p.id)
147 159 if (sub!=nil) and (sub.points!=nil) and p and p.full_score
148 160 ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)]
149 161 else
150 162 ustat << [0,false]
151 163 end
152 164 end
153 165 @scorearray << ustat
154 166 end
155 167 if params[:commit] == 'download csv' then
156 168 csv = gen_csv_from_scorearray(@scorearray,@problems)
157 169 send_data csv, filename: 'last_score.csv'
158 170 else
159 171 render template: 'user_admin/user_stat'
160 172 end
161 173 end
162 174
163 175 def user_stat_max
164 176 if params[:commit] == 'download csv'
165 177 @problems = Problem.all
166 178 else
167 179 @problems = Problem.available_problems
168 180 end
169 181 @users = User.includes(:contests).includes(:contest_stat).all
170 182 @scorearray = Array.new
171 183 #set up range from param
172 184 since_id = params.fetch(:since_id, 0).to_i
173 185 until_id = params.fetch(:until_id, 0).to_i
174 186 @users.each do |u|
175 187 ustat = Array.new
176 188 ustat[0] = u
177 189 @problems.each do |p|
178 190 max_points = 0
179 191 Submission.find_in_range_by_user_and_problem(u.id,p.id,since_id,until_id).each do |sub|
180 192 max_points = sub.points if sub and sub.points and (sub.points > max_points)
181 193 end
182 194 ustat << [(max_points.to_f*100/p.full_score).round, (max_points>=p.full_score)]
183 195 end
184 196 @scorearray << ustat
185 197 end
186 198
187 199 if params[:commit] == 'download csv' then
188 200 csv = gen_csv_from_scorearray(@scorearray,@problems)
189 201 send_data csv, filename: 'max_score.csv'
190 202 else
191 203 render template: 'user_admin/user_stat'
192 204 end
193 205 end
194 206
195 207 def import
196 208 if params[:file]==''
197 209 flash[:notice] = 'Error importing no file'
198 210 redirect_to :action => 'index' and return
199 211 end
200 212 import_from_file(params[:file])
201 213 end
202 214
203 215 def random_all_passwords
204 216 users = User.all
205 217 @prefix = params[:prefix] || ''
206 218 @non_admin_users = User.find_non_admin_with_prefix(@prefix)
207 219 @changed = false
208 220 if request.request_method == 'POST'
209 221 @non_admin_users.each do |user|
210 222 password = random_password
211 223 user.password = password
212 224 user.password_confirmation = password
213 225 user.save
214 226 end
215 227 @changed = true
216 228 end
217 229 end
218 230
219 231 # contest management
220 232
221 233 def contests
222 234 @contest, @users = find_contest_and_user_from_contest_id(params[:id])
223 235 @contests = Contest.enabled
224 236 end
225 237
226 238 def assign_from_list
227 239 contest_id = params[:users_contest_id]
228 240 org_contest, users = find_contest_and_user_from_contest_id(contest_id)
229 241 contest = Contest.find(params[:new_contest][:id])
230 242 if !contest
231 243 flash[:notice] = 'Error: no contest'
232 244 redirect_to :action => 'contests', :id =>contest_id
233 245 end
234 246
235 247 note = []
236 248 users.each do |u|
237 249 u.contests = [contest]
238 250 note << u.login
239 251 end
240 252 flash[:notice] = 'User(s) ' + note.join(', ') +
241 253 " were successfully reassigned to #{contest.title}."
242 254 redirect_to :action => 'contests', :id =>contest.id
243 255 end
244 256
245 257 def add_to_contest
246 258 user = User.find(params[:id])
247 259 contest = Contest.find(params[:contest_id])
248 260 if user and contest
249 261 user.contests << contest
250 262 end
251 263 redirect_to :action => 'index'
252 264 end
253 265
254 266 def remove_from_contest
255 267 user = User.find(params[:id])
256 268 contest = Contest.find(params[:contest_id])
257 269 if user and contest
258 270 user.contests.delete(contest)
259 271 end
260 272 redirect_to :action => 'index'
261 273 end
262 274
263 275 def contest_management
264 276 end
265 277
266 278 def manage_contest
267 279 contest = Contest.find(params[:contest][:id])
268 280 if !contest
269 281 flash[:notice] = 'You did not choose the contest.'
270 282 redirect_to :action => 'contest_management' and return
271 283 end
272 284
273 285 operation = params[:operation]
274 286
275 287 if not ['add','remove','assign'].include? operation
276 288 flash[:notice] = 'You did not choose the operation to perform.'
277 289 redirect_to :action => 'contest_management' and return
278 290 end
279 291
280 292 lines = params[:login_list]
281 293 if !lines or lines.blank?
282 294 flash[:notice] = 'You entered an empty list.'
283 295 redirect_to :action => 'contest_management' and return
284 296 end
285 297
286 298 note = []
287 299 users = []
288 300 lines.split("\n").each do |line|
289 301 user = User.find_by_login(line.chomp)
You need to be logged in to leave comments. Login now