MDL-57604 mod_assign: fix incorrect user being selector from chooser
authorDan Poltawski <dan@moodle.com>
Mon, 9 Jan 2017 20:18:50 +0000 (20:18 +0000)
committerDan Poltawski <dan@moodle.com>
Mon, 9 Jan 2017 20:36:30 +0000 (20:36 +0000)
Previously there was a logic bug where by if some students were filtered
from the list, the label for another student could be attached to the
'filered' student.

This fix reworks the promises a bit so we render the student summary and
then build an row with the explict user id, rather than itterating through
the array later.

I also remove the storing of the initial promise in a varible because
this is an easy way to introduce a bug (not waiting for the .then()
chained promise)

mod/assign/amd/build/participant_selector.min.js
mod/assign/amd/src/participant_selector.js

index c5b0452..671a70a 100644 (file)
Binary files a/mod/assign/amd/build/participant_selector.min.js and b/mod/assign/amd/build/participant_selector.min.js differ
index f03466c..60b6816 100644 (file)
@@ -35,12 +35,7 @@ define(['core/ajax', 'jquery', 'core/templates'], function(ajax, $, templates) {
          * @return {Array}
          */
         processResults: function(selector, data) {
-            var results = [];
-            var i = 0;
-            for (i = 0; i < data.length; i++) {
-                results[i] = {value: data[i].id, label: data[i].label};
-            }
-            return results;
+            return data;
         },
 
         /**
@@ -62,12 +57,10 @@ define(['core/ajax', 'jquery', 'core/templates'], function(ajax, $, templates) {
                 filterstrings[$(element).attr('name')] = $(element).prop('checked');
             });
 
-            var promise = ajax.call([{
+            ajax.call([{
                 methodname: 'mod_assign_list_participants',
                 args: {assignid: assignmentid, groupid: groupid, filter: query, limit: 30, includeenrolments: false}
-            }]);
-
-            promise[0].then(function(results) {
+            }])[0].then(function(results) {
                 var promises = [];
                 var identityfields = $('[data-showuseridentity]').data('showuseridentity').split(',');
 
@@ -94,23 +87,18 @@ define(['core/ajax', 'jquery', 'core/templates'], function(ajax, $, templates) {
                             }
                         });
                         ctx.identity = identity.join(', ');
-                        promises.push(templates.render('mod_assign/list_participant_user_summary', ctx));
+                        promises.push(templates.render('mod_assign/list_participant_user_summary', ctx).then(function(html) {
+                            return {value: user.id, label: html};
+                        }));
                     }
                 });
-
-                // When all the templates have been rendered, call the success handler.
-                $.when.apply($.when, promises).then(function() {
-                    var args = arguments,
-                        i = 0;
-
-                    $.each(results, function(index, user) {
-                        user.label = args[i];
-                        i++;
-                    });
-
-                    success(results);
-                });
-            }, failure);
+                // Do the dance for $.when()
+                return $.when.apply($, promises);
+            }).then(function() {
+                // Undo the $.when() dance from arguments object into an array..
+                var users = Array.prototype.slice.call(arguments);
+                success(users);
+            }).catch(failure);
         }
     };
 });