etq_exam.py
7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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 = "Exam"
name = fields.Char(string="Name", translate=True)
active = fields.Boolean(string="Active", default=True, required=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",
copy=True)
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
def action_print_question(self):
return self.env.ref(
'exam_test_quiz.report_question_action').report_action(self)
class EtqQuestion(models.Model):
_name = "etq.question"
_description = "Exam 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",
copy=True)
question_options_blank = fields.One2many('etq.question.optionblank',
'question_id',
string="Fill in the Blank Options",
copy=True)
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")