diff --git a/app/controllers/homework_commons_controller.rb b/app/controllers/homework_commons_controller.rb index 1c6b606e5..8e3436aa6 100644 --- a/app/controllers/homework_commons_controller.rb +++ b/app/controllers/homework_commons_controller.rb @@ -453,7 +453,9 @@ class HomeworkCommonsController < ApplicationController # 课堂结束后不能再更新 unless @course.is_end + UpdateHomeworkSettingService.call(@homework, publish_params) # 作业未发布时,unified_setting参数不能为空 +=begin if @homework.publish_time.nil? || @homework.publish_time > Time.now tip_exception("缺少统一设置的参数") if params[:unified_setting].nil? if params[:unified_setting] || @course.course_groups_count == 0 @@ -549,6 +551,7 @@ class HomeworkCommonsController < ApplicationController @homework.end_time = @homework.max_group_end_time end end +=end # 补交设置 tip_exception("缺少allow_late参数") if params[:allow_late].nil? @@ -635,7 +638,7 @@ class HomeworkCommonsController < ApplicationController @homework.save! if score_change && @homework.end_or_late_none_group - UpdateShixunWorkScoreJob.perform_now(@homework.id) + UpdateShixunWorkScoreJob.perform_later(@homework.id) elsif update_eff_score && (@homework.end_or_late_none_group || @homework.max_efficiency > 0) # 更新所有学生的效率分(作业允许补交且补交已截止 或者 作业不允许补交且提交已截止 或者作业已计算过效率分) HomeworksService.new.update_student_eff_score HomeworkCommon.find_by(id: @homework.id) @@ -826,12 +829,14 @@ class HomeworkCommonsController < ApplicationController end end - - HomeworkCommonPushNotifyJob.perform_later(@homework.id, publish_group_ids) if send_tiding normal_status(0, "更新成功") else tip_exception("课堂已结束不能再更新") end + rescue ApplicationService::Error => ex + uid_logger(ex.message) + render_error(ex.message) + raise ActiveRecord::Rollback rescue Exception => e uid_logger(e.backtrace) tip_exception(e.message) @@ -1653,4 +1658,8 @@ class HomeworkCommonsController < ApplicationController homework_bank end + def publish_params + params.permit(:unified_setting, :publish_time, :end_time, group_settings: []) + end + end diff --git a/app/services/update_homework_publish_setting_service.rb b/app/services/update_homework_publish_setting_service.rb new file mode 100644 index 000000000..10287ce64 --- /dev/null +++ b/app/services/update_homework_publish_setting_service.rb @@ -0,0 +1,110 @@ +class UpdateHomeworkSettingService < ApplicationService + attr_reader :homework, :params + + def initialize(homework, params) + @params = params + @homework = homework + end + + def call + course = homework.course + # 作业未发布时,unified_setting参数不能为空 + if homework.publish_time.nil? || homework.publish_time > Time.now + raise(Error, "缺少统一设置的参数") if params[:unified_setting].nil? + if params[:unified_setting] || course.course_groups_count == 0 + raise(Error, "发布时间不能为空") if params[:publish_time].blank? + raise(Error, "截止时间不能为空") if params[:end_time].blank? + raise(Error, "发布时间不能早于当前时间") if params[:publish_time] <= Time.now.strftime("%Y-%m-%d %H:%M:%S") + raise(Error, "截止时间不能早于当前时间") if params[:end_time] <= Time.now.strftime("%Y-%m-%d %H:%M:%S") + raise(Error, "截止时间不能早于发布时间") if params[:publish_time] > params[:end_time] + raise(Error, "截止时间不能晚于课堂结束时间(#{course.end_date.end_of_day.strftime("%Y-%m-%d %H:%M")})") if + course.end_date.present? && params[:end_time] > course.end_date.end_of_day + + homework.unified_setting = 1 + homework.homework_group_settings.destroy_all + homework.publish_time = params[:publish_time] + # 截止时间为空时取发布时间后一个月 + homework.end_time = params[:end_time] + + else + raise(Error, "分班发布设置不能为空") if params[:group_settings].blank? + # 创建作业的分班设置 + create_homework_group_settings homework + + setting_group_ids = [] + + params[:group_settings].each do |setting| + raise(Error, "分班id不能为空") if setting[:group_id].length == 0 + raise(Error, "发布时间不能为空") if setting[:publish_time].blank? + raise(Error, "截止时间不能为空") if setting[:end_time].blank? + raise(Error, "发布时间不能早于当前时间") if setting[:publish_time] <= strf_time(Time.now) + raise(Error, "截止时间不能早于当前时间") if setting[:end_time] <= strf_time(Time.now) + raise(Error, "截止时间不能早于发布时间") if setting[:publish_time] > setting[:end_time] + raise(Error, "截止时间不能晚于课堂结束时间(#{course.end_date.end_of_day.strftime("%Y-%m-%d %H:%M")})") if + course.end_date.present? && setting[:end_time] > course.end_date.end_of_day + + + publish_time = setting[:publish_time] == "" ? Time.now : setting[:publish_time] + # 截止时间为空时取发布时间后一个月 + end_time = setting[:end_time] + HomeworkGroupSetting.where(homework_common_id: homework.id, course_group_id: setting[:group_id]). + update_all(publish_time: publish_time, end_time: end_time) + setting_group_ids << setting[:group_id] + end + + # 未设置的分班:发布时间和截止时间都为nil + HomeworkGroupSetting.where.not(course_group_id: setting_group_ids).where(homework_common_id: homework.id). + update_all(publish_time: nil, end_time: nil) + + # 记录已发布需要发消息的分班 + publish_group_ids = HomeworkGroupSetting.where(homework_common_id: homework.id).group_published.pluck(:course_group_id) + + homework.unified_setting = 0 + homework.publish_time = homework.min_group_publish_time + homework.end_time = homework.max_group_end_time + end + + # 如果作业立即发布则更新状态、发消息 + if homework.publish_time <= Time.now and homework_detail_manual.comment_status == 0 + homework_detail_manual.comment_status = 1 + send_tiding = true + end + + # 作业在"提交中"状态时 + else + if homework.end_time > Time.now && homework.unified_setting + raise(Error, "截止时间不能为空") if params[:end_time].blank? + raise(Error, "截止时间不能早于当前时间") if params[:end_time] <= strf_time(Time.now) + raise(Error, "截止时间不能晚于课堂结束时间(#{course.end_date.end_of_day.strftime("%Y-%m-%d %H:%M")})") if + course.end_date.present? && params[:end_time] > strf_time(course.end_date.end_of_day) + + homework.end_time = params[:end_time] + + elsif !homework.unified_setting + create_homework_group_settings homework + raise(Error, "分班发布设置不能为空") if params[:group_settings].blank? + params[:group_settings].each do |setting| + group_settings = HomeworkGroupSetting.where(homework_common_id: homework.id, course_group_id: setting[:group_id]) + + raise(Error, "分班id不能为空") if setting[:group_id].length == 0 + raise(Error, "发布时间不能为空") if setting[:publish_time].blank? + raise(Error, "截止时间不能为空") if setting[:end_time].blank? + # 如果该发布规则 没有已发布的分班则需判断发布时间 + raise(Error, "发布时间不能早于等于当前时间") if setting[:publish_time] <= strf_time(Time.now) && group_settings.group_published.count == 0 + + raise(Error, "截止时间不能早于等于当前时间") if setting[:end_time] <= strf_time(Time.now) && group_settings.none_end.count > 0 + raise(Error, "截止时间不能早于发布时间") if setting[:publish_time] > setting[:end_time] + raise(Error, "截止时间不能晚于课堂结束时间(#{course.end_date.end_of_day.strftime("%Y-%m-%d %H:%M")})") if + course.end_date.present? && setting[:end_time] > strf_time(course.end_date.end_of_day) + + group_settings.none_published.update_all(publish_time: setting[:publish_time]) + group_settings.none_end.update_all(end_time: setting[:end_time]) + end + + homework.end_time = homework.max_group_end_time + end + end + homework.save! + HomeworkCommonPushNotifyJob.perform_later(homework.id, publish_group_ids) if send_tiding + end +end \ No newline at end of file