| 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 | 
            * Contain the logic for the add random question modal.
  | 
        
        
            | 
            | 
           18 | 
            *
  | 
        
        
            | 
            | 
           19 | 
            * @module     mod_quiz/add_question_modal
  | 
        
        
            | 
            | 
           20 | 
            * @copyright  2023 Andrew Lyons <andrew@nicols.co.uk>
  | 
        
        
            | 
            | 
           21 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           22 | 
            */
  | 
        
        
            | 
            | 
           23 | 
              | 
        
        
            | 
            | 
           24 | 
           import Modal from 'core/modal';
  | 
        
        
           | 1441 | 
           ariadna | 
           25 | 
           import * as Fragment from 'core/fragment';
  | 
        
        
            | 
            | 
           26 | 
           import {getString} from 'core/str';
  | 
        
        
            | 
            | 
           27 | 
           import AutoComplete from 'core/form-autocomplete';
  | 
        
        
           | 1 | 
           efrain | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
           export default class AddQuestionModal extends Modal {
  | 
        
        
            | 
            | 
           30 | 
               configure(modalConfig) {
  | 
        
        
            | 
            | 
           31 | 
                   // Add question modals are always large.
  | 
        
        
            | 
            | 
           32 | 
                   modalConfig.large = true;
  | 
        
        
            | 
            | 
           33 | 
              | 
        
        
            | 
            | 
           34 | 
                   // Always show on creation.
  | 
        
        
            | 
            | 
           35 | 
                   modalConfig.show = true;
  | 
        
        
           | 11 | 
           efrain | 
           36 | 
                   modalConfig.removeOnClose = true;
  | 
        
        
           | 1 | 
           efrain | 
           37 | 
              | 
        
        
            | 
            | 
           38 | 
                   // Apply question modal configuration.
  | 
        
        
            | 
            | 
           39 | 
                   this.setContextId(modalConfig.contextId);
  | 
        
        
            | 
            | 
           40 | 
                   this.setAddOnPageId(modalConfig.addOnPage);
  | 
        
        
            | 
            | 
           41 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           42 | 
                   // Store the quiz module id for when we need to POST to the quiz.
  | 
        
        
            | 
            | 
           43 | 
                   // This is because the URL cmid param will change during filter operations as we will be in another bank context.
  | 
        
        
            | 
            | 
           44 | 
                   this.quizCmId = modalConfig.quizCmId;
  | 
        
        
            | 
            | 
           45 | 
                   this.bankCmId = modalConfig.bankCmId;
  | 
        
        
            | 
            | 
           46 | 
              | 
        
        
            | 
            | 
           47 | 
                   // Store the original title of the modal, so we can revert back to it once we have switched to another bank.
  | 
        
        
            | 
            | 
           48 | 
                   this.originalTitle = modalConfig.title;
  | 
        
        
            | 
            | 
           49 | 
              | 
        
        
           | 1 | 
           efrain | 
           50 | 
                   // Apply standard configuration.
  | 
        
        
            | 
            | 
           51 | 
                   super.configure(modalConfig);
  | 
        
        
            | 
            | 
           52 | 
               }
  | 
        
        
            | 
            | 
           53 | 
              | 
        
        
            | 
            | 
           54 | 
               constructor(root) {
  | 
        
        
            | 
            | 
           55 | 
                   super(root);
  | 
        
        
            | 
            | 
           56 | 
              | 
        
        
            | 
            | 
           57 | 
                   this.contextId = null;
  | 
        
        
            | 
            | 
           58 | 
                   this.addOnPageId = null;
  | 
        
        
            | 
            | 
           59 | 
               }
  | 
        
        
            | 
            | 
           60 | 
              | 
        
        
            | 
            | 
           61 | 
               /**
  | 
        
        
            | 
            | 
           62 | 
                * Save the Moodle context id that the question bank is being
  | 
        
        
            | 
            | 
           63 | 
                * rendered in.
  | 
        
        
            | 
            | 
           64 | 
                *
  | 
        
        
            | 
            | 
           65 | 
                * @method setContextId
  | 
        
        
            | 
            | 
           66 | 
                * @param {Number} id
  | 
        
        
            | 
            | 
           67 | 
                */
  | 
        
        
            | 
            | 
           68 | 
               setContextId(id) {
  | 
        
        
            | 
            | 
           69 | 
                   this.contextId = id;
  | 
        
        
            | 
            | 
           70 | 
               }
  | 
        
        
            | 
            | 
           71 | 
              | 
        
        
            | 
            | 
           72 | 
               /**
  | 
        
        
            | 
            | 
           73 | 
                * Retrieve the saved Moodle context id.
  | 
        
        
            | 
            | 
           74 | 
                *
  | 
        
        
            | 
            | 
           75 | 
                * @method getContextId
  | 
        
        
            | 
            | 
           76 | 
                * @return {Number}
  | 
        
        
            | 
            | 
           77 | 
                */
  | 
        
        
            | 
            | 
           78 | 
               getContextId() {
  | 
        
        
            | 
            | 
           79 | 
                   return this.contextId;
  | 
        
        
            | 
            | 
           80 | 
               }
  | 
        
        
            | 
            | 
           81 | 
              | 
        
        
            | 
            | 
           82 | 
               /**
  | 
        
        
            | 
            | 
           83 | 
                * Set the id of the page that the question should be added to
  | 
        
        
            | 
            | 
           84 | 
                * when the user clicks the add to quiz link.
  | 
        
        
            | 
            | 
           85 | 
                *
  | 
        
        
            | 
            | 
           86 | 
                * @method setAddOnPageId
  | 
        
        
            | 
            | 
           87 | 
                * @param {Number} id
  | 
        
        
            | 
            | 
           88 | 
                */
  | 
        
        
            | 
            | 
           89 | 
               setAddOnPageId(id) {
  | 
        
        
            | 
            | 
           90 | 
                   this.addOnPageId = id;
  | 
        
        
            | 
            | 
           91 | 
               }
  | 
        
        
            | 
            | 
           92 | 
              | 
        
        
            | 
            | 
           93 | 
               /**
  | 
        
        
            | 
            | 
           94 | 
                * Returns the saved page id for the question to be added to.
  | 
        
        
            | 
            | 
           95 | 
                *
  | 
        
        
            | 
            | 
           96 | 
                * @method getAddOnPageId
  | 
        
        
            | 
            | 
           97 | 
                * @return {Number}
  | 
        
        
            | 
            | 
           98 | 
                */
  | 
        
        
            | 
            | 
           99 | 
               getAddOnPageId() {
  | 
        
        
            | 
            | 
           100 | 
                   return this.addOnPageId;
  | 
        
        
            | 
            | 
           101 | 
               }
  | 
        
        
            | 
            | 
           102 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           103 | 
               /**
  | 
        
        
            | 
            | 
           104 | 
                * Update the modal with a list of banks to switch to and enhance the standard selects to Autocomplete fields.
  | 
        
        
            | 
            | 
           105 | 
                *
  | 
        
        
            | 
            | 
           106 | 
                * @param {String} Selector for the original select element.
  | 
        
        
            | 
            | 
           107 | 
                * @return {Promise} Modal.
  | 
        
        
            | 
            | 
           108 | 
                */
  | 
        
        
            | 
            | 
           109 | 
               async handleSwitchBankContentReload(Selector) {
  | 
        
        
            | 
            | 
           110 | 
                   this.setTitle(getString('selectquestionbank', 'mod_quiz'));
  | 
        
        
            | 
            | 
           111 | 
              | 
        
        
            | 
            | 
           112 | 
                   // Create a 'Go back' button and set it in the footer.
  | 
        
        
            | 
            | 
           113 | 
                   const el = document.createElement('button');
  | 
        
        
            | 
            | 
           114 | 
                   el.classList.add('btn', 'btn-primary');
  | 
        
        
            | 
            | 
           115 | 
                   el.textContent = await getString('gobacktoquiz', 'mod_quiz');
  | 
        
        
            | 
            | 
           116 | 
                   el.setAttribute('data-action', 'go-back');
  | 
        
        
            | 
            | 
           117 | 
                   el.setAttribute('value', this.bankCmId);
  | 
        
        
            | 
            | 
           118 | 
                   this.setFooter(el);
  | 
        
        
            | 
            | 
           119 | 
              | 
        
        
            | 
            | 
           120 | 
                   this.setBody(
  | 
        
        
            | 
            | 
           121 | 
                       Fragment.loadFragment(
  | 
        
        
            | 
            | 
           122 | 
                           'mod_quiz',
  | 
        
        
            | 
            | 
           123 | 
                           'switch_question_bank',
  | 
        
        
            | 
            | 
           124 | 
                           this.getContextId(),
  | 
        
        
            | 
            | 
           125 | 
                           {
  | 
        
        
            | 
            | 
           126 | 
                               'quizcmid': this.quizCmId,
  | 
        
        
            | 
            | 
           127 | 
                               'bankcmid': this.bankCmId,
  | 
        
        
            | 
            | 
           128 | 
                           })
  | 
        
        
            | 
            | 
           129 | 
                   );
  | 
        
        
            | 
            | 
           130 | 
                   const placeholder = await getString('searchbyname', 'mod_quiz');
  | 
        
        
            | 
            | 
           131 | 
                   await this.getBodyPromise();
  | 
        
        
            | 
            | 
           132 | 
                   await AutoComplete.enhance(
  | 
        
        
            | 
            | 
           133 | 
                       Selector,
  | 
        
        
            | 
            | 
           134 | 
                       false,
  | 
        
        
            | 
            | 
           135 | 
                       'core_question/question_banks_datasource',
  | 
        
        
            | 
            | 
           136 | 
                       placeholder,
  | 
        
        
            | 
            | 
           137 | 
                       false,
  | 
        
        
            | 
            | 
           138 | 
                       true,
  | 
        
        
            | 
            | 
           139 | 
                       '',
  | 
        
        
            | 
            | 
           140 | 
                       true
  | 
        
        
            | 
            | 
           141 | 
                   );
  | 
        
        
            | 
            | 
           142 | 
              | 
        
        
            | 
            | 
           143 | 
                   // Hide the selection element as we don't need it.
  | 
        
        
            | 
            | 
           144 | 
                   document.querySelector('.search-banks .form-autocomplete-selection')?.classList.add('d-none');
  | 
        
        
            | 
            | 
           145 | 
              | 
        
        
            | 
            | 
           146 | 
                   return this;
  | 
        
        
            | 
            | 
           147 | 
               }
  | 
        
        
           | 1 | 
           efrain | 
           148 | 
           }
  |