class GraduationWork < ApplicationRecord # work_status: 0 未提交, 1 按时提交, 2 延时提交 belongs_to :user belongs_to :course belongs_to :project, optional: true belongs_to :graduation_task, optional: true belongs_to :commit_user, class_name: 'User', foreign_key: :commit_user_id, optional: true has_many :attachments, as: :container, dependent: :destroy has_many :tidings, as: :container, dependent: :destroy has_many :graduation_work_scores, dependent: :destroy has_many :graduation_work_comment_assignations, dependent: :destroy validates :description, length: { maximum: 5000 } scope :has_committed, lambda { where("work_status != 0") } # 未提交 scope :unfinished, -> {where(work_status: 0)} # 按时提交 scope :finished, -> {where(work_status: 1)} # 延迟提交 scope :delay_finished, -> {where(work_status: 2)} #根据graduation_task_id查找 scope :find_by_task, lambda{|ids| where(graduation_task_id: ids)} scope :find_by_task_user, lambda{|ids| where(user_id: ids)} before_save :set_work_score def set_work_score unless self.ultimate_score if self.teacher_score.present? && self.cross_score.nil? self.final_score = self.teacher_score elsif self.cross_score.present? && self.teacher_score.nil? self.final_score = self.cross_score elsif self.teacher_score.present? && self.cross_score.present? self.final_score = ((self.cross_score + self.teacher_score).to_f / 2.0).try(:round, 2) end if self.final_score score = self.final_score - self.late_penalty self.work_score = (score < 0 ? 0 : score).to_f.try(:round, 2) if score else self.work_score = nil end end end def delete_atta atta last_score = graduation_work_scores.where.not(score: nil).last atta.author_id == user_id && (!last_score.present? || last_score.try(:created_at) < atta.created_on) end # 分班名 def class_grouping_name CourseMember.find_by(user_id: self.user_id, course_id: self.course_id, role: 4).try(:course_group).try(:name) || '未分班' end # 分组名 def grouping_name self.group_id == 0 ? "--" : "分组#{self.group_id}" end #用户是否有查看分数的权限 def check_score_power? current_user, course_identity self.work_score.present? || course_identity < Course::STUDENT || self.user_id = current_user.id end # 作品是否能够分配指导老师 def assign_power?(course_identity) course_identity < Course::STUDENT && self.graduation_task.cross_comment.present? && self.graduation_task.comment_status == 2 end # 老师评阅分 def teacher_comment_score current_user, course_identity # 为提交 if self.work_status == 0 "--" else # 未打分 if self.teacher_score.nil? "未批阅" else # 是否有权限看 if self.check_score_power?(current_user, course_identity) format("%.1f", self.teacher_score.round(1)) else "**" end end end end # 交叉评阅分 def cross_comment_score current_user, course_identity if self.work_status == 0 "--" else if self.cross_score.nil? "未批阅" else if self.check_score_power?(current_user, course_identity) "#{format("%.1f", self.cross_score.round(1))}" else "**" end end end end def cross_comment_num graduation_work_scores.where(reviewer_role: 2).group_by(&:user_id).count end def scored? graduation_work_scores.where.not(reviewer_role: 3).exists? end end