diff --git a/app/controllers/subjects_controller.rb b/app/controllers/subjects_controller.rb index efc117d6a..da88e3258 100644 --- a/app/controllers/subjects_controller.rb +++ b/app/controllers/subjects_controller.rb @@ -3,9 +3,10 @@ class SubjectsController < ApplicationController # before_action :check_auth, except: [:index] before_action :check_account, except: [:index, :show, :right_banner] before_action :find_subject, except: [:index, :create, :new, :append_to_stage, :add_shixun_to_stage] - before_action :allowed, only: [:update, :edit, :destroy, :publish, :cancel_publish, :cancel_has_publish, - :search_members, :add_subject_members, :statistics, :shixun_report, :school_report, - :up_member_position, :down_member_position, :update_team_title, :statistics_info] + before_action :allowed, only: [:update, :edit, :destroy, :publish, :cancel_publish, :cancel_has_publish, :apply_public, + :search_members, :add_subject_members, :statistics, :shixun_report, :school_report, + :cancel_has_public, :up_member_position, :down_member_position, :update_team_title, + :statistics_info, :cancel_public] before_action :require_admin, only: [:copy_subject] before_action :shixun_marker, only: [:add_shixun_to_stage] @@ -33,16 +34,16 @@ class SubjectsController < ApplicationController laboratory_join = " AND subjects.id in #{subject_ids} " if select - @subjects = Subject.find_by_sql("SELECT subjects.id, subjects.user_id, subjects.name, subjects.stages_count, subjects.repertoire_id, subjects.status, + @subjects = Subject.find_by_sql("SELECT subjects.id, subjects.user_id, subjects.name, subjects.stages_count, subjects.repertoire_id, subjects.status, subjects.public, subjects.shixuns_count, subjects.excellent, sum(shixuns.myshixuns_count) AS myshixun_member_count FROM subjects join stage_shixuns on stage_shixuns.subject_id = subjects.id join shixuns on shixuns.id = stage_shixuns.shixun_id where - subjects.hidden = 0 AND subjects.status = 2 AND subjects.name like '%#{search}%' #{laboratory_join} + subjects.hidden = 0 AND subjects.public = 2 AND subjects.name like '%#{search}%' #{laboratory_join} AND subjects.repertoire_id = #{select} GROUP BY subjects.id ORDER BY myshixun_member_count DESC") else - @subjects = Subject.find_by_sql("SELECT subjects.id, subjects.user_id, subjects.name, subjects.stages_count, subjects.repertoire_id, subjects.status, + @subjects = Subject.find_by_sql("SELECT subjects.id, subjects.user_id, subjects.name, subjects.stages_count, subjects.repertoire_id, subjects.status, subjects.public, subjects.shixuns_count, subjects.excellent, sum(shixuns.myshixuns_count) AS myshixun_member_count FROM subjects join stage_shixuns on stage_shixuns.subject_id = subjects.id join shixuns on shixuns.id = stage_shixuns.shixun_id where - subjects.hidden = 0 AND subjects.status = 2 AND subjects.name like '%#{search}%' #{laboratory_join} + subjects.hidden = 0 AND subjects.public = 2 AND subjects.name like '%#{search}%' #{laboratory_join} GROUP BY subjects.id ORDER BY myshixun_member_count DESC") end else @@ -59,7 +60,7 @@ class SubjectsController < ApplicationController elsif reorder == "publish_time" @subjects = @subjects.unhidden else - @subjects = @subjects.visible.unhidden + @subjects = @subjects.publiced.unhidden end # 类型 @@ -72,7 +73,7 @@ class SubjectsController < ApplicationController end # 排序 - order_str = (reorder == "publish_time" ? "homepage_show desc, excellent desc, status = 2 desc, publish_time asc" : "homepage_show desc, excellent desc, updated_at desc") + order_str = (reorder == "publish_time" ? "homepage_show desc, excellent desc, public = 2 desc, publish_time asc" : "homepage_show desc, excellent desc, updated_at desc") @subjects = @subjects.reorder(order_str) end @@ -273,14 +274,44 @@ class SubjectsController < ApplicationController end def publish + @subject.update_attributes(status: 2) + end + + def cancel_publish + begin + apply = ApplyAction.where(container_type: "ApplySubject", container_id: @subject.id).order("created_at desc").first + if apply && apply.status == 0 + apply.update_attributes(status: 3) + apply.tidings.destroy_all + end + @subject.update_attributes(status: 0) + rescue => e + uid_logger_error(e.message) + tip_exception("撤销申请失败") + end + end + + def cancel_has_publish + begin + @subject.update_attributes(:status => 0) + rescue => e + uid_logger_error(e.message) + tip_exception("撤销发布失败") + raise ActiveRecord::Rollback + end + end + + # 申请公开 + def apply_public + tip_exception(-1, "请先发布课程再申请公开") if @subject.status != 2 apply = ApplyAction.where(container_type: "ApplySubject", container_id: @subject.id).order("created_at desc").first if apply && apply.status == 0 @status = 0 else - @subject.update_attributes(status: 1) + @subject.update_attributes(public: 1) ApplyAction.create(container_type: "ApplySubject", container_id: @subject.id, user_id: current_user.id, status: 0) begin - status = Educoder::Sms.send(mobile: '18711011226', send_type:'publish_subject' , name: '管理员') + Educoder::Sms.send(mobile: '18711011226', send_type:'publish_subject' , name: '管理员') rescue => e uid_logger_error("发送验证码出错: #{e}") end @@ -288,28 +319,28 @@ class SubjectsController < ApplicationController end end - def cancel_publish + def cancel_public begin apply = ApplyAction.where(container_type: "ApplySubject", container_id: @subject.id).order("created_at desc").first if apply && apply.status == 0 apply.update_attributes(status: 3) apply.tidings.destroy_all end - @subject.update_attributes(status: 0) + @subject.update_attributes(public: 0) + render_ok rescue => e uid_logger_error(e.message) tip_exception("撤销申请失败") - raise ActiveRecord::Rollback end end - def cancel_has_publish + def cancel_has_public begin - @subject.update_attributes(:status => 0) + @subject.update_attributes(:public => 0) + render_ok rescue => e uid_logger_error(e.message) - tip_exception("撤销发布失败") - raise ActiveRecord::Rollback + tip_exception("撤销公开失败") end end diff --git a/app/helpers/admins/subjects_helper.rb b/app/helpers/admins/subjects_helper.rb index 56738fd86..b2c1dbd55 100644 --- a/app/helpers/admins/subjects_helper.rb +++ b/app/helpers/admins/subjects_helper.rb @@ -6,6 +6,6 @@ module Admins::SubjectsHelper when 1 then 'text-warning' when 2 then 'text-success' end - raw content_tag(:span, t("subject.status.#{subject.status}"), class: style) + raw content_tag(:span, t("subject.public.#{subject.public}"), class: style) end end \ No newline at end of file diff --git a/app/models/subject.rb b/app/models/subject.rb index 99ba4b72b..70f9700df 100644 --- a/app/models/subject.rb +++ b/app/models/subject.rb @@ -40,6 +40,7 @@ class Subject < ApplicationRecord scope :visible, lambda{where(status: 2)} scope :published, lambda{where(status: 1)} scope :unhidden, lambda{where(hidden: 0)} + scope :publiced, lambda{ where(public: 2) } after_create :send_tiding def send_tiding diff --git a/app/queries/admins/subject_query.rb b/app/queries/admins/subject_query.rb index 935f52bb3..ba410312f 100644 --- a/app/queries/admins/subject_query.rb +++ b/app/queries/admins/subject_query.rb @@ -21,7 +21,7 @@ class Admins::SubjectQuery < ApplicationQuery when 'applying' then 1 when 'published' then 2 end - subjects = subjects.where(status: status) if status + subjects = subjects.where(public: status) if status # 创建者单位 if params[:school_id].present? diff --git a/app/queries/weapps/subject_query.rb b/app/queries/weapps/subject_query.rb index 5ec0c1ed9..8bfe9e9a8 100644 --- a/app/queries/weapps/subject_query.rb +++ b/app/queries/weapps/subject_query.rb @@ -8,7 +8,7 @@ class Weapps::SubjectQuery < ApplicationQuery end def call - subjects = @current_laboratory.subjects.unhidden.visible + subjects = @current_laboratory.subjects.unhidden.publiced # 课程体系的过滤 if params[:sub_discipline_id].present? diff --git a/app/services/admins/subject_auths/agree_apply_service.rb b/app/services/admins/subject_auths/agree_apply_service.rb index 465e3e903..ec5fec4bb 100644 --- a/app/services/admins/subject_auths/agree_apply_service.rb +++ b/app/services/admins/subject_auths/agree_apply_service.rb @@ -10,7 +10,7 @@ class Admins::SubjectAuths::AgreeApplyService < ApplicationService def call ActiveRecord::Base.transaction do apply.update!(status: 1, dealer_id: user.id) - subject.update!(status: 2, publish_time: Time.now) + subject.update!(public: 2, publish_time: Time.now) deal_tiding! end diff --git a/app/services/admins/subject_auths/refuse_apply_service.rb b/app/services/admins/subject_auths/refuse_apply_service.rb index f51d55185..45f2d44d3 100644 --- a/app/services/admins/subject_auths/refuse_apply_service.rb +++ b/app/services/admins/subject_auths/refuse_apply_service.rb @@ -10,7 +10,7 @@ class Admins::SubjectAuths::RefuseApplyService < ApplicationService def call ActiveRecord::Base.transaction do - subject.update!(status: 0) + subject.update!(public: 0) apply.update!(status: 2, reason: reason, dealer_id: user.id) deal_tiding! diff --git a/app/views/subjects/apply_public.json.jbuilder b/app/views/subjects/apply_public.json.jbuilder new file mode 100644 index 000000000..b2266356f --- /dev/null +++ b/app/views/subjects/apply_public.json.jbuilder @@ -0,0 +1,2 @@ +json.status @status +json.message @status == 0 ? "已公开过申请,请等待管理员审核" : "公开申请已提交,请等待管理员的审核" \ No newline at end of file diff --git a/app/views/subjects/cancel_has_publish.json.jbuilder b/app/views/subjects/cancel_has_publish.json.jbuilder index 60f06b5df..224327f30 100644 --- a/app/views/subjects/cancel_has_publish.json.jbuilder +++ b/app/views/subjects/cancel_has_publish.json.jbuilder @@ -1,2 +1,2 @@ -json.status 1 +json.status 0 json.subject_id @subject.id \ No newline at end of file diff --git a/app/views/subjects/publish.json.jbuilder b/app/views/subjects/publish.json.jbuilder index 8484543db..813c3391c 100644 --- a/app/views/subjects/publish.json.jbuilder +++ b/app/views/subjects/publish.json.jbuilder @@ -1,2 +1,2 @@ -json.status @status -json.message @status == 0 ? "已发布过申请,请等待管理员审核" : "发布申请已提交,请等待管理员的审核" \ No newline at end of file +json.status 0 +json.message "申请成功" \ No newline at end of file diff --git a/config/locales/subjects/zh-CN.yml b/config/locales/subjects/zh-CN.yml index 648bd4320..55d3dd9c0 100644 --- a/config/locales/subjects/zh-CN.yml +++ b/config/locales/subjects/zh-CN.yml @@ -1,6 +1,10 @@ 'zh-CN': subject: status: + '0': 编辑中 + '1': 审核中 + '2': 已发布 + public: '0': 编辑中 '1': 审核中 '2': 已发布 \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 9c6cc1c35..9455f7719 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -406,9 +406,12 @@ Rails.application.routes.draw do resources :subjects, path: :paths do member do get 'choose_subject_shixun' - get 'publish' - get 'cancel_publish' - get 'cancel_has_publish' + post 'publish' + post :apply_public + post :cancel_public + post 'cancel_publish' + post 'cancel_has_publish' + post :cancel_has_public get 'statistics' get 'statistics_info' get 'shixun_report' diff --git a/db/migrate/20200220023507_add_public_for_subjects.rb b/db/migrate/20200220023507_add_public_for_subjects.rb new file mode 100644 index 000000000..1783ca4af --- /dev/null +++ b/db/migrate/20200220023507_add_public_for_subjects.rb @@ -0,0 +1,5 @@ +class AddPublicForSubjects < ActiveRecord::Migration[5.2] + def change + add_column :subjects, :public, :integer, :default => 0 + end +end diff --git a/db/migrate/20200220023756_migrate_subject_status.rb b/db/migrate/20200220023756_migrate_subject_status.rb new file mode 100644 index 000000000..e036e6cdb --- /dev/null +++ b/db/migrate/20200220023756_migrate_subject_status.rb @@ -0,0 +1,7 @@ +class MigrateSubjectStatus < ActiveRecord::Migration[5.2] + def change + Subject.unhidden.visible.update_all(public: 2) + Subject.where(status: 1, id: ApplyAction.where(container_type: 'ApplySubject', status: 0) + .pluck(:container_id)).update_all(status: 2, public: 1) + end +end