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 |
* Course selector adaptor for auto-complete form element.
|
|
|
18 |
*
|
|
|
19 |
* @module core/form-course-selector
|
|
|
20 |
* @copyright 2016 Damyon Wiese <damyon@moodle.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
* @since 3.1
|
|
|
23 |
*/
|
|
|
24 |
define(['core/ajax', 'jquery'], function(ajax, $) {
|
|
|
25 |
|
|
|
26 |
return {
|
|
|
27 |
// Public variables and functions.
|
|
|
28 |
processResults: function(selector, data) {
|
|
|
29 |
// Mangle the results into an array of objects.
|
|
|
30 |
var results = [];
|
|
|
31 |
var i = 0;
|
|
|
32 |
var excludelist = String($(selector).data('exclude')).split(',');
|
|
|
33 |
|
|
|
34 |
for (i = 0; i < data.courses.length; i++) {
|
|
|
35 |
if (excludelist.indexOf(String(data.courses[i].id)) === -1) {
|
|
|
36 |
results.push({value: data.courses[i].id, label: data.courses[i].displayname});
|
|
|
37 |
}
|
|
|
38 |
}
|
|
|
39 |
return results;
|
|
|
40 |
},
|
|
|
41 |
|
|
|
42 |
transport: function(selector, query, success, failure) {
|
|
|
43 |
var el = $(selector);
|
|
|
44 |
|
|
|
45 |
// Parse some data-attributes from the form element.
|
|
|
46 |
var requiredcapabilities = el.data('requiredcapabilities');
|
|
|
47 |
if (requiredcapabilities.trim() !== "") {
|
|
|
48 |
requiredcapabilities = requiredcapabilities.split(',');
|
|
|
49 |
} else {
|
|
|
50 |
requiredcapabilities = [];
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
var limittoenrolled = el.data('limittoenrolled');
|
|
|
54 |
var includefrontpage = el.data('includefrontpage');
|
|
|
55 |
var onlywithcompletion = el.data('onlywithcompletion');
|
|
|
56 |
|
|
|
57 |
// Build the query.
|
|
|
58 |
var promises = null;
|
|
|
59 |
|
|
|
60 |
if (typeof query === "undefined") {
|
|
|
61 |
query = '';
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
var searchargs = {
|
|
|
65 |
criterianame: 'search',
|
|
|
66 |
criteriavalue: query,
|
|
|
67 |
page: 0,
|
|
|
68 |
perpage: 100,
|
|
|
69 |
requiredcapabilities: requiredcapabilities,
|
|
|
70 |
limittoenrolled: limittoenrolled,
|
|
|
71 |
onlywithcompletion: onlywithcompletion
|
|
|
72 |
};
|
|
|
73 |
|
|
|
74 |
var calls = [{
|
|
|
75 |
methodname: 'core_course_search_courses', args: searchargs
|
|
|
76 |
}];
|
|
|
77 |
if (includefrontpage) {
|
|
|
78 |
calls.push({
|
|
|
79 |
methodname: 'core_course_get_courses',
|
|
|
80 |
args: {
|
|
|
81 |
options: {
|
|
|
82 |
ids: [includefrontpage]
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
});
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
// Go go go!
|
|
|
89 |
promises = ajax.call(calls);
|
|
|
90 |
$.when.apply($.when, promises).done(function(data, site) {
|
|
|
91 |
if (site && site.length == 1) {
|
|
|
92 |
var frontpage = site.pop();
|
|
|
93 |
var matches = query === ''
|
|
|
94 |
|| frontpage.fullname.toUpperCase().indexOf(query.toUpperCase()) > -1
|
|
|
95 |
|| frontpage.shortname.toUpperCase().indexOf(query.toUpperCase()) > -1;
|
|
|
96 |
if (matches) {
|
|
|
97 |
data.courses.splice(0, 0, frontpage);
|
|
|
98 |
}
|
|
|
99 |
}
|
|
|
100 |
success(data);
|
|
|
101 |
}).fail(failure);
|
|
|
102 |
}
|
|
|
103 |
};
|
|
|
104 |
});
|