diff --git a/Gemfile b/Gemfile index 7326febc3..854267ee8 100644 --- a/Gemfile +++ b/Gemfile @@ -75,6 +75,7 @@ group :development, :test do gem "test-unit", "~>3.0" end gem 'rspec-rails', '~> 3.0' + gem 'factory_girl_rails' end # Gems used only for assets and not required diff --git a/app/controllers/contests_controller.rb b/app/controllers/contests_controller.rb index f6ff98d30..9a6882075 100644 --- a/app/controllers/contests_controller.rb +++ b/app/controllers/contests_controller.rb @@ -29,14 +29,35 @@ class ContestsController < ApplicationController member = ContestMember.new(:user_id => User.current.id) @contest.contest_members << member - ContestMemberRole.create(:contest_member_id => member.id, :role_id => 14) + 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 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 + private def find_contest diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index cfc87d169..6e77aa0c4 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -1,6 +1,63 @@ class WorksController < ApplicationController + layout "base_contests" + before_filter :find_contest, :only => [:index,:new,:create] + before_filter :find_contestwork, :only => [:edit,:update,:destroy,:score_rule_set,:alert_open_student_works,:open_student_works,:set_score_open,:alert_score_open_modal] + before_filter :admin_of_contest, :only => [:new, :create, :edit, :update, :destroy,:score_rule_set,:alert_open_student_works,:open_student_works] + before_filter :member_of_contest, :only => [:index] + def index end + + def show + + end + + def new + + end + + def create + + end + + def update + + end + + def edit + + end + + def destroy + + end + + private + + #获取竞赛 + def find_contest + @contest = Contest.find params[:contest] + rescue + render_404 + end + #获取题目 + def find_contestwork + @contestwork = Work.find params[:id] + @work_detail_group = @contestwork.work_detail_group + @contest = @contestwork.contest + rescue + render_404 + end + #是不是管理员 + def admin_of_contest + render_403 unless User.current.admin_of_contest?(@contest) || User.current.admin? + end + + #当前用户是不是竞赛的成员 + def member_of_contest + render_403 unless @contest.is_public==1 || User.current.member_of_contest?(@contest) || User.current.admin? + end + end diff --git a/app/models/applied_contest.rb b/app/models/applied_contest.rb new file mode 100644 index 000000000..184368c72 --- /dev/null +++ b/app/models/applied_contest.rb @@ -0,0 +1,17 @@ +class AppliedContest < ActiveRecord::Base + belongs_to :contest + belongs_to :user + #status :0 新建 1 已批准 2 拒绝 + attr_accessible :role, :status, :contest_id, :user_id + + has_many :applied_messages, :class_name => 'AppliedMessage', :as => :applied, :dependent => :destroy + after_create :send_appliled_message + + # 仅仅给项目管理人员发送消息 + def send_appliled_message + self.contest.managers.each do |member| + self.applied_messages << AppliedMessage.new(:user_id => member.user_id, :status => 0, :viewed => false, :applied_user_id => self.user_id, :role => self.role) + end + # end + end +end diff --git a/app/models/contest.rb b/app/models/contest.rb index 4a8d35262..20f20873f 100644 --- a/app/models/contest.rb +++ b/app/models/contest.rb @@ -18,7 +18,7 @@ class Contest < ActiveRecord::Base validates_format_of :name,:with =>/^[^ ]+[a-zA-Z0-9_\u4e00-\u9fa5\s\S]*$/ before_destroy :delete_all_members - after_create :act_as_contest_activity + #after_create :act_as_contest_activity scope :visible, lambda {|*args| where(Contest.where("is_delete =?", 0).visible_condition(args.shift || User.current, *args)) } diff --git a/app/models/contest_member.rb b/app/models/contest_member.rb index b16ef4cfb..bbc437543 100644 --- a/app/models/contest_member.rb +++ b/app/models/contest_member.rb @@ -10,7 +10,7 @@ class ContestMember < ActiveRecord::Base validates_presence_of :principal validates_uniqueness_of :user_id, :scope => [:contest_id] - validate :validate_role + #validate :validate_role def role end diff --git a/app/models/contest_member_role.rb b/app/models/contest_member_role.rb index a702c52c8..5d0a2b20f 100644 --- a/app/models/contest_member_role.rb +++ b/app/models/contest_member_role.rb @@ -7,7 +7,7 @@ class ContestMemberRole < ActiveRecord::Base after_destroy :remove_member_if_empty validates_presence_of :role - validate :validate_role_member + #validate :validate_role_member def validate_role_member errors.add :role_id, :invalid if role && !role.member? diff --git a/app/services/contests_service.rb b/app/services/contests_service.rb new file mode 100644 index 000000000..00b634b7c --- /dev/null +++ b/app/services/contests_service.rb @@ -0,0 +1,45 @@ +#coding=utf-8 +class ContestsService + include ApplicationHelper + include ContestsHelper + include ApiHelper + include ActionView::Helpers::DateHelper + + #多个角色加入竞赛 + def join_contest_roles params,current_user + contest = Contest.find_by_invite_code(params[:invite_code]) if params[:invite_code] + go_contestgroup_flag = 0 + contest_id = 0 + + @state = 10 + if contest + if contest[:is_delete] == 1 + @state = 11 + elsif contest[:invite_code_halt] == 1 + @state = 14 + else + if current_user.member_of_contest?(contest) #如果已经是成员 + @state = 3 + else + if params[:invite_code].present? + role_ids = params[:role] + role_str = role_ids.join(",") + + #如果已经发送过消息了,那么就要给个提示 + if AppliedContest.where(:contest_id => contest.id, :user_id => current_user.id, :role => role_str, :status => 0).count != 0 + @state = 7 + else + AppliedContest.create(:contest_id => contest.id, :user_id => current_user.id, :role => role_str, :status => 0) + @state = 6 + end + else + @state = 1 + end + end + end + else + @state = 4 + end + {:state => @state, :contest => contest, :go_contestgroup_flag => go_contestgroup_flag, :contest_id => contest_id} + end +end \ No newline at end of file diff --git a/app/views/contests/_join_contest_box.html.erb b/app/views/contests/_join_contest_box.html.erb new file mode 100644 index 000000000..40161d03d --- /dev/null +++ b/app/views/contests/_join_contest_box.html.erb @@ -0,0 +1,77 @@ +
加入成功
'+ + '确 定密码错误
'+ + '确 定您已经加入了竞赛
'+ + '确 定您输入的邀请码错误
'+ + '确 定您还未登录
'+ + '确 定申请成功,请等待审核
'+ + '确 定您已经发送过申请了,请耐心等待
'+ + '确 定您已经是该班级的教师了
'+ + '确 定您已经是该班级的教辅了
'+ + '确 定您已经是该竞赛的管理员了
'+ + '确 定该竞赛已归档,请联系老师
'+ + '确 定您已经发送过申请了,请耐心等待
'+ + '确 定申请成功,请等待审核
'+ + '确 定此二维码已停用,请与管理员联系
'+ + '确 定未知错误,请稍后再试
'+ + '确 定