Skip to content

Commit

Permalink
MDL-34115 fix sorting in blocks admin UI
Browse files Browse the repository at this point in the history
Comparison of arrays is not locale aware.
  • Loading branch information
skodak committed Jun 30, 2012
1 parent 6be7840 commit c82d309
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
23 changes: 15 additions & 8 deletions admin/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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>';
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions lib/tests/textlib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/textlib.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit c82d309

Please sign in to comment.