Commit | Line | Data |
---|---|---|
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 | ||
26 | define(['jquery', 'core/ajax', 'core/templates'], function($, Ajax, Templates) { | |
27 | ||
28 | return /** @alias module:enrol_manual/form-potential-user-selector */ { | |
29 | ||
30 | processResults: function(selector, results) { | |
31 | var users = []; | |
32 | $.each(results, function(index, user) { | |
33 | users.push({ | |
34 | value: user.id, | |
35 | label: user._label | |
36 | }); | |
37 | }); | |
38 | return users; | |
39 | }, | |
40 | ||
41 | transport: function(selector, query, success, failure) { | |
42 | var promise; | |
0fa35b1a | 43 | var courseid = $(selector).attr('courseid'); |
a60e8ba5 DW |
44 | if (typeof courseid === "undefined") { |
45 | courseid = '1'; | |
46 | } | |
0fa35b1a | 47 | var enrolid = $(selector).attr('enrolid'); |
a60e8ba5 DW |
48 | if (typeof enrolid === "undefined") { |
49 | enrolid = ''; | |
50 | } | |
51 | ||
52 | promise = Ajax.call([{ | |
53 | methodname: 'core_enrol_get_potential_users', | |
54 | args: { | |
55 | courseid: courseid, | |
56 | enrolid: enrolid, | |
57 | search: query, | |
58 | searchanywhere: true, | |
59 | page: 0, | |
60 | perpage: 30 | |
61 | } | |
62 | }]); | |
63 | ||
64 | promise[0].then(function(results) { | |
65 | var promises = [], | |
66 | i = 0; | |
67 | ||
68 | // Render the label. | |
0fa35b1a | 69 | $.each(results, function(index, user) { |
a60e8ba5 DW |
70 | var ctx = user, |
71 | identity = []; | |
72 | $.each(['idnumber', 'email', 'phone1', 'phone2', 'department', 'institution'], function(i, k) { | |
73 | if (typeof user[k] !== 'undefined' && user[k] !== '') { | |
74 | ctx.hasidentity = true; | |
75 | identity.push(user[k]); | |
76 | } | |
77 | }); | |
78 | ctx.identity = identity.join(', '); | |
79 | promises.push(Templates.render('enrol_manual/form-user-selector-suggestion', ctx)); | |
80 | }); | |
81 | ||
82 | // Apply the label to the results. | |
83 | return $.when.apply($.when, promises).then(function() { | |
84 | var args = arguments; | |
0fa35b1a | 85 | $.each(results, function(index, user) { |
a60e8ba5 DW |
86 | user._label = args[i]; |
87 | i++; | |
88 | }); | |
0fa35b1a | 89 | success(results); |
2e1615f2 | 90 | return; |
a60e8ba5 DW |
91 | }); |
92 | ||
2e1615f2 | 93 | }).fail(failure); |
a60e8ba5 DW |
94 | } |
95 | ||
96 | }; | |
97 | ||
98 | }); |