MDL-80880 quiz: change display of previous attempts summary
[moodle.git] / question / qengine.js
blobf5f120f119e42acba6f7ca5f1ba41d5b537de187
1 // This file is part of Moodle - http://moodle.org/
2 //
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16 /**
17  * JavaScript required by the question engine.
18  *
19  * @package    moodlecore
20  * @subpackage questionengine
21  * @copyright  2008 The Open University
22  * @deprecated since Moodle 4.0
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
27 M.core_scroll_manager = M.core_scroll_manager || {};
29 // TODO Remove the scroll manager and deprecation layer in 4.6 MDL-76685.
30 /* eslint-disable */
31 var loadedPromise = new Promise(function(resolve) {
32     require(['core/scroll_manager'], function(ScrollManager) {
33         var transitionLayer = {};
35         var deprecatedNotice = function(functionName, newFunctionName) {
36             window.console.error(
37                 "The " + functionName + " function has been deprecated. " +
38                 "Please use core/scroll_manager::" + newFunctionName + "() instead"
39             );
40         };
42         transitionLayer.save_scroll_pos = function(Y, element) {
43             deprecatedNotice('save_scroll_pos', 'saveScrollPos');
44             ScrollManager.saveScrollPos(element);
45         };
47         transitionLayer.scroll_to_saved_pos = function() {
48             deprecatedNotice('scroll_to_saved_pos', 'scrollToSavedPosition');
49             ScrollManager.scrollToSavedPosition();
50         };
52         M.core_scroll_manager = transitionLayer;
54         resolve(transitionLayer);
55     });
56 });
58 var callPromisedFunction = function(functionName, args) {
59     loadedPromise.then(function(transitionLayer) {
60         transitionLayer[functionName].apply(null, args);
61     });
64 if (!M.core_scroll_manager.save_scroll_pos) {
65     // Note: This object is short lived.
66     // It only lives until the new scroll manager is loaded, at which point it is replaced.
68     /**
69      * In the form that contains the element, set the value of the form field with
70      * name scrollpos to the current scroll position. If there is no element with
71      * that name, it creates a hidden form field with that name within the form.
72      * @deprecated since Moodle 4.0
73      * @see core/scroll_manager
74      * @param element the element in the form. Should be something that can be
75      *      passed to Y.one.
76      */
77     M.core_scroll_manager.save_scroll_pos = function(Y, element) {
78         callPromisedFunction(M.core_scroll_manager.save_scroll_pos, [Y, element]);
79     };
81     /**
82      * Event handler that can be used on a link. Assumes that the link already
83      * contains at least one URL parameter.
84      * @deprecated since Moodle 4.0
85      * @see core/scroll_manager
86      */
87     M.core_scroll_manager.save_scroll_action = function() {
88         Y.log("The scroll_to_saved_pos function has been deprecated. " +
89             "Please use initLinksScrollPos in core/scroll_manager instead.", 'moodle-core-notification', 'warn');
90     };
92     /**
93      * If there is a parameter like scrollpos=123 in the URL, scroll to that saved position.
94      * @deprecated since Moodle 4.0
95      * @see core/scroll_manager
96      * @todo Final deprecation on Moodle 4.4 MDL-72438
97      */
98     M.core_scroll_manager.scroll_to_saved_pos = function(Y) {
99         callPromisedFunction(M.core_scroll_manager.scroll_to_saved_pos, Y);
100     };
102 /* eslint-enable */
104 M.core_question_engine = M.core_question_engine || {};
107  * Flag used by M.core_question_engine.prevent_repeat_submission.
108  */
109 M.core_question_engine.questionformalreadysubmitted = false;
112  * Initialise a question submit button. This saves the scroll position and
113  * sets the fragment on the form submit URL so the page reloads in the right place.
114  * @deprecated since Moodle 4.0
115  * @see core_question/question_engine
116  * @param button the id of the button in the HTML.
117  */
118 M.core_question_engine.init_submit_button = function(Y, button) {
119     Y.log("The core_question_engine.init_submit_button function has been deprecated. " +
120         "Please use initSubmitButton in core_question/question_engine instead.", 'moodle-core-notification', 'warn');
122     require(['core_form/submit'], function(submit) {
123         submit.init(button);
124     });
125     var totalQuestionsInPage = document.querySelectorAll('div.que').length;
126     var buttonel = document.getElementById(button);
127     var outeruniqueid = buttonel.closest('.que').id;
128     Y.on('click', function(e) {
129         M.core_scroll_manager.save_scroll_pos(Y, button);
130         if (totalQuestionsInPage > 1) {
131             // Only change the form action if the page have more than one question.
132             buttonel.form.action = buttonel.form.action + '#' + outeruniqueid;
133         }
134     }, buttonel);
138  * Initialise a form that contains questions printed using print_question.
139  * This has the effect of:
140  * 1. Turning off browser autocomlete.
141  * 2. Stopping enter from submitting the form (or toggling the next flag) unless
142  *    keyboard focus is on the submit button or the flag.
143  * 3. Removes any '.questionflagsavebutton's, since we have JavaScript to toggle
144  *    the flags using ajax.
145  * 4. Scroll to the position indicated by scrollpos= in the URL, if it is there.
146  * 5. Prevent the user from repeatedly submitting the form.
147  * @param Y the Yahoo object. Needs to have the DOM and Event modules loaded.
148  * @param form something that can be passed to Y.one, to find the form element.
149  * @deprecated since Moodle 4.0
150  * @see core_question/question_engine
151  * @todo Final deprecation on Moodle 4.4 MDL-72438
152  */
153 M.core_question_engine.init_form = function(Y, form) {
154     Y.log("The core_question_engine.init_form function has been deprecated. " +
155         "Please use init_form in core_question/question_engine instead.", 'moodle-core-notification', 'warn');
157     Y.one(form).setAttribute('autocomplete', 'off');
159     Y.on('submit', M.core_question_engine.prevent_repeat_submission, form, form, Y);
161     Y.on('key', function (e) {
162         if (!e.target.test('a') && !e.target.test('input[type=submit]') &&
163                 !e.target.test('input[type=img]') && !e.target.test('textarea') && !e.target.test('[contenteditable=true]')) {
164             e.preventDefault();
165         }
166     }, form, 'press:13');
168     Y.one(form).all('.questionflagsavebutton').remove();
170     M.core_scroll_manager.scroll_to_saved_pos(Y);
174  * Event handler to stop a question form being submitted more than once.
175  * @param e the form submit event.
176  * @param form the form element.
177  * @deprecated since Moodle 4.0
178  * @see core_question/question_engine
179  * @todo Final deprecation on Moodle 4.4 MDL-72438
180  */
181 M.core_question_engine.prevent_repeat_submission = function(e, Y) {
182     Y.log("The prevent_repeat_submission function has been deprecated. " +
183         "Please use preventRepeatSubmission in core_question/question_engine instead.", 'moodle-core-notification', 'warn');
185     if (M.core_question_engine.questionformalreadysubmitted) {
186         e.halt();
187         return;
188     }
190     setTimeout(function() {
191         Y.all('input[type=submit]').set('disabled', true);
192     }, 0);
193     M.core_question_engine.questionformalreadysubmitted = true;