77 lines
3.3 KiB
Python

from odoo import models, fields, api
from odoo.exceptions import AccessError
class EducationLMSCourse(models.Model):
_name = 'education.lms.course'
_description = 'LMS Course'
name = fields.Char(string='Name', required=True)
subject_id = fields.Many2one('education.subject', string='Subject', required=True)
program_id = fields.Many2one('education.program', string='Program', required=True)
academic_year_id = fields.Many2one('education.academic.year', string='Academic Year', required=True)
term_id = fields.Many2one('education.academic.term', string='Term')
batch_id = fields.Many2one('education.batch', string='Batch')
faculty_id = fields.Many2one('res.users', string='Faculty / Instructor', required=True, default=lambda self: self.env.user)
description = fields.Html(string='Description')
state = fields.Selection([
('draft', 'Draft'),
('published', 'Published'),
('archived', 'Archived')
], string='State', default='draft', required=True)
active = fields.Boolean(default=True)
section_ids = fields.One2many('education.lms.section', 'course_id', string='Sections')
assignment_ids = fields.One2many('education.lms.assignment', 'course_id', string='Assignments')
assignment_count = fields.Integer(string='Assignment Count', compute='_compute_assignment_count')
submission_count = fields.Integer(string='Submission Count', compute='_compute_submission_count')
@api.depends('assignment_ids')
def _compute_assignment_count(self):
for record in self:
record.assignment_count = len(record.assignment_ids)
def _compute_submission_count(self):
for record in self:
submissions = self.env['education.lms.submission'].search([('assignment_id.course_id', '=', record.id)])
record.submission_count = len(submissions)
def action_publish(self):
for record in self:
record.state = 'published'
def action_draft(self):
for record in self:
record.state = 'draft'
def action_archive(self):
for record in self:
record.state = 'archived'
@api.constrains('faculty_id')
def _check_faculty_ownership(self):
if self.env.su or self.env.user.has_group('education_lms.group_education_lms_admin'):
return
for record in self:
if record.faculty_id.id != self.env.user.id:
raise AccessError("Faculty members can only manage courses assigned to themselves. Ownership cannot be transferred or spoofed.")
def write(self, vals):
if not self.env.su and not self.env.user.has_group('education_lms.group_education_lms_admin'):
for record in self:
if record.faculty_id.id != self.env.user.id:
raise AccessError("You can only edit courses assigned to you.")
return super().write(vals)
def unlink(self):
if not self.env.su and not self.env.user.has_group('education_lms.group_education_lms_admin'):
for record in self:
if record.faculty_id.id != self.env.user.id:
raise AccessError("You can only delete courses assigned to you.")
return super().unlink()