from odoo import models, fields MODULE_SELECTION = [ ("reading", "Reading"), ("listening", "Listening"), ("writing", "Writing"), ("speaking", "Speaking"), ("level", "Level"), ] class EncoachStat(models.Model): _name = "encoach.stat" _description = "EnCoach Exam Statistic" _order = "date desc" user_id = fields.Many2one("res.users", required=True, index=True) exam_id = fields.Many2one("encoach.exam", index=True) exercise = fields.Char() session_id = fields.Many2one("encoach.session", index=True) date = fields.Datetime(default=fields.Datetime.now) module = fields.Selection(MODULE_SELECTION, index=True) solutions = fields.Json(help="User solution objects for this stat") stat_type = fields.Char(string="Type") time_spent = fields.Integer(help="Seconds spent on this exercise") inactivity = fields.Integer(help="Seconds of inactivity") assignment_id = fields.Many2one("encoach.assignment", index=True) score_correct = fields.Integer(default=0) score_total = fields.Integer(default=0) score_missing = fields.Integer(default=0) is_disabled = fields.Boolean(default=False) shuffle_maps = fields.Json() is_practice = fields.Boolean(default=False) pdf_url = fields.Char() legacy_id = fields.Char(index=True) def to_encoach_dict(self): self.ensure_one() result = { "id": self.id, "user": self.user_id.id, "exam": self.exam_id.id if self.exam_id else None, "exercise": self.exercise or "", "session": self.session_id.id if self.session_id else None, "date": ( int(self.date.timestamp() * 1000) if self.date else None ), "module": self.module, "solutions": self.solutions or [], "type": self.stat_type or "", "timeSpent": self.time_spent, "inactivity": self.inactivity, "assignment": ( self.assignment_id.id if self.assignment_id else None ), "score": { "correct": self.score_correct, "total": self.score_total, "missing": self.score_missing, }, "isDisabled": self.is_disabled, "shuffleMaps": self.shuffle_maps or [], "isPractice": self.is_practice, } if self.pdf_url: result["pdf"] = {"path": self.pdf_url, "version": "1"} return result