MDL-34115 fix sorting in blocks admin UI

Comparison of arrays is not locale aware.
This commit is contained in:
Petr Škoda 2012-06-30 08:09:56 +02:00
parent 6be7840ce6
commit c82d309d07
3 changed files with 22 additions and 9 deletions

View File

@ -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 File

@ -471,6 +471,12 @@ class collatorlib_testcase extends basic_testcase {
$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 File

@ -616,7 +616,7 @@ class textlib {
* @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 */