| 1 | efrain | 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.
 | 
        
           |  |  | 12 | //
 | 
        
           |  |  | 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/>.
 | 
        
           |  |  | 15 |   | 
        
           |  |  | 16 | /**
 | 
        
           |  |  | 17 |  * Render the question slot template for each question in the quiz edit view.
 | 
        
           |  |  | 18 |  *
 | 
        
           |  |  | 19 |  * @module     mod_quiz/question_slot
 | 
        
           |  |  | 20 |  * @copyright  2021 Catalyst IT Australia Pty Ltd
 | 
        
           |  |  | 21 |  * @author     Guillermo Gomez Arias <guillermogomez@catalyst-au.net>
 | 
        
           |  |  | 22 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 23 |  */
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 | import {call as fetchMany} from 'core/ajax';
 | 
        
           |  |  | 26 | import Notification from 'core/notification';
 | 
        
           |  |  | 27 |   | 
        
           |  |  | 28 | /**
 | 
        
           |  |  | 29 |  * Set the question version for the slot.
 | 
        
           |  |  | 30 |  *
 | 
        
           |  |  | 31 |  * @param {Number} slotId
 | 
        
           |  |  | 32 |  * @param {Number} newVersion
 | 
        
           |  |  | 33 |  * @return {Array} The modified question version
 | 
        
           |  |  | 34 |  */
 | 
        
           |  |  | 35 | const setQuestionVersion = (slotId, newVersion) => fetchMany([{
 | 
        
           |  |  | 36 |     methodname: 'mod_quiz_set_question_version',
 | 
        
           |  |  | 37 |     args: {
 | 
        
           |  |  | 38 |         slotid: slotId,
 | 
        
           |  |  | 39 |         newversion: newVersion,
 | 
        
           |  |  | 40 |     }
 | 
        
           |  |  | 41 | }])[0];
 | 
        
           |  |  | 42 |   | 
        
           |  |  | 43 | /**
 | 
        
           |  |  | 44 |  * Replace the container with a new version.
 | 
        
           |  |  | 45 |  */
 | 
        
           |  |  | 46 | const registerEventListeners = () => {
 | 
        
           |  |  | 47 |     document.addEventListener('change', e => {
 | 
        
           |  |  | 48 |         if (!e.target.matches('[data-action="mod_quiz-select_slot"][data-slot-id]')) {
 | 
        
           |  |  | 49 |             return;
 | 
        
           |  |  | 50 |         }
 | 
        
           |  |  | 51 |   | 
        
           |  |  | 52 |         const slotId = e.target.dataset.slotId;
 | 
        
           |  |  | 53 |         const newVersion = parseInt(e.target.value);
 | 
        
           |  |  | 54 |   | 
        
           | 1441 | ariadna | 55 |         setQuestionVersion(slotId, newVersion === 0 ? null : newVersion)
 | 
        
           | 1 | efrain | 56 |             .then(() => {
 | 
        
           |  |  | 57 |                 location.reload();
 | 
        
           |  |  | 58 |                 return;
 | 
        
           |  |  | 59 |             })
 | 
        
           |  |  | 60 |             .catch(Notification.exception);
 | 
        
           |  |  | 61 |     });
 | 
        
           |  |  | 62 | };
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 | /**
 | 
        
           |  |  | 65 |  * Entrypoint of the js.
 | 
        
           |  |  | 66 |  */
 | 
        
           |  |  | 67 | export const init = () => {
 | 
        
           |  |  | 68 |     registerEventListeners();
 | 
        
           |  |  | 69 | };
 |