socialforge/app/controllers/contest_members_controller.rb

152 lines
5.4 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#encoding=utf-8
class ContestMembersController < ApplicationController
helper :users
helper :contest_members
helper :contests
before_filter :find_contest_member, :only => [:update, :destroy]
def create
@contest = Contest.find(params[:contest])
if params[:membership] && @contest
@create_member_error_messages = "角色不能留空" unless params[:membership][:role_ids]
@create_member_error_messages = "用户不能留空" unless params[:membership][:user_ids]
members = []
if params[:membership][:user_ids] && params[:membership][:role_ids]
attrs = params[:membership].dup
user_ids = attrs.delete(:user_ids)
role_ids = params[:membership][:role_ids]
user_ids.each do |user_id|
member = ContestMember.new(:user_id => user_id)
members << member
contest_member_roles = member.contest_member_roles
if role_ids.include?('14') && role_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 => role_ids[0].to_i)
end
if role_ids.include?('15')
ContestantForContest.create(:student_id => user_id, :contest_id => @contest.id)
end
#给新成员和老师发送加入课程的消息发送者id放在CourseMessage的course_message_id字段中
#course_message_type设置为JoinCourse
#status = 0 表示给学生发status = 1表示给老师发
course_join = ContestMessage.new(:user_id =>user_id, :contest_message_id=>User.current.id,:contest_id => @contest.id,:contest_message_type=>"JoinContest", :content => role_ids, :viewed => false, :status => 0)
course_join.save
#ContestMessage.create(:user_id => User.current.id, :contest_message_id => user_id, :contest_message_id => @contest.id, :contest_message_type => "JoinContest",:content => role_ids, :viewed => false, :status => 1)
end
end
@contest.contest_members << members
members = @contest.member_principals.includes(:roles, :principal).all.sort
end
@roles = Role.where("id in (13, 14, 15)")
respond_to do |format|
format.html { redirect_to_settings_in_courses }
format.js { @members = members;}
format.api {
@member = members.first
if @member.valid?
render :action => 'show', :status => :created, :location => membership_url(@member)
else
render_validation_errors(@member)
end
}
end
end
def show
end
def contest_member_autocomplete
@contest = Contest.find(params[:contest])
@flag = params[:flag] || false
respond_to do |format|
format.js
end
end
def update
if params[:membership]
new_role_ids = params[:membership][:role_ids]
if new_role_ids.size != 0 && new_role_ids != @contest_member.contest_member_roles.map{|ro| ro.role_id.to_s}
destroy_member_roles = @contest_member.contest_member_roles
if destroy_member_roles.any?
destroy_member_roles.each(&:destroy)
end
contest_member_roles = @contest_member.contest_member_roles
if new_role_ids.include?('14') && new_role_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 => new_role_ids[0].to_i)
end
joined = ContestantForContest.where('student_id = ? and contest_id = ?', @contest_member.user_id, @contest.id)
joined.each do |join|
join.destroy
end
if new_role_ids.include?('15')
ContestantForContest.create(:student_id => @contest_member.user_id, :contest_id => @contest.id)
end
end
@contest_member.save
@members = @contest.member_principals.includes(:roles, :principal).all.sort
@roles = Role.where("id in (13, 14, 15)")
@member = @contest.contest_members.new
end
saved = @member.save
respond_to do |format|
format.html { redirect_to settings_contest_path(@contest) }
format.js
format.api {
if saved
render_api_ok
else
render_validation_errors(@member)
end
}
end
end
def destroy
if request.delete? && @contest_member.deletable?
@contest_member.destroy
joined = ContestantForContest.where('student_id = ? and contest_id = ?', @contest_member.user_id, @contest.id)
joined.each do |join|
join.destroy
end
@roles = Role.where("id in (13, 14, 15)")
@members = @contest.member_principals.includes(:roles, :principal).all.sort
#移出课程发送消息
ContestMessage.create(:user_id => @contest_member.user_id, :contest_id => @contest.id, :contest_message_type => "RemoveFromContest", :viewed => false, :contest_message_id => User.current.id)
end
respond_to do |format|
format.html { redirect_to settings_contest_path(@contest) }
format.js
format.api {
if @contest_member.destroyed?
render_api_ok
else
head :unprocessable_entity
end
}
end
end
private
def find_contest_member
@contest_member = ContestMember.find params[:id]
@contest = @contest_member.contest
end
end