From 94420851a5e07a607c65572c488ed8fb65f57f7a Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Wed, 22 Jan 2020 13:49:28 +0800 Subject: [PATCH] MDL-67585 core_course: add support for custom titles to content_item Plugins have always been able to return either a string or a lang string when implementing the hook, 'get_shortcuts'. Since content_items will be the replacement for that stdClass implementation, we need a way for plugins to continue to have this flexibility. This just provides a small contract and some classes that plugins can use in future. --- course/classes/local/entity/content_item.php | 10 +-- .../local/entity/lang_string_title.php | 62 +++++++++++++++++++ course/classes/local/entity/string_title.php | 57 +++++++++++++++++ course/classes/local/entity/title.php | 35 +++++++++++ course/tests/content_item_test.php | 20 +++++- 5 files changed, 176 insertions(+), 8 deletions(-) create mode 100644 course/classes/local/entity/lang_string_title.php create mode 100644 course/classes/local/entity/string_title.php create mode 100644 course/classes/local/entity/title.php diff --git a/course/classes/local/entity/content_item.php b/course/classes/local/entity/content_item.php index b6554936599e8..cd7c264d4ee24 100644 --- a/course/classes/local/entity/content_item.php +++ b/course/classes/local/entity/content_item.php @@ -39,7 +39,7 @@ class content_item { /** @var string $name the name. */ private $name; - /** @var string $title the title. */ + /** @var title $title the title. */ private $title; /** @var \moodle_url $link the url for the content item's setup page (usually mod/edit.php). */ @@ -62,14 +62,14 @@ class content_item { * * @param int $id Id number. * @param string $name Name of the item, not human readable. - * @param string $title Human readable title for the item. + * @param title $title Human readable title for the item. * @param \moodle_url $link The URL to the creation page, with any item specific params * @param string $icon HTML containing the icon for the item * @param string $help The description of the item. * @param int $archetype the archetype for the content item (see MOD_ARCHETYPE_X definitions in lib/moodlelib.php). * @param string $componentname the name of the component/plugin with which this content item is associated. */ - public function __construct(int $id, string $name, string $title, \moodle_url $link, string $icon, string $help, + public function __construct(int $id, string $name, title $title, \moodle_url $link, string $icon, string $help, int $archetype, string $componentname) { $this->id = $id; $this->name = $name; @@ -128,9 +128,9 @@ public function get_name(): string { /** * Get the human readable title of this item. * - * @return string + * @return title */ - public function get_title(): string { + public function get_title(): title { return $this->title; } diff --git a/course/classes/local/entity/lang_string_title.php b/course/classes/local/entity/lang_string_title.php new file mode 100644 index 0000000000000..17b3de6c59376 --- /dev/null +++ b/course/classes/local/entity/lang_string_title.php @@ -0,0 +1,62 @@ +. + +/** + * Contains the lang_string_title class of value object, providing access to the value of a lang string. + * + * @package core + * @subpackage course + * @copyright 2020 Jake Dallimore + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +namespace core_course\local\entity; + +defined('MOODLE_INTERNAL') || die(); + +/** + * The lang_string_title class of value object, providing access to the value of a lang string. + * + * @copyright 2020 Jake Dallimore + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class lang_string_title implements title { + + /** @var string $component the component name. */ + private $component; + + /** @var string $identifier the string identifier. */ + private $identifier; + + /** + * The lang_string_title constructor. + * + * @param string $identifier the component name. + * @param string $component the string identifier. + */ + public function __construct(string $identifier, string $component) { + $this->identifier = $identifier; + $this->component = $component; + } + + /** + * Returns the value of the wrapped string. + * + * @return string the value of the string. + */ + public function get_value(): string { + return get_string($this->identifier, $this->component); + } +} diff --git a/course/classes/local/entity/string_title.php b/course/classes/local/entity/string_title.php new file mode 100644 index 0000000000000..e7da7815b6da2 --- /dev/null +++ b/course/classes/local/entity/string_title.php @@ -0,0 +1,57 @@ +. + +/** + * Contains the string_title class of value object, which provides access to a simple string. + * + * @package core + * @subpackage course + * @copyright 2020 Jake Dallimore + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +namespace core_course\local\entity; + +defined('MOODLE_INTERNAL') || die(); + +/** + * The string_title class of value object, which provides access to a simple string. + * + * @copyright 2020 Jake Dallimore + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class string_title implements title { + + /** @var string $title the title string. */ + private $title; + + /** + * The string_title constructor. + * + * @param string $title a string. + */ + public function __construct(string $title) { + $this->title = $title; + } + + /** + * Return the value of the wrapped string. + * + * @return string + */ + public function get_value(): string { + return $this->title; + } +} diff --git a/course/classes/local/entity/title.php b/course/classes/local/entity/title.php new file mode 100644 index 0000000000000..2bcc4a26250a6 --- /dev/null +++ b/course/classes/local/entity/title.php @@ -0,0 +1,35 @@ +. + +/** + * Contains the title value object interface, which provides a basic interface to a string. + * + * @package core + * @subpackage course + * @copyright 2020 Jake Dallimore + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +namespace core_course\local\entity; + +defined('MOODLE_INTERNAL') || die(); + +interface title { + + /** + * Get the value of this title. + */ + public function get_value(): string; +} diff --git a/course/tests/content_item_test.php b/course/tests/content_item_test.php index ec06747576199..174529d87a17a 100644 --- a/course/tests/content_item_test.php +++ b/course/tests/content_item_test.php @@ -27,6 +27,8 @@ defined('MOODLE_INTERNAL') || die(); use core_course\local\entity\content_item; +use core_course\local\entity\lang_string_title; +use core_course\local\entity\string_title; /** * Tests for the \core_course\local\entity\content_item class. @@ -44,16 +46,28 @@ class content_item_testcase extends \advanced_testcase { public function test_content_item() { $this->resetAfterTest(); - $contentitem = new content_item(22, 'Item name', 'Item title', new \moodle_url('mod_edit.php'), '', - 'Description of the module', MOD_ARCHETYPE_RESOURCE, 'mod_page'); + $contentitem = new content_item(22, 'Item name', new lang_string_title('modulename', 'mod_assign'), + new \moodle_url('mod_edit.php'), '', 'Description of the module', MOD_ARCHETYPE_RESOURCE, 'mod_page'); $this->assertEquals(22, $contentitem->get_id()); $this->assertEquals('Item name', $contentitem->get_name()); - $this->assertEquals('Item title', $contentitem->get_title()); + $this->assertEquals('Assignment', $contentitem->get_title()->get_value()); $this->assertEquals(new \moodle_url('mod_edit.php'), $contentitem->get_link()); $this->assertEquals('', $contentitem->get_icon()); $this->assertEquals('Description of the module', $contentitem->get_help()); $this->assertEquals(MOD_ARCHETYPE_RESOURCE, $contentitem->get_archetype()); $this->assertEquals('mod_page', $contentitem->get_component_name()); } + + /** + * Test confirming that plugins can return custom titles for a content item. + */ + public function test_content_item_custom_string_title() { + $this->resetAfterTest(); + + $contentitem = new content_item(22, 'Item name', new string_title('My custom string'), + new \moodle_url('mod_edit.php'), '', 'Description of the module', MOD_ARCHETYPE_RESOURCE, 'mod_page'); + + $this->assertEquals('My custom string', $contentitem->get_title()->get_value()); + } }