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
 * User selector module.
18
 *
19
 * @module     tool_lp/form-user-selector
20
 * @copyright  2015 Frédéric Massart - FMCorz.net
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
define(['jquery', 'core/ajax', 'core/templates'], function($, Ajax, Templates) {
25
 
26
    return /** @alias module:tool_lp/form-user-selector */ {
27
 
28
        processResults: function(selector, results) {
29
            var users = [];
30
            $.each(results, function(index, user) {
31
                users.push({
32
                    value: user.id,
33
                    label: user._label
34
                });
35
            });
36
            return users;
37
        },
38
 
39
        transport: function(selector, query, success, failure) {
40
            var promise;
41
            var capability = $(selector).data('capability');
42
            if (typeof capability === "undefined") {
43
                capability = '';
44
            }
45
 
46
            promise = Ajax.call([{
47
                methodname: 'tool_lp_search_users',
48
                args: {
49
                    query: query,
50
                    capability: capability
51
                }
52
            }]);
53
 
54
            promise[0].then(function(results) {
55
                var promises = [],
56
                    i = 0;
57
 
58
                // Render the label.
59
                $.each(results.users, function(index, user) {
60
                    var ctx = user,
61
                        identity = [];
62
                    $.each(['idnumber', 'email', 'phone1', 'phone2', 'department', 'institution'], function(i, k) {
63
                        if (typeof user[k] !== 'undefined' && user[k] !== '') {
64
                            ctx.hasidentity = true;
65
                            identity.push(user[k]);
66
                        }
67
                    });
68
                    ctx.identity = identity.join(', ');
69
                    promises.push(Templates.render('tool_lp/form-user-selector-suggestion', ctx));
70
                });
71
 
72
                // Apply the label to the results.
73
                return $.when.apply($.when, promises).then(function() {
74
                    var args = arguments;
75
                    $.each(results.users, function(index, user) {
76
                        user._label = args[i];
77
                        i++;
78
                    });
79
                    success(results.users);
80
                    return;
81
                });
82
 
83
            }).catch(failure);
84
        }
85
 
86
    };
87
 
88
});