You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/models/course_attendance.rb

70 lines
2.2 KiB

5 years ago
class CourseAttendance < ApplicationRecord
5 years ago
# status: 0: 未开启1已开启2已截止
5 years ago
# mode: 0 两种签到1 二维码签到2 数字签到3 快捷签到
5 years ago
enum mode: { ALL: 0, QRCODE: 1, NUMBER: 2, QUICK: 3 }
5 years ago
5 years ago
belongs_to :course
belongs_to :user
has_many :course_attendance_groups, dependent: :destroy
5 years ago
has_many :course_member_attendances, dependent: :destroy
5 years ago
has_one :course_act, class_name: 'CourseActivity', as: :course_act, dependent: :destroy
5 years ago
validates :name, presence: true, length: { maximum: 60, too_long: "不能超过60个字符" }
5 years ago
validates :mode, presence: true
validates :attendance_date, presence: true
validates :start_time, presence: true
validates :end_time, presence: true
after_create :generate_attendance_code, :act_as_course_activity
5 years ago
5 years ago
# 正常签到人数
def normal_count
course_member_attendances.select{|member_attendance| member_attendance.attendance_status == "NORMAL"}.size
end
# 请假人数
def leave_count
course_member_attendances.select{|member_attendance| member_attendance.attendance_status == "LEAVE"}.size
end
# 旷课人数
def absence_count
course_member_attendances.select{|member_attendance| member_attendance.attendance_status == "ABSENCE"}.size
end
5 years ago
# 总人数
def all_count
course_member_attendances.size
end
5 years ago
def current_attendance?
a_start_time = "#{attendance_date} #{start_time}".to_time
a_end_time = "#{attendance_date} #{end_time}".to_time
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
5 years ago
# 延迟生成邀请码
def attendance_code
return generate_attendance_code
end
# 生成邀请码
CODES = %W(2 3 4 5 6 7 8 9 A B C D E F G H J K L N M O P Q R S T U V W X Y Z)
def generate_attendance_code
code = read_attribute(:attendance_code)
if !code || code.size < 4
code = CODES.sample(4).join
return generate_attendance_code if CourseAttendance.where(attendance_code: code).present?
update_attribute(:attendance_code, code)
end
code
end
5 years ago
end