加入竞赛

This commit is contained in:
cxt 2016-12-22 15:08:15 +08:00
parent 1d23b2cf65
commit 4c5a2aa40b
19 changed files with 345 additions and 12 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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)) }

View File

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

View File

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

View File

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

View File

@ -0,0 +1,77 @@
<div id="sy_popup_box" style="width:460px;">
<div class="sy_popup_top">
<h3 class="fl">欢迎加入竞赛</h3>
<a href="javascript:void(0);" class="sy_icons_close fr" onclick="hideModal()"></a>
<div class="cl"></div>
</div>
<div class="sy_popup_con">
<%= form_tag({:controller => 'contests', :action => 'join_contest_multi_role'}, :remote => true, :method => :post, :id => 'new-watcher-form') do %>
<ul class="sy_popup_add ">
<li>
<label>竞赛邀请码:</label>
<input class=" sy_input_txt fl" name="invite_code" placeholder="请输入四位竞赛邀请码">
<div class="cl"></div>
</li>
<li>
<label>身份:</label>
<input type="checkbox" name="role[]" value="13" id="join_course_role_13" class="ml5 mr5"/><span class="mr10">管理员</span>
<input type="checkbox" name="role[]" value="14" id="join_course_role_14" class="ml5 mr5"/><span class="mr10">评委</span>
<input type="checkbox" name="role[]" value="15" id="join_course_role_15" class="ml5 mr5"/><span class="mr10">参赛者</span>
<div class="cl"></div>
</li>
<p id="none_checked_notice" class="c_red none f12" style="margin-left: 42px;">请至少选择一个身份</p>
<li class="mt10">
<label>&nbsp;</label>
<a href="javascript:void(0);" class="sy_btn_blue fl" onclick="submit_join_course();">确&nbsp;&nbsp;定</a>
<a href="javascript:void(0);" class="sy_btn_grey fl ml20" onclick="hideModal()">取&nbsp;&nbsp;消</a>
<div class="cl"></div>
</li>
</ul>
<% end %>
</div>
</div>
<script>
function submit_join_course(){
if ($("input[name='role[]']:checked").length >= 1){
$("#none_checked_notice").hide();
$('#new-watcher-form').submit();
hideModal();
}else{
$("#none_checked_notice").show();
}
}
function disable_contestant_choose(){
if($("#join_course_role_13").is(":checked") || $("#join_course_role_14").is(":checked")) {
$("#join_course_role_15").attr('disabled', 'disabled');
} else {
$("#join_course_role_15").removeAttr('disabled');
}
}
$(function() {
$("#join_course_role_13").on('click', function(){
if($("#join_course_role_13").is(":checked") || $("#join_course_role_14").is(":checked")) {
$("#join_course_role_15").attr('disabled', 'disabled');
} else {
$("#join_course_role_15").removeAttr('disabled');
}
});
$("#join_course_role_14").on('click', function(){
if($("#join_course_role_13").is(":checked") || $("#join_course_role_14").is(":checked")) {
$("#join_course_role_15").attr('disabled', 'disabled');
} else {
$("#join_course_role_15").removeAttr('disabled');
}
});
$("#join_course_role_15").on('click', function(){
if($("#join_course_role_15").is(":checked")) {
$("#join_course_role_13").attr('disabled', 'disabled');
$("#join_course_role_14").attr('disabled', 'disabled');
} else {
$("#join_course_role_13").removeAttr('disabled');
$("#join_course_role_14").removeAttr('disabled');
}
});
});
</script>

View File

@ -0,0 +1,2 @@
var htmlvalue = "<%= escape_javascript(render :partial => 'join_contest_box') %>";
pop_box_new(htmlvalue,460,220);

View File

@ -0,0 +1,85 @@
<% if @state %>
<% if @state == 0 %>
var htmlvalue = '<div id="muban_popup_box" style="width:300px;"><div class="muban_popup_top"><h3 class="fl">提示</h3><a href="javascript:void(0);" class="muban_icons_close fr"></a></div>'+
'<div class="clear mt15"><p class="text_c f14">加入成功</p><div class="cl"></div>'+
'<a href="javascript:void(0);" class="fr sy_btn_blue mt10" style="margin-right: 119px;" onclick="refresh_current_contest();">确&nbsp;&nbsp;定</a></div></div>';
pop_box_new(htmlvalue, 300, 140);
<% elsif @state == 1 %>
var htmlvalue = '<div id="muban_popup_box" style="width:300px;"><div class="muban_popup_top"><h3 class="fl">提示</h3><a href="javascript:void(0);" class="muban_icons_close fr"></a></div>'+
'<div class="clear mt15"><p class="text_c f14">密码错误</p><div class="cl"></div>'+
'<a href="javascript:void(0);" class="fr sy_btn_blue mt10" style="margin-right: 119px;" onclick="hideModal();">确&nbsp;&nbsp;定</a></div></div>';
pop_box_new(htmlvalue, 300, 140);
<% elsif @state == 3 %>
var htmlvalue = '<div id="muban_popup_box" style="width:300px;"><div class="muban_popup_top"><h3 class="fl">提示</h3><a href="javascript:void(0);" class="muban_icons_close fr"></a></div>'+
'<div class="clear mt15"><p class="text_c f14">您已经加入了竞赛</p><div class="cl"></div>'+
'<a href="javascript:void(0);" class="fr sy_btn_blue mt10" style="margin-right: 119px;" onclick="refresh_current_contest();">确&nbsp;&nbsp;定</a></div></div>';
pop_box_new(htmlvalue, 300, 140);
<% elsif @state == 4 %>
var htmlvalue = '<div id="muban_popup_box" style="width:300px;"><div class="muban_popup_top"><h3 class="fl">提示</h3><a href="javascript:void(0);" class="muban_icons_close fr"></a></div>'+
'<div class="clear mt15"><p class="text_c f14">您输入的邀请码错误</p><div class="cl"></div>'+
'<a href="javascript:void(0);" class="fr sy_btn_blue mt10" style="margin-right: 119px;" onclick="hideModal();">确&nbsp;&nbsp;定</a></div></div>';
pop_box_new(htmlvalue, 300, 140);
<% elsif @state == 5 %>
var htmlvalue = '<div id="muban_popup_box" style="width:300px;"><div class="muban_popup_top"><h3 class="fl">提示</h3><a href="javascript:void(0);" class="muban_icons_close fr"></a></div>'+
'<div class="clear mt15"><p class="text_c f14">您还未登录</p><div class="cl"></div>'+
'<a href="javascript:void(0);" class="fr sy_btn_blue mt10" style="margin-right: 119px;" onclick="hideModal();">确&nbsp;&nbsp;定</a></div></div>';
pop_box_new(htmlvalue, 300, 140);
<% elsif @state == 6 %>
var htmlvalue = '<div id="muban_popup_box" style="width:300px;"><div class="muban_popup_top"><h3 class="fl">提示</h3><a href="javascript:void(0);" class="muban_icons_close fr"></a></div>'+
'<div class="clear mt15"><p class="text_c f14">申请成功,请等待审核</p><div class="cl"></div>'+
'<a href="javascript:void(0);" class="fr sy_btn_blue mt10" style="margin-right: 119px;" onclick="hideModal();">确&nbsp;&nbsp;定</a></div></div>';
pop_box_new(htmlvalue, 300, 140);
<% elsif @state == 7%>
var htmlvalue = '<div id="muban_popup_box" style="width:300px;"><div class="muban_popup_top"><h3 class="fl">提示</h3><a href="javascript:void(0);" class="muban_icons_close fr"></a></div>'+
'<div class="clear mt15"><p class="text_c f14">您已经发送过申请了,请耐心等待</p><div class="cl"></div>'+
'<a href="javascript:void(0);" class="fr sy_btn_blue mt10" style="margin-right: 119px;" onclick="hideModal();">确&nbsp;&nbsp;定</a></div></div>';
pop_box_new(htmlvalue, 300, 140);
<% elsif @state == 8%>
var htmlvalue = '<div id="muban_popup_box" style="width:300px;"><div class="muban_popup_top"><h3 class="fl">提示</h3><a href="javascript:void(0);" class="muban_icons_close fr"></a></div>'+
'<div class="clear mt15"><p class="text_c f14">您已经是该班级的教师了</p><div class="cl"></div>'+
'<a href="javascript:void(0);" class="fr sy_btn_blue mt10" style="margin-right: 119px;" onclick="refresh_current_contest();">确&nbsp;&nbsp;定</a></div></div>';
pop_box_new(htmlvalue, 300, 140);
<% elsif @state == 9%>
var htmlvalue = '<div id="muban_popup_box" style="width:300px;"><div class="muban_popup_top"><h3 class="fl">提示</h3><a href="javascript:void(0);" class="muban_icons_close fr"></a></div>'+
'<div class="clear mt15"><p class="text_c f14">您已经是该班级的教辅了</p><div class="cl"></div>'+
'<a href="javascript:void(0);" class="fr sy_btn_blue mt10" style="margin-right: 119px;" onclick="refresh_current_contest();">确&nbsp;&nbsp;定</a></div></div>';
pop_box_new(htmlvalue, 300, 140);
<% elsif @state == 10%>
var htmlvalue = '<div id="muban_popup_box" style="width:300px;"><div class="muban_popup_top"><h3 class="fl">提示</h3><a href="javascript:void(0);" class="muban_icons_close fr"></a></div>'+
'<div class="clear mt15"><p class="text_c f14">您已经是该竞赛的管理员了</p><div class="cl"></div>'+
'<a href="javascript:void(0);" class="fr sy_btn_blue mt10" style="margin-right: 119px;" onclick="refresh_current_contest();">确&nbsp;&nbsp;定</a></div></div>';
pop_box_new(htmlvalue, 300, 140);
<% elsif @state == 11%>
var htmlvalue = '<div id="muban_popup_box" style="width:300px;"><div class="muban_popup_top"><h3 class="fl">提示</h3><a href="javascript:void(0);" class="muban_icons_close fr"></a></div>'+
'<div class="clear mt15"><p class="text_c f14">该竞赛已归档,请联系老师</p><div class="cl"></div>'+
'<a href="javascript:void(0);" class="fr sy_btn_blue mt10" style="margin-right: 119px;" onclick="hideModal();">确&nbsp;&nbsp;定</a></div></div>';
pop_box_new(htmlvalue, 300, 140);
<% elsif @state == 12 %>
var htmlvalue = '<div id="muban_popup_box" style="width:300px;"><div class="muban_popup_top"><h3 class="fl">提示</h3><a href="javascript:void(0);" class="muban_icons_close fr"></a></div>'+
'<div class="clear mt15"><p class="text_c f14">您已经发送过申请了,请耐心等待</p><div class="cl"></div>'+
'<a href="javascript:void(0);" class="fr sy_btn_blue mt10" style="margin-right: 119px;" onclick="hideModal();">确&nbsp;&nbsp;定</a></div></div>';
pop_box_new(htmlvalue, 300, 140);
<% elsif @state == 13 %>
var htmlvalue = '<div id="muban_popup_box" style="width:300px;"><div class="muban_popup_top"><h3 class="fl">提示</h3><a href="javascript:void(0);" class="muban_icons_close fr"></a></div>'+
'<div class="clear mt15"><p class="text_c f14">申请成功,请等待审核</p><div class="cl"></div>'+
'<a href="javascript:void(0);" class="fr sy_btn_blue mt10" style="margin-right: 119px;" onclick="hideModal();">确&nbsp;&nbsp;定</a></div></div>';
pop_box_new(htmlvalue, 300, 140);
<% elsif @state == 14 %>
var htmlvalue = '<div id="muban_popup_box" style="width:300px;"><div class="muban_popup_top"><h3 class="fl">提示</h3><a href="javascript:void(0);" class="muban_icons_close fr"></a></div>'+
'<div class="clear mt15"><p class="text_c f14">此二维码已停用,请与管理员联系</p><div class="cl"></div>'+
'<a href="javascript:void(0);" class="fr sy_btn_blue mt10" style="margin-right: 119px;" onclick="hideModal();">确&nbsp;&nbsp;定</a></div></div>';
pop_box_new(htmlvalue, 300, 140);
<% else %>
var htmlvalue = '<div id="muban_popup_box" style="width:300px;"><div class="muban_popup_top"><h3 class="fl">提示</h3><a href="javascript:void(0);" class="muban_icons_close fr"></a></div>'+
'<div class="clear mt15"><p class="text_c f14">未知错误,请稍后再试</p><div class="cl"></div>'+
'<a href="javascript:void(0);" class="fr sy_btn_blue mt10" style="margin-right: 119px;" onclick="hideModal();">确&nbsp;&nbsp;定</a></div></div>';
pop_box_new(htmlvalue, 300, 140);
<% end %>
<% end %>
function refresh_current_contest(){
hideModal();
<% if @contest %>
window.location.href= "<%=contest_path(@contest.id) %>";
<% end %>
}

View File

@ -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%>
<div id="homepageLeftMenuContests">
<ul class="user_sub_menu" id="user_contests_li">
<%= render :partial => 'layouts/user_contests', :locals => {:courses => contests,:user => @user,:all_count => all_count,:page => 0} %>
<%= render :partial => 'layouts/user_contests', :locals => {:contests => contests,:user => @user,:all_count => all_count,:page => 0} %>
</ul>
</div>
<% if !contests.empty? %>
@ -12,6 +12,6 @@
<script>
$(function() {
$('#user_hide_contest').hide();
$('#user_hide_course').hide();
})
</script>

View File

@ -14,8 +14,8 @@
<ul class="user_navmore_txt">
<% if is_teacher %>
<li class="clear">
<%= link_to "作品",works_index_path(:contest => contest.id), :class => 'user_navmore_li fl', :target => '_blank' %>
<%= link_to '+',works_index_path(:contest => contest.id,:is_new => 1), :class => 'user_navmore_add fl', :title => '发布作品',:target => '_blank' %>
<%= link_to "作品",works_path(:contest => contest.id), :class => 'user_navmore_li fl', :target => '_blank' %>
<%= link_to '+',works_path(:contest => contest.id,:is_new => 1), :class => 'user_navmore_add fl', :title => '发布作品',:target => '_blank' %>
</li>
<li class="clear">
<%= link_to "通知", contest_news_index_path(contest), :class => 'user_navmore_li fl',:target => '_blank' %>
@ -23,7 +23,7 @@
</li>
<% else %>
<li>
<%= link_to "作业",works_index_path(:contest => contest.id), :class => 'user_navmore_li', :target => '_blank' %>
<%= link_to "作业",works_path(:contest => contest.id), :class => 'user_navmore_li', :target => '_blank' %>
<div class="cl"></div>
</li>
<li>

View File

@ -62,7 +62,7 @@
<%= link_to "新建竞赛", new_contest_path(), :target => "_blank", :style => "font-size:14px;" %>
</li>
<li id="user_04" class="user_icons_addclass">
<%= link_to "加入竞赛",join_contest_contests_path(),:remote => true, :method => "post", :style => "font-size:14px;" %>
<%= link_to "加入竞赛",join_contest_contests_path(),:remote => true, :method => "get", :style => "font-size:14px;" %>
</li>
<% end %>
</ul>

View File

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

View File

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

View File

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

View File

@ -0,0 +1,8 @@
FactoryGirl.define do
factory :applied_contest do
contest nil
user nil
role ""
end
end

View File

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe AppliedContest, :type => :model do
pending "add some examples to (or delete) #{__FILE__}"
end