# HG changeset patch # User Jittat Fakcharoenphol # Date 2010-03-03 18:46:52 # Node ID f8e3b2e72e4a5f0be16f1ff0a0f73da31234b32a # Parent 973f76ab730ea7fb5234e1b8bf9fd9c549049ac5 manages users in contests diff --git a/app/controllers/user_admin_controller.rb b/app/controllers/user_admin_controller.rb --- a/app/controllers/user_admin_controller.rb +++ b/app/controllers/user_admin_controller.rb @@ -16,6 +16,7 @@ def list @users = User.find(:all) @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at'] + @contests = Contest.all(:conditions => {:enabled => true}) end def active @@ -150,6 +151,68 @@ @changed = true end end + + # contest management + + def add_to_contest + user = User.find(params[:id]) + contest = Contest.find(params[:contest_id]) + if user and contest + user.contests << contest + end + redirect_to :action => 'list' + end + + def remove_from_contest + user = User.find(params[:id]) + contest = Contest.find(params[:contest_id]) + if user and contest + user.contests.delete(contest) + end + redirect_to :action => 'list' + end + + def contest_management + end + + def manage_contest + contest = Contest.find(params[:contest][:id]) + if !contest + flash[:notice] = 'You did not choose the contest.' + redirect_to :action => 'contest_management' and return + end + + operation = params[:operation] + + if operation!='add' and operation!='remove' + flash[:notice] = 'You did not choose the operation to perform.' + redirect_to :action => 'contest_management' and return + end + + lines = params[:login_list] + if !lines or lines.blank? + flash[:notice] = 'You entered an empty list.' + redirect_to :action => 'contest_management' and return + end + + note = [] + lines.split("\n").each do |line| + puts line + user = User.find_by_login(line.chomp) + puts user + if user + if operation=='add' + user.contests << contest + else + user.contests.delete(contest) + end + note << user.login + end + end + flash[:notice] = 'User(s) ' + note.join(', ') + + ' were successfully modified. ' + redirect_to :action => 'contest_management' + end # admin management diff --git a/app/models/problem.rb b/app/models/problem.rb --- a/app/models/problem.rb +++ b/app/models/problem.rb @@ -1,7 +1,7 @@ class Problem < ActiveRecord::Base belongs_to :description - has_and_belongs_to_many :contests + has_and_belongs_to_many :contests, :uniq => true has_many :test_pairs, :dependent => :delete_all validates_presence_of :name diff --git a/app/models/user.rb b/app/models/user.rb --- a/app/models/user.rb +++ b/app/models/user.rb @@ -21,7 +21,7 @@ belongs_to :site belongs_to :country - has_and_belongs_to_many :contests + has_and_belongs_to_many :contests, :uniq => true named_scope :activated_users, :conditions => {:activated => true} diff --git a/app/views/main/list.html.haml b/app/views/main/list.html.haml --- a/app/views/main/list.html.haml +++ b/app/views/main/list.html.haml @@ -32,15 +32,16 @@ = render :partial => 'problem', :collection => @problems - else - @contest_problems.each do |cp| - %h2{:class =>'contest-title'} - = "#{cp[:contest] ? cp[:contest].title : 'Public problems'}" - %table.info - %tr.info-head - %th - %th Tasks - %th # of sub(s) - %th Results - = render :partial => 'problem', :collection => cp[:problems] + - if cp[:problems].length > 0 + %h2{:class =>'contest-title'} + = "#{cp[:contest] ? cp[:contest].title : 'Public problems'}" + %table.info + %tr.info-head + %th + %th Tasks + %th # of sub(s) + %th Results + = render :partial => 'problem', :collection => cp[:problems] %hr/ diff --git a/app/views/user_admin/admin.html.haml b/app/views/user_admin/admin.html.haml --- a/app/views/user_admin/admin.html.haml +++ b/app/views/user_admin/admin.html.haml @@ -1,7 +1,7 @@ %h1 Administrators -%table - %tr +%table{:class => 'info'} + %tr{:class => 'info-head'} %th # %th Login %th Full name diff --git a/app/views/user_admin/contest_management.html.haml b/app/views/user_admin/contest_management.html.haml new file mode 100644 --- /dev/null +++ b/app/views/user_admin/contest_management.html.haml @@ -0,0 +1,16 @@ +%h1 Bulk edit users in contests + +- form_tag :action => 'manage_contest' do + List users' login below; one per line. + %br/ + = text_area_tag 'login_list', nil, :rows => 25, :cols => 80 + %br/ + You want to + = select(nil,"operation",[['add users to','add'],['remove users from','remove']]) + contest + = select("contest","id",Contest.all.collect {|c| [c.title, c.id]}) +      + = submit_tag "Perform action", :confirm => 'Are you sure?' + +%hr/ += link_to '[go back to index]', :action => 'index' diff --git a/app/views/user_admin/list.rhtml b/app/views/user_admin/list.rhtml --- a/app/views/user_admin/list.rhtml +++ b/app/views/user_admin/list.rhtml @@ -33,27 +33,51 @@ <%= link_to '[View administrators]', :action => 'admin' %> <%= link_to '[Random passwords]', :action => 'random_all_passwords' %> <%= link_to '[View active users]', :action => 'active' %> + <% if Configuration.multicontests? %> + <%= link_to '[Manage bulk users in contests]', :action => 'contest_management' %> + <% end %> - - - <% for column in User.content_columns %> - <% if !@hidden_columns.index(column.name) %> - +
<%= column.human_name %>
+ + <% for column in User.content_columns %> + <% if !@hidden_columns.index(column.name) %> + + <% end %> <% end %> - <% end %> + + + + <% if Configuration.multicontests? %> + + + <% end %> <% for user in @users %> - - <% for column in User.content_columns %> - <% if !@hidden_columns.index(column.name) %> - + "> + <% for column in User.content_columns %> + <% if !@hidden_columns.index(column.name) %> + + <% end %> <% end %> - <% end %> + <% if Configuration.multicontests? %> + + + <% end %> <% end %>
<%= column.human_name %>ContestsOther enabled contests
<%=h user.send(column.name) %>
<%=h user.send(column.name) %><%= link_to 'Show', :action => 'show', :id => user %> <%= link_to 'Edit', :action => 'edit', :id => user %> <%= link_to 'Destroy', { :action => 'destroy', :id => user }, :confirm => 'Are you sure?', :method => :post %> + <% user.contests.each do |contest| %> + <%= contest.name %> [<%= link_to 'x', :action => 'remove_from_contest', :id => user.id, :contest_id => contest.id %>] + <% end %> + + <% @contests.each do |contest| %> + <% if not user.contests.all.find {|c| c.id==contest.id } %> + <%= contest.name %> [<%= link_to '+', :action => 'add_to_contest', :id => user.id, :contest_id => contest.id %>] + <% end %> + <% end %> +