From 9bde634789e6c2877f3de7f5fcbbb6585d9ade77 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Thu, 5 Mar 2020 01:19:58 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BE=E5=A0=82=E7=AD=BE=E5=88=B0=E5=8A=A8?= =?UTF-8?q?=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/weapps/courses_controller.rb | 20 +++++++++++++++++--- app/models/course_activity.rb | 9 +++++---- app/models/course_attendance.rb | 9 ++++++++- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/app/controllers/weapps/courses_controller.rb b/app/controllers/weapps/courses_controller.rb index a0ee59971..2fa3e0693 100644 --- a/app/controllers/weapps/courses_controller.rb +++ b/app/controllers/weapps/courses_controller.rb @@ -8,18 +8,32 @@ class Weapps::CoursesController < Weapps::BaseController 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) + + # 签到数据 + attendances = @course.course_attendances + current_date = Date.current + current_end_time = Time.current.strftime("%H:%M:%S") + if @user_course_identity == Course::STUDENT + attendances = attendances.joins(:course_attendance_groups).where(course_attendance_groups: {course_group_id: member.try(:course_group_id).to_i}) + .where("attendance_date < '#{current_date}' or (attendance_date = '#{current_date}' and start_time < '#{current_end_time}')") + end + attendance_ids = attendances.blank? ? "(-1)" : "(" + attendances.pluck(:id).join(",") + ")" + + homework_commons = @course.homework_commons.where(homework_type: ["practice", "normal"]).homework_published 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) + 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") + (course_act_type = 'HomeworkCommon' and course_act_id in #{homework_ids}) or + (course_act_type = 'CourseAttendance' and course_act_id in #{attendance_ids})").order("id desc") @activities_count = activities.size @activities = paginate activities.includes(:course_act, user: :user_extension) end diff --git a/app/models/course_activity.rb b/app/models/course_activity.rb index d9930596c..e31da31a8 100644 --- a/app/models/course_activity.rb +++ b/app/models/course_activity.rb @@ -2,10 +2,11 @@ class CourseActivity < ApplicationRecord belongs_to :course_act, polymorphic: true belongs_to :course belongs_to :user - belongs_to :exercise - belongs_to :poll - belongs_to :course_message - belongs_to :homework_common + belongs_to :exercise, optional: true + belongs_to :poll, optional: true + belongs_to :course_message, optional: true + belongs_to :homework_common, optional: true + belongs_to :course_attendance, optional: true # after_create :add_course_lead diff --git a/app/models/course_attendance.rb b/app/models/course_attendance.rb index 72a09750c..1b1d1ed2f 100644 --- a/app/models/course_attendance.rb +++ b/app/models/course_attendance.rb @@ -9,13 +9,15 @@ class CourseAttendance < ApplicationRecord has_many :course_attendance_groups, dependent: :destroy has_many :course_member_attendances, dependent: :destroy + has_one :course_act, class_name: 'CourseActivity', as: :course_act, dependent: :destroy + validates :name, presence: true validates :mode, presence: true validates :attendance_date, presence: true validates :start_time, presence: true validates :end_time, presence: true - after_create :generate_attendance_code + after_create :generate_attendance_code, :act_as_course_activity # 正常签到人数 def normal_count @@ -43,6 +45,11 @@ class CourseAttendance < ApplicationRecord a_start_time < Time.current && Time.current < a_end_time end + #课程动态公共表记录 + def act_as_course_activity + CourseActivity.create(user_id: user_id, course_id: course_id, course_act: self) + end + # 延迟生成邀请码 def attendance_code return generate_attendance_code