diff --git a/addons/survey/controllers/main.py b/addons/survey/controllers/main.py index 783f3477732..6025c86e406 100644 --- a/addons/survey/controllers/main.py +++ b/addons/survey/controllers/main.py @@ -214,6 +214,24 @@ class WebsiteSurvey(http.Controller): _logger.warning("[survey] No answer has been found for question %s marked as non skipped" % answer_tag) return json.dumps(ret) + # AJAX scores loading for quiz correction mode + @http.route(['/survey/scores//'], + type='http', auth='public', multilang=True, website=True) + def get_scores(self, survey, token, page=None, **post): + cr, uid, context = request.cr, request.uid, request.context + user_input_line_obj = request.registry['survey.user_input_line'] + ret = {} + + # Fetch answers + ids = user_input_line_obj.search(cr, uid, [('user_input_id.token', '=', token)], context=context) + previous_answers = user_input_line_obj.browse(cr, uid, ids, context=context) + + # Compute score for each question + for answer in previous_answers: + tmp_score = ret.get(answer.question_id.id, 0.0) + ret.update({answer.question_id.id: tmp_score + answer.quizz_mark}) + return json.dumps(ret) + # AJAX submission of a page @http.route(['/survey/submit/'], type='http', auth='public', multilang=True, website=True) diff --git a/addons/survey/static/src/js/survey.js b/addons/survey/static/src/js/survey.js index 2d97250b0ce..f0096ddd3ee 100644 --- a/addons/survey/static/src/js/survey.js +++ b/addons/survey/static/src/js/survey.js @@ -30,7 +30,9 @@ $(document).ready(function () { var prefill_controller = the_form.attr("data-prefill"); var validate_controller = the_form.attr("data-validate"); var submit_controller = the_form.attr("data-submit"); + var scores_controller = the_form.attr("data-scores"); var print_mode = false; + var quiz_correction_mode = false; // Printing mode: will disable all the controls in the form if (_.isUndefined(submit_controller)) { @@ -38,6 +40,11 @@ $(document).ready(function () { print_mode = true; } + // Quizz correction mode + if (! _.isUndefined(scores_controller)) { + quiz_correction_mode = true; + } + // Custom code for right behavior of radio buttons with comments box $('.js_comments>input[type="text"]').focusin(function(){ $(this).prev().find('>input').attr("checked","checked"); @@ -88,6 +95,20 @@ $(document).ready(function () { return prefill_def; } + // Display score if quiz correction mode + function display_scores(){ + var score_def = $.ajax(scores_controller, {dataType: "json"}) + .done(function(json_data){ + _.each(json_data, function(value, key){ + the_form.find("span[data-score-question=" + key + "]").text("Your score: " + value); + }); + }) + .fail(function(){ + console.warn("[survey] Unable to load score data"); + }); + return score_def; + } + // Parameters for form submission $('.js_surveyform').ajaxForm({ url: submit_controller, @@ -126,6 +147,9 @@ $(document).ready(function () { // Launch prefilling prefill(); + if(quiz_correction_mode === true){ + display_scores(); + } console.debug("[survey] Custom JS for survey loaded!"); }); diff --git a/addons/survey/views/survey_templates.xml b/addons/survey/views/survey_templates.xml index cf0a0d0603f..9a342fa128d 100644 --- a/addons/survey/views/survey_templates.xml +++ b/addons/survey/views/survey_templates.xml @@ -256,7 +256,7 @@

-
+