diff --git a/app/assets/javascripts/admins/competition_settings/index.js b/app/assets/javascripts/admins/competition_settings/index.js index 87c4e4a01..35ced0197 100644 --- a/app/assets/javascripts/admins/competition_settings/index.js +++ b/app/assets/javascripts/admins/competition_settings/index.js @@ -29,6 +29,9 @@ $(document).on('turbolinks:load', function(){ $(".nav-setting-form .enroll_end_time").datetimepicker(timeOptions); + $(".stage-update-form .section-start-time").datetimepicker(timeOptions); + $(".stage-update-form .section-end-time").datetimepicker(timeOptions); + defineDateRangeSelect('.teaching-mode-date'); // defineTimeRangeSelect('.competition-start-end-date'); @@ -209,7 +212,7 @@ $(function () { $("#requireForm").on("click",".delRequrieBtn",function () { $(this).parents(".requireForm_item").remove(); - }) + }); $('.competition-staff-settings').on('click', '.mutiple-limited-radio', function(){ var radio = $(this); @@ -248,8 +251,6 @@ $(function () { }); }); - - // 排行榜设置 //删除小阶段 $("#large_panel").on("click",".small_panel_item_del",function () { @@ -260,7 +261,89 @@ $(function () { console.log(i); $(list).find(".subName").eq(i).html("第"+parseInt(i+1)+"阶段"); } - }) + }); + + $('form.stage-update-form').validate({ + errorElement: 'span', + errorClass: 'danger text-danger', + rules: { + stage_name: "required", + "stage[][start_time]": "required", + "stage[][end_time]": "required", + "stage[][mission_count]": { + required: true, + min: 1 + }, + "stage[][entry]": { + required: true, + min: 1 + }, + score_rate: { + required: true, + range: [0, 100] + } + }, + messages: { + "stage[][mission_count]": { + min: ">=1" + }, + "stage[][entry]": { + min: ">=1" + }, + } + }); + + $('form.stage-update-form').on('click', ".update-stage", function () { + var updateForm = $(this).parents("form"); + $(this).attr('disabled', 'disabled'); + updateForm.find('.error').html(''); + var valid = updateForm.valid(); + + updateForm.find('input[name="stage[][identifiers][]"]').each(function(_, e){ + var $ele = $(e); + if($ele.val() === undefined || $ele.val().length === 0){ + $ele.addClass('danger text-danger'); + valid = false; + } else { + $ele.removeClass('danger text-danger'); + } + }); + + if (!valid) return; + + updateForm.find('input[name="stage[][mission_count]"]').each(function(_, e){ + var $missionCount = $(e); + var $entryCount = $(e).parents("div.row").find('input[name="stage[][mission_count]"]'); + if(parseInt($missionCount.val()) > parseInt($entryCount.val()) ){ + $missionCount.addClass('danger text-danger'); + $missionCount.after('不能大于总任务数'); + valid = false; + } else { + $missionCount.removeClass('danger text-danger'); + $missionCount.siblings().remove(); + } + }); + + $.ajax({ + method: 'POST', + dataType: 'json', + url: updateForm.attr('action'), + data: new FormData(updateForm[0]), + processData: false, + contentType: false, + success: function (data) { + $.notify({message: '保存成功'}); + // window.location.reload(); + }, + error: function (res) { + var data = res.responseJSON; + $navForm.find('.error').html(data.message); + }, + complete: function () { + $navForm.find('.submit-btn').attr('disabled', false); + } + }); + }); }); @@ -382,8 +465,6 @@ function addNewTab() { ' %\n' + '
\n' + ' 删除\n' + ' 保存\n' + @@ -391,4 +472,4 @@ function addNewTab() { ' '; $("#large_panel").append(html); -} +} \ No newline at end of file diff --git a/app/controllers/admins/competition_stages_controller.rb b/app/controllers/admins/competition_stages_controller.rb index 537240bde..5445d59a9 100644 --- a/app/controllers/admins/competition_stages_controller.rb +++ b/app/controllers/admins/competition_stages_controller.rb @@ -1,16 +1,18 @@ class Admins::CompetitionStagesController < Admins::BaseController def create - if @competition.competition_stages.exists?(name: params[:stage_name]) + Admins::CompetitionStageUpdateService.call(current_competition, update_form_params) + render_ok + if current_competition.competition_stages.exists?(name: params[:stage_name]) render_error "已存在同名的阶段" else - @competition.competition_stages << CompetitionStage.new(name: params[:stage_name]) + current_competition.competition_stages << CompetitionStage.new(name: params[:stage_name]) render_ok end end def update - current_stage.update_attributes!(name: params[:stage_name], score_rate: params[:score_rate]) + Admins::CompetitionStageUpdateService.call(current_competition, update_form_params, current_stage) render_ok end @@ -78,6 +80,11 @@ class Admins::CompetitionStagesController < Admins::BaseController end def current_stage - @_current_stage ||= CompetitionStage.find_by!(competition_id: params[:competition_id], id: params[:stage_id]) + @_current_stage ||= CompetitionStage.find_by!(competition_id: params[:competition_id], id: params[:id]) + end + + def update_form_params + params.permit(:stage_name, :score_rate, stage: [:start_time, :end_time, :mission_count, :entry, :score_source, identifiers: []]) end + end \ No newline at end of file diff --git a/app/controllers/competitions/teachers_controller.rb b/app/controllers/competitions/teachers_controller.rb index 76c7cfe6a..04ab1bdd0 100644 --- a/app/controllers/competitions/teachers_controller.rb +++ b/app/controllers/competitions/teachers_controller.rb @@ -7,6 +7,7 @@ class Competitions::TeachersController < Competitions::BaseController end teachers = User.joins(:user_extension).where(status: 1, user_extensions: { identity: 0 }) + teachers = teachers.where(user_extensions: { school_id: current_competition.region_schools.pluck(:school_id) }) if current_competition.region_schools.size > 0 teachers = teachers.where.not(id: params[:teacher_ids]) if params[:teacher_ids].present? teachers = teachers.where('LOWER(CONCAT(lastname, firstname, login, nickname)) LIKE ?', "%#{keyword}%") @teachers = teachers.includes(user_extension: :school).limit(10) diff --git a/app/models/competition_stage_section.rb b/app/models/competition_stage_section.rb index 027e7ec69..3c8794ad8 100644 --- a/app/models/competition_stage_section.rb +++ b/app/models/competition_stage_section.rb @@ -1,4 +1,5 @@ class CompetitionStageSection < ApplicationRecord + # score_source 0: 经验值, 1:预测准确率 belongs_to :competition belongs_to :competition_stage diff --git a/app/services/admins/competition_stage_create_service.rb b/app/services/admins/competition_stage_create_service.rb new file mode 100644 index 000000000..d11276ef3 --- /dev/null +++ b/app/services/admins/competition_stage_create_service.rb @@ -0,0 +1,29 @@ +class Admins::CompetitionStageCreateService < ApplicationService + attr_reader :competition, :params + + def initialize(competition, params) + @params = params + @competition = competition + end + + def call + ActiveRecord::Base.transaction do + stage = CompetitionStage.create!(competition_id: competition.id, name: params[:stage_name], score_rate: (params[:score_rate].to_i / 100).round(2)) + + stage.competition_stage_sections.destroy_all + + params[:stage].each do |section| + stage_section = CompetitionStageSection.create!(competition_id: competition.id, competition_stage_id: stage.id, + start_time: section["start_time"], end_time: section["end_time"], + mission_count: section["mission_count"], entry: section["entry"], + score_source: section["score_source"]) + section["identifiers"].each do |identifier| + CompetitionEntry.create!(competition_stage_section_id: stage_section.id, competition_stage_id: stage.id, + shixun_identifier: identifier) + end + end + + stage + end + end +end \ No newline at end of file diff --git a/app/services/admins/competition_stage_update_service.rb b/app/services/admins/competition_stage_update_service.rb new file mode 100644 index 000000000..51bed5b8d --- /dev/null +++ b/app/services/admins/competition_stage_update_service.rb @@ -0,0 +1,30 @@ +class Admins::CompetitionStageUpdateService < ApplicationService + attr_reader :competition, :params, :stage + + def initialize(competition, params, stage) + @params = params + @competition = competition + @stage = stage + end + + def call + ActiveRecord::Base.transaction do + stage.update_attributes!(name: params[:stage_name], score_rate: (params[:score_rate].to_i / 100).round(2)) + + stage.competition_stage_sections.destroy_all + + params[:stage].each do |section| + stage_section = CompetitionStageSection.create!(competition_id: competition.id, competition_stage_id: stage.id, + start_time: section["start_time"], end_time: section["end_time"], + mission_count: section["mission_count"], entry: section["entry"], + score_source: section["score_source"]) + section["identifiers"].each do |identifier| + CompetitionEntry.create!(competition_stage_section_id: stage_section.id, competition_stage_id: stage.id, + shixun_identifier: identifier) + end + end + + stage + end + end +end \ No newline at end of file diff --git a/app/services/competitions/create_personal_team_service.rb b/app/services/competitions/create_personal_team_service.rb index 82adec0cf..998a13880 100644 --- a/app/services/competitions/create_personal_team_service.rb +++ b/app/services/competitions/create_personal_team_service.rb @@ -11,7 +11,7 @@ class Competitions::CreatePersonalTeamService < ApplicationService def call raise Error, '个人赛才能报名' unless competition.personal? - raise Error, '本竞赛只对指定单位/学校开放' unless competition.open?(user) + raise Error, '本竞赛只面向部分学校/单位开放,你暂时没有参赛资格' unless competition.open?(user) is_teacher = user.is_teacher? raise Error, '本竞赛的参赛者限定为:学生' if is_teacher && competition.teacher_enroll_forbidden? diff --git a/app/services/competitions/join_team_service.rb b/app/services/competitions/join_team_service.rb index 7ec453013..daaca30d1 100644 --- a/app/services/competitions/join_team_service.rb +++ b/app/services/competitions/join_team_service.rb @@ -13,7 +13,7 @@ class Competitions::JoinTeamService < ApplicationService invite_code = params[:invite_code].to_s.strip raise Error, '战队邀请码不能为空' if invite_code.blank? - raise Error, '本竞赛只对指定单位/学校开放' unless competition.open?(user) + raise Error, '本竞赛只面向部分学校/单位开放,你暂时没有参赛资格' unless competition.open?(user) is_teacher = user.is_teacher? raise Error, '本竞赛的参赛者限定为:学生' if is_teacher && competition.teacher_enroll_forbidden? diff --git a/app/views/admins/competition_settings/index.html.erb b/app/views/admins/competition_settings/index.html.erb index a8ee780e2..21ca680c2 100644 --- a/app/views/admins/competition_settings/index.html.erb +++ b/app/views/admins/competition_settings/index.html.erb @@ -341,10 +341,10 @@