From 46e2e573e3991c41cb2777aa5de139740743a060 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Fri, 25 Oct 2019 17:04:46 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AB=9E=E8=B5=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admins/competition_settings/index.js | 702 +++++++++++------- .../admins/competition_stages_controller.rb | 1 - .../competition_basic_setting_service.rb | 13 +- .../competition_stage_update_service.rb | 2 +- .../competition_settings/index.html.erb | 16 +- .../admins/competition_stages/destroy.js.erb | 1 + 6 files changed, 445 insertions(+), 290 deletions(-) create mode 100644 app/views/admins/competition_stages/destroy.js.erb diff --git a/app/assets/javascripts/admins/competition_settings/index.js b/app/assets/javascripts/admins/competition_settings/index.js index e974d2550..fa533f1d3 100644 --- a/app/assets/javascripts/admins/competition_settings/index.js +++ b/app/assets/javascripts/admins/competition_settings/index.js @@ -131,303 +131,463 @@ $(document).on('turbolinks:load', function(){ templateSelection: function(item){ return item.name || item.text; } - } + }; $('.sponsor-select').select2(selectOptions); $('.allow-school-select').select2(selectOptions); - } -}); -//添加主办方或者开放范围 -function addSponsor(item){ - var html=''; - $(item).parents(".sponsorPanel").append(html); -} -//删除 -function del_sponsor(item){ - $(item).parents(".sponsor_label").remove(); -} -$(function () { - //MD编辑 - // $("#MD_typeFrom").on("click",".add_MD_type",function () { - // - // var length=$(".MD_type").find(".add_MD_type").length + 1; - // var html='
\n' + - // '
\n' + - // ' \n' + - // '
\n' + - // '
\n' + - // '
\n' + - // ' \n' + - // ' \n' + - // '
'; - // $("#MD_typeFrom").append(html); - // }) - // $("#MD_typeFrom").on("click",".del_MD_type",function () { - // $(this).parents(".MD_type").remove(); - // }) - - //链接 - $(".nav-setting-form").on("click",".add_linkBtn",function () { - var length=$(".nav-setting-form").find(".linkFormItem").length + 1; - var html='
\n' + - '
\n' + - ' \n' + - '
\n' + - '
\n' + - '
\n' + - '
\n' + - '
\n' + - ' +\n' + - ' ×\n' + - '
'; - $(this).parents(".linkFormItem").after(html); - }); - - $(".nav-setting-form").on("click", ".del_linkBtn", function () { - $(this).parents(".linkFormItem").remove(); - }); - - //有关报名要求 - $(".addRequireBtn").on("click",function () { - var length=$("#requireForm").find(".requireForm_item").length + 1; - var html='
\n' + - '
  
\n' + - '
\n' + - ' \n' + - '
\n' + - ' ~\n' + - '
\n' + - ' \n' + - '
\n' + - ' \n' + - '
\n' + - ' \n' + - '
\n' + - '
\n' + - ' \n' + - '
\n' + - '
\n' + - ' \n' + - ' \n' + - ' \n' + - ' \n' + - '
\n' + - '
'; - $("#requireForm").append(html); - }); - - $("#requireForm").on("click",".delRequrieBtn",function () { - $(this).parents(".requireForm_item").remove(); - }); - - $('.competition-staff-settings').on('click', '.mutiple-limited-radio', function(){ - var radio = $(this); - if (radio.is(':checked')) { - radio.parent().parent().siblings().find('.mutiple-limited-radio').attr('checked', false) - } else { - radio.parent().parent().siblings().find('.mutiple-limited-radio').attr('checked', true) - } - }); - - var $navForm = $('form.nav-setting-form'); - $navForm.on('click', ".submit-btn", function () { - $navForm.find('.submit-btn').attr('disabled', 'disabled'); - $navForm.find('.error').html(''); - var valid = $navForm.valid(); - - if (!valid) return; - $.ajax({ - method: 'POST', - dataType: 'json', - url: $navForm.attr('action'), - data: new FormData($navForm[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); + // 排行榜 + //链接 + $(".nav-setting-form").on("click",".add_linkBtn",function () { + var length=$(".nav-setting-form").find(".linkFormItem").length + 1; + var html='
\n' + + '
\n' + + ' \n' + + '
\n' + + '
\n' + + '
\n' + + '
\n' + + '
\n' + + ' +\n' + + ' ×\n' + + '
'; + $(this).parents(".linkFormItem").after(html); + }); + + $(".nav-setting-form").on("click", ".del_linkBtn", function () { + $(this).parents(".linkFormItem").remove(); + }); + + //有关报名要求 + $(".addRequireBtn").on("click",function () { + var length=$("#requireForm").find(".requireForm_item").length + 1; + var html='
\n' + + '
  
\n' + + '
\n' + + ' \n' + + '
\n' + + ' ~\n' + + '
\n' + + ' \n' + + '
\n' + + ' \n' + + '
\n' + + ' \n' + + '
\n' + + '
\n' + + ' \n' + + '
\n' + + '
\n' + + ' \n' + + ' \n' + + ' \n' + + ' \n' + + '
\n' + + '
'; + $("#requireForm").append(html); + }); + + $("#requireForm").on("click",".delRequrieBtn",function () { + $(this).parents(".requireForm_item").remove(); + }); + + $('.competition-staff-settings').on('click', '.mutiple-limited-radio', function(){ + var radio = $(this); + if (radio.is(':checked')) { + radio.parent().parent().siblings().find('.mutiple-limited-radio').attr('checked', false) + } else { + radio.parent().parent().siblings().find('.mutiple-limited-radio').attr('checked', true) } }); - }); + + var $navForm = $('form.nav-setting-form'); + $navForm.on('click', ".submit-btn", function () { + $navForm.find('.submit-btn').attr('disabled', 'disabled'); + $navForm.find('.error').html(''); + var valid = $navForm.valid(); + + if (!valid) return; + $.ajax({ + method: 'POST', + dataType: 'json', + url: $navForm.attr('action'), + data: new FormData($navForm[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); + } + }); + }); // 排行榜设置 - //删除小阶段 - $("#large_panel").on("click",".small_panel_item_del",function () { - var list = $(this).parents(".small_panel"); - $(this).parents(".small_panel_item").remove(); - - for(var i=0;i < $(list).find(".subName").length;i++){ - 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] + //删除小阶段 + $("#large_panel").on("click",".small_panel_item_del",function () { + var list = $(this).parents(".small_panel"); + $(this).parents(".small_panel_item").remove(); + + for(var i=0;i < $(list).find(".subName").length;i++){ + console.log(i); + $(list).find(".subName").eq(i).html("第"+parseInt(i+1)+"阶段"); } - }, - messages: { - "stage[][mission_count]": { - min: ">=1" - }, - "stage[][entry]": { - min: ">=1" - }, - } - }); - - $('.competition-chart-setting').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'); + }); + + // $('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" + // }, + // } + // }); + + $('.competition-chart-setting').on('click', ".update-stage", function () { + var updateForm = $(this).parents("form"); + $(this).attr('disabled', 'disabled'); + updateForm.find('.error').html(''); + // var valid = updateForm.valid(); + var valid = true; + + var $stageName = updateForm.find('input[name="stage_name"]'); + if($stageName.val() === undefined || $stageName.val().length === 0){ + $stageName.addClass('danger text-danger'); valid = false; } else { - $ele.removeClass('danger text-danger'); + $stageName.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('不能大于总任务数'); + var $scoreRate = updateForm.find('input[name="score_rate"]'); + if($scoreRate.val() === undefined || $scoreRate.val().length === 0){ + $scoreRate.addClass('danger text-danger'); + valid = false; + } else if (parseInt($scoreRate.val()) > 100 || parseInt($scoreRate.val()) < 0) { + $scoreRate.addClass('danger text-danger'); + $scoreRate.after('0-100之间的数值'); valid = false; } else { - $missionCount.removeClass('danger text-danger'); - $missionCount.siblings().remove(); + $scoreRate.removeClass('danger text-danger'); + $scoreRate.siblings().remove(); } + + updateForm.find('input[name="stage[][start_time]"]').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'); + } + }); + + updateForm.find('input[name="stage[][end_time]"]').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'); + } + }); + + updateForm.find('input[name="stage[][mission_count]"]').each(function(i, e){ + var $ele = $(e); + var $entry = updateForm.find('input[name="stage[][entry]"]').eq(i); + if($ele.val() === undefined || $ele.val().length === 0){ + $ele.addClass('danger text-danger'); + valid = false; + } else if (parseInt($ele.val()) < 1) { + $ele.addClass('danger text-danger'); + $ele.after('大于等于1'); + valid = false; + } else if (parseInt($ele.val()) > parseInt($entry.val())) { + $ele.addClass('danger text-danger'); + $ele.after('不能大于总任务数'); + valid = false; + } else { + $ele.removeClass('danger text-danger'); + $ele.siblings().remove(); + } + }); + + updateForm.find('input[name="stage[][entry]"]').each(function(_, e){ + var $ele = $(e); + if($ele.val() === undefined || $ele.val().length === 0){ + $ele.addClass('danger text-danger'); + valid = false; + } else if (parseInt($ele.val()) < 1) { + $ele.addClass('danger text-danger'); + $ele.after('大于等于1'); + valid = false; + } else { + $ele.removeClass('danger text-danger'); + $ele.siblings().remove(); + } + }); + + 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); + } + }); }); - $.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); + $(".competition-chart-stages").on("click", ".add-new-tab", function () { + if($(".new-stage-form").length > 0){ + alert("请先保存上一个tab"); + } else { + var count = parseInt($("#large_panel").find(".large_panel_part").length)+1; + var html = '
' + + '
\n' + + ' tab标题\n' + + '
\n' + + ' \n' + + '
\n' + + ' 总排行榜占比:\n' + + '
\n' + + ' \n' + + '
%\n' + + '
\n' + + ' 新增子阶段\n' + + '
\n' + + ' 删除\n' + + ' 保存\n' + + '
\n' + + '
\n' + + '
\n' + + ' 第1阶段\n' + + '
\n' + + '
\n' + + ' 有效时间:\n' + + '
\n' + + ' \n' + + '
\n' + + ' ~\n' + + '
\n' + + ' \n' + + '
\n' + + ' 任务完成要求:\n' + + '
\n' + + ' \n' + + '
\n' + + ' /\n' + + '
\n' + + ' \n' + + '
\n' + + ' (总任务)\n' + + ' 成绩来源:\n' + + '
\n' + + ' \n' + + '
\n' + + '
\n' + + '
\n' + + '
\n' + + ' 任务1\n' + + '
\n' + + ' \n' + + '
\n' + + '
\n' + + '
\n' + + ' 任务2\n' + + '
\n' + + ' \n' + + '
\n' + + '
\n' + + '
\n' + + ' 任务3\n' + + '
\n' + + ' \n' + + '
\n' + + '
\n' + + '
\n' + + '
\n' + + ' \n' + + ' 删除\n' + + ' \n' + + '
\n' + + '
'; + $("#large_panel").append(html); + + $(".stage-update-form .section-start-time").datetimepicker(timeOptions); + $(".stage-update-form .section-end-time").datetimepicker(timeOptions); } }); - }); -}); + //新增子阶段 + $(".competition-chart-stages").on("click", ".add-task-sub", function () { + var index = $(this).parents(".large_panel_part").attr("attr_line"); + var count= 0; -//新增子阶段 -function add_task_sub(item){ - var index = $(item).parents(".large_panel_part").attr("attr_line"); - var count= 0; + console.log("sdfsf"); + console.log($("#small_panel_"+index).find(".small_panel_item").length > 0); - console.log("sdfsf"); - console.log($("#small_panel_"+index).find(".small_panel_item").length > 0); + if($("#small_panel_"+index).find(".small_panel_item").length > 0){ + count = parseInt($("#small_panel_"+index).find(".small_panel_item").last().attr("count")) + 1; - if($("#small_panel_"+index).find(".small_panel_item").length > 0){ - count = parseInt($("#small_panel_"+index).find(".small_panel_item").last().attr("count")) + 1; + console.log($("#small_panel_"+index).find(".small_panel_item").last().attr("count")); + }else{ + count = 1; + } - console.log($("#small_panel_"+index).find(".small_panel_item").last().attr("count")); - }else{ - count = 1; + var showCount=parseInt($("#small_panel_"+index).find(".small_panel_item").length) + 1; + + + var html='
\n' + + ' 第'+showCount+'阶段\n' + + '
\n' + + '
\n' + + ' 有效时间:\n' + + '
\n' + + ' \n' + + '
\n' + + ' ~\n' + + '
\n' + + ' \n' + + '
\n' + + ' 任务完成要求:\n' + + '
\n' + + ' \n' + + '
\n' + + ' /\n' + + '
\n' + + ' \n' + + '
\n' + + ' (总任务)\n' + + ' 成绩来源:\n' + + '
\n' + + ' \n' + + '
\n' + + '
\n' + + '
\n'+ + '
\n' + + ' 任务1\n' + + '
\n' + + ' \n' + + '
\n' + + '
\n' + + '
\n' + + ' 任务2\n' + + '
\n' + + ' \n' + + '
\n' + + '
\n' + + '
\n' + + ' 任务3\n' + + '
\n' + + ' \n' + + '
\n' + + '
\n' + + '
\n' + + '
\n' + + ' \n' + + ' 删除\n' + + ' \n' + + '
'; + $("#small_panel_"+index).append(html); + + $(".stage-update-form .section-start-time").datetimepicker(timeOptions); + $(".stage-update-form .section-end-time").datetimepicker(timeOptions); + }); } +}); - var showCount=parseInt($("#small_panel_"+index).find(".small_panel_item").length) + 1; - - - var html='
\n' + - ' 第'+showCount+'阶段\n' + - '
\n' + - '
\n' + - ' 有效时间:\n' + - '
\n' + - ' \n' + - '
\n' + - ' ~\n' + - '
\n' + - ' \n' + - '
\n' + - ' 任务完成要求:\n' + - '
\n' + - ' \n' + - '
\n' + - ' /\n' + - '
\n' + - ' \n' + - '
\n' + - ' (总任务)\n' + - ' 成绩来源:\n' + - '
\n' + - ' \n' + - '
\n' + - '
\n' + - '
\n' + - '
\n' + - ' \n' + - ' 删除\n' + - ' \n' + - '
'; - $("#small_panel_"+index).append(html); +//添加主办方或者开放范围 +function addSponsor(item){ + var html=''; + $(item).parents(".sponsorPanel").append(html); +} +//删除 +function del_sponsor(item){ + $(item).parents(".sponsor_label").remove(); } // 小阶段修改总任务数 @@ -448,7 +608,7 @@ function change_total(item) { for(var i=0;i < count-divCount ;i++){ html+='
任务'+(divCount+i+1)+'\n' + '
\n' + - '\n' + + '\n' + '
\n' + '
'; } @@ -494,7 +654,7 @@ function addNewTab(competition_id) { ' \n' + ' %\n' + '
\n' + - ' 新增子阶段\n' + + ' 新增子阶段\n' + '
\n' + ' 删除\n' + ' 保存\n' + diff --git a/app/controllers/admins/competition_stages_controller.rb b/app/controllers/admins/competition_stages_controller.rb index 63e2429e3..49eabe8c8 100644 --- a/app/controllers/admins/competition_stages_controller.rb +++ b/app/controllers/admins/competition_stages_controller.rb @@ -12,7 +12,6 @@ class Admins::CompetitionStagesController < Admins::BaseController def destroy current_stage.destroy! - render_ok end def create_stage_section diff --git a/app/services/admins/competition_basic_setting_service.rb b/app/services/admins/competition_basic_setting_service.rb index fa8264f36..fd1c1453a 100644 --- a/app/services/admins/competition_basic_setting_service.rb +++ b/app/services/admins/competition_basic_setting_service.rb @@ -36,8 +36,9 @@ class Admins::CompetitionBasicSettingService < ApplicationService # 主办方设置 params[:sponsor_schools] = Array.wrap(params[:sponsor_schools]).map(&:to_i) - new_school_ids = params[:sponsor_schools] - competition.sponsor_schools.pluck(:school_id) - delete_school_ids = competition.sponsor_schools.pluck(:school_id) - params[:sponsor_schools] + new_sponsor_school_ids = competition.sponsor_schools.pluck(:school_id) + new_school_ids = params[:sponsor_schools] - new_sponsor_school_ids + delete_school_ids = new_sponsor_school_ids - params[:sponsor_schools] new_school_ids.each do |school_id| CompetitionSchool.create!(competition_id: competition.id, school_id: school_id, source: 'sponsor') end @@ -45,13 +46,13 @@ class Admins::CompetitionBasicSettingService < ApplicationService # 开放范围设置 params[:region_schools] = Array.wrap(params[:region_schools]).map(&:to_i) - new_region_school_ids = params[:region_schools] - competition.region_schools.pluck(:school_id) - delete_region_school_ids = competition.region_schools.pluck(:school_id) - params[:region_schools] + old_region_school_ids = competition.region_schools.pluck(:school_id) + new_region_school_ids = params[:region_schools] - old_region_school_ids + delete_region_school_ids = old_region_school_ids - params[:region_schools] new_region_school_ids.each do |school_id| CompetitionSchool.create!(competition_id: competition.id, school_id: school_id, source: 'region') end - competition.region_schools.where(school_id: delete_region_school_ids).destroy_all - + competition.region_schools.where(school_id: delete_region_school_ids).destroy_all unless delete_region_school_ids.length == 0 competition end diff --git a/app/services/admins/competition_stage_update_service.rb b/app/services/admins/competition_stage_update_service.rb index 51bed5b8d..da7fb4c6d 100644 --- a/app/services/admins/competition_stage_update_service.rb +++ b/app/services/admins/competition_stage_update_service.rb @@ -9,7 +9,7 @@ class Admins::CompetitionStageUpdateService < ApplicationService def call ActiveRecord::Base.transaction do - stage.update_attributes!(name: params[:stage_name], score_rate: (params[:score_rate].to_i / 100).round(2)) + stage.update_attributes!(name: params[:stage_name], score_rate: (params[:score_rate].to_i / 100.0).round(2)) stage.competition_stage_sections.destroy_all diff --git a/app/views/admins/competition_settings/index.html.erb b/app/views/admins/competition_settings/index.html.erb index 8f72ed5e8..f2ed61622 100644 --- a/app/views/admins/competition_settings/index.html.erb +++ b/app/views/admins/competition_settings/index.html.erb @@ -322,10 +322,10 @@ <% if @competition.mode == 1 %> -
+
排行榜设置 - + 新增tab + + 新增tab 实训ID填写示例:实训地址为https://www.educoder.net/shixuns/u5plmgka/challenges,则填写“u5plmgka”
@@ -335,7 +335,7 @@ <% if @competition.competition_stages.count > 0 %> <% @competition.competition_stages.includes(competition_stage_sections: :competition_entries).each_with_index do |stage, index| %> <%= form_tag(admins_competition_competition_stage_path(competition_id: @competition.id, id: stage.id), method: :put, class: 'stage-update-form flex-1', remote: true) do %> -
+
tab标题
@@ -346,11 +346,11 @@
% - 删除 + <%= delete_link '删除', admins_competition_competition_stage_path(competition_id: @competition.id, id: stage.id), class: 'btn btn-default delete-stage ml20' %> 保存
@@ -485,11 +485,5 @@ <% end %>
- -
-
- -
-
<% end %> \ No newline at end of file diff --git a/app/views/admins/competition_stages/destroy.js.erb b/app/views/admins/competition_stages/destroy.js.erb new file mode 100644 index 000000000..cdc8c3f7d --- /dev/null +++ b/app/views/admins/competition_stages/destroy.js.erb @@ -0,0 +1 @@ +window.location.reload(); \ No newline at end of file