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 @@ +
+
+

欢迎加入竞赛

+ +
+
+
+ <%= form_tag({:controller => 'contests', :action => 'join_contest_multi_role'}, :remote => true, :method => :post, :id => 'new-watcher-form') do %> + + <% end %> +
+
+ \ No newline at end of file diff --git a/app/views/contests/join_contest.js.erb b/app/views/contests/join_contest.js.erb new file mode 100644 index 000000000..fe124ea36 --- /dev/null +++ b/app/views/contests/join_contest.js.erb @@ -0,0 +1,2 @@ +var htmlvalue = "<%= escape_javascript(render :partial => 'join_contest_box') %>"; +pop_box_new(htmlvalue,460,220); diff --git a/app/views/contests/join_contest_multi_role.js.erb b/app/views/contests/join_contest_multi_role.js.erb new file mode 100644 index 000000000..8c9f5063d --- /dev/null +++ b/app/views/contests/join_contest_multi_role.js.erb @@ -0,0 +1,85 @@ + +<% if @state %> +<% if @state == 0 %> +var htmlvalue = '

提示

'+ + '

加入成功

'+ + '确  定
'; +pop_box_new(htmlvalue, 300, 140); +<% elsif @state == 1 %> +var htmlvalue = '

提示

'+ + '

密码错误

'+ + '确  定
'; +pop_box_new(htmlvalue, 300, 140); +<% elsif @state == 3 %> +var htmlvalue = '

提示

'+ + '

您已经加入了竞赛

'+ + '确  定
'; +pop_box_new(htmlvalue, 300, 140); +<% elsif @state == 4 %> +var htmlvalue = '

提示

'+ + '

您输入的邀请码错误

'+ + '确  定
'; +pop_box_new(htmlvalue, 300, 140); +<% elsif @state == 5 %> +var htmlvalue = '

提示

'+ + '

您还未登录

'+ + '确  定
'; +pop_box_new(htmlvalue, 300, 140); +<% elsif @state == 6 %> +var htmlvalue = '

提示

'+ + '

申请成功,请等待审核

'+ + '确  定
'; +pop_box_new(htmlvalue, 300, 140); +<% elsif @state == 7%> +var htmlvalue = '

提示

'+ + '

您已经发送过申请了,请耐心等待

'+ + '确  定
'; +pop_box_new(htmlvalue, 300, 140); +<% elsif @state == 8%> +var htmlvalue = '

提示

'+ + '

您已经是该班级的教师了

'+ + '确  定
'; +pop_box_new(htmlvalue, 300, 140); +<% elsif @state == 9%> +var htmlvalue = '

提示

'+ + '

您已经是该班级的教辅了

'+ + '确  定
'; +pop_box_new(htmlvalue, 300, 140); +<% elsif @state == 10%> +var htmlvalue = '

提示

'+ + '

您已经是该竞赛的管理员了

'+ + '确  定
'; +pop_box_new(htmlvalue, 300, 140); +<% elsif @state == 11%> +var htmlvalue = '

提示

'+ + '

该竞赛已归档,请联系老师

'+ + '确  定
'; +pop_box_new(htmlvalue, 300, 140); +<% elsif @state == 12 %> +var htmlvalue = '

提示

'+ + '

您已经发送过申请了,请耐心等待

'+ + '确  定
'; +pop_box_new(htmlvalue, 300, 140); +<% elsif @state == 13 %> +var htmlvalue = '

提示

'+ + '

申请成功,请等待审核

'+ + '确  定
'; +pop_box_new(htmlvalue, 300, 140); +<% elsif @state == 14 %> +var htmlvalue = '

提示

'+ + '

此二维码已停用,请与管理员联系

'+ + '确  定
'; +pop_box_new(htmlvalue, 300, 140); +<% else %> +var htmlvalue = '

提示

'+ + '

未知错误,请稍后再试

'+ + '确  定
'; +pop_box_new(htmlvalue, 300, 140); +<% end %> +<% end %> +function refresh_current_contest(){ + hideModal(); + <% if @contest %> + window.location.href= "<%=contest_path(@contest.id) %>"; + <% end %> +} diff --git a/app/views/layouts/_homepage_left_contest_list.html.erb b/app/views/layouts/_homepage_left_contest_list.html.erb index 7c96ac804..d8635c288 100644 --- a/app/views/layouts/_homepage_left_contest_list.html.erb +++ b/app/views/layouts/_homepage_left_contest_list.html.erb @@ -1,7 +1,7 @@ -<% all_count = @user.contests.visible.where("is_delete =?", 0).count%> +<% all_count = @user.favorite_contests.visible.where("is_delete =?", 0).count%>
<% if !contests.empty? %> @@ -12,6 +12,6 @@ \ No newline at end of file diff --git a/app/views/layouts/_user_contests.html.erb b/app/views/layouts/_user_contests.html.erb index 4e0b5aa94..553cc3e1c 100644 --- a/app/views/layouts/_user_contests.html.erb +++ b/app/views/layouts/_user_contests.html.erb @@ -14,8 +14,8 @@ diff --git a/config/routes.rb b/config/routes.rb index 067662cd1..991ac8086 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -612,6 +612,7 @@ RedmineApp::Application.routes.draw do resources :contests do collection do get 'join_contest' + post 'join_contest_multi_role' end member do diff --git a/db/migrate/20161220062249_create_contests.rb b/db/migrate/20161220062249_create_contests.rb index a158af1de..13121efa0 100644 --- a/db/migrate/20161220062249_create_contests.rb +++ b/db/migrate/20161220062249_create_contests.rb @@ -20,7 +20,7 @@ class CreateContests < ActiveRecord::Migration end for i in 1 .. 779 - Contest.create(:user_id => 2, :name => 'test', :is_public => false) + Contest.create(:user_id => 2, :name => 'test', :is_public => false, :is_delete => true) end end end diff --git a/db/migrate/20161222063638_create_applied_contests.rb b/db/migrate/20161222063638_create_applied_contests.rb new file mode 100644 index 000000000..a7e77951c --- /dev/null +++ b/db/migrate/20161222063638_create_applied_contests.rb @@ -0,0 +1,14 @@ +class CreateAppliedContests < ActiveRecord::Migration + def change + create_table :applied_contests do |t| + t.references :contest + t.references :user + t.integer :role + t.integer :status + + t.timestamps + end + add_index :applied_contests, :contest_id + add_index :applied_contests, :user_id + end +end diff --git a/spec/factories/applied_contests.rb b/spec/factories/applied_contests.rb new file mode 100644 index 000000000..c52f31497 --- /dev/null +++ b/spec/factories/applied_contests.rb @@ -0,0 +1,8 @@ +FactoryGirl.define do + factory :applied_contest do + contest nil +user nil +role "" + end + +end diff --git a/spec/models/applied_contest_spec.rb b/spec/models/applied_contest_spec.rb new file mode 100644 index 000000000..aa150bacc --- /dev/null +++ b/spec/models/applied_contest_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe AppliedContest, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end