MDL-51324 forms: Add a new course selector
[moodle.git] / lib / amd / src / form-course-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  * Course selector adaptor for auto-complete form element.
18  *
19  * @module     core/form-course-selector
20  * @class      form-course-selector
21  * @package    core
22  * @copyright  2016 Damyon Wiese <damyon@moodle.com>
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  * @since      3.1
25  */
26 define(['core/ajax', 'jquery'], function(ajax, $) {
28     return /** @alias module:core/form-course-selector */ {
29         // Public variables and functions.
30         processResults: function(selector, data) {
31             // Mangle the results into an array of objects.
32             var results = [], i = 0;
33             var excludelist = String($(selector).data('exclude')).split(',');
35             for (i = 0; i < data.courses.length; i++) {
36                 if (excludelist.indexOf(String(data.courses[i].id)) === -1) {
37                     results.push({ value: data.courses[i].id, label: data.courses[i].displayname });
38                 }
39             }
40             return results;
41         },
43         transport: function(selector, query, success, failure) {
44             // Parse some data-attributes from the form element.
45             var requiredcapabilities = $(selector).data('requiredcapabilities');
46             if (requiredcapabilities.trim() !== "") {
47                 requiredcapabilities = requiredcapabilities.split(',');
48             } else {
49                 requiredcapabilities = [];
50             }
51             // Build the query.
52             var promise = null;
54             if (typeof query === "undefined") {
55                 query = '';
56             }
58             var searchargs = {
59                 criterianame: 'search',
60                 criteriavalue: query,
61                 page: 0,
62                 perpage: 100,
63                 requiredcapabilities: requiredcapabilities
64             };
65             // Go go go!
66             promise = ajax.call([{
67                 methodname: 'core_course_search_courses', args: searchargs
68             }]);
70             promise[0].done(success);
71             promise[0].fail(failure);
73             return promise;
74         }
75     };
76 });