summaryrefslogtreecommitdiff
path: root/addons/website_slides_survey/controllers/main.py
diff options
context:
space:
mode:
authorFlorent Lejoly <fle@odoo.com>2019-11-27 12:51:28 +0000
committerThibault Delavallée <tde@odoo.com>2020-04-16 10:26:32 +0000
commit24033e4aaf65a52fedaa13fc0846eb745238f09d (patch)
treec15a062d15ba6d1776ad2a4fee237735d6951d51 /addons/website_slides_survey/controllers/main.py
parentdd53c515c8c4d63ec714c765db911f2407bae175 (diff)
[IMP] website_slides,_survey: allow certification creation at frontend
Purpose ======= Before this commit, in the front end, you had to create a certification before adding it to a course. This commit allows creation of certification in front-end with pre-filled settings. Functional Spec =============== 1. On a course, add content, type certification, you can choose an existing one (normal flow) or create a new one in the dropdown by typing a new survey name (new flow). 3. When submitting (Publish or save as draft), a certification survey, with pre-filled settings is created and linked to the slide. Pre-filled settings: * One page per question * Attempts limit : 1 * Time limit : no limit * Scoring without answers * Passing score : 70% * Is Certification : True * Email Template : Certification: Send by email * Give Badge : False 4. After submit, the user stays on the Course Content. 5. A toaster is displayed in the top right saying: Toaster's Title: Certification created Content: Follow this link to add questions to your certification. <a>Edit certification</a> A website editor can select a certification (if available) for the creation of a slide. Task id: 1981399 PR #43577 Co-Authored-By: Florent Lejoly <fle@odoo.com> Co-Authored-By: David Beguin <dbe@odoo.com>
Diffstat (limited to 'addons/website_slides_survey/controllers/main.py')
-rw-r--r--addons/website_slides_survey/controllers/main.py58
1 files changed, 56 insertions, 2 deletions
diff --git a/addons/website_slides_survey/controllers/main.py b/addons/website_slides_survey/controllers/main.py
index cda54c29322..a0b4cf9b53c 100644
--- a/addons/website_slides_survey/controllers/main.py
+++ b/addons/website_slides_survey/controllers/main.py
@@ -1,13 +1,67 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
-from odoo import http
+from odoo import http, _
from odoo.http import request
+from odoo.exceptions import AccessError
+from odoo.addons.website_slides.controllers.main import WebsiteSlides
+
+
+class WebsiteSlidesSurvey(WebsiteSlides):
-class WebsiteSlidesSurvey(http.Controller):
@http.route(['/slides_survey/certification/search_read'], type='json', auth='user', methods=['POST'], website=True)
def slides_certification_search_read(self, fields):
+ can_create = request.env['survey.survey'].check_access_rights('create', raise_exception=False)
return {
'read_results': request.env['survey.survey'].search_read([('certification', '=', True)], fields),
+ 'can_create': can_create,
}
+
+ # ------------------------------------------------------------
+ # Overrides
+ # ------------------------------------------------------------
+
+ @http.route(['/slides/add_slide'], type='json', auth='user', methods=['POST'], website=True)
+ def create_slide(self, *args, **post):
+ create_new_survey = post['slide_type'] == "certification" and post.get('survey') and not post['survey']['id']
+ linked_survey_id = int(post.get('survey', {}).get('id') or 0)
+
+ if create_new_survey:
+ # If user cannot create a new survey, no need to create the slide either.
+ if not request.env['survey.survey'].check_access_rights('create', raise_exception=False):
+ return {'error': _('You are not allowed to create a survey.')}
+
+ # Create survey first as certification slide needs a survey_id (constraint)
+ post['survey_id'] = request.env['survey.survey'].create({
+ 'title': post['survey']['title'],
+ 'background_image': post['image_1920'],
+ 'questions_layout': 'page_per_question',
+ 'is_attempts_limited': True,
+ 'attempts_limit': 1,
+ 'is_time_limited': False,
+ 'scoring_type': 'scoring_without_answers',
+ 'certification': True,
+ 'scoring_success_min': 70.0,
+ 'certification_mail_template_id': request.env.ref('survey.mail_template_certification').id,
+ }).id
+ elif linked_survey_id:
+ try:
+ request.env['survey.survey'].browse([linked_survey_id]).read(['title'])
+ except AccessError:
+ return {'error': _('You are not allowed to link a certification.')}
+
+ post['survey_id'] = post['survey']['id']
+
+ # Then create the slide
+ result = super(WebsiteSlidesSurvey, self).create_slide(*args, **post)
+
+ if create_new_survey:
+ # Set the redirect_url used in toaster
+ action_id = request.env.ref('survey.action_survey_form').id
+ result.update({
+ 'redirect_url': '/web#id=%s&action=%s&model=survey.survey&view_type=form' % (post['survey_id'], action_id),
+ 'redirect_to_certification': True
+ })
+
+ return result