Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 * Frameworks datasource.
18
 *
19
 * This module is compatible with core/form-autocomplete.
20
 *
21
 * @module     tool_lp/frameworks_datasource
22
 * @copyright  2016 Frédéric Massart - FMCorz.net
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
define(['jquery', 'core/ajax', 'core/notification'], function($, Ajax, Notification) {
27
 
28
    return /** @alias module:tool_lpmigrate/frameworks_datasource */ {
29
 
30
        /**
31
         * List frameworks.
32
         *
33
         * @param {Number} contextId The context ID.
34
         * @param {Object} options Additional parameters to pass to the external function.
35
         * @return {Promise}
36
         */
37
        list: function(contextId, options) {
38
            var args = {
39
                    context: {
40
                        contextid: contextId
41
                    }
42
                };
43
 
44
            $.extend(args, typeof options === 'undefined' ? {} : options);
45
            return Ajax.call([{
46
                methodname: 'core_competency_list_competency_frameworks',
47
                args: args
48
            }])[0];
49
        },
50
 
51
        /**
52
         * Process the results for auto complete elements.
53
         *
54
         * @param {String} selector The selector of the auto complete element.
55
         * @param {Array} results An array or results.
56
         * @return {Array} New array of results.
57
         */
58
        processResults: function(selector, results) {
59
            var options = [];
60
            $.each(results, function(index, data) {
61
                options.push({
62
                    value: data.id,
63
                    label: data.shortname + ' ' + data.idnumber
64
                });
65
            });
66
            return options;
67
        },
68
 
69
        /**
70
         * Source of data for Ajax element.
71
         *
72
         * @param {String} selector The selector of the auto complete element.
73
         * @param {String} query The query string.
74
         * @param {Function} callback A callback function receiving an array of results.
75
         */
76
        /* eslint-disable promise/no-callback-in-promise */
77
        transport: function(selector, query, callback) {
78
            var el = $(selector),
79
                contextId = el.data('contextid'),
80
                onlyVisible = el.data('onlyvisible');
81
 
82
            if (!contextId) {
83
                throw new Error('The attribute data-contextid is required on ' + selector);
84
            }
85
            this.list(contextId, {
86
                query: query,
87
                onlyvisible: onlyVisible,
88
            }).then(callback).catch(Notification.exception);
89
        }
90
    };
91
 
92
});