| 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 |  * Allow the user to search for learners within the user report.
 | 
        
           |  |  | 18 |  *
 | 
        
           |  |  | 19 |  * @module    gradereport_user/user
 | 
        
           |  |  | 20 |  * @copyright 2023 Mathew May <mathew.solutions>
 | 
        
           |  |  | 21 |  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 22 |  */
 | 
        
           |  |  | 23 | import UserSearch from 'core_user/comboboxsearch/user';
 | 
        
           |  |  | 24 | import {renderForPromise, replaceNodeContents} from 'core/templates';
 | 
        
           |  |  | 25 | import * as Repository from 'core_grades/searchwidget/repository';
 | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 | export default class User extends UserSearch {
 | 
        
           |  |  | 28 |   | 
        
           | 1441 | ariadna | 29 |     /**
 | 
        
           |  |  | 30 |      * Construct the class.
 | 
        
           |  |  | 31 |      *
 | 
        
           |  |  | 32 |      * @param {string} baseUrl The base URL for the page.
 | 
        
           |  |  | 33 |      */
 | 
        
           |  |  | 34 |     constructor(baseUrl) {
 | 
        
           | 1 | efrain | 35 |         super();
 | 
        
           | 1441 | ariadna | 36 |         this.baseUrl = baseUrl;
 | 
        
           | 1 | efrain | 37 |     }
 | 
        
           |  |  | 38 |   | 
        
           | 1441 | ariadna | 39 |     static init(baseUrl) {
 | 
        
           |  |  | 40 |         return new User(baseUrl);
 | 
        
           | 1 | efrain | 41 |     }
 | 
        
           |  |  | 42 |   | 
        
           |  |  | 43 |     /**
 | 
        
           |  |  | 44 |      * Build the content then replace the node.
 | 
        
           |  |  | 45 |      */
 | 
        
           |  |  | 46 |     async renderDropdown() {
 | 
        
           |  |  | 47 |         const {html, js} = await renderForPromise('core_user/comboboxsearch/resultset', {
 | 
        
           |  |  | 48 |             users: this.getMatchedResults().slice(0, 5),
 | 
        
           |  |  | 49 |             hasresults: this.getMatchedResults().length > 0,
 | 
        
           |  |  | 50 |             instance: this.instance,
 | 
        
           |  |  | 51 |             matches: this.getDatasetSize(),
 | 
        
           |  |  | 52 |             searchterm: this.getSearchTerm(),
 | 
        
           |  |  | 53 |             selectall: this.selectAllResultsLink(),
 | 
        
           |  |  | 54 |         });
 | 
        
           |  |  | 55 |         replaceNodeContents(this.getHTMLElements().searchDropdown, html, js);
 | 
        
           |  |  | 56 |         // Remove aria-activedescendant when the available options change.
 | 
        
           |  |  | 57 |         this.searchInput.removeAttribute('aria-activedescendant');
 | 
        
           |  |  | 58 |     }
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 |     /**
 | 
        
           |  |  | 61 |      * Build up the view all link.
 | 
        
           |  |  | 62 |      *
 | 
        
           |  |  | 63 |      * @returns {string|*}
 | 
        
           |  |  | 64 |      */
 | 
        
           |  |  | 65 |     selectAllResultsLink() {
 | 
        
           | 1441 | ariadna | 66 |         const url = new URL(this.baseUrl);
 | 
        
           |  |  | 67 |         url.searchParams.set('userid', 0);
 | 
        
           |  |  | 68 |         url.searchParams.set('searchvalue', this.getSearchTerm());
 | 
        
           |  |  | 69 |         return url.toString();
 | 
        
           | 1 | efrain | 70 |     }
 | 
        
           |  |  | 71 |   | 
        
           |  |  | 72 |     /**
 | 
        
           |  |  | 73 |      * Build up the link that is dedicated to a particular result.
 | 
        
           |  |  | 74 |      *
 | 
        
           |  |  | 75 |      * @param {Number} userID The ID of the user selected.
 | 
        
           |  |  | 76 |      * @returns {string|*}
 | 
        
           |  |  | 77 |      */
 | 
        
           |  |  | 78 |     selectOneLink(userID) {
 | 
        
           | 1441 | ariadna | 79 |         const url = new URL(this.baseUrl);
 | 
        
           |  |  | 80 |         url.searchParams.set('userid', userID);
 | 
        
           |  |  | 81 |         url.searchParams.set('searchvalue', this.getSearchTerm());
 | 
        
           |  |  | 82 |         return url.toString();
 | 
        
           | 1 | efrain | 83 |     }
 | 
        
           |  |  | 84 |   | 
        
           |  |  | 85 |     /**
 | 
        
           |  |  | 86 |      * Get the data we will be searching against in this component.
 | 
        
           |  |  | 87 |      *
 | 
        
           |  |  | 88 |      * @returns {Promise<*>}
 | 
        
           |  |  | 89 |      */
 | 
        
           |  |  | 90 |     fetchDataset() {
 | 
        
           |  |  | 91 |         // Small typing checks as sometimes groups don't exist therefore the element returns a empty string.
 | 
        
           |  |  | 92 |         const gts = typeof (this.groupID) === "string" && this.groupID === '' ? 0 : this.groupID;
 | 
        
           |  |  | 93 |         return Repository.userFetch(this.courseID, gts).then((r) => r.users);
 | 
        
           |  |  | 94 |     }
 | 
        
           |  |  | 95 | }
 |