weekly release 3.8dev
[moodle.git] / enrol / manual / amd / src / form-potential-user-selector.js
CommitLineData
a60e8ba5
DW
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 * Potential user selector module.
18 *
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
24 */
25
3dbda9f8
DM
26define(['jquery', 'core/ajax', 'core/templates', 'core/str'], function($, Ajax, Templates, Str) {
27
28 /** @var {Number} Maximum number of users to show. */
29 var MAXUSERS = 100;
a60e8ba5
DW
30
31 return /** @alias module:enrol_manual/form-potential-user-selector */ {
32
33 processResults: function(selector, results) {
34 var users = [];
3dbda9f8
DM
35 if ($.isArray(results)) {
36 $.each(results, function(index, user) {
37 users.push({
38 value: user.id,
39 label: user._label
40 });
a60e8ba5 41 });
3dbda9f8
DM
42 return users;
43
44 } else {
45 return results;
46 }
a60e8ba5
DW
47 },
48
49 transport: function(selector, query, success, failure) {
50 var promise;
0fa35b1a 51 var courseid = $(selector).attr('courseid');
26208463 52 var userfields = $(selector).attr('userfields').split(',');
a60e8ba5
DW
53 if (typeof courseid === "undefined") {
54 courseid = '1';
55 }
0fa35b1a 56 var enrolid = $(selector).attr('enrolid');
a60e8ba5
DW
57 if (typeof enrolid === "undefined") {
58 enrolid = '';
59 }
60
61 promise = Ajax.call([{
62 methodname: 'core_enrol_get_potential_users',
63 args: {
64 courseid: courseid,
65 enrolid: enrolid,
66 search: query,
67 searchanywhere: true,
68 page: 0,
3dbda9f8 69 perpage: MAXUSERS + 1
a60e8ba5
DW
70 }
71 }]);
72
73 promise[0].then(function(results) {
74 var promises = [],
75 i = 0;
76
3dbda9f8
DM
77 if (results.length <= MAXUSERS) {
78 // Render the label.
79 $.each(results, function(index, user) {
80 var ctx = user,
81 identity = [];
26208463 82 $.each(userfields, function(i, k) {
3dbda9f8
DM
83 if (typeof user[k] !== 'undefined' && user[k] !== '') {
84 ctx.hasidentity = true;
85 identity.push(user[k]);
86 }
87 });
88 ctx.identity = identity.join(', ');
89 promises.push(Templates.render('enrol_manual/form-user-selector-suggestion', ctx));
a60e8ba5 90 });
a60e8ba5 91
3dbda9f8
DM
92 // Apply the label to the results.
93 return $.when.apply($.when, promises).then(function() {
94 var args = arguments;
95 $.each(results, function(index, user) {
96 user._label = args[i];
97 i++;
98 });
99 success(results);
100 return;
a60e8ba5 101 });
3dbda9f8
DM
102
103 } else {
104 return Str.get_string('toomanyuserstoshow', 'core', '>' + MAXUSERS).then(function(toomanyuserstoshow) {
105 success(toomanyuserstoshow);
106 return;
107 });
108 }
a60e8ba5 109
2e1615f2 110 }).fail(failure);
a60e8ba5
DW
111 }
112
113 };
114
115});