Skip to content

Commit

Permalink
MDL-67585 core_course: add support for custom titles to content_item
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
snake committed Feb 20, 2020
1 parent dd8048e commit 9442085
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 8 deletions.
10 changes: 5 additions & 5 deletions course/classes/local/entity/content_item.php
Expand Up @@ -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). */
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
62 changes: 62 additions & 0 deletions course/classes/local/entity/lang_string_title.php
@@ -0,0 +1,62 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* 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 <jrhdallimore@gmail.com>
* @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 <jrhdallimore@gmail.com>
* @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);
}
}
57 changes: 57 additions & 0 deletions course/classes/local/entity/string_title.php
@@ -0,0 +1,57 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Contains the string_title class of value object, which provides access to a simple string.
*
* @package core
* @subpackage course
* @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com>
* @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 <jrhdallimore@gmail.com>
* @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;
}
}
35 changes: 35 additions & 0 deletions course/classes/local/entity/title.php
@@ -0,0 +1,35 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Contains the title value object interface, which provides a basic interface to a string.
*
* @package core
* @subpackage course
* @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com>
* @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;
}
20 changes: 17 additions & 3 deletions course/tests/content_item_test.php
Expand Up @@ -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.
Expand All @@ -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'), '<img src="test">',
'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'), '<img src="test">', '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('<img src="test">', $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'), '<img src="test">', 'Description of the module', MOD_ARCHETYPE_RESOURCE, 'mod_page');

$this->assertEquals('My custom string', $contentitem->get_title()->get_value());
}
}

0 comments on commit 9442085

Please sign in to comment.