diff --git a/addons/hr_recruitment_survey/data/survey_demo.xml b/addons/hr_recruitment_survey/data/survey_demo.xml index 26ec8876a3e..08b93472297 100644 --- a/addons/hr_recruitment_survey/data/survey_demo.xml +++ b/addons/hr_recruitment_survey/data/survey_demo.xml @@ -2,7 +2,7 @@ Recruitment Form - + token hr_recruitment diff --git a/addons/survey/__manifest__.py b/addons/survey/__manifest__.py index 246b1775397..3d8aeab4541 100644 --- a/addons/survey/__manifest__.py +++ b/addons/survey/__manifest__.py @@ -22,6 +22,8 @@ sent mails with personal token for the invitation of the survey. 'mail', 'web_tour'], 'data': [ + 'views/survey_report_templates.xml', + 'views/survey_reports.xml', 'data/mail_template_data.xml', 'data/survey_data.xml', 'data/ir_actions_data.xml', @@ -39,6 +41,7 @@ sent mails with personal token for the invitation of the survey. 'demo': [ 'data/survey_demo_user.xml', 'data/survey_demo_feedback.xml', + 'data/survey_demo_certification.xml', 'data/survey.user_input_line.csv' ], 'installable': True, diff --git a/addons/survey/controllers/main.py b/addons/survey/controllers/main.py index 8111459f11d..35d505721de 100644 --- a/addons/survey/controllers/main.py +++ b/addons/survey/controllers/main.py @@ -9,7 +9,7 @@ from datetime import datetime from dateutil.relativedelta import relativedelta from math import ceil -from odoo import fields, http +from odoo import fields, http, _ from odoo.addons.base.models.ir_ui_view import keep_query from odoo.exceptions import UserError from odoo.http import request @@ -120,7 +120,7 @@ class Survey(http.Controller): answer_sudo = access_data['answer_sudo'] if error_key == 'survey_void' and access_data['can_answer']: - return request.render("survey.survey_void", {'survey': survey_sudo}) + return request.render("survey.survey_void", {'survey': survey_sudo, 'answer': answer_sudo}) elif error_key == 'survey_closed' and access_data['can_answer']: return request.render("survey.survey_expired", {'survey': survey_sudo}) elif error_key == 'survey_auth' and answer_sudo.token: @@ -136,7 +136,7 @@ class Survey(http.Controller): elif error_key == 'answer_deadline' and answer_sudo.token: return request.render("survey.survey_expired", {'survey': survey_sudo}) elif error_key == 'answer_done' and answer_sudo.token: - return request.render("survey.sfinished", {'survey': survey_sudo, 'token': answer_sudo.token, 'user_input': answer_sudo}) + return request.render("survey.sfinished", {'survey': survey_sudo, 'token': answer_sudo.token, 'answer': answer_sudo}) return werkzeug.utils.redirect("/") @@ -151,6 +151,25 @@ class Survey(http.Controller): return werkzeug.utils.redirect('/') return request.redirect('/survey/start/%s?%s' % (survey_sudo.access_token, keep_query('*', answer_token=answer_sudo.token))) + @http.route('/survey/retry//', type='http', auth='public', website=True) + def survey_retry(self, survey_token, answer_token, **post): + """ This route is called whenever the user has attempts left and hits the 'Retry' button + after failing the survey.""" + access_data = self._get_access_data(survey_token, answer_token, ensure_token=True) + if access_data['validity_code'] is not True and access_data['validity_code'] != 'answer_done': + return self._redirect_with_error(access_data, access_data['validity_code']) + + survey_sudo, answer_sudo = access_data['survey_sudo'], access_data['answer_sudo'] + if not answer_sudo: + # attempts to 'retry' without having tried first + return werkzeug.utils.redirect("/") + + try: + retry_answer_sudo = survey_sudo._create_answer(user=request.env.user, partner=answer_sudo.partner_id, email=answer_sudo.email) + except: + return werkzeug.utils.redirect("/") + return request.redirect('/survey/start/%s?%s' % (survey_sudo.access_token, keep_query('*', answer_token=retry_answer_sudo.token))) + @http.route('/survey/start/', type='http', auth='public', website=True) def survey_start(self, survey_token, answer_token=None, email=False, **post): """ Start a survey by providing @@ -179,11 +198,34 @@ class Survey(http.Controller): # Select the right page if answer_sudo.state == 'new': # Intro page - data = {'survey': survey_sudo, 'page': None, 'answer': answer_sudo} + data = {'survey': survey_sudo, 'answer': answer_sudo, 'page': 0} return request.render('survey.survey_init', data) else: return request.redirect('/survey/fill/%s/%s' % (survey_sudo.access_token, answer_sudo.token)) + # Survey direct link to a specific page + @http.route('/survey/page///', + type='http', auth='public', website=True) + def survey_change_page(self, survey_token, answer_token, page_id, **post): + """ Method called when the user switches from one page to another using the breadcrumbs links + in the survey layout. + TODO: Right now, the answers that are not submitted are LOST when changing from one page to another + using this method. + + The survey "submit" mechanism needs to be refactored entirely to make this more user-friendly.""" + # Controls if the survey can be displayed + access_data = self._get_access_data(survey_token, answer_token, ensure_token=False) + if access_data['validity_code'] is not True: + return self._redirect_with_error(access_data, access_data['validity_code']) + + survey_sudo, answer_sudo = access_data['survey_sudo'], access_data['answer_sudo'] + + return request.render('survey.survey', { + 'survey': survey_sudo, + 'page': request.env['survey.question'].sudo().browse(page_id), + 'answer': answer_sudo + }) + @http.route('/survey/fill//', type='http', auth='public', website=True) def survey_display_page(self, survey_token, answer_token, prev=None, **post): access_data = self._get_access_data(survey_token, answer_token, ensure_token=True) @@ -192,27 +234,42 @@ class Survey(http.Controller): survey_sudo, answer_sudo = access_data['survey_sudo'], access_data['answer_sudo'] + if survey_sudo.is_time_limited and not answer_sudo.start_datetime: + # init start date when user starts filling in the survey + answer_sudo.write({ + 'start_datetime': fields.Datetime.now() + }) + # Select the right page if answer_sudo.state == 'new': # First page - page, page_nr, last = survey_sudo.next_page(answer_sudo, 0, go_back=False) - data = {'survey': survey_sudo, 'page': page, 'page_nr': page_nr, 'answer': answer_sudo} + page, last = survey_sudo.next_page(answer_sudo, 0, go_back=False) + data = { + 'survey': survey_sudo, + 'page': page, + 'answer': answer_sudo + } if last: data.update({'last': True}) return request.render('survey.survey', data) elif answer_sudo.state == 'done': # Display success message return request.render('survey.sfinished', {'survey': survey_sudo, - 'user_input': answer_sudo}) + 'answer': answer_sudo}) elif answer_sudo.state == 'skip': flag = (True if prev and prev == 'prev' else False) - page, page_nr, last = survey_sudo.next_page(answer_sudo, answer_sudo.last_displayed_page_id.id, go_back=flag) + page, last = survey_sudo.next_page(answer_sudo, answer_sudo.last_displayed_page_id.id, go_back=flag) #special case if you click "previous" from the last page, then leave the survey, then reopen it from the URL, avoid crash if not page: - page, page_nr, last = survey_sudo.next_page(answer_sudo, answer_sudo.last_displayed_page_id.id, go_back=True) + page, last = survey_sudo.next_page(answer_sudo, answer_sudo.last_displayed_page_id.id, go_back=True) - data = {'survey': survey_sudo, 'page': page, 'page_nr': page_nr, 'answer': answer_sudo} + data = { + 'survey': survey_sudo, + 'page': page, + 'answer': answer_sudo + } if last: data.update({'last': True}) + return request.render('survey.survey', data) else: return request.render("survey.403", {'survey': survey_sudo}) @@ -280,17 +337,25 @@ class Survey(http.Controller): ret = {} for answer in answer_sudo.user_input_line_ids: tmp_score = ret.get(answer.question_id.id, 0.0) - ret.update({answer.question_id.id: tmp_score + answer.quizz_mark}) + ret.update({answer.question_id.id: tmp_score + answer.answer_score}) return json.dumps(ret) @http.route('/survey/submit//', type='http', methods=['POST'], auth='public', website=True) def survey_submit(self, survey_token, answer_token, **post): - """ TDE NOTE: original comment: # AJAX submission of a page -> AJAX / http ?? """ + """ Submit a page from the survey. + This will take into account the validation errors and store the answers to the questions. + If the time limit is reached, errors will be skipped, answers wil be ignored and + survey state will be forced to 'done' + + TDE NOTE: original comment: # AJAX submission of a page -> AJAX / http ?? """ access_data = self._get_access_data(survey_token, answer_token, ensure_token=True) if access_data['validity_code'] is not True: return {} survey_sudo, answer_sudo = access_data['survey_sudo'], access_data['answer_sudo'] + if not survey_sudo._has_attempts_left(answer_sudo.partner_id, answer_sudo.email): + # prevent cheating with users creating multiple 'user_input' before their last attempt + return {} page_id = int(post['page_id']) questions = request.env['survey.question'].sudo().search([ @@ -298,32 +363,41 @@ class Survey(http.Controller): ('page_id', '=', page_id) ]) - # Answer validation errors = {} - for question in questions: - answer_tag = "%s_%s_%s" % (survey_sudo.id, page_id, question.id) - errors.update(question.validate_question(post, answer_tag)) + # Answer validation + if not answer_sudo.is_time_limit_reached: + for question in questions: + answer_tag = "%s_%s_%s" % (survey_sudo.id, page_id, question.id) + errors.update(question.validate_question(post, answer_tag)) ret = {} if len(errors): # Return errors messages to webpage ret['errors'] = errors else: - for question in questions: - answer_tag = "%s_%s_%s" % (survey_sudo.id, page_id, question.id) - request.env['survey.user_input_line'].sudo().save_lines(answer_sudo.id, question, post, answer_tag) - - go_back = post['button_submit'] == 'previous' - next_page, _, last = request.env['survey.survey'].next_page(answer_sudo, page_id, go_back=go_back) - vals = {'last_displayed_page_id': page_id} - if next_page is None and not go_back: - vals.update({'state': 'done'}) + if answer_sudo.is_time_limit_reached: + go_back = False + answer_sudo._send_certification() + vals = {'state': 'done'} else: - vals.update({'state': 'skip'}) + for question in questions: + answer_tag = "%s_%s_%s" % (survey_sudo.id, page_id, question.id) + request.env['survey.user_input_line'].sudo().save_lines(answer_sudo.id, question, post, answer_tag) + + go_back = post['button_submit'] == 'previous' + next_page, last = request.env['survey.survey'].next_page(answer_sudo, page_id, go_back=go_back) + vals = {'last_displayed_page_id': page_id} + if next_page is None and not go_back: + answer_sudo._send_certification() + vals.update({'state': 'done'}) + else: + vals.update({'state': 'skip'}) + answer_sudo.write(vals) ret['redirect'] = '/survey/fill/%s/%s' % (survey_sudo.access_token, answer_token) if go_back: ret['redirect'] += '?prev=prev' + return json.dumps(ret) @http.route('/survey/print/', type='http', auth='public', website=True) @@ -342,7 +416,7 @@ class Survey(http.Controller): 'survey': survey_sudo, 'answer': answer_sudo, 'page_nr': 0, - }) + 'quizz_correction': survey_sudo.scoring_type != 'scoring_without_answers' and answer_sudo}) @http.route('/survey/results/', type='http', auth='user', website=True) def survey_report(self, survey, answer_token=None, **post): @@ -424,6 +498,12 @@ class Survey(http.Controller): page_dict['question_ids'].append(question_dict) result['page_ids'].append(page_dict) + + if survey.scoring_type in ['scoring_with_answers', 'scoring_without_answers']: + scoring_data = self._get_scoring_data(survey) + result['success_rate'] = scoring_data['success_rate'] + result['scoring_graph_data'] = json.dumps(scoring_data['graph_data']) + return result def _get_filter_data(self, post): @@ -463,3 +543,36 @@ class Survey(http.Controller): values.append({'text': data['rows'].get(row), 'count': data['result'].get((row, answer))}) result.append({'key': data['answers'].get(answer), 'values': values}) return json.dumps(result) + + def _get_scoring_data(self, survey): + """Performs a read_group to fetch the count of failed/passed tests in a single query.""" + + count_data = request.env['survey.user_input'].read_group( + [('survey_id', '=', survey.id), ('state', '=', 'done'), ('test_entry', '=', False)], + ['quizz_passed', 'id:count_distinct'], + ['quizz_passed'] + ) + + quizz_passed_count = 0 + quizz_failed_count = 0 + for count_data_item in count_data: + if count_data_item['quizz_passed']: + quizz_passed_count = count_data_item['quizz_passed_count'] + else: + quizz_failed_count = count_data_item['quizz_passed_count'] + + graph_data = [{ + 'text': _('Passed'), + 'count': quizz_passed_count, + 'color': '#2E7D32' + }, { + 'text': _('Missed'), + 'count': quizz_failed_count, + 'color': '#C62828' + }] + + total_quizz_passed = quizz_passed_count + quizz_failed_count + return { + 'success_rate': round((quizz_passed_count / total_quizz_passed) * 100, 1) if total_quizz_passed > 0 else 0, + 'graph_data': graph_data + } diff --git a/addons/survey/data/mail_template_data.xml b/addons/survey/data/mail_template_data.xml index ededcfb9bcc..de05a6a22d9 100644 --- a/addons/survey/data/mail_template_data.xml +++ b/addons/survey/data/mail_template_data.xml @@ -27,5 +27,43 @@ + + + Certification: Send by email + + ${(object.partner_id.email_formatted or object.email) |safe} + ${object.partner_id.id} + Certification: ${object.survey_id.display_name} + + Certification Document + ${object.partner_id.lang} + + +
+ + + + +
+ + + + Certification: ${object.survey_id.display_name} +
+ + + + +
+

Dear ${object.partner_id.name or 'participant'}

+

+ Here is, in attachment, your certification document for + ${object.survey_id.display_name} +

+

Congratulations for succeeding the test!

+
+
+
+
\ No newline at end of file diff --git a/addons/survey/data/survey_data.xml b/addons/survey/data/survey_data.xml index 87cefb946c9..a900d03ce8a 100644 --- a/addons/survey/data/survey_data.xml +++ b/addons/survey/data/survey_data.xml @@ -5,7 +5,7 @@ Draft - + @@ -20,11 +20,5 @@ - - Permanent - - - - diff --git a/addons/survey/data/survey_demo_certification.xml b/addons/survey/data/survey_demo_certification.xml new file mode 100644 index 00000000000..edbb8c561c5 --- /dev/null +++ b/addons/survey/data/survey_demo_certification.xml @@ -0,0 +1,302 @@ + + + + + + MyCompany Vendor Certification + 4ead4bc8-b8f2-4760-a682-1fde8ddb95ac + + public + + + scoring_with_answers + + + limited + 10.0 + + 2 + <p>Test your vendor skills!.</p> + <p></p> + + + + Products + + 1 + + <p>Test your knowledge of your products!</p> + + + + + 2 + Do we sell Acoustic Bloc Screens? + simple_choice + dropdown + + + + + 1 + No + + + + 2 + Yes + + 2.0 + + + + + 3 + Select all the existing products + multiple_choice + 4 + + + + 1 + Chair floor protection + + 1.0 + + + + 2 + Fanta + -1.0 + + + + 3 + Conference chair + + 1.0 + + + + 4 + Drawer + + 1.0 + + + + 5 + Customizable Lamp + -1.0 + + + + + 4 + Select all the available customizations for our Customizable Desk + multiple_choice + 4 + + + + 1 + Color + + 1.0 + + + + 2 + Height + -1.0 + + + + 3 + Width + + -1.0 + + + + 4 + Legs + + 1.0 + + + + 5 + Number of drawers + -1.0 + + + + + 5 + How many versions of the Corner Desk do we have? + simple_choice + dropdown + + + + + 1 + 1 + + + + 2 + 2 + + 2.0 + + + + 3 + 3 + + + + 4 + 4 + + + + + 6 + Do you think we have missing products in our catalog? (not rated) + free_text + + + + Prices + + + 7 + <p>Test your knowledge of our prices.</p> + + + + + 8 + How much do we sell our Cable Management Box? + simple_choice + dropdown + + + + + 1 + 20$ + + + + 2 + 50$ + + + + 3 + 80$ + + + + 4 + 100$ + + 2.0 + + + + 5 + 200$ + + + + 6 + 300$ + + + + + 9 + Select all the the products that sell for 100$ or more + multiple_choice + 2 + + + + 1 + Corner Desk Right Sit + 1.0 + + + + + 2 + Desk Combination + 1.0 + + + + + 3 + Cabinet with Doors + -1.0 + + + + 4 + Large Desk + 1.0 + + + + + 5 + Letter Tray + -1.0 + + + + 6 + Office Chair Black + -1.0 + + + + + 10 + What do you think about our prices (not rated)? + simple_choice + dropdown + + + + + 1 + Very underpriced + + + + 2 + Underpriced + + + + 3 + Correctly priced + + + + 4 + A little bit overpriced + + + + 5 + A lot overpriced + + + \ No newline at end of file diff --git a/addons/survey/models/survey_question.py b/addons/survey/models/survey_question.py index 21efbc2a8e6..47ff57329f3 100644 --- a/addons/survey/models/survey_question.py +++ b/addons/survey/models/survey_question.py @@ -54,6 +54,7 @@ class SurveyQuestion(models.Model): survey_id = fields.Many2one('survey.survey', string='Survey', ondelete='cascade') page_id = fields.Many2one('survey.question', string='Page', compute="_compute_page_id", store=True) question_ids = fields.One2many('survey.question', string='Questions', compute="_compute_question_ids") + scoring_type = fields.Selection(related='survey_id.scoring_type', string='Scoring Type', readonly=True) sequence = fields.Integer('Sequence', default=10) # Question is_page = fields.Boolean('Is a page?') @@ -350,6 +351,11 @@ class SurveyQuestion(models.Model): self.ensure_one() return list(self.survey_id.question_and_page_ids).index(self) + def get_correct_answer_ids(self): + self.ensure_one() + + return self.labels_ids.filtered(lambda label: label.is_correct) + class SurveyLabel(models.Model): """ A suggested answer for a question """ _name = 'survey.label' @@ -361,7 +367,9 @@ class SurveyLabel(models.Model): question_id_2 = fields.Many2one('survey.question', string='Question 2', ondelete='cascade') sequence = fields.Integer('Label Sequence order', default=10) value = fields.Char('Suggested value', translate=True, required=True) - quizz_mark = fields.Float('Score for this choice', help="A positive score indicates a correct choice; a negative or null score indicates a wrong answer") + is_correct = fields.Boolean('Is a correct answer') + answer_score = fields.Float('Score for this choice', + help="A positive score indicates a correct choice; a negative or null score indicates a wrong answer") @api.one @api.constrains('question_id', 'question_id_2') diff --git a/addons/survey/models/survey_survey.py b/addons/survey/models/survey_survey.py index ac194d8f452..3e01d287a8b 100644 --- a/addons/survey/models/survey_survey.py +++ b/addons/survey/models/survey_survey.py @@ -31,7 +31,6 @@ class Survey(models.Model): description = fields.Html("Description", translate=True, help="A long description of the purpose of the survey") color = fields.Integer('Color Index', default=0) thank_you_message = fields.Html("Thanks Message", translate=True, help="This message will be displayed when survey is completed") - quizz_mode = fields.Boolean("Quizz Mode") active = fields.Boolean("Active", default=True) question_and_page_ids = fields.One2many('survey.question', 'survey_id', string='Pages and Questions', copy=True) page_ids = fields.One2many('survey.question', string='Pages', compute="_compute_page_and_question_ids") @@ -60,8 +59,29 @@ class Survey(models.Model): answer_count = fields.Integer("Started", compute="_compute_survey_statistic") answer_done_count = fields.Integer("Completed", compute="_compute_survey_statistic") + # scoring and certification fields + scoring_type = fields.Selection([ + ('no_scoring', 'No scoring'), + ('scoring_with_answers', 'Scoring with answers at the end'), + ('scoring_without_answers', 'Scoring without answers at the end')], + string="Scoring", required=True, default='no_scoring') + passing_score = fields.Float('Passing score (%)', required=True, default=80.0) + is_attempts_limited = fields.Boolean('Limited number of attempts', + help="Check this option if you want to limit the number of attempts per user") + attempts_limit = fields.Integer('Number of attempts', default=1) + is_time_limited = fields.Boolean('The survey is limited in time') + time_limit = fields.Float("Time limit (minutes)") + certificate = fields.Boolean('Certificate') + certification_mail_template_id = fields.Many2one( + 'mail.template', 'Certification Email Template', + domain="[('model', '=', 'survey.user_input')]", + help="Automated email sent to the user when he succeeds the certification, containing his certification document.") + _sql_constraints = [ - ('access_token_unique', 'unique(access_token)', 'Access token should be unique') + ('access_token_unique', 'unique(access_token)', 'Access token should be unique'), + ('certificate_check', "CHECK( scoring_type!='no_scoring' OR certificate=False )", 'You can only create certifications for surveys that have a scoring mechanism.'), + ('time_limit_check', "CHECK( (is_time_limited=False) OR (time_limit is not null AND time_limit > 0) )", 'The time limit needs to be a positive number if the survey is time limited.'), + ('attempts_limit_check', "CHECK( (is_attempts_limited=False) OR (attempts_limit is not null AND attempts_limit > 0) )", 'The attempts limit needs to be a positive number if the survey has a limited number of attempts.') ] @api.multi @@ -93,12 +113,45 @@ class Survey(models.Model): for survey in self: survey.public_url = urls.url_join(base_url, "survey/start/%s" % (survey.access_token)) + @api.multi + @api.depends('question_and_page_ids.labels_ids.answer_score') + def _compute_total_possible_score(self): + for survey in self: + survey.total_possible_score = sum([ + answer_score if answer_score > 0 else 0 + for answer_score in survey.question_and_page_ids.mapped('labels_ids.answer_score')]) + @api.depends('question_and_page_ids') def _compute_page_and_question_ids(self): for survey in self: survey.page_ids = survey.question_and_page_ids.filtered(lambda question: question.is_page) survey.question_ids = survey.question_and_page_ids - survey.page_ids + @api.onchange('passing_score') + def _onchange_passing_score(self): + if self.passing_score < 0 or self.passing_score > 100: + self.passing_score = 80.0 + + @api.onchange('scoring_type') + def _onchange_scoring_type(self): + if self.scoring_type != 'no_scoring': + self.certificate = False + + @api.onchange('users_login_required', 'access_mode') + def _onchange_access_mode(self): + if self.access_mode == 'public' and not self.users_login_required: + self.is_attempts_limited = False + + @api.onchange('attempts_limit') + def _onchange_attempts_limit(self): + if self.attempts_limit <= 0: + self.attempts_limit = 1 + + @api.onchange('is_time_limited', 'time_limit') + def _onchange_time_limit(self): + if self.is_time_limited and (not self.time_limit or self.time_limit <= 0): + self.time_limit = 10 + @api.model def _read_group_stage_ids(self, stages, domain, order): """ Read group customization in order to display all the stages in the @@ -126,7 +179,7 @@ class Survey(models.Model): self.check_access_rights('read') self.check_access_rule('read') - tokens = self.env['survey.user_input'] + answers = self.env['survey.user_input'] for survey in self: if partner and not user and partner.user_ids: user = partner.user_ids[0] @@ -146,9 +199,9 @@ class Survey(models.Model): answer_vals['email'] = email answer_vals.update(additional_vals) - tokens += tokens.create(answer_vals) + answers += answers.create(answer_vals) - return tokens + return answers @api.multi def _check_answer_creation(self, user, partner, email, test_entry=False): @@ -171,6 +224,8 @@ class Survey(models.Model): raise UserError(_('Creating token for external people is not allowed for surveys requesting authentication.')) if self.access_mode == 'internal' and (not user or not user.has_group('base.group_user')): raise UserError(_('Creating token for anybody else than employees is not allowed for internal surveys.')) + if not self._has_attempts_left(partner or (user and user.partner_id), email): + raise UserError(_('No attempts left.')) @api.model def next_page(self, user_input, page_id, go_back=False): @@ -195,23 +250,23 @@ class Survey(models.Model): # First page if page_id == 0: - return (pages[0][1], 0, len(pages) == 1) + return (pages[0][1], len(pages) == 1) current_page_index = pages.index(next(p for p in pages if p[1].id == page_id)) # All the pages have been displayed if current_page_index == len(pages) - 1 and not go_back: - return (None, -1, False) + return (None, False) # Let's get back, baby! elif go_back and survey.users_can_go_back: - return (pages[current_page_index - 1][1], current_page_index - 1, False) + return (pages[current_page_index - 1][1], False) else: # This will show the last page if current_page_index == len(pages) - 2: - return (pages[current_page_index + 1][1], current_page_index + 1, True) + return (pages[current_page_index + 1][1], True) # This will show a regular page else: - return (pages[current_page_index + 1][1], current_page_index + 1, False) + return (pages[current_page_index + 1][1], False) @api.multi def filter_input_ids(self, filters, finished=False): @@ -278,7 +333,7 @@ class Survey(models.Model): # Calculate and return statistics for choice if question.question_type in ['simple_choice', 'multiple_choice']: comments = [] - answers = OrderedDict((label.id, {'text': label.value, 'count': 0, 'answer_id': label.id}) for label in question.labels_ids) + answers = OrderedDict((label.id, {'text': label.value, 'count': 0, 'answer_id': label.id, 'answer_score': label.answer_score}) for label in question.labels_ids) for input_line in input_lines: if input_line.answer_type == 'suggestion' and answers.get(input_line.value_suggested.id) and (not(current_filters) or input_line.user_input_id.id in current_filters): answers[input_line.value_suggested.id]['count'] += 1 @@ -435,3 +490,30 @@ class Survey(models.Model): 'search_default_invite': 1}) action['context'] = ctx return action + + @api.multi + def _has_attempts_left(self, partner, email): + self.ensure_one() + + if (self.access_mode != 'public' or self.users_login_required) and self.is_attempts_limited: + return self._get_number_of_attempts_lefts(partner, email) > 0 + + return True + + @api.multi + def _get_number_of_attempts_lefts(self, partner, email): + """ Returns the number of attempts left. """ + self.ensure_one() + + domain = [ + ('survey_id', '=', self.id), + ('test_entry', '=', False), + ('state', '=', 'done') + ] + + if partner: + domain = expression.AND([domain, [('partner_id', '=', partner.id)]]) + else: + domain = expression.AND([domain, [('email', '=', email)]]) + + return self.attempts_limit - self.env['survey.user_input'].search_count(domain) diff --git a/addons/survey/models/survey_user.py b/addons/survey/models/survey_user.py index 90ed34e2497..d7300eecb06 100644 --- a/addons/survey/models/survey_user.py +++ b/addons/survey/models/survey_user.py @@ -9,6 +9,8 @@ import uuid from odoo import api, fields, models, _ from odoo.exceptions import ValidationError +from dateutil.relativedelta import relativedelta + email_validator = re.compile(r"[^@]+@[^@]+\.[^@]+") _logger = logging.getLogger(__name__) @@ -30,6 +32,8 @@ class SurveyUserInput(models.Model): # description survey_id = fields.Many2one('survey.survey', string='Survey', required=True, readonly=True, ondelete='cascade') + start_datetime = fields.Datetime('Start date and time', readonly=True) + is_time_limit_reached = fields.Boolean("Is time limit reached?", compute='_compute_is_time_limit_reached') input_type = fields.Selection([ ('manually', 'Manual'), ('link', 'Invitation')], string='Answer Type', default='manually', required=True, readonly=True, @@ -49,12 +53,27 @@ class SurveyUserInput(models.Model): # answers user_input_line_ids = fields.One2many('survey.user_input_line', 'user_input_id', string='Answers', copy=True) deadline = fields.Datetime('Deadline', help="Datetime until customer can open the survey and submit answers") - quizz_score = fields.Float("Score for the quiz", compute="_compute_quizz_score", default=0.0) - @api.depends('user_input_line_ids.quizz_mark') + quizz_score = fields.Float("Score for the quiz (%)", compute="_compute_quizz_score", default=0.0) + # Stored for performance reasons while displaying results page + quizz_passed = fields.Boolean('Quizz Passed', compute='_compute_quizz_passed', store=True) + + @api.multi + @api.depends('user_input_line_ids.answer_score', 'survey_id.total_possible_score') def _compute_quizz_score(self): for user_input in self: - user_input.quizz_score = sum(user_input.user_input_line_ids.mapped('quizz_mark')) + total_possible_score = user_input.survey_id.total_possible_score + if total_possible_score == 0: + user_input.quizz_score = 0 + else: + score = (sum(user_input.user_input_line_ids.mapped('answer_score')) / total_possible_score) * 100 + user_input.quizz_score = round(score, 2) if score > 0 else 0 + + @api.multi + @api.depends('quizz_score', 'survey_id.passing_score') + def _compute_quizz_passed(self): + for user_input in self: + user_input.quizz_passed = user_input.quizz_score >= user_input.survey_id.passing_score _sql_constraints = [ ('unique_token', 'UNIQUE (token)', 'A token must be unique!'), @@ -97,6 +116,23 @@ class SurveyUserInput(models.Model): 'url': '/survey/print/%s?answer_token=%s' % (self.survey_id.access_token, self.token) } + @api.depends('start_datetime', 'survey_id.is_time_limited', 'survey_id.time_limit') + def _compute_is_time_limit_reached(self): + """ Checks that the user_input is not exceeding the survey's time limit. """ + for user_input in self: + user_input.is_time_limit_reached = user_input.survey_id.is_time_limited and fields.Datetime.now() \ + > user_input.start_datetime + relativedelta(minutes=user_input.survey_id.time_limit) + + @api.multi + def _send_certification(self): + """ Will send the certification email with attached document if + - The survey is a certification + - It has a certification_mail_template_id set + - The user succeeded the test """ + for user_input in self: + if user_input.survey_id.certificate and user_input.quizz_passed and user_input.survey_id.certification_mail_template_id: + user_input.survey_id.certification_mail_template_id.send_mail(user_input.id, notif_layout="mail.mail_notification_light") + class SurveyUserInputLine(models.Model): _name = 'survey.user_input_line' @@ -122,7 +158,7 @@ class SurveyUserInputLine(models.Model): value_free_text = fields.Text('Free Text answer') value_suggested = fields.Many2one('survey.label', string="Suggested answer") value_suggested_row = fields.Many2one('survey.label', string="Row answer") - quizz_mark = fields.Float('Score given for this choice') + answer_score = fields.Float('Score given for this choice') @api.constrains('skipped', 'answer_type') def _answered_or_skipped(self): @@ -145,7 +181,7 @@ class SurveyUserInputLine(models.Model): def _get_mark(self, value_suggested): label = self.env['survey.label'].browse(int(value_suggested)) - mark = label.quizz_mark if label.exists() else 0.0 + mark = label.answer_score if label.exists() else 0.0 return mark @api.model_create_multi @@ -153,14 +189,14 @@ class SurveyUserInputLine(models.Model): for vals in vals_list: value_suggested = vals.get('value_suggested') if value_suggested: - vals.update({'quizz_mark': self._get_mark(value_suggested)}) + vals.update({'answer_score': self._get_mark(value_suggested)}) return super(SurveyUserInputLine, self).create(vals_list) @api.multi def write(self, vals): value_suggested = vals.get('value_suggested') if value_suggested: - vals.update({'quizz_mark': self._get_mark(value_suggested)}) + vals.update({'answer_score': self._get_mark(value_suggested)}) return super(SurveyUserInputLine, self).write(vals) @api.model diff --git a/addons/survey/static/src/img/certification_background.png b/addons/survey/static/src/img/certification_background.png new file mode 100644 index 00000000000..966d05cf903 Binary files /dev/null and b/addons/survey/static/src/img/certification_background.png differ diff --git a/addons/survey/static/src/js/survey_result.js b/addons/survey/static/src/js/survey_result.js index 1cc571ed78c..f70aefe9a1f 100644 --- a/addons/survey/static/src/js/survey_result.js +++ b/addons/survey/static/src/js/survey_result.js @@ -67,7 +67,7 @@ if(!$('.js_surveyresult').length) { } //load chart to svg element chart:initialized chart, response:AJAX response, quistion_id:if of survey question, tick_limit:text length limit - function load_chart(chart, response, question_id, tick_limit, graph_type){ + function load_chart(chart, response, svgSelector, tick_limit, graph_type){ // Custom Tick fuction for replacing long text with '...' var customtick_function = function(d){ if(! this || d.length <= tick_limit){ @@ -83,7 +83,7 @@ if(!$('.js_surveyresult').length) { chart.yAxis .tickFormat(d3.format('d')); } - d3.select('#graph_question_' + question_id + ' svg') + d3.select(svgSelector) .datum(response) .transition().duration(500).call(chart); nv.utils.windowResize(chart.update); @@ -95,26 +95,35 @@ if(!$('.js_surveyresult').length) { var question_id = $(graph).attr("data-question_id"); var graph_type = $(graph).attr("data-graph_type"); var graph_data = JSON.parse($(graph).attr("graph-data")); + var svgSelector = '#graph_question_' + question_id + ' svg'; if(graph_type == 'multi_bar'){ nv.addGraph(function(){ var chart = init_multibar_chart(); - return load_chart(chart, graph_data, question_id, 25); + return load_chart(chart, graph_data, svgSelector, 25); }); } else if(graph_type == 'bar'){ nv.addGraph(function() { var chart = init_bar_chart(); - return load_chart(chart, graph_data, question_id, 35); + return load_chart(chart, graph_data, svgSelector, 35); }); } else if(graph_type == 'pie'){ nv.addGraph(function() { var chart = init_pie_chart(); - return load_chart(chart, graph_data, question_id, 25, 'pie'); + return load_chart(chart, graph_data, svgSelector, 25, 'pie'); }); } }); + var $scoringResultsChart = $('#scoring_results_chart'); + if ($scoringResultsChart.length > 0) { + nv.addGraph(function () { + var chart = init_pie_chart(); + return load_chart(chart, $scoringResultsChart.data('graph_data'), '#scoring_results_chart svg', 25, 'pie'); + }); + } + // Script for filter $('td.survey_answer').hover(function(){ $(this).find('i.fa-filter').removeClass('invisible'); diff --git a/addons/survey/static/src/js/survey_timer.js b/addons/survey/static/src/js/survey_timer.js new file mode 100644 index 00000000000..bb7af08104f --- /dev/null +++ b/addons/survey/static/src/js/survey_timer.js @@ -0,0 +1,45 @@ +odoo.define('survey.timer', function (require) { +'use strict'; + +require('web.dom_ready'); + +if (!$('.js_survey_timer').length) { + return $.Deferred().reject("DOM doesn't contain '.js_survey_timer'"); +} + +var $parent = $('.js_survey_timer'); +var timeLimitMinutes = parseInt($parent.data('time_limit_minutes')); + +if (timeLimitMinutes <= 0) { + return $.Deferred().reject("Timer is not positive"); +} + +var countDownDate = moment.utc($parent.data('timer')).add(timeLimitMinutes, 'minutes'); + +if (countDownDate.diff(moment.utc(), 'seconds') < 0) { + return $.Deferred().reject("Timer is already finished"); +} + +var formatTime = function (time) { + return time > 9 ? time : '0' + time; +}; + +var $timer = $parent.find('.timer'); +var interval = null; +var updateTimer = function () { + var timeLeft = countDownDate.diff(moment.utc(), 'seconds'); + + if (timeLeft >= 0) { + var timeLeftMinutes = parseInt(timeLeft / 60); + var timeLeftSeconds = timeLeft - (timeLeftMinutes * 60); + $timer.html(formatTime(timeLeftMinutes) + ':' + formatTime(timeLeftSeconds)); + } else { + clearInterval(interval); + $('.js_surveyform').submit(); + } +}; + +updateTimer(); +interval = setInterval(updateTimer, 1000); + +}); diff --git a/addons/survey/static/src/scss/survey_reports.scss b/addons/survey/static/src/scss/survey_reports.scss new file mode 100644 index 00000000000..ac31d56abed --- /dev/null +++ b/addons/survey/static/src/scss/survey_reports.scss @@ -0,0 +1,17 @@ +.certification-wrapper { + background: url(/survey/static/src/img/certification_background.png) no-repeat; + background-size: 100% 100%; + height: 610px; + width: 860px; + + .certification { + margin-left: 350px; + padding-top: 140px; + width: 510px; + text-align: center; + + h2 { + color: #876B81; + } + } +} diff --git a/addons/survey/static/src/scss/survey_templates.scss b/addons/survey/static/src/scss/survey_templates.scss new file mode 100644 index 00000000000..d54c3853625 --- /dev/null +++ b/addons/survey/static/src/scss/survey_templates.scss @@ -0,0 +1,8 @@ +.o_survey_header { + padding-top: 15px; + padding-bottom: 25px; +} + +.js_question-wrapper { + padding-bottom: 25px; +} diff --git a/addons/survey/static/src/scss/survey_views.scss b/addons/survey/static/src/scss/survey_views.scss new file mode 100644 index 00000000000..92f4dd73a31 --- /dev/null +++ b/addons/survey/static/src/scss/survey_views.scss @@ -0,0 +1,5 @@ +.o_kanban_card_survey { + .certification_icon { + font-size: 20px; + } +} diff --git a/addons/survey/tests/common.py b/addons/survey/tests/common.py index a37b7cd6b91..13b988f9ec0 100644 --- a/addons/survey/tests/common.py +++ b/addons/survey/tests/common.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. +import re + from collections import Counter from contextlib import contextmanager from functools import partial @@ -156,17 +158,17 @@ class SurveyCase(common.SavepointCase): } if qtype in ('simple_choice', 'multiple_choice'): base_qvalues['labels_ids'] = [ - (0, 0, {'value': label['value'], 'quizz_mark': label.get('quizz_mark', 0)}) + (0, 0, {'value': label['value'], 'answer_score': label.get('answer_score', 0)}) for label in kwargs.pop('labels') ] elif qtype == 'matrix': base_qvalues['matrix_subtype'] = kwargs.pop('matrix_subtype', 'simple') base_qvalues['labels_ids'] = [ - (0, 0, {'value': label['value'], 'quizz_mark': label.get('quizz_mark', 0)}) + (0, 0, {'value': label['value'], 'answer_score': label.get('answer_score', 0)}) for label in kwargs.pop('labels') ] base_qvalues['labels_ids_2'] = [ - (0, 0, {'value': label['value'], 'quizz_mark': label.get('quizz_mark', 0)}) + (0, 0, {'value': label['value'], 'answer_score': label.get('answer_score', 0)}) for label in kwargs.pop('labels_2') ] else: @@ -199,3 +201,7 @@ class SurveyCase(common.SavepointCase): base_alvals[answer_fname] = answer_value base_alvals.update(kwargs) return self.env['survey.user_input_line'].create(base_alvals) + + def _find_csrf_token(self, text): + csrf_token_re = re.compile("(input.+csrf_token.+value=\")([_a-zA-Z0-9]{51})", re.MULTILINE) + return csrf_token_re.search(text).groups()[1] diff --git a/addons/survey/tests/test_certification_flow.py b/addons/survey/tests/test_certification_flow.py new file mode 100644 index 00000000000..e596e34fa7d --- /dev/null +++ b/addons/survey/tests/test_certification_flow.py @@ -0,0 +1,148 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from unittest.mock import patch + +from odoo.addons.base.models.ir_mail_server import IrMailServer +from odoo.addons.survey.tests import common +from odoo.tests import tagged +from odoo.tests.common import HttpCase + + +@tagged('functional') +class TestCertificationFlow(common.SurveyCase, HttpCase): + def _answer_question(self, question, answer, answer_token, csrf_token, button_submit='next'): + # Employee submits the question answer + post_data = self._format_submission_data(question, answer, {'csrf_token': csrf_token, 'token': answer_token, 'button_submit': button_submit}) + response = self._access_submit(question.survey_id, answer_token, post_data) + self.assertResponse(response, 200) + + # Employee is redirected on next question + response = self._access_page(question.survey_id, answer_token) + self.assertResponse(response, 200) + + def _format_submission_data(self, question, answer, additional_post_data): + post_data = {} + post_data['question_id'] = question.id + if question.question_type == 'multiple_choice': + values = answer + for value in values: + key = "%s_%s_%s" % (question.survey_id.id, question.id, value) + post_data[key] = value + else: + key = "%s_%s" % (question.survey_id.id, question.id) + post_data[key] = answer + post_data.update(**additional_post_data) + return post_data + + def test_flow_certificate(self): + # Step: survey user creates the certification + # -------------------------------------------------- + with self.sudo(self.survey_user): + certification = self.env['survey.survey'].create({ + 'title': 'User Certification for SO lines', + 'access_mode': 'public', + 'users_login_required': True, + 'questions_layout': 'page_per_question', + 'users_can_go_back': True, + 'scoring_type': 'scoring_with_answers', + 'passing_score': 85.0, + 'certificate': True, + 'certification_mail_template_id': self.env.ref('survey.mail_template_certification').id, + 'is_time_limited': True, + 'time_limit': 10, + 'stage_id': self.env['survey.stage'].search([('closed', '=', False)]).id + }) + + q01 = self._add_question( + None, 'When do you know it\'s the right time to use the SO line model?', 'simple_choice', + sequence=1, + constr_mandatory=True, constr_error_msg='Please select an answer', survey_id=certification.id, + labels=[ + {'value': 'Please stop'}, + {'value': 'Only on the SO form'}, + {'value': 'Only on the Survey form'}, + {'value': 'Easy, all the time!!!', 'is_correct': True, 'answer_score': 2.0} + ]) + + q02 = self._add_question( + None, 'On average, how many lines of code do you need when you use SO line widgets?', 'simple_choice', + sequence=2, + constr_mandatory=True, constr_error_msg='Please select an answer', survey_id=certification.id, + labels=[ + {'value': '1'}, + {'value': '5', 'is_correct': True, 'answer_score': 2.0}, + {'value': '100'}, + {'value': '1000'} + ]) + + q03 = self._add_question( + None, 'What do you think about SO line widgets (not rated)?', 'free_text', + sequence=3, + constr_mandatory=True, constr_error_msg='Please tell us what you think', survey_id=certification.id) + + q04 = self._add_question( + None, 'On a scale of 1 to 10, how much do you like SO line widgets (not rated)?', 'simple_choice', + sequence=4, + constr_mandatory=True, constr_error_msg='Please tell us what you think', survey_id=certification.id, + labels=[ + {'value': '-1'}, + {'value': '0'}, + {'value': '100'} + ]) + + q05 = self._add_question( + None, 'Select all the correct "types" of SO lines', 'multiple_choice', + sequence=5, + constr_mandatory=False, survey_id=certification.id, + labels=[ + {'value': 'sale_order', 'is_correct': True, 'answer_score': 1.0}, + {'value': 'survey_page', 'is_correct': True, 'answer_score': 1.0}, + {'value': 'survey_question', 'is_correct': True, 'answer_score': 1.0}, + {'value': 'a_future_and_yet_unknown_model', 'is_correct': True, 'answer_score': 1.0}, + {'value': 'none', 'answer_score': -1.0} + ]) + + # Step: employee takes the certification + # -------------------------------------------------- + self.authenticate('user_emp', 'user_emp') + + # Employee opens start page + response = self._access_start(certification) + self.assertResponse(response, 200, [certification.title, 'Time limit for this survey', '10 minutes']) + + # -> this should have generated a new user_input with a token + user_inputs = self.env['survey.user_input'].search([('survey_id', '=', certification.id)]) + self.assertEqual(len(user_inputs), 1) + self.assertEqual(user_inputs.partner_id, self.user_emp.partner_id) + answer_token = user_inputs.token + + # Employee begins survey with first page + response = self._access_page(certification, answer_token) + self.assertResponse(response, 200) + csrf_token = self._find_csrf_token(response.text) + + with patch.object(IrMailServer, 'connect'): + self._answer_question(q01, q01.labels_ids.ids[3], answer_token, csrf_token) + self._answer_question(q02, q02.labels_ids.ids[1], answer_token, csrf_token) + self._answer_question(q03, "I think they're great!", answer_token, csrf_token) + self._answer_question(q04, q04.labels_ids.ids[0], answer_token, csrf_token, button_submit='previous') + self._answer_question(q03, "Just kidding, I don't like it...", answer_token, csrf_token) + self._answer_question(q04, q04.labels_ids.ids[0], answer_token, csrf_token) + self._answer_question(q05, [q05.labels_ids.ids[0], q05.labels_ids.ids[1], q05.labels_ids.ids[3]], answer_token, csrf_token) + + user_inputs.invalidate_cache() + # Check that certification is successfully passed + self.assertEqual(user_inputs.quizz_score, 87.5) + self.assertTrue(user_inputs.quizz_passed) + + # Check answer correction is taken into account + self.assertNotIn("I think they're great!", user_inputs.mapped('user_input_line_ids.value_free_text')) + self.assertIn("Just kidding, I don't like it...", user_inputs.mapped('user_input_line_ids.value_free_text')) + + certification_email = self.env['mail.mail'].search([], limit=1, order="create_date desc") + # Check certification email correctly sent and contains document + self.assertIn("User Certification for SO lines", certification_email.subject) + self.assertIn("employee@example.com", certification_email.email_to) + self.assertEqual(len(certification_email.attachment_ids), 1) + self.assertEqual(certification_email.attachment_ids[0].name, 'Certification Document.html') diff --git a/addons/survey/tests/test_survey_flow.py b/addons/survey/tests/test_survey_flow.py index f83352800da..bda59666e3d 100644 --- a/addons/survey/tests/test_survey_flow.py +++ b/addons/survey/tests/test_survey_flow.py @@ -20,10 +20,6 @@ class TestSurveyFlow(common.SurveyCase, HttpCase): def _access_submit(self, survey, token, post_data): return self.url_open('/survey/submit/%s/%s' % (survey.access_token, token), data=post_data) - def _find_csrf_token(self, text): - csrf_token_re = re.compile("(input.+csrf_token.+value=\")([_a-zA-Z0-9]{51})", re.MULTILINE) - return csrf_token_re.search(text).groups()[1] - def _format_submission_data(self, page, answer_data, additional_post_data): post_data = {} post_data['page_id'] = page.id diff --git a/addons/survey/views/assets.xml b/addons/survey/views/assets.xml index ae93c5d3380..91a99c535b6 100644 --- a/addons/survey/views/assets.xml +++ b/addons/survey/views/assets.xml @@ -11,6 +11,7 @@ @@ -25,10 +26,22 @@ + + + + diff --git a/addons/survey/views/survey_question_views.xml b/addons/survey/views/survey_question_views.xml index eec66365a20..4521926791e 100644 --- a/addons/survey/views/survey_question_views.xml +++ b/addons/survey/views/survey_question_views.xml @@ -95,12 +95,16 @@ - + + + + + minutes - + + + + @@ -107,12 +117,13 @@ + -
+
-

+
+

+
+