1 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 * Potential user selector module.
19 * @module enrol_manual/form-potential-user-selector
20 * @class form-potential-user-selector
21 * @package enrol_manual
22 * @copyright 2016 Damyon Wiese
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 define(['jquery', 'core/ajax', 'core/templates', 'core/str'], function($, Ajax, Templates, Str) {
28 /** @var {Number} Maximum number of users to show. */
31 return /** @alias module:enrol_manual/form-potential-user-selector */ {
33 processResults: function(selector, results) {
35 if ($.isArray(results)) {
36 $.each(results, function(index, user) {
49 transport: function(selector, query, success, failure) {
51 var courseid = $(selector).attr('courseid');
52 var userfields = $(selector).attr('userfields').split(',');
53 if (typeof courseid === "undefined") {
56 var enrolid = $(selector).attr('enrolid');
57 if (typeof enrolid === "undefined") {
61 promise = Ajax.call([{
62 methodname: 'core_enrol_get_potential_users',
73 promise[0].then(function(results) {
77 if (results.length <= MAXUSERS) {
79 $.each(results, function(index, user) {
82 $.each(userfields, function(i, k) {
83 if (typeof user[k] !== 'undefined' && user[k] !== '') {
84 ctx.hasidentity = true;
85 identity.push(user[k]);
88 ctx.identity = identity.join(', ');
89 promises.push(Templates.render('enrol_manual/form-user-selector-suggestion', ctx));
92 // Apply the label to the results.
93 return $.when.apply($.when, promises).then(function() {
95 $.each(results, function(index, user) {
96 user._label = args[i];
104 return Str.get_string('toomanyuserstoshow', 'core', '>' + MAXUSERS).then(function(toomanyuserstoshow) {
105 success(toomanyuserstoshow);