#encoding=utf-8 class ContestsController < ApplicationController include ContestsHelper helper :contest_members helper :users before_filter :find_contest, :only => [:show, :settings, :update, :destroy, :contest_activities, :search_member, :private_or_public, :switch_role, :set_invite_code_halt, :renew] before_filter :is_logged, :only => [:index, :new, :create] before_filter :is_admin?, :only => [:settings, :set_invite_code_halt, :destroy] before_filter :is_member?, :only => [:show, :contest_activities] layout "base_contests" def show @left_nav_type = 1 respond_to do |format| format.js format.html end end def contest_activities #更新创建课程消息状态 contest_request_messages = ContestMessage.where(:user_id => User.current.id, :contest_id => @contest.id, :contest_message_type => "ContestRequestDealResult", :viewed => false) contest_request_messages.update_all(:viewed => true) contest_activities = @contest.contest_activities @page = params[:page] ? params[:page].to_i + 1 : 0 if params[:type].present? case params[:type] when "work" @contest_activities = contest_activities.where("contest_act_type = 'Work'").order('updated_at desc') when "news" @contest_activities = contest_activities.where("contest_act_type = 'News'").order('updated_at desc') when "message" @contest_activities = contest_activities.where("contest_act_type = 'Message'").order('updated_at desc') when "poll" @contest_activities = contest_activities.where("contest_act_type = 'Poll'").order('updated_at desc') when "attachment" @contest_activities = contest_activities.where("contest_act_type = 'Attachment'").order('updated_at desc') when "journalsForMessage" @contest_activities = contest_activities.where("contest_act_type = 'JournalsForMessage'").order('updated_at desc') when "news" @contest_activities = contest_activities.where("contest_act_type = 'News'").order('updated_at desc') else @contest_activities = contest_activities.order('updated_at desc') end else @contest_activities = contest_activities.order('updated_at desc') end @contest_activities_count = @contest_activities.count @contest_activities = @contest_activities.limit(10).offset(@page * 10) @type = params[:type] @left_nav_type = 2 respond_to do |format| format.js format.html format.api end end def new if User.current.login? @contest = Contest.new render :layout => 'new_base' else redirect_to signin_url end end def create @contest = Contest.new @contest.name = params[:contest][:name] params[:contest][:is_public] ? @contest.is_public = 1 : @contest.is_public = 0 @contest.user_id = User.current.id if @contest && @contest.save #unless User.current.admin? member = ContestMember.new(:user_id => User.current.id) @contest.contest_members << member ContestMemberRole.create(:contest_member_id => member.id, :role_id => 13) @contest.contest_acts << ContestActivity.new(:user_id => @contest.user_id,:contest_id => @contest.id) respond_to do |format| format.html {redirect_to contest_url(@contest)} end end end def update @contest.name = params[:contest][:name] @contest.description = params[:contest][:description] # @project.organization_id = params[:organization_id] params[:contest][:is_public] == "on" ? @contest.is_public = 1 : @contest.is_public = 0 @contest.save_attachments(params[:attachments]) begin if @contest.save respond_to do |format| format.html{redirect_to contest_path(@contest)} end end rescue Exception => e @message = e.message end end def destroy @contest.update_attributes(:is_delete => true) end def private_or_public @contest.update_attributes(:is_public => !@contest.is_public) respond_to do |format| format.js end end #设置竞赛公开或私有 def set_contest_attribute contest_id = params[:contest].to_i @contest = Contest.find(contest_id) @user = User.find(params[:user_id]) @contest.is_public? ? @contest.update_attribute(:is_public, false) : @contest.update_attribute(:is_public, true) respond_to do |format| format.js end end def settings if params[:tab] && params[:tab] == 'boards' @tab = 2 else @tab = 0 end @member ||= @contest.contest_members.new @roles = Role.where("id in (13, 14, 15)") @members = @contest.member_principals.includes(:roles, :principal).all.sort end def join_contest end def join_contest_multi_role if User.current.logged? cs = ContestsService.new @user = User.current join = cs.join_contest_roles params,@user @state = join[:state] @contest = join[:contest] else @state = 5 #未登录 end @object_id = @contest.id if @contest respond_to do |format| format.js end end #处理加入竞赛成为管理员、评委、参赛者的请求 #status 1 同意 2 拒绝 def dealwith_apply_request @msg = AppliedMessage.find(params[:msg_id]) #AppliedMessage role 13 管理员 14 评委 15 参赛者 applied_contest = @msg.applied apply_user = User.find(applied_contest.user_id) ids = applied_contest.role.split(",") # [@msg.content] msg content保存的是申请的职位角色 integer_ids = [] ids.each do |role_id| integer_ids << role_id.to_i end case params[:agree] when 'Y' if ContestMember.where(:user_id => apply_user.id, :contest_id => applied_contest.contest_id).first.nil? member = ContestMember.new(:user_id => apply_user.id) Contest.find(applied_contest.contest_id).contest_members << member else member = ContestMember.where(:user_id => apply_user.id, :contest_id => applied_contest.contest_id).first end contest_member_roles = member.contest_member_roles if integer_ids.include?(14) && integer_ids.include?(13) contest_member_roles << ContestMemberRole.new(:role_id => 13) contest_member_roles << ContestMemberRole.new(:role_id => 14, :is_current => 0) else contest_member_roles << ContestMemberRole.new(:role_id => integer_ids[0]) end ContestMessage.create(:user_id => apply_user.id, :contest_id => applied_contest.contest_id, :viewed => false,:contest_message_id=>User.current.id,:content=>applied_contest.role,:contest_message_type=>'ContestRequestDealResult',:status=>1) applied_contest.applied_messages.update_all(:status => 1, :viewed => 1, :applied_user_id => User.current.id) @msg.update_attributes(:status => 1, :viewed => 1, :applied_user_id => User.current.id) applied_contest.update_attributes(:status => 1) if integer_ids.include?(15) ContestantForContest.create(:student_id => apply_user.id, :contest_id =>applied_contest.contest_id) end when 'N' ContestMessage.create(:user_id => apply_user.id, :contest_id => applied_contest.contest_id, :viewed => false,:contest_message_id=>User.current.id,:content=>applied_contest.role,:contest_message_type=>'ContestRequestDealResult',:status=>2) applied_contest.applied_messages.update_all(:status => 2, :viewed => 1, :applied_user_id => User.current.id) @msg.update_attributes(:status => 2, :viewed => 1, :applied_user_id => User.current.id) applied_contest.update_attributes(:status => 2) end respond_to do |format| format.js end end def switch_role members = @contest.contest_members.where("user_id = #{params[:user_id]}") unless members.blank? curr_role = ContestMemberRole.find_by_contest_member_id_and_role_id(members.first.id, params[:curr_role]) tar_role = ContestMemberRole.find_by_contest_member_id_and_role_id(members.first.id, params[:tar_role]) unless (curr_role.nil? || tar_role.nil?) curr_role.update_column('is_current', 0) tar_role.update_column('is_current', 1) end end redirect_to contest_path(@contest) end def search_member if User.current.admin_of_contest?(@contest) || User.current.admin? q = "#{params[:name].strip}" @roles = Role.givable.all[11..13] if q.nil? || q == "" @members = @contest.member_principals.includes(:roles, :principal).all.sort else @members = searchmember_by_name(@contest.member_principals.includes(:roles, :principal).all.sort,q) end else render_403 end end # 邀请码停用/启用 def set_invite_code_halt if User.current.admin_of_contest?(@contest) || User.current.admin? @contest.update_attribute('invite_code_halt', @contest.invite_code_halt == 0 ? 1 : 0) end end # 恢复已删除的竞赛 def renew if User.current.admin? @contest.update_attributes(:is_delete => false) redirect_to contest_path(@contest) else return 404 end end private def find_contest if params[:id].to_i < 780 render_403 return end @contest = Contest.find(params[:id]) if @contest.is_delete and !User.current.admin? render_404 return end rescue ActiveRecord::RecordNotFound render_404 end def is_logged redirect_to signin_path unless User.current.logged? end def is_admin? unless User.current.admin_of_contest?(@contest) || User.current.admin? render_403 return end end def is_member? if User.current.member_of_contest?(@contest) || User.current.admin? || @contest.is_public else render_403 return end end end