etq_exam.py 6.79 KB
from odoo import models, fields, api
import logging

# import requests
from odoo.http import request

# from datetime import datetime
from odoo.tools import html_escape as escape, ustr
# , image_resize_and_sharpen, \
#     image_save_for_web
import unicodedata
import re

_logger = logging.getLogger(__name__)


class EtqExam(models.Model):
    _name = "etq.exam"
    _description = "etq.exam"
    name = fields.Char(string="Name", translate=True)
    slug = fields.Char(string="Slug", compute="slug_me", store="True")
    show_correct_questions = fields.Boolean(string="Show Correct Answers?")
    questions = fields.One2many('etq.question', 'exam_id', string="Questions")
    fill_mode = fields.Selection(
        [('all', 'All Questions'), ('random', 'Random')], string="Fill Mode",
        default="all")
    fill_mode_random_number = fields.Integer(
        string="Number of Random Questions")

    @api.onchange('fill_mode')
    def _onchange_fill_mode(self):
        if self.fill_mode == "random":
            self.fill_mode_random_number = len(self.questions)

    # @api.multi
    def view_quiz(self):
        quiz_url = request.httprequest.host_url + "exam/" + str(self.slug)
        return {
            'type': 'ir.actions.act_url',
            'target': 'new',
            'url': quiz_url
        }

    # @api.one
    @api.depends('name')
    def slug_me(self):
        for rec in self:
            if rec.name:
                s = ustr(rec.name)
                uni = unicodedata.normalize('NFKD', s).encode('ascii',
                                                              'ignore').decode(
                    'ascii')
                slug = re.sub('[\W_]', ' ', uni).strip().lower()
                slug = re.sub('[-\s]+', '-', slug)

                rec.slug = slug


class EtqQuestion(models.Model):
    _name = "etq.question"
    _description = "etq.question"
    _rec_name = "question"

    exam_id = fields.Many2one('etq.exam', string="Exam ID")
    image = fields.Binary(string="Image")
    question = fields.Html(string="Question")
    question_rendered = fields.Html(string="Question Render",
                                    compute="render_question", sanitize=False)
    question_type = fields.Selection([('multi_choice', 'Multiple Choice'),
                                      ('fill_blank', 'Fill in the Blank')],
                                     default="multi_choice",
                                     string="Question Type")
    question_options = fields.One2many('etq.question.option', 'question_id',
                                       string="Multiple Choice Options")
    question_options_blank = fields.One2many('etq.question.optionblank',
                                             'question_id',
                                             string="Fill in the Blank Options")
    num_options = fields.Integer(string="Options", compute="calc_options")
    num_correct = fields.Integer(string="Correct Options",
                                 compute="calc_correct")

    # @api.one
    @api.depends('question')
    def render_question(self):
        for rec in self:
            if rec.question:
                temp_string = rec.question
                temp_string = temp_string.replace(
                    "{1}",
                    "<i><input name=\"question" +
                    str(rec.id) +
                    "option1\" size=\"5\" style=\"border:none;border-bottom: 1px black solid;\" type=\"text\"/></i>")
                temp_string = temp_string.replace(
                    "{2}",
                    "<i><input name=\"question" + str(rec.id) +
                    "option2\" size=\"5\" style=\"border:none;border-bottom: 1px black solid;\" type=\"text\"/></i>")
                temp_string = temp_string.replace(
                    "{3}",
                    "<i><input name=\"question" + str(rec.id) +
                    "option3\" size=\"5\" style=\"border:none;border-bottom: 1px black solid;\" type=\"text\"/></i>")
                temp_string = temp_string.replace("{4}",
                                                  "<i><input name=\"question" + str(
                                                      rec.id) + "option4\" size=\"5\" style=\"border:none;border-bottom: 1px black solid;\" type=\"text\"/></i>")
                temp_string = temp_string.replace("{5}",
                                                  "<i><input name=\"question" + str(
                                                      rec.id) + "option5\" size=\"5\" style=\"border:none;border-bottom: 1px black solid;\" type=\"text\"/></i>")
                temp_string = temp_string.replace("{6}",
                                                  "<i><input name=\"question" + str(
                                                      rec.id) + "option6\" size=\"5\" style=\"border:none;border-bottom: 1px black solid;\" type=\"text\"/></i>")
                temp_string = temp_string.replace("{7}",
                                                  "<i><input name=\"question" + str(
                                                      rec.id) + "option7\" size=\"5\" style=\"border:none;border-bottom: 1px black solid;\" type=\"text\"/></i>")
                temp_string = temp_string.replace("{8}",
                                                  "<i><input name=\"question" + str(
                                                      rec.id) + "option8\" size=\"5\" style=\"border:none;border-bottom: 1px black solid;\" type=\"text\"/></i>")
                temp_string = temp_string.replace("{9}",
                                                  "<i><input name=\"question" + str(
                                                      rec.id) + "option9\" size=\"5\" style=\"border:none;border-bottom: 1px black solid;\" type=\"text\"/></i>")
                _logger.info(temp_string)
                rec.question_rendered = temp_string

    #
    # @api.one
    @api.depends('question_options')
    def calc_options(self):
        for rec in self:
            rec.num_options = rec.question_options.search_count(
                [('question_id', '=', rec.id)])

    #
    # @api.one
    @api.depends('question_options')
    def calc_correct(self):
        for rec in self:
            rec.num_correct = self.question_options.search_count(
                [('question_id', '=', rec.id), ('correct', '=', True)])


class EtqQuestionOptions(models.Model):
    _name = "etq.question.option"
    _description = "etq.question.option"
    _rec_name = "option"

    question_id = fields.Many2one('etq.question', string="Question ID")
    option = fields.Char(string="Option")
    correct = fields.Boolean(string="Correct")


class EtqQuestionOptionBlank(models.Model):
    _name = "etq.question.optionblank"
    _description = "etq.question.optionblank"

    question_id = fields.Many2one('etq.question', string="Question ID")
    answer = fields.Char(string="Blank Answer")