Permalink
Browse files

MDL-28599 textlib Separated collator to collatorlib with static metho…

…ds, and added support for locale aware sorting of objects
  • Loading branch information...
1 parent 9805fb9 commit d609d962619a9477f31d62f1ec227c439ed8edd2 Sam Hemelryk committed Aug 10, 2011
View
@@ -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]);
}
View
@@ -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()));
@@ -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);
@@ -46,7 +46,7 @@ function get_content() {
}
}
- textlib_get_instance()->asort($modfullnames);
+ collatorlib::asort($modfullnames);
foreach ($modfullnames as $modname => $modfullname) {
if ($modname === 'resources') {
@@ -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);
View
@@ -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);
}
@@ -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);
}
View
@@ -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');
}
@@ -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);
}
}
}
View
@@ -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');
View
@@ -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');
View
@@ -519,7 +519,7 @@ function filter_get_all_installed() {
}
}
}
- textlib_get_instance()->asort($filternames);
+ collatorlib::asort($filternames);
return $filternames;
}
View
@@ -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);
}
View
@@ -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();
@@ -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;
}
@@ -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() {
@@ -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;
}
View
@@ -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);
+ }
}
View
@@ -1228,7 +1228,7 @@ function get_import_export_formats($type) {
}
}
- textlib_get_instance()->asort($fileformatnames);
+ collatorlib::asort($fileformatnames);
return $fileformatnames;
}
@@ -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');
@@ -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;
+ }
+}
Oops, something went wrong.

0 comments on commit d609d96

Please sign in to comment.