MDL-63293 enrol_manual: observe user paging config in enrol dialog.
[moodle.git] / enrol / manual / amd / src / form-potential-user-selector.js
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/>.
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  */
26 define(['jquery', 'core/ajax', 'core/templates', 'core/str'], function($, Ajax, Templates, Str) {
28     return /** @alias module:enrol_manual/form-potential-user-selector */ {
30         processResults: function(selector, results) {
31             var users = [];
32             if ($.isArray(results)) {
33                 $.each(results, function(index, user) {
34                     users.push({
35                         value: user.id,
36                         label: user._label
37                     });
38                 });
39                 return users;
41             } else {
42                 return results;
43             }
44         },
46         transport: function(selector, query, success, failure) {
47             var promise;
48             var courseid = $(selector).attr('courseid');
49             var userfields = $(selector).attr('userfields').split(',');
50             if (typeof courseid === "undefined") {
51                 courseid = '1';
52             }
53             var enrolid = $(selector).attr('enrolid');
54             if (typeof enrolid === "undefined") {
55                 enrolid = '';
56             }
57             var perpage = $(selector).attr('perpage');
58             if (typeof perpage === "undefined") {
59                 perpage = 100;
60             }
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,
70                     perpage: perpage + 1
71                 }
72             }]);
74             promise[0].then(function(results) {
75                 var promises = [],
76                     i = 0;
78                 if (results.length <= perpage) {
79                     // Render the label.
80                     $.each(results, function(index, user) {
81                         var ctx = user,
82                             identity = [];
83                         $.each(userfields, function(i, k) {
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));
91                     });
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;
102                     });
104                 } else {
105                     return Str.get_string('toomanyuserstoshow', 'core', '>' + perpage).then(function(toomanyuserstoshow) {
106                         success(toomanyuserstoshow);
107                         return;
108                     });
109                 }
111             }).fail(failure);
112         }
114     };
116 });