From d06a987343bf599839f3c3b2f30d0a3dd11ddead Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Wed, 3 Oct 2012 18:46:07 +0100 Subject: [PATCH 1/2] MDL-35776 user ajax: fix sort order in chrome. It seems that Chrome orders fields of objects in order of array key. Therefore we must stop using user.id in the PHP arrays, and instead ensure that we use sequential numbers. This commit fixes the user selector. Conflicts: user/selector/module.js user/selector/search.php --- user/selector/module.js | 14 ++++++++------ user/selector/search.php | 13 ++++++++----- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/user/selector/module.js b/user/selector/module.js index de2f500d804..120fb7865ef 100644 --- a/user/selector/module.js +++ b/user/selector/module.js @@ -221,8 +221,9 @@ M.core_user.init_user_selector = function (Y, name, hash, extrafields, lastsearc // Output each optgroup. var count = 0; - for (var groupname in data.results) { - this.output_group(groupname, data.results[groupname], selectedusers, true); + for (var key in data.results) { + var groupdata = data.results[key]; + this.output_group(groupdata.name, groupdata.users, selectedusers, true); count++; } if (!count) { @@ -248,13 +249,14 @@ M.core_user.init_user_selector = function (Y, name, hash, extrafields, lastsearc output_group : function(groupname, users, selectedusers, processsingle) { var optgroup = Y.Node.create(''); var count = 0; - for (var userid in users) { - var user = users[userid]; - var option = Y.Node.create(''); + for (var key in users) { + var user = users[key]; + var option = Y.Node.create(''); if (user.disabled) { option.set('disabled', true); - } else if (selectedusers===true || selectedusers[userid]) { + } else if (selectedusers===true || selectedusers[user.id]) { option.set('selected', true); + delete selectedusers[user.id]; } else { option.set('selected', false); } diff --git a/user/selector/search.php b/user/selector/search.php index fcaf74ec3bb..ab282749a76 100644 --- a/user/selector/search.php +++ b/user/selector/search.php @@ -78,17 +78,20 @@ if (isset($options['file'])) { $userselector = new $classname($name, $options); // Do the search and output the results. -$users = $userselector->find_users($search); -foreach ($users as &$group) { - foreach ($group as $user) { +$results = $userselector->find_users($search); +$json = array(); +foreach ($results as $groupname => $users) { + $groupdata = array('name' => $groupname, 'users' => array()); + foreach ($users as $user) { $output = new stdClass; $output->id = $user->id; $output->name = $userselector->output_user($user); if (!empty($user->disabled)) { $output->disabled = true; } - $group[$user->id] = $output; + $groupdata['users'][] = $output; } + $json[] = $groupdata; } -echo json_encode(array('results' => $users)); +echo json_encode(array('results' => $json)); From 8b3818d97bc2464d3c324db95b074d798c711416 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Wed, 3 Oct 2012 18:46:07 +0100 Subject: [PATCH 2/2] MDL-35776 user ajax: fix sort order in chrome. It seems that Chrome orders fields of objects in order of array key. Therefore we must stop using user.id in the PHP arrays, and instead ensure that we use sequential numbers. This commit fixes the enrol UI. --- enrol/ajax.php | 8 ++++++-- enrol/manual/ajax.php | 10 +++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/enrol/ajax.php b/enrol/ajax.php index 8a0bdfc09a7..48b3fca31eb 100644 --- a/enrol/ajax.php +++ b/enrol/ajax.php @@ -54,9 +54,9 @@ echo $OUTPUT->header(); // send headers $manager = new course_enrolment_manager($PAGE, $course); -$outcome = new stdClass; +$outcome = new stdClass(); $outcome->success = true; -$outcome->response = new stdClass; +$outcome->response = new stdClass(); $outcome->error = ''; switch ($action) { @@ -99,6 +99,10 @@ switch ($action) { $user->fullname = fullname($user); unset($user->id); } + // Chrome will display users in the order of the array keys, so we need + // to ensure that the results ordered array keys. Fortunately, the JavaScript + // does not care what the array keys are. It uses user.id where necessary. + $outcome->response['users'] = array_values($outcome->response['users']); $outcome->success = true; break; default: diff --git a/enrol/manual/ajax.php b/enrol/manual/ajax.php index a9d5bb7f804..b599b95a552 100644 --- a/enrol/manual/ajax.php +++ b/enrol/manual/ajax.php @@ -53,9 +53,9 @@ echo $OUTPUT->header(); // send headers $manager = new course_enrolment_manager($PAGE, $course); -$outcome = new stdClass; +$outcome = new stdClass(); $outcome->success = true; -$outcome->response = new stdClass; +$outcome->response = new stdClass(); $outcome->error = ''; switch ($action) { @@ -79,6 +79,10 @@ switch ($action) { } $user->extrafields = implode(', ', $fieldvalues); } + // Chrome will display users in the order of the array keys, so we need + // to ensure that the results ordered array keys. Fortunately, the JavaScript + // does not care what the array keys are. It uses user.id where necessary. + $outcome->response['users'] = array_values($outcome->response['users']); $outcome->success = true; break; case 'enrol': @@ -135,4 +139,4 @@ switch ($action) { throw new enrol_ajax_exception('unknowajaxaction'); } -echo json_encode($outcome); \ No newline at end of file +echo json_encode($outcome);