Skip to content
This repository has been archived by the owner on Apr 8, 2022. It is now read-only.

Commit

Permalink
Merge branch 'wip-MDL-28599-master-r4' of git://github.com/samhemelry…
Browse files Browse the repository at this point in the history
…k/moodle
  • Loading branch information
stronk7 committed Sep 6, 2011
2 parents dfdb005 + d609d96 commit b49c28a
Show file tree
Hide file tree
Showing 19 changed files with 393 additions and 51 deletions.
2 changes: 1 addition & 1 deletion admin/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@
$tablerows[] = array(strip_tags($strblockname), $row); // first element will be used for sorting
}

textlib_get_instance()->asort($tablerows);
collatorlib::asort($tablerows);
foreach ($tablerows as $row) {
$table->add_data($row[1]);
}
Expand Down
2 changes: 1 addition & 1 deletion admin/localplugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
}
$plugins[$plugin] = $strpluginname;
}
textlib_get_instance()->asort($plugins);
collatorlib::asort($plugins);

foreach ($plugins as $plugin => $name) {
$delete = new moodle_url($PAGE->url, array('delete' => $plugin, 'sesskey' => sesskey()));
Expand Down
2 changes: 1 addition & 1 deletion admin/registration/forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public function definition() {
$mform->addHelpButton('urlstring', 'siteurl', 'hub');

$languages = get_string_manager()->get_list_of_languages();
textlib_get_instance()->asort($languages);
collatorlib::asort($languages);
$mform->addElement('select', 'language', get_string('sitelang', 'hub'),
$languages);
$mform->setType('language', PARAM_ALPHANUMEXT);
Expand Down
2 changes: 1 addition & 1 deletion blocks/activity_modules/block_activity_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function get_content() {
}
}

textlib_get_instance()->asort($modfullnames);
collatorlib::asort($modfullnames);

foreach ($modfullnames as $modname => $modfullname) {
if ($modname === 'resources') {
Expand Down
2 changes: 1 addition & 1 deletion blocks/community/forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public function definition() {
$mform->setDefault('licence', $licence);

$languages = get_string_manager()->get_list_of_languages();
textlib_get_instance()->asort($languages);
collatorlib::asort($languages);
$languages = array_merge(array('all' => get_string('any')), $languages);
$mform->addElement('select', 'language', get_string('language'), $languages);
$mform->setDefault('language', $language);
Expand Down
4 changes: 3 additions & 1 deletion course/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ function definition() {
$themes=array();
$themes[''] = get_string('forceno');
foreach ($themeobjects as $key=>$theme) {
$themes[$key] = $theme->name;
if (empty($theme->hidefromselector)) {
$themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
}
}
$mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
}
Expand Down
4 changes: 3 additions & 1 deletion course/editcategory_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ function definition() {
$themes = array(''=>get_string('forceno'));
$allthemes = get_list_of_themes();
foreach ($allthemes as $key=>$theme) {
$themes[$key] = $theme->name;
if (empty($theme->hidefromselector)) {
$themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
}
}
$mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
}
Expand Down
4 changes: 2 additions & 2 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,7 @@ function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modname
$modnamesplural[$mod->name] = get_string("modulenameplural", "$mod->name");
}
}
textlib_get_instance()->asort($modnames);
collatorlib::asort($modnames);
} else {
print_error("nomodules", 'debug');
}
Expand All @@ -1231,7 +1231,7 @@ function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modname
$modnamesused[$mod->modname] = $modnames[$mod->modname];
}
if ($modnamesused) {
textlib_get_instance()->asort($modnamesused);
collatorlib::asort($modnamesused);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion course/publish/forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public function definition() {
$mform->addHelpButton('description', 'description', 'hub');

$languages = get_string_manager()->get_list_of_languages();
textlib_get_instance()->asort($languages);
collatorlib::asort($languages);
$mform->addElement('select', 'language', get_string('language'), $languages);
$mform->setDefault('language', $defaultlanguage);
$mform->addHelpButton('language', 'language', 'hub');
Expand Down
2 changes: 1 addition & 1 deletion lib/blocklib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1754,7 +1754,7 @@ function block_add_block_ui($page, $output) {
$menu[$block->name] = $blockobject->get_title();
}
}
textlib_get_instance()->asort($menu);
collatorlib::asort($menu);

$actionurl = new moodle_url($page->url, array('sesskey'=>sesskey()));
$select = new single_select($actionurl, 'bui_addblock', $menu, null, array(''=>get_string('adddots')), 'add_block');
Expand Down
2 changes: 1 addition & 1 deletion lib/filterlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ function filter_get_all_installed() {
}
}
}
textlib_get_instance()->asort($filternames);
collatorlib::asort($filternames);
return $filternames;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/gradelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ function grade_get_categories_menu($courseid, $includenew=false) {
foreach ($categories as $category) {
$cats[$category->id] = $category->get_name();
}
textlib_get_instance()->asort($cats);
collatorlib::asort($cats);

return ($result+$cats);
}
Expand Down
10 changes: 5 additions & 5 deletions lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -6143,7 +6143,7 @@ public function get_list_of_countries($returnall = false, $lang = NULL) {
}

$countries = $this->load_component_strings('core_countries', $lang);
textlib_get_instance()->asort($countries);
collatorlib::asort($countries);
if (!$returnall and !empty($CFG->allcountrycodes)) {
$enabled = explode(',', $CFG->allcountrycodes);
$return = array();
Expand Down Expand Up @@ -6315,12 +6315,12 @@ public function get_list_of_translations($returnall = false) {

if (!empty($CFG->langcache) and !empty($this->menucache)) {
// cache the list so that it can be used next time
textlib_get_instance()->asort($languages);
collatorlib::asort($languages);
file_put_contents($this->menucache, json_encode($languages));
}
}

textlib_get_instance()->asort($languages);
collatorlib::asort($languages);

return $languages;
}
Expand Down Expand Up @@ -6739,7 +6739,6 @@ function get_list_of_charsets() {
/**
* Returns a list of valid and compatible themes
*
* @global object
* @return array
*/
function get_list_of_themes() {
Expand All @@ -6757,7 +6756,8 @@ function get_list_of_themes() {
$theme = theme_config::load($themename);
$themes[$themename] = $theme;
}
asort($themes);

collatorlib::asort_objects_by_method($themes, 'get_theme_name');

return $themes;
}
Expand Down
9 changes: 9 additions & 0 deletions lib/outputlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,15 @@ public function get_all_block_regions() {
}
return $regions;
}

/**
* Returns the human readable name of the theme
*
* @return string
*/
public function get_theme_name() {
return get_string('pluginname', 'theme_'.$this->name);
}
}


Expand Down
2 changes: 1 addition & 1 deletion lib/questionlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,7 @@ function get_import_export_formats($type) {
}
}

textlib_get_instance()->asort($fileformatnames);
collatorlib::asort($fileformatnames);
return $fileformatnames;
}

Expand Down
148 changes: 126 additions & 22 deletions lib/simpletest/testtextlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,28 +272,6 @@ public function test_strtotitle() {
$this->assertIdentical(textlib::strtotitle($str), "Žluťoučký Koníček");
}

public function test_asort() {
global $SESSION;
$SESSION->lang = 'en'; // make sure we test en language to get consistent results, hopefully all systems have this locale

$arr = array('b'=>'ab', 1=>'aa', 0=>'cc');
textlib::asort($arr);
$this->assertIdentical(array_keys($arr), array(1, 'b', 0));
$this->assertIdentical(array_values($arr), array('aa', 'ab', 'cc'));

if (extension_loaded('intl')) {
$error = 'Collation aware sorting not supported';
} else {
$error = 'Collation aware sorting not supported, PHP extension "intl" is not available.';
}

$arr = array('a'=>'áb', 'b'=>'ab', 1=>'aa', 0=>'cc');
textlib::asort($arr);
$this->assertIdentical(array_keys($arr), array(1, 'b', 'a', 0), $error);

unset($SESSION->lang);
}

public function test_deprecated_textlib_get_instance() {
$textlib = textlib_get_instance();
$this->assertIdentical($textlib->substr('abc', 1, 1), 'b');
Expand All @@ -307,3 +285,129 @@ public function test_deprecated_textlib_get_instance() {
$this->assertIdentical($textlib->strtotitle('abc ABC'), 'Abc Abc');
}
}

/**
* Unit tests for our utf-8 aware collator.
*
* Used for sorting.
*
* @package core
* @subpackage lib
* @copyright 2011 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class collatorlib_test extends UnitTestCase {

protected $initiallang = null;
protected $error = null;

public function setUp() {
global $SESSION;
if (isset($SESSION->lang)) {
$this->initiallang = $SESSION->lang;
}
$SESSION->lang = 'en'; // make sure we test en language to get consistent results, hopefully all systems have this locale
if (extension_loaded('intl')) {
$this->error = 'Collation aware sorting not supported';
} else {
$this->error = 'Collation aware sorting not supported, PHP extension "intl" is not available.';
}
parent::setUp();
}
public function tearDown() {
global $SESSION;
parent::tearDown();
if ($this->initiallang !== null) {
$SESSION->lang = $this->initiallang;
$this->initiallang = null;
} else {
unset($SESSION->lang);
}
}
function test_asort() {
$arr = array('b' => 'ab', 1 => 'aa', 0 => 'cc');
collatorlib::asort($arr);
$this->assertIdentical(array_keys($arr), array(1, 'b', 0));
$this->assertIdentical(array_values($arr), array('aa', 'ab', 'cc'));

$arr = array('a' => 'áb', 'b' => 'ab', 1 => 'aa', 0=>'cc');
collatorlib::asort($arr);
$this->assertIdentical(array_keys($arr), array(1, 'b', 'a', 0), $this->error);
$this->assertIdentical(array_values($arr), array('aa', 'ab', 'áb', 'cc'), $this->error);
}
function test_asort_objects_by_method() {
$objects = array(
'b' => new string_test_class('ab'),
1 => new string_test_class('aa'),
0 => new string_test_class('cc')
);
collatorlib::asort_objects_by_method($objects, 'get_protected_name');
$this->assertIdentical(array_keys($objects), array(1, 'b', 0));
$this->assertIdentical($this->get_ordered_names($objects, 'get_protected_name'), array('aa', 'ab', 'cc'));

$objects = array(
'a' => new string_test_class('áb'),
'b' => new string_test_class('ab'),
1 => new string_test_class('aa'),
0 => new string_test_class('cc')
);
collatorlib::asort_objects_by_method($objects, 'get_private_name');
$this->assertIdentical(array_keys($objects), array(1, 'b', 'a', 0), $this->error);
$this->assertIdentical($this->get_ordered_names($objects, 'get_private_name'), array('aa', 'ab', 'áb', 'cc'), $this->error);
}
function test_asort_objects_by_property() {
$objects = array(
'b' => new string_test_class('ab'),
1 => new string_test_class('aa'),
0 => new string_test_class('cc')
);
collatorlib::asort_objects_by_property($objects, 'publicname');
$this->assertIdentical(array_keys($objects), array(1, 'b', 0));
$this->assertIdentical($this->get_ordered_names($objects, 'publicname'), array('aa', 'ab', 'cc'));

$objects = array(
'a' => new string_test_class('áb'),
'b' => new string_test_class('ab'),
1 => new string_test_class('aa'),
0 => new string_test_class('cc')
);
collatorlib::asort_objects_by_property($objects, 'publicname');
$this->assertIdentical(array_keys($objects), array(1, 'b', 'a', 0), $this->error);
$this->assertIdentical($this->get_ordered_names($objects, 'publicname'), array('aa', 'ab', 'áb', 'cc'), $this->error);
}
protected function get_ordered_names($objects, $methodproperty = 'get_protected_name') {
$return = array();
foreach ($objects as $object) {
if ($methodproperty == 'publicname') {
$return[] = $object->publicname;
} else {
$return[] = $object->$methodproperty();
}
}
return $return;
}
}
/**
* Simple class used to work with the unit test.
*
* @package core
* @subpackage lib
* @copyright 2011 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class string_test_class extends stdClass {
public $publicname;
protected $protectedname;
private $privatename;
public function __construct($name) {
$this->publicname = $name;
$this->protectedname = $name;
$this->privatename = $name;
}
public function get_protected_name() {
return $this->protectedname;
}
public function get_private_name() {
return $this->publicname;
}
}
Loading

0 comments on commit b49c28a

Please sign in to comment.