| 1 |
efrain |
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 |
* @copyright 2016 Damyon Wiese
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
|
|
|
24 |
define(['jquery', 'core/ajax', 'core/templates', 'core/str'], function($, Ajax, Templates, Str) {
|
|
|
25 |
|
|
|
26 |
return /** @alias module:enrol_manual/form-potential-user-selector */ {
|
|
|
27 |
|
|
|
28 |
processResults: function(selector, results) {
|
|
|
29 |
var users = [];
|
|
|
30 |
if ($.isArray(results)) {
|
|
|
31 |
$.each(results, function(index, user) {
|
|
|
32 |
users.push({
|
|
|
33 |
value: user.id,
|
|
|
34 |
label: user._label
|
|
|
35 |
});
|
|
|
36 |
});
|
|
|
37 |
return users;
|
|
|
38 |
|
|
|
39 |
} else {
|
|
|
40 |
return results;
|
|
|
41 |
}
|
|
|
42 |
},
|
|
|
43 |
|
|
|
44 |
transport: function(selector, query, success, failure) {
|
|
|
45 |
var promise;
|
|
|
46 |
var courseid = $(selector).attr('courseid');
|
|
|
47 |
var userfields = $(selector).attr('userfields').split(',');
|
|
|
48 |
if (typeof courseid === "undefined") {
|
|
|
49 |
courseid = '1';
|
|
|
50 |
}
|
|
|
51 |
var enrolid = $(selector).attr('enrolid');
|
|
|
52 |
if (typeof enrolid === "undefined") {
|
|
|
53 |
enrolid = '';
|
|
|
54 |
}
|
|
|
55 |
var perpage = parseInt($(selector).attr('perpage'));
|
|
|
56 |
if (isNaN(perpage)) {
|
|
|
57 |
perpage = 100;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
promise = Ajax.call([{
|
|
|
61 |
methodname: 'core_enrol_get_potential_users',
|
|
|
62 |
args: {
|
|
|
63 |
courseid: courseid,
|
|
|
64 |
enrolid: enrolid,
|
|
|
65 |
search: query,
|
|
|
66 |
searchanywhere: true,
|
|
|
67 |
page: 0,
|
|
|
68 |
perpage: perpage + 1
|
|
|
69 |
}
|
|
|
70 |
}]);
|
|
|
71 |
|
|
|
72 |
promise[0].then(function(results) {
|
|
|
73 |
var promises = [],
|
|
|
74 |
i = 0;
|
|
|
75 |
|
|
|
76 |
if (results.length <= perpage) {
|
|
|
77 |
// Render the label.
|
|
|
78 |
const profileRegex = /^profile_field_(.*)$/;
|
|
|
79 |
$.each(results, function(index, user) {
|
|
|
80 |
var ctx = user,
|
|
|
81 |
identity = [];
|
|
|
82 |
$.each(userfields, function(i, k) {
|
|
|
83 |
const result = profileRegex.exec(k);
|
|
|
84 |
if (result) {
|
|
|
85 |
if (user.customfields) {
|
|
|
86 |
user.customfields.forEach(function(customfield) {
|
|
|
87 |
if (customfield.shortname === result[1]) {
|
|
|
88 |
ctx.hasidentity = true;
|
|
|
89 |
identity.push(customfield.value);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
});
|
|
|
93 |
}
|
|
|
94 |
} else {
|
|
|
95 |
if (typeof user[k] !== 'undefined' && user[k] !== '') {
|
|
|
96 |
ctx.hasidentity = true;
|
|
|
97 |
identity.push(user[k]);
|
|
|
98 |
}
|
|
|
99 |
}
|
|
|
100 |
});
|
|
|
101 |
ctx.identity = identity.join(', ');
|
|
|
102 |
promises.push(Templates.render('enrol_manual/form-user-selector-suggestion', ctx));
|
|
|
103 |
});
|
|
|
104 |
|
|
|
105 |
// Apply the label to the results.
|
|
|
106 |
return $.when.apply($.when, promises).then(function() {
|
|
|
107 |
var args = arguments;
|
|
|
108 |
$.each(results, function(index, user) {
|
|
|
109 |
user._label = args[i];
|
|
|
110 |
i++;
|
|
|
111 |
});
|
|
|
112 |
success(results);
|
|
|
113 |
return;
|
|
|
114 |
});
|
|
|
115 |
|
|
|
116 |
} else {
|
|
|
117 |
return Str.get_string('toomanyuserstoshow', 'core', '>' + perpage).then(function(toomanyuserstoshow) {
|
|
|
118 |
success(toomanyuserstoshow);
|
|
|
119 |
return;
|
|
|
120 |
});
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
}).fail(failure);
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
};
|
|
|
127 |
|
|
|
128 |
});
|