shcool/learning_center/models/course_homework_submit.py
2026-06-16 19:21:41 +08:00

129 lines
5.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from odoo import api, fields, models
class CourseHomeworkSubmit(models.Model):
_name = 'course.homework.submit'
_description = '作业提交记录'
_rec_name = 'display_name'
_inherit = ['mail.thread']
_order = 'homework_id, student_id'
display_name = fields.Char(string='显示名称', compute='_compute_display_name', store=True)
student_id = fields.Many2one('student.info', string='学生', required=True)
homework_id = fields.Many2one('course.homework', string='作业', required=True, ondelete='cascade')
@api.depends('homework_id.name', 'student_id.stu_name')
def _compute_display_name(self):
for record in self:
record.display_name = f"{record.student_id.stu_name} - {record.homework_id.name}"
course_name = fields.Char(related="homework_id.course_id.name", string="课程名称", readonly=True)
course_code = fields.Char(related="homework_id.course_id.code", string="课程代码", readonly=True)
course_credit = fields.Float(related="homework_id.course_id.credit", string="学分", readonly=True)
homework_name = fields.Char(related="homework_id.name", string="作业名称", readonly=True)
homework_total_score = fields.Float(related="homework_id.total_score", string="作业总分", readonly=True)
homework_deadline = fields.Datetime(related="homework_id.deadline", string="截止时间", readonly=True)
homework_late_deadline = fields.Datetime(related="homework_id.late_deadline", string="补交截止时间", readonly=True)
student_name = fields.Char(related="student_id.stu_name", string="姓名", readonly=True)
student_no = fields.Char(related="student_id.stu_num", string="学号", readonly=True)
student_class_id = fields.Many2one(related="student_id.class_id", string="班级", readonly=True)
student_major_id = fields.Many2one(related="student_id.major_id", string="专业", readonly=True)
student_phone = fields.Char(related="student_id.stu_phone", string="手机号", readonly=True)
submit_file = fields.Binary(string='提交文件', attachment=True)
submit_filename = fields.Char(string='文件名')
submit_content = fields.Html(string='提交内容')
submit_time = fields.Datetime(string='提交时间')
score = fields.Float(string='得分')
comment = fields.Html(string='批改评语')
grader_id = fields.Many2one('res.users', string='批改人')
grade_time = fields.Datetime(string='批改时间')
state = fields.Selection([
('unsubmit', '未提交'),
('submitted', '已提交'),
('graded', '已批改'),
], string='状态', default='submitted')
is_late = fields.Boolean(string='是否逾期', compute='_compute_is_late')
def _compute_is_late(self):
for record in self:
deadline = record.homework_id.deadline
if deadline and record.submit_time:
record.is_late = record.submit_time > deadline
else:
record.is_late = False
def action_grade(self):
self.state = 'graded'
self.grade_time = fields.Datetime.now()
def action_regrade(self):
self.state = 'submitted'
self.grade_time = False
# 在 CourseHomeworkSubmit 类中添加
grade_level = fields.Selection([
('A', '优秀(A)'),
('B', '良好(B)'),
('C', '中等(C)'),
('D', '及格(D)'),
('F', '不及格(F)'),
], string='等级', compute='_compute_grade_level', store=True)
@api.depends('score', 'homework_id.total_score')
def _compute_grade_level(self):
for record in self:
if record.score and record.homework_id.total_score:
percent = record.score / record.homework_id.total_score * 100
if percent >= 90:
record.grade_level = 'A'
elif percent >= 80:
record.grade_level = 'B'
elif percent >= 70:
record.grade_level = 'C'
elif percent >= 60:
record.grade_level = 'D'
else:
record.grade_level = 'F'
else:
record.grade_level = False
@api.model
def create(self, vals):
submit_rec = super().create(vals)
self._check_submit_change_state(submit_rec)
return submit_rec
def write(self, vals):
res = super().write(vals)
for rec in self:
self._check_submit_change_state(rec)
return res
def _check_submit_change_state(self, submit_rec):
"""统一提取状态判断逻辑create和write共用"""
# 仅空白未提交状态才自动切换
if submit_rec.state != 'unsubmit':
return
# 判断:有提交文件 或者 填写了提交内容 = 完成提交
has_file = bool(submit_rec.submit_file)
has_content = bool(submit_rec.submit_content)
if has_file or has_content:
submit_rec.write({
'state': 'submitted',
'submit_time': fields.Datetime.now()
})
# class CourseHomeworkSubmitBatchGrade(models.TransientModel):
# _name = 'course.homework.submit.batch.grade'
# _description = '作业批量批改向导'
#
# homework_id = fields.Many2one('course.homework', string='作业')
# total_count = fields.Integer(string='待批改总数')
# score = fields.Float(string='统一分数')
# comment = fields.Text(string='统一评语')
#
# # 批量批改执行方法
# def action_batch_grade(self):
# # 自行补充批量更新分数、评语、状态的业务逻辑
# return {'type': 'ir.actions.act_window_close'}