From 486776fd17b71dd51ca595c5962f6792bc4b4f09 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Wed, 26 Feb 2020 12:14:15 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=B0=8F=E7=A8=8B=E5=BA=8F=E7=9A=84?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E9=A6=96=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/homework_commons_controller.rb | 4 ++-- app/controllers/weapps/courses_controller.rb | 17 +++++++++++++++++ app/models/course.rb | 6 ++++-- app/models/course_activity.rb | 17 +++++++++++++++++ app/models/course_message.rb | 8 ++++++++ app/models/homework_common.rb | 2 +- .../courses/course_activities.json.jbuilder | 12 ++++++++++++ config/routes.rb | 7 ++++--- ...20200226031118_migrate_course_message_act.rb | 5 +++++ lib/tasks/homework_publishtime.rake | 2 +- 10 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 app/views/weapps/courses/course_activities.json.jbuilder create mode 100644 db/migrate/20200226031118_migrate_course_message_act.rb diff --git a/app/controllers/homework_commons_controller.rb b/app/controllers/homework_commons_controller.rb index 363e5780d..082c8118d 100644 --- a/app/controllers/homework_commons_controller.rb +++ b/app/controllers/homework_commons_controller.rb @@ -983,8 +983,8 @@ class HomeworkCommonsController < ApplicationController end homework.homework_detail_manual.update_attributes!(comment_status: 1) - if homework.course_acts.size == 0 - homework.course_acts << CourseActivity.new(user_id: homework.user_id, course_id: homework.course_id) + if homework.course_act.blank? + homework.course_act << CourseActivity.new(user_id: homework.user_id, course_id: homework.course_id) end # 发消息 HomeworkCommonPushNotifyJob.perform_later(homework.id, tiding_group_ids) diff --git a/app/controllers/weapps/courses_controller.rb b/app/controllers/weapps/courses_controller.rb index e3af2310d..ccd5d1bcf 100644 --- a/app/controllers/weapps/courses_controller.rb +++ b/app/controllers/weapps/courses_controller.rb @@ -6,6 +6,23 @@ class Weapps::CoursesController < Weapps::BaseController before_action :teacher_allowed, only: [:edit, :update] before_action :teacher_or_admin_allowed, only: [:change_member_roles, :delete_course_teachers] + def course_activities + @course = current_course + homework_commons = @course.homework_commons.where(homework_type: ["practice", "normal"]).homework_published + member = @course.course_members.find_by(user_id: current_user.id, is_active: 1) + if (@user_course_identity == Course::STUDENT && member.try(:course_group_id).to_i == 0) || @user_course_identity > Course::STUDENT + homework_commons = homework_commons.unified_setting + elsif @user_course_identity == Course::STUDENT + not_homework_ids = @course.homework_group_settings.none_published.where("course_group_id = #{member.try(:course_group_id)}").pluck(:homework_common_id) + homework_commons = homework_commons.where.not(id: not_homework_ids) + end + homework_ids = homework_commons.blank? ? "(-1)" : "(" + homework_commons.pluck(:id).join(",") + ")" + + activities = @course.course_activities.where("course_act_type in ('Course', 'CourseMessage') or + (course_act_type = 'HomeworkCommon' and course_act_id in #{homework_ids})").order("id desc") + @activities = paginate activities.includes(:course_act, user: :user_extension) + end + def create # return render_error("只有老师身份才能创建课堂") unless current_user.is_teacher? course = Course.new(tea_id: current_user.id) diff --git a/app/models/course.rb b/app/models/course.rb index 8de6ef9b6..08e2e36f5 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -70,7 +70,9 @@ class Course < ApplicationRecord # 课堂动态 - has_many :course_acts, class_name: 'CourseActivity', as: :course_act, dependent: :destroy + has_one :course_act, class_name: 'CourseActivity', as: :course_act, dependent: :destroy + has_many :course_activities + has_many :tidings, as: :container, dependent: :destroy # 开放课堂 @@ -300,7 +302,7 @@ class Course < ApplicationRecord #课程动态公共表记录 def act_as_course_activity - self.course_acts << CourseActivity.new(user_id: tea_id, course_id: id) + self.course_act << CourseActivity.new(user_id: tea_id, course_id: id) end # 当前老师分班下的所有学生 diff --git a/app/models/course_activity.rb b/app/models/course_activity.rb index 6c0aaaf48..d9930596c 100644 --- a/app/models/course_activity.rb +++ b/app/models/course_activity.rb @@ -4,9 +4,26 @@ class CourseActivity < ApplicationRecord belongs_to :user belongs_to :exercise belongs_to :poll + belongs_to :course_message + belongs_to :homework_common # after_create :add_course_lead + def container_name + case course_act_type + when "HomeworkCommon" + course_act&.name + when "Exercise" + course_act&.exercise_name + when "Poll" + course_act&.poll_name + when "Message" + course_act&.subject + else + "" + end + end + # 发布新课导语 # 导语要放置在课程创建信息之后 def add_course_lead diff --git a/app/models/course_message.rb b/app/models/course_message.rb index a3578d500..b09613ec4 100644 --- a/app/models/course_message.rb +++ b/app/models/course_message.rb @@ -2,6 +2,7 @@ class CourseMessage < ApplicationRecord enum status: { UNHANDLED: 0, PASSED: 1, REJECTED: 2 } belongs_to :course belongs_to :user + has_one :course_act, class_name: 'CourseActivity', as: :course_act, dependent: :destroy scope :find_by_course, ->(course) { where(course_id: course.id) } scope :join_course_requests, -> { where(course_message_type: "JoinCourseRequest") } @@ -9,6 +10,8 @@ class CourseMessage < ApplicationRecord scope :unhandled_join_course_requests_by_course, ->(course) { find_by_course(course).join_course_requests.unhandled } + after_create :act_as_course_activity + def pass! update!(status: :PASSED) send_deal_tiding(1) @@ -25,6 +28,11 @@ class CourseMessage < ApplicationRecord private + #课程动态公共表记录 + def act_as_course_activity + self.course_act << CourseActivity.new(user_id: course_message_id, course_id: course_id) + end + def send_deal_tiding deal_status # 发送申请处理结果消息 Tiding.create!( diff --git a/app/models/homework_common.rb b/app/models/homework_common.rb index de245517d..f3a2e85f8 100644 --- a/app/models/homework_common.rb +++ b/app/models/homework_common.rb @@ -28,7 +28,7 @@ class HomeworkCommon < ApplicationRecord belongs_to :course_second_category, optional: true # 课堂动态 - has_many :course_acts, class_name: 'CourseActivity', as: :course_act, dependent: :destroy + has_one :course_act, class_name: 'CourseActivity', as: :course_act, dependent: :destroy has_many :tidings, as: :container, dependent: :destroy # 实训作业的分班查重记录 has_many :homework_group_reviews, :dependent => :destroy diff --git a/app/views/weapps/courses/course_activities.json.jbuilder b/app/views/weapps/courses/course_activities.json.jbuilder new file mode 100644 index 000000000..498189cf8 --- /dev/null +++ b/app/views/weapps/courses/course_activities.json.jbuilder @@ -0,0 +1,12 @@ +json.activities @activities do |activity| + json.(activity, :course_act_id, :course_act_type) + json.author do + user = activity.user + json.name user.real_name + json.login user.login + json.img url_to_avatar(user) + end + json.created_at activity.created_at.strftime('%m-%d %H:%M:') + json.container_name activity.container_name + json.container_type activity.course_act_type == "HomeworkCommon" ? activity.course_act&.homework_type : "" +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 33576e1d0..7592432dd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1040,12 +1040,13 @@ Rails.application.routes.draw do member do get :shixun_homework_category get :teachers - delete :delete_course_teachers - post :change_member_roles get :students - delete :delete_course_students get :course_groups get :basic_info + get :course_activities + post :change_member_roles + delete :delete_course_teachers + delete :delete_course_students end collection do diff --git a/db/migrate/20200226031118_migrate_course_message_act.rb b/db/migrate/20200226031118_migrate_course_message_act.rb new file mode 100644 index 000000000..18bc276bc --- /dev/null +++ b/db/migrate/20200226031118_migrate_course_message_act.rb @@ -0,0 +1,5 @@ +class MigrateCourseMessageAct < ActiveRecord::Migration[5.2] + def change + CourseActivity.where(course_act_type: "JoinCourse").update_all(course_act_type: "CourseMessage") + end +end diff --git a/lib/tasks/homework_publishtime.rake b/lib/tasks/homework_publishtime.rake index 6ba4d7bba..180afcdc2 100644 --- a/lib/tasks/homework_publishtime.rake +++ b/lib/tasks/homework_publishtime.rake @@ -45,7 +45,7 @@ namespace :homework_publishtime do end end - homework.course_acts << CourseActivity.new(user_id: homework.user_id, course_id: homework.course_id) if !homework.course_acts.exists? + homework.course_act << CourseActivity.new(user_id: homework.user_id, course_id: homework.course_id) if !homework.course_act.present? end # 分组设置发布时间的作业 From bb5dcfd7e31e4e7016771971d6bc94251a03bf7e Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Wed, 26 Feb 2020 12:24:31 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=99=BA=E8=83=BD=E7=BB=84=E5=8D=B7?= =?UTF-8?q?=E5=BA=94=E6=8E=92=E9=99=A4=E6=9C=AA=E5=8F=91=E5=B8=83=E7=9A=84?= =?UTF-8?q?=E7=BC=96=E7=A8=8B=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/queries/optional_item_query.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/queries/optional_item_query.rb b/app/queries/optional_item_query.rb index 61ebf8b69..a12b62b93 100644 --- a/app/queries/optional_item_query.rb +++ b/app/queries/optional_item_query.rb @@ -19,7 +19,7 @@ class OptionalItemQuery < ApplicationQuery end if hacks.present? - items = ItemBank.where(container_id: hacks.pluck(:id), container_type: "Hack").or(ItemBank.where(id: items.pluck(:id))) + items = ItemBank.where(container_id: hacks.where(status: 1).pluck(:id), container_type: "Hack").or(ItemBank.where(id: items.pluck(:id))) end public = source.present? ? source.to_i : 1