diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index 470315bdc..c8707d72e 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -26,14 +26,14 @@ class CoursesController < ApplicationController :base_info, :get_historical_courses, :create_group_by_importing_file, :attahcment_category_list,:export_member_scores_excel, :duplicate_course, :switch_to_teacher, :switch_to_assistant, :switch_to_student, :exit_course, - :informs, :update_informs, :online_learning, :update_task_position, :tasks_list, :join_excellent_course] + :informs, :update_informs, :new_informs, :online_learning, :update_task_position, :tasks_list, :join_excellent_course] before_action :user_course_identity, except: [:join_excellent_course, :index, :create, :new, :apply_to_join_course, :search_course_list, :get_historical_course_students, :mine, :search_slim, :board_list] before_action :teacher_allowed, only: [:update, :destroy, :settings, :search_teacher_candidate, :transfer_to_course_group, :delete_from_course, :search_users, :add_students_by_search, :get_historical_courses, :add_teacher_popup, :add_teacher] before_action :admin_allowed, only: [:set_invite_code_halt, :set_public_or_private, :change_course_admin, - :set_course_group, :create_group_by_importing_file, :update_informs, + :set_course_group, :create_group_by_importing_file, :update_informs, :new_informs, :update_task_position, :tasks_list] before_action :teacher_or_admin_allowed, only: [:graduation_group_list, :create_graduation_group, :join_graduation_group, :change_course_teacher, :export_member_scores_excel, :course_group_list, @@ -42,6 +42,7 @@ class CoursesController < ApplicationController before_action :find_board, only: :board_list before_action :validate_page_size, only: :mine before_action :course_tasks, only: [:tasks_list, :update_task_position] + before_action :validate_inform_params, only: [:update_informs, :new_informs] if RUBY_PLATFORM =~ /linux/ require 'simple_xlsx_reader' @@ -159,7 +160,7 @@ class CoursesController < ApplicationController CourseMember.create!(course_id: @course.id, user_id: s_member.user_id, role: 2) end - Inform.create(container: @course, description: @subject.learning_notes) + Inform.create(container: @course, description: @subject.learning_notes, name: "学习须知") end course_module_types = params[:course_module_types] @@ -234,14 +235,20 @@ class CoursesController < ApplicationController end def informs - + @informs = @course.informs end - def update_informs - tip_exception("公告内容不能为空") if params[:description].blank? - inform = @course.inform || Inform.new(container: @course) + def new_informs + inform = Inform.new(container: @course) + inform.name = params[:name] inform.description = params[:description] inform.save! + normal_status("创建成功") + end + + def update_informs + inform = @course.informs.find_by(id: params[:inform_id]) + inform.update_attributes!(name: params[:name], description: params[:description]) normal_status("更新成功") end @@ -1259,6 +1266,11 @@ class CoursesController < ApplicationController end end + def validate_inform_params + tip_exception("公告标题不能为空") if params[:name].blank? + tip_exception("公告内容不能为空") if params[:description].blank? + end + # def find_container # case params[:container_type] # when 'shixun_homework', 'common_homework', 'group_homework' diff --git a/app/models/course.rb b/app/models/course.rb index e2fc5140b..5a2a065ba 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -9,7 +9,7 @@ class Course < ApplicationRecord # 所属实践课程 belongs_to :subject, optional: true - has_one :inform, as: :container, dependent: :destroy + has_many :informs, as: :container, dependent: :destroy has_many :course_infos, dependent: :destroy # 课堂左侧导航栏的模块 diff --git a/app/models/inform.rb b/app/models/inform.rb index faf1d48e6..5caf80c5f 100644 --- a/app/models/inform.rb +++ b/app/models/inform.rb @@ -1,3 +1,6 @@ class Inform < ApplicationRecord belongs_to :container, polymorphic: true, optional: true + + validates :name, length: { maximum: 60 } + validates :description, length: { maximum: 5000 } end diff --git a/app/views/courses/informs.json.jbuilder b/app/views/courses/informs.json.jbuilder index d584be917..7fc396184 100644 --- a/app/views/courses/informs.json.jbuilder +++ b/app/views/courses/informs.json.jbuilder @@ -1 +1,5 @@ -json.description @course.inform&.description \ No newline at end of file +json.informs @informs do |inform| + json.id inform.id + json.name inform.name + json.description inform.description +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index de4f46a40..5ed8ecd69 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -349,6 +349,7 @@ Rails.application.routes.draw do post 'exit_course' get 'informs' post 'update_informs' + post 'new_informs' get 'online_learning' post 'join_excellent_course' get 'tasks_list'