Show More
Commit Description:
[web] fix time.new, time.now to use gmtime...
Commit Description:
[web] fix time.new, time.now to use gmtime
git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@249 6386c4cd-e34a-4fa8-8920-d93eb39b512e
References:
File last commit:
Show/Diff file:
Action:
app/controllers/user_admin_controller.rb
| 158 lines
| 3.6 KiB
| text/x-ruby
| RubyLexer
|
|
r0 | class UserAdminController < ApplicationController | ||
|
r105 | before_filter :admin_authorization | ||
|
r0 | |||
def index | ||||
list | ||||
render :action => 'list' | ||||
end | ||||
# GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html) | ||||
verify :method => :post, :only => [ :destroy, :create, :update ], | ||||
:redirect_to => { :action => :list } | ||||
def list | ||||
|
r1 | @users = User.find(:all) | ||
|
r0 | end | ||
def show | ||||
@user = User.find(params[:id]) | ||||
end | ||||
def new | ||||
@user = User.new | ||||
end | ||||
def create | ||||
@user = User.new(params[:user]) | ||||
if @user.save | ||||
flash[:notice] = 'User was successfully created.' | ||||
redirect_to :action => 'list' | ||||
else | ||||
render :action => 'new' | ||||
end | ||||
end | ||||
|
r4 | def create_from_list | ||
lines = params[:user_list] | ||||
lines.split("\n").each do |line| | ||||
items = line.split | ||||
if items.length==5 | ||||
user = User.new | ||||
user.login = items[0] | ||||
user.full_name = "#{items[1]} #{items[2]}" | ||||
user.alias = items[3] | ||||
user.password = items[4] | ||||
user.password_confirmation = items[4] | ||||
user.save | ||||
end | ||||
end | ||||
redirect_to :action => 'list' | ||||
end | ||||
|
r0 | def edit | ||
@user = User.find(params[:id]) | ||||
end | ||||
def update | ||||
@user = User.find(params[:id]) | ||||
if @user.update_attributes(params[:user]) | ||||
flash[:notice] = 'User was successfully updated.' | ||||
redirect_to :action => 'show', :id => @user | ||||
else | ||||
render :action => 'edit' | ||||
end | ||||
end | ||||
def destroy | ||||
User.find(params[:id]).destroy | ||||
redirect_to :action => 'list' | ||||
end | ||||
def user_stat | ||||
@problems = Problem.find_available_problems | ||||
@users = User.find(:all) | ||||
@scorearray = Array.new | ||||
@users.each do |u| | ||||
ustat = Array.new | ||||
ustat[0] = u.login | ||||
|
r63 | ustat[1] = u.full_name | ||
|
r0 | @problems.each do |p| | ||
|
r35 | sub = Submission.find_last_by_user_and_problem(u.id,p.id) | ||
if (sub!=nil) and (sub.points!=nil) | ||||
|
r8 | ustat << [sub.points, (sub.points>=p.full_score)] | ||
|
r0 | else | ||
|
r8 | ustat << [0,false] | ||
|
r0 | end | ||
end | ||||
@scorearray << ustat | ||||
end | ||||
end | ||||
|
r106 | |||
def import | ||||
if params[:file]=='' | ||||
flash[:notice] = 'Error importing no file' | ||||
redirect_to :action => 'list' and return | ||||
end | ||||
import_from_file(params[:file]) | ||||
end | ||||
protected | ||||
def import_from_file(f) | ||||
data_hash = YAML.load(f) | ||||
@import_log = "" | ||||
country_data = data_hash[:countries] | ||||
site_data = data_hash[:sites] | ||||
user_data = data_hash[:users] | ||||
# import country | ||||
countries = {} | ||||
country_data.each_pair do |id,country| | ||||
c = Country.find_by_name(country[:name]) | ||||
if c!=nil | ||||
countries[id] = c | ||||
@import_log << "Found #{country[:name]}\n" | ||||
else | ||||
countries[id] = Country.new(:name => country[:name]) | ||||
countries[id].save | ||||
@import_log << "Created #{country[:name]}\n" | ||||
end | ||||
end | ||||
# import sites | ||||
sites = {} | ||||
site_data.each_pair do |id,site| | ||||
s = Site.find_by_name(site[:name]) | ||||
if s!=nil | ||||
@import_log << "Found #{site[:name]}\n" | ||||
else | ||||
s = Site.new(:name => site[:name]) | ||||
@import_log << "Created #{site[:name]}\n" | ||||
end | ||||
s.password = site[:password] | ||||
s.country = countries[site[:country_id]] | ||||
s.save | ||||
sites[id] = s | ||||
end | ||||
# import users | ||||
user_data.each_pair do |id,user| | ||||
u = User.find_by_login(user[:login]) | ||||
if u!=nil | ||||
@import_log << "Found #{user[:login]}\n" | ||||
else | ||||
u = User.new(:login => user[:login]) | ||||
@import_log << "Created #{user[:login]}\n" | ||||
end | ||||
u.full_name = user[:name] | ||||
u.password = user[:password] | ||||
u.country = countries[user[:country_id]] | ||||
u.site = sites[user[:site_id]] | ||||
u.save | ||||
end | ||||
end | ||||
|
r0 | end | ||