From 4e5d87f41cba54b3ef0fd73c76a1a4c8256276b7 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Tue, 13 Aug 2019 11:33:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E8=AF=BE=E5=A0=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/application_controller.rb | 4 ++ app/controllers/courses_controller.rb | 48 +++++++++++++------ app/models/course.rb | 27 +++++++---- app/models/subject.rb | 8 ++++ ...20190813015633_add_new_column_to_course.rb | 8 ++++ 5 files changed, 70 insertions(+), 25 deletions(-) create mode 100644 db/migrate/20190813015633_add_new_column_to_course.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index db6ae7b57..f251b91d0 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -567,6 +567,10 @@ class ApplicationController < ActionController::Base time.blank? ? '' : time.strftime("%Y-%m-%d %H:%M:%S") end + def strf_date(date) + date.blank? ? '' : date.strftime("%Y-%m-%d") + end + def logger_error(error) Rails.logger.error(error.message) error.backtrace.each { |msg| Rails.logger.error(msg) } diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index 75d515186..440c25a40 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -117,13 +117,18 @@ class CoursesController < ApplicationController authentication: params[:authentication], professional_certification: params[:professional_certification]) @course.tea_id = current_user.id - @course_list_name = params[:course_list_name].strip - @course_list = CourseList.find_by(name: @course_list_name) - if @course_list - @course.course_list_id = @course_list.id + if params[:subject_id].blank? + @course_list_name = params[:course_list_name].strip + @course_list = CourseList.find_by(name: @course_list_name) + if @course_list + @course.course_list_id = @course_list.id + else + new_course_list = CourseList.create!(name: @course_list_name, user_id: current_user.id, is_admin: 0) + @course.course_list_id = new_course_list.id + end else - new_course_list = CourseList.create!(name: @course_list_name, user_id: current_user.id, is_admin: 0) - @course.course_list_id = new_course_list.id + @course.start_date = params[:start_date] + @course.subject_id = params[:subject_id] end @course.is_end = @course.end_date.present? && @course.end_date < Date.today @@ -151,7 +156,6 @@ class CoursesController < ApplicationController begin extra_params = Hash.new extra_params[:school_id] = @school.id - extra_params[:is_public] = params[:is_public].present? ? params[:is_public] : 0 if @course.is_end && (course_params[:end_date].blank? || course_params[:end_date].to_date > Date.today) extra_params[:is_end] = 0 @@ -162,13 +166,18 @@ class CoursesController < ApplicationController extra_params[:authentication] = params[:authentication] extra_params[:professional_certification] = params[:professional_certification] - @course_list_name = params[:course_list_name].strip - @course_list = CourseList.find_by(name: @course_list_name) - if @course_list - extra_params[:course_list_id] = @course_list.id + if @course.subject + @course.start_date = params[:start_date] else - new_course_list = CourseList.create(name: @course_list_name, user_id: current_user.id, is_admin: 0) - extra_params[:course_list_id] = new_course_list.id + extra_params[:is_public] = params[:is_public].present? ? params[:is_public] : 0 + @course_list_name = params[:course_list_name].strip + @course_list = CourseList.find_by(name: @course_list_name) + if @course_list + extra_params[:course_list_id] = @course_list.id + else + new_course_list = CourseList.create(name: @course_list_name, user_id: current_user.id, is_admin: 0) + extra_params[:course_list_id] = new_course_list.id + end end @course.update_attributes!(course_params.merge(extra_params)) @@ -1075,9 +1084,18 @@ class CoursesController < ApplicationController def validate_course_name tip_exception("课堂名称不能为空!") if params[:course][:name].blank? - tip_exception("课程名称不能为空!") if params[:course_list_name].blank? - tip_exception("课堂名称应以课程名称开头命名") unless params[:course][:name].index(params[:course_list_name]) && + if params[:subject_id].blank? + tip_exception("课程名称不能为空!") if params[:course_list_name].blank? + tip_exception("课堂名称应以课程名称开头命名") unless params[:course][:name].index(params[:course_list_name]) && params[:course][:name].index(params[:course_list_name]) == 0 + else + @subject = Subject.find_by!(id: params[:subject_id]) + tip_exception("开始时间不能为空") if params[:start_date].blank? + tip_exception("结束时间不能为空") if params[:end_date].blank? + tip_exception("结束时间必须晚于开始时间") if params[:end_date] <= params[:start_date] + tip_exception("开始时间和结束时间不能与往期开课时间重叠") if @subject.max_course_end_date && params[:start_date] <= strf_date(@subject.max_course_end_date) + tip_exception("开放课堂必须包含公告栏和在线学习模块") unless params[:course_module_types].include?("announcement") && params[:course_module_types].include?("online_learning") + end tip_exception("课堂所属单位不能为空!") if params[:school].blank? tip_exception("请至少添加一个课堂模块") if params[:course_module_types].blank? @school = School.find_by!(name: params[:school].strip) diff --git a/app/models/course.rb b/app/models/course.rb index 25a017faa..7025b05d5 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -7,6 +7,9 @@ class Course < ApplicationRecord belongs_to :school, class_name: 'School', foreign_key: :school_id #定义一个方法school,该方法通过school_id来调用School表 belongs_to :course_list + # 所属实践课程 + belongs_to :subject, optional: true + has_many :course_infos, dependent: :destroy # 课堂左侧导航栏的模块 has_many :course_modules, dependent: :destroy @@ -176,7 +179,7 @@ class Course < ApplicationRecord end def all_course_module_types - %w[activity shixun_homework common_homework group_homework graduation exercise poll attachment board course_group] + %w[activity announcement online_learning shixun_homework common_homework group_homework graduation exercise poll attachment board course_group] end def get_course_module_by_type(type) @@ -334,6 +337,8 @@ class Course < ApplicationRecord def get_name_by_type(type) case type when 'activity' then '动态' + when 'announcement' then '公告栏' + when 'online_learning' then '在线学习' when 'shixun_homework' then '实训作业' when 'common_homework' then '普通作业' when 'group_homework' then '分组作业' @@ -350,15 +355,17 @@ class Course < ApplicationRecord def get_position_by_type(type) case type when 'activity' then 1 - when 'shixun_homework' then 2 - when 'common_homework' then 3 - when 'group_homework' then 4 - when 'graduation' then 5 - when 'exercise' then 6 - when 'poll' then 7 - when 'attachment' then 8 - when 'board' then 9 - when 'course_group' then 10 + when 'announcement' then 2 + when 'online_learning' then 3 + when 'shixun_homework' then 4 + when 'common_homework' then 5 + when 'group_homework' then 6 + when 'graduation' then 7 + when 'exercise' then 8 + when 'poll' then 9 + when 'attachment' then 10 + when 'board' then 11 + when 'course_group' then 12 else 100 end end diff --git a/app/models/subject.rb b/app/models/subject.rb index eb8930e3b..3ef513416 100644 --- a/app/models/subject.rb +++ b/app/models/subject.rb @@ -18,6 +18,9 @@ class Subject < ApplicationRecord has_many :tidings, as: :container, dependent: :destroy has_many :stages, -> { order("stages.position ASC") }, dependent: :destroy + # 开放课堂 + has_many :courses + validates :name, length: { maximum: 40 } validates :description, length: { maximum: 5000 } validates :learning_notes, length: { maximum: 500 } @@ -31,6 +34,11 @@ class Subject < ApplicationRecord self.tidings << Tiding.new(user_id: self.user_id, trigger_user_id: self.user_id, belong_container_id: self.id, belong_container_type: 'Subject', tiding_type: "System", viewed: 0) end + # 所有开课课堂的最大结束时间 + def max_course_end_date + courses.pluck(:end_date).max + end + # 挑战过路径的成员数 def member_count shixuns.pluck(:myshixuns_count).sum diff --git a/db/migrate/20190813015633_add_new_column_to_course.rb b/db/migrate/20190813015633_add_new_column_to_course.rb new file mode 100644 index 000000000..cfadc4ce4 --- /dev/null +++ b/db/migrate/20190813015633_add_new_column_to_course.rb @@ -0,0 +1,8 @@ +class AddNewColumnToCourse < ActiveRecord::Migration[5.2] + def change + add_column :courses, :start_date, :date + add_column :courses, :subject_id, :integer, default: 0 + + add_index :courses, :subject_id + end +end