MDL-57604 mod_assign: Adding some missing stuff in 32_STABLE
[moodle.git] / mod / assign / amd / src / participant_selector.js
blob60b6816ab2cc4ea897c01d943a04a8547fd8cf08
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.
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  * Custom auto-complete adapter to load users from the assignment list_participants webservice.
18  *
19  * @module     mod_assign/participants_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) {
26     return /** @alias module:mod_assign/participants_selector */ {
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         },
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 = [];
56             filters.each(function(index, element) {
57                 filterstrings[$(element).attr('name')] = $(element).prop('checked');
58             });
60             ajax.call([{
61                 methodname: 'mod_assign_list_participants',
62                 args: {assignid: assignmentid, groupid: groupid, filter: query, limit: 30, includeenrolments: false}
63             }])[0].then(function(results) {
64                 var promises = [];
65                 var identityfields = $('[data-showuseridentity]').data('showuseridentity').split(',');
67                 // We got the results, now we loop over them and render each one from a template.
68                 $.each(results, function(index, user) {
69                     var ctx = user,
70                         identity = [],
71                         show = true;
73                     if (filterstrings.filter_submitted && !user.submitted) {
74                         show = false;
75                     }
76                     if (filterstrings.filter_notsubmitted && user.submitted) {
77                         show = false;
78                     }
79                     if (filterstrings.filter_requiregrading && !user.requiregrading) {
80                         show = false;
81                     }
82                     if (show) {
83                         $.each(identityfields, function(i, k) {
84                             if (typeof user[k] !== 'undefined' && user[k] !== '') {
85                                 ctx.hasidentity = true;
86                                 identity.push(user[k]);
87                             }
88                         });
89                         ctx.identity = identity.join(', ');
90                         promises.push(templates.render('mod_assign/list_participant_user_summary', ctx).then(function(html) {
91                             return {value: user.id, label: html};
92                         }));
93                     }
94                 });
95                 // Do the dance for $.when()
96                 return $.when.apply($, promises);
97             }).then(function() {
98                 // Undo the $.when() dance from arguments object into an array..
99                 var users = Array.prototype.slice.call(arguments);
100                 success(users);
101             }).catch(failure);
102         }
103     };