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
 * Custom auto-complete adapter to load users from the assignment list_participants webservice.
18
 *
19
 * @module     mod_assign/participant_selector
20
 * @copyright  2015 Damyon Wiese <damyon@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
define(['core/ajax', 'jquery', 'core/templates'], function(ajax, $, templates) {
24
 
25
 
26
    return /** @alias module:mod_assign/participants_selector */ {
27
 
28
        // Public variables and functions.
29
        /**
30
         * Process the results returned from transport (convert to value + label)
31
         *
32
         * @method processResults
33
         * @param {String} selector
34
         * @param {Array} data
35
         * @return {Array}
36
         */
37
        processResults: function(selector, data) {
38
            return data;
39
        },
40
 
41
        /**
42
         * Fetch results based on the current query. This also renders each result from a template before returning them.
43
         *
44
         * @method transport
45
         * @param {String} selector Selector for the original select element
46
         * @param {String} query Current search string
47
         * @param {Function} success Success handler
48
         * @param {Function} failure Failure handler
49
         */
50
        transport: function(selector, query, success, failure) {
51
            var assignmentid = $(selector).attr('data-assignmentid');
52
            var groupid = $(selector).attr('data-groupid');
53
            var filters = $('[data-region="configure-filters"] input[type="checkbox"]');
54
            var filterstrings = [];
55
 
56
            filters.each(function(index, element) {
57
                filterstrings[$(element).attr('name')] = $(element).prop('checked');
58
            });
59
 
60
            ajax.call([{
61
                methodname: 'mod_assign_list_participants',
62
                args: {
63
                    assignid: assignmentid,
64
                    groupid: groupid,
65
                    filter: query,
66
                    limit: 30,
67
                    includeenrolments: false,
68
                    tablesort: true
69
                }
70
            }])[0].then(function(results) {
71
                var promises = [];
72
                var identityfields = $('[data-showuseridentity]').data('showuseridentity').split(',');
73
 
74
                // We got the results, now we loop over them and render each one from a template.
75
                $.each(results, function(index, user) {
76
                    var ctx = user,
77
                        identity = [],
78
                        show = true;
79
 
80
                    if (filterstrings.filter_submitted && !user.submitted) {
81
                        show = false;
82
                    }
83
                    if (filterstrings.filter_notsubmitted && user.submitted) {
84
                        show = false;
85
                    }
86
                    if (filterstrings.filter_requiregrading && !user.requiregrading) {
87
                        show = false;
88
                    }
89
                    if (filterstrings.filter_grantedextension && !user.grantedextension) {
90
                        show = false;
91
                    }
92
                    if (show) {
93
                        $.each(identityfields, function(i, k) {
94
                            if (typeof user[k] !== 'undefined' && user[k] !== '') {
95
                                ctx.hasidentity = true;
96
                                identity.push(user[k]);
97
                            }
98
                        });
99
                        ctx.identity = identity.join(', ');
100
                        promises.push(templates.render('mod_assign/list_participant_user_summary', ctx).then(function(html) {
101
                            return {value: user.id, label: html};
102
                        }));
103
                    }
104
                });
105
                // Do the dance for $.when()
106
                return $.when.apply($, promises);
107
            }).then(function() {
108
                var users = [];
109
 
110
                // Determine if we've been passed any arguments..
111
                if (arguments[0]) {
112
                    // Undo the $.when() dance from arguments object into an array..
113
                    users = Array.prototype.slice.call(arguments);
114
                }
115
 
116
                success(users);
117
                return;
118
            }).catch(failure);
119
        }
120
    };
121
});