Skip to content
Browse files

MDL-34115 fix sorting in blocks admin UI

Comparison of arrays is not locale aware.
  • Loading branch information...
1 parent a5f6ea2 commit b273b20a7cff97b5a25a9f3c276e2def44e14441 @skodak skodak committed Jun 30, 2012
Showing with 22 additions and 9 deletions.
  1. +15 −8 admin/blocks.php
  2. +6 −0 lib/tests/textlib_test.php
  3. +1 −1 lib/textlib.class.php
View
23 admin/blocks.php
@@ -131,12 +131,25 @@
$table->setup();
$tablerows = array();
+ // Sort blocks using current locale.
+ $blocknames = array();
foreach ($blocks as $blockid=>$block) {
$blockname = $block->name;
+ if (file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
+ $blocknames[$blockid] = get_string('pluginname', 'block_'.$blockname);
+ } else {
+ $blocknames[$blockid] = $blockname;
+ }
+ }
+ collatorlib::asort($blocknames);
+
+ foreach ($blocknames as $blockid=>$strblockname) {
+ $block = $blocks[$blockid];
+ $blockname = $block->name;
if (!file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
$blockobject = false;
- $strblockname = '<span class="notifyproblem">'.$blockname.' ('.get_string('missingfromdisk').')</span>';
+ $strblockname = '<span class="notifyproblem">'.$strblockname.' ('.get_string('missingfromdisk').')</span>';
$plugin = new stdClass();
$plugin->version = $block->version;
@@ -151,7 +164,6 @@
$incompatible[] = $block;
continue;
}
- $strblockname = get_string('pluginname', 'block_'.$blockname);
}
$delete = '<a href="blocks.php?delete='.$blockid.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>';
@@ -222,12 +234,7 @@
$delete,
$settings
);
- $tablerows[] = array(strip_tags($strblockname), $row); // first element will be used for sorting
- }
-
- collatorlib::asort($tablerows);
- foreach ($tablerows as $row) {
- $table->add_data($row[1]);
+ $table->add_data($row);
}
$table->print_html();
View
6 lib/tests/textlib_test.php
@@ -471,6 +471,12 @@ public function test_asort() {
$this->assertSame(array_keys($arr), array(0, 'b', 1));
$this->assertTrue($result);
+ // test sorting of array of arrays - first element should be used for actual comparison
+ $arr = array(0=>array('bb', 'z'), 1=>array('ab', 'a'), 2=>array('zz', 'x'));
+ $result = collatorlib::asort($arr, collatorlib::SORT_REGULAR);
+ $this->assertSame(array_keys($arr), array(1, 0, 2));
+ $this->assertTrue($result);
+
$arr = array('a' => 'áb', 'b' => 'ab', 1 => 'aa', 0=>'cc', 'x' => 'Áb',);
$result = collatorlib::asort($arr);
$this->assertSame(array_values($arr), array('aa', 'ab', 'áb', 'Áb', 'cc'), $this->error);
View
2 lib/textlib.class.php
@@ -616,7 +616,7 @@ public static function asort(array &$arr, $sortflag = null) {
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class collatorlib {
- /** @const compare items as strings, equivalent to Collator::SORT_REGULAR */
+ /** @const compare items using general PHP comparison, equivalent to Collator::SORT_REGULAR, this may bot be locale aware! */
const SORT_REGULAR = 0;
/** @const compare items as strings, equivalent to Collator::SORT_STRING */

0 comments on commit b273b20

Please sign in to comment.
Something went wrong with that request. Please try again.