| 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-cohort-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.cohorts.length; i++) {
 | 
        
           |  |  | 35 |                 if (excludelist.indexOf(String(data.cohorts[i].id)) === -1) {
 | 
        
           |  |  | 36 |                     results.push({value: data.cohorts[i].id, label: data.cohorts[i].name});
 | 
        
           |  |  | 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 |   | 
        
           |  |  | 47 |             // Build the query.
 | 
        
           |  |  | 48 |             var promises = null;
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 |             if (typeof query === "undefined") {
 | 
        
           |  |  | 51 |                 query = '';
 | 
        
           |  |  | 52 |             }
 | 
        
           |  |  | 53 |             var contextid = el.data('contextid');
 | 
        
           |  |  | 54 |   | 
        
           |  |  | 55 |             var searchargs = {
 | 
        
           |  |  | 56 |                 query: query,
 | 
        
           |  |  | 57 |                 includes: 'parents',
 | 
        
           |  |  | 58 |                 limitfrom: 0,
 | 
        
           |  |  | 59 |                 limitnum: 100,
 | 
        
           |  |  | 60 |                 context: {contextid: contextid}
 | 
        
           |  |  | 61 |             };
 | 
        
           |  |  | 62 |   | 
        
           |  |  | 63 |             var calls = [{
 | 
        
           |  |  | 64 |                 methodname: 'core_cohort_search_cohorts', args: searchargs
 | 
        
           |  |  | 65 |             }];
 | 
        
           |  |  | 66 |   | 
        
           |  |  | 67 |             // Go go go!
 | 
        
           |  |  | 68 |             promises = ajax.call(calls);
 | 
        
           |  |  | 69 |             $.when.apply($.when, promises).done(function(data) {
 | 
        
           |  |  | 70 |                 success(data);
 | 
        
           |  |  | 71 |             }).fail(failure);
 | 
        
           |  |  | 72 |         }
 | 
        
           |  |  | 73 |     };
 | 
        
           |  |  | 74 | });
 |