| 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 | 
            * Usage column selector js.
  | 
        
        
            | 
            | 
           18 | 
            *
  | 
        
        
            | 
            | 
           19 | 
            * @module     qbank_usage/usage
  | 
        
        
            | 
            | 
           20 | 
            * @copyright  2021 Catalyst IT Australia Pty Ltd
  | 
        
        
            | 
            | 
           21 | 
            * @author     Safat Shahin <safatshahin@catalyst-au.net>
  | 
        
        
            | 
            | 
           22 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           23 | 
            */
  | 
        
        
            | 
            | 
           24 | 
              | 
        
        
            | 
            | 
           25 | 
           import Fragment from 'core/fragment';
  | 
        
        
            | 
            | 
           26 | 
           import ModalCancel from 'core/modal_cancel';
  | 
        
        
            | 
            | 
           27 | 
           import Notification from 'core/notification';
  | 
        
        
            | 
            | 
           28 | 
           import * as Str from 'core/str';
  | 
        
        
            | 
            | 
           29 | 
              | 
        
        
            | 
            | 
           30 | 
           let modal = null;
  | 
        
        
            | 
            | 
           31 | 
              | 
        
        
            | 
            | 
           32 | 
           /**
  | 
        
        
            | 
            | 
           33 | 
            * Event listeners for the module.
  | 
        
        
            | 
            | 
           34 | 
            *
  | 
        
        
            | 
            | 
           35 | 
            * @method clickEvent
  | 
        
        
            | 
            | 
           36 | 
            * @param {int} questionId
  | 
        
        
            | 
            | 
           37 | 
            * @param {int} contextId
  | 
        
        
            | 
            | 
           38 | 
            * @param {boolean} specificVersion Is the view listing specific question versions?
  | 
        
        
            | 
            | 
           39 | 
            */
  | 
        
        
            | 
            | 
           40 | 
           const usageEvent = async(questionId, contextId, specificVersion) => {
  | 
        
        
            | 
            | 
           41 | 
               const args = {
  | 
        
        
            | 
            | 
           42 | 
                   questionid: questionId,
  | 
        
        
            | 
            | 
           43 | 
                   specificversion: specificVersion,
  | 
        
        
            | 
            | 
           44 | 
               };
  | 
        
        
            | 
            | 
           45 | 
               if (modal === null) {
  | 
        
        
            | 
            | 
           46 | 
                   try {
  | 
        
        
            | 
            | 
           47 | 
                       modal = await ModalCancel.create({
  | 
        
        
            | 
            | 
           48 | 
                           title: Str.get_string('usageheader', 'qbank_usage'),
  | 
        
        
            | 
            | 
           49 | 
                           body: Fragment.loadFragment('qbank_usage', 'question_usage', contextId, args),
  | 
        
        
            | 
            | 
           50 | 
                           large: true,
  | 
        
        
            | 
            | 
           51 | 
                           show: true,
  | 
        
        
            | 
            | 
           52 | 
                       });
  | 
        
        
            | 
            | 
           53 | 
                   } catch (e) {
  | 
        
        
            | 
            | 
           54 | 
                       Notification.exception(e);
  | 
        
        
            | 
            | 
           55 | 
                       return;
  | 
        
        
            | 
            | 
           56 | 
                   }
  | 
        
        
            | 
            | 
           57 | 
              | 
        
        
            | 
            | 
           58 | 
                   modal.getRoot().on('click', 'a[href].page-link', function(e) {
  | 
        
        
            | 
            | 
           59 | 
                       e.preventDefault();
  | 
        
        
            | 
            | 
           60 | 
                       let attr = e.target.getAttribute("href");
  | 
        
        
            | 
            | 
           61 | 
                       if (attr !== '#') {
  | 
        
        
            | 
            | 
           62 | 
                           args.querystring = attr;
  | 
        
        
            | 
            | 
           63 | 
                           modal.setBody(Fragment.loadFragment('qbank_usage', 'question_usage', contextId, args));
  | 
        
        
            | 
            | 
           64 | 
                       }
  | 
        
        
            | 
            | 
           65 | 
                   });
  | 
        
        
            | 
            | 
           66 | 
                   // Version selection event.
  | 
        
        
            | 
            | 
           67 | 
                   modal.getRoot().on('change', '#question_usage_version_dropdown', function(e) {
  | 
        
        
            | 
            | 
           68 | 
                       args.questionid = e.target.value;
  | 
        
        
            | 
            | 
           69 | 
                       modal.setBody(Fragment.loadFragment('qbank_usage', 'question_usage', contextId, args));
  | 
        
        
            | 
            | 
           70 | 
                   });
  | 
        
        
            | 
            | 
           71 | 
               } else {
  | 
        
        
            | 
            | 
           72 | 
                   modal.setBody(Fragment.loadFragment('qbank_usage', 'question_usage', contextId, args));
  | 
        
        
            | 
            | 
           73 | 
                   modal.show();
  | 
        
        
            | 
            | 
           74 | 
               }
  | 
        
        
            | 
            | 
           75 | 
              | 
        
        
            | 
            | 
           76 | 
           };
  | 
        
        
            | 
            | 
           77 | 
              | 
        
        
            | 
            | 
           78 | 
           /**
  | 
        
        
            | 
            | 
           79 | 
            * Entrypoint of the js.
  | 
        
        
            | 
            | 
           80 | 
            *
  | 
        
        
            | 
            | 
           81 | 
            * @method init
  | 
        
        
            | 
            | 
           82 | 
            * @param {boolean} specificVersion Is the view listing specific question versions?
  | 
        
        
            | 
            | 
           83 | 
            */
  | 
        
        
            | 
            | 
           84 | 
           export const init = (specificVersion = false) => {
  | 
        
        
            | 
            | 
           85 | 
               const target = document.querySelector('#categoryquestions');
  | 
        
        
            | 
            | 
           86 | 
               if (target !== null) {
  | 
        
        
            | 
            | 
           87 | 
                   target.addEventListener('click', (e) => {
  | 
        
        
            | 
            | 
           88 | 
                       if (e.target.dataset.target && e.target.dataset.target.includes('questionusagepreview')) {
  | 
        
        
            | 
            | 
           89 | 
                           // Call for the event listener to listed for clicks in any usage count row.
  | 
        
        
            | 
            | 
           90 | 
                           usageEvent(e.target.dataset.questionid, e.target.dataset.contextid, specificVersion);
  | 
        
        
            | 
            | 
           91 | 
                       }
  | 
        
        
            | 
            | 
           92 | 
                   });
  | 
        
        
            | 
            | 
           93 | 
               }
  | 
        
        
            | 
            | 
           94 | 
           };
  |