| 1 | 
           efrain | 
           1 | 
           // This file is part of Moodle - https://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 | 
            * Provides the required functionality for an autocomplete element to select a user.
  | 
        
        
            | 
            | 
           18 | 
            *
  | 
        
        
            | 
            | 
           19 | 
            * @module      core_user/form_user_selector
  | 
        
        
            | 
            | 
           20 | 
            * @copyright   2020 David Mudrák <david@moodle.com>
  | 
        
        
            | 
            | 
           21 | 
            * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           22 | 
            */
  | 
        
        
            | 
            | 
           23 | 
              | 
        
        
            | 
            | 
           24 | 
           import Ajax from 'core/ajax';
  | 
        
        
            | 
            | 
           25 | 
           import {render as renderTemplate} from 'core/templates';
  | 
        
        
            | 
            | 
           26 | 
           import {getString} from 'core/str';
  | 
        
        
            | 
            | 
           27 | 
              | 
        
        
            | 
            | 
           28 | 
           /**
  | 
        
        
            | 
            | 
           29 | 
            * Load the list of users matching the query and render the selector labels for them.
  | 
        
        
            | 
            | 
           30 | 
            *
  | 
        
        
            | 
            | 
           31 | 
            * @param {String} selector The selector of the auto complete element.
  | 
        
        
            | 
            | 
           32 | 
            * @param {String} query The query string.
  | 
        
        
            | 
            | 
           33 | 
            * @param {Function} callback A callback function receiving an array of results.
  | 
        
        
            | 
            | 
           34 | 
            * @param {Function} failure A function to call in case of failure, receiving the error message.
  | 
        
        
            | 
            | 
           35 | 
            */
  | 
        
        
            | 
            | 
           36 | 
           export async function transport(selector, query, callback, failure) {
  | 
        
        
            | 
            | 
           37 | 
              | 
        
        
            | 
            | 
           38 | 
               const request = {
  | 
        
        
            | 
            | 
           39 | 
                   methodname: 'core_user_search_identity',
  | 
        
        
            | 
            | 
           40 | 
                   args: {
  | 
        
        
            | 
            | 
           41 | 
                       query: query
  | 
        
        
            | 
            | 
           42 | 
                   }
  | 
        
        
            | 
            | 
           43 | 
               };
  | 
        
        
            | 
            | 
           44 | 
              | 
        
        
            | 
            | 
           45 | 
               try {
  | 
        
        
            | 
            | 
           46 | 
                   const response = await Ajax.call([request])[0];
  | 
        
        
            | 
            | 
           47 | 
              | 
        
        
            | 
            | 
           48 | 
                   if (response.overflow) {
  | 
        
        
            | 
            | 
           49 | 
                       const msg = await getString('toomanyuserstoshow', 'core', '>' + response.maxusersperpage);
  | 
        
        
            | 
            | 
           50 | 
                       callback(msg);
  | 
        
        
            | 
            | 
           51 | 
              | 
        
        
            | 
            | 
           52 | 
                   } else {
  | 
        
        
            | 
            | 
           53 | 
                       let labels = [];
  | 
        
        
            | 
            | 
           54 | 
                       response.list.forEach(user => {
  | 
        
        
            | 
            | 
           55 | 
                           labels.push(renderTemplate('core_user/form_user_selector_suggestion', user));
  | 
        
        
            | 
            | 
           56 | 
                       });
  | 
        
        
            | 
            | 
           57 | 
                       labels = await Promise.all(labels);
  | 
        
        
            | 
            | 
           58 | 
              | 
        
        
            | 
            | 
           59 | 
                       response.list.forEach((user, index) => {
  | 
        
        
            | 
            | 
           60 | 
                           user.label = labels[index];
  | 
        
        
            | 
            | 
           61 | 
                       });
  | 
        
        
            | 
            | 
           62 | 
              | 
        
        
            | 
            | 
           63 | 
                       callback(response.list);
  | 
        
        
            | 
            | 
           64 | 
                   }
  | 
        
        
            | 
            | 
           65 | 
              | 
        
        
            | 
            | 
           66 | 
               } catch (e) {
  | 
        
        
            | 
            | 
           67 | 
                   failure(e);
  | 
        
        
            | 
            | 
           68 | 
               }
  | 
        
        
            | 
            | 
           69 | 
           }
  | 
        
        
            | 
            | 
           70 | 
              | 
        
        
            | 
            | 
           71 | 
           /**
  | 
        
        
            | 
            | 
           72 | 
            * Process the results for auto complete elements.
  | 
        
        
            | 
            | 
           73 | 
            *
  | 
        
        
            | 
            | 
           74 | 
            * @param {String} selector The selector of the auto complete element.
  | 
        
        
            | 
            | 
           75 | 
            * @param {Array} results An array or results returned by {@see transport()}.
  | 
        
        
            | 
            | 
           76 | 
            * @return {Array} New array of the selector options.
  | 
        
        
            | 
            | 
           77 | 
            */
  | 
        
        
            | 
            | 
           78 | 
           export function processResults(selector, results) {
  | 
        
        
            | 
            | 
           79 | 
              | 
        
        
            | 
            | 
           80 | 
               if (!Array.isArray(results)) {
  | 
        
        
            | 
            | 
           81 | 
                   return results;
  | 
        
        
            | 
            | 
           82 | 
              | 
        
        
            | 
            | 
           83 | 
               } else {
  | 
        
        
            | 
            | 
           84 | 
                   return results.map(result => ({value: result.id, label: result.label}));
  | 
        
        
            | 
            | 
           85 | 
               }
  | 
        
        
            | 
            | 
           86 | 
           }
  |