weekly release 4.5dev
[moodle.git] / question / qengine.js
blob1dff913472464fb07f60e8ed0d4a05921350bda1
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 /* eslint-enable */
94 M.core_question_engine = M.core_question_engine || {};
96 /**
97  * Initialise a question submit button. This saves the scroll position and
98  * sets the fragment on the form submit URL so the page reloads in the right place.
99  * @deprecated since Moodle 4.0
100  * @see core_question/question_engine
101  * @param button the id of the button in the HTML.
102  */
103 M.core_question_engine.init_submit_button = function(Y, button) {
104     Y.log("The core_question_engine.init_submit_button function has been deprecated. " +
105         "Please use initSubmitButton in core_question/question_engine instead.", 'moodle-core-notification', 'warn');
107     require(['core_form/submit'], function(submit) {
108         submit.init(button);
109     });
110     var totalQuestionsInPage = document.querySelectorAll('div.que').length;
111     var buttonel = document.getElementById(button);
112     var outeruniqueid = buttonel.closest('.que').id;
113     Y.on('click', function(e) {
114         M.core_scroll_manager.save_scroll_pos(Y, button);
115         if (totalQuestionsInPage > 1) {
116             // Only change the form action if the page have more than one question.
117             buttonel.form.action = buttonel.form.action + '#' + outeruniqueid;
118         }
119     }, buttonel);