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