MDL-74071 output: code docs for activity header API to aid IDE hints.

This commit is contained in:
Paul Holden 2022-03-02 16:58:15 +00:00
parent 9cd77c4130
commit 7f3d270a37
4 changed files with 75 additions and 5 deletions

View File

@ -87,12 +87,19 @@ class activity_header implements \renderable, \templatable {
/**
* Bulk set class member variables. Only updates variables which have corresponding setters
*
* @param array $config
* @param mixed[] $config Array of variables to set, with keys being their name. Valid names/types as follows:
* 'hidecompletion' => bool
* 'additionalnavitems' => url_select
* 'hideoverflow' => bool
* 'title' => string
* 'description' => string
*/
public function set_attrs(array $config): void {
foreach ($config as $key => $value) {
if (method_exists($this, "set_$key")) {
$this->{"set_$key"}($value);
} else {
debugging("Invalid class member variable: {$key}", DEBUG_DEVELOPER);
}
}
}

View File

@ -683,6 +683,12 @@ class theme_config {
*/
public $usescourseindex = false;
/**
* Configuration for the page activity header
* @var array
*/
public $activityheaderconfig = [];
/**
* Load the config.php file for a particular theme, and return an instance
* of this class. (That is, this is a factory method.)

View File

@ -26,9 +26,11 @@
*/
defined('MOODLE_INTERNAL') || die();
use core\navigation\views\primary;
use core\navigation\views\secondary;
use core\navigation\output\primary as primaryoutput;
use core\output\activity_header;
/**
* $PAGE is a central store of information about the current page we are
@ -53,6 +55,8 @@ use core\navigation\output\primary as primaryoutput;
* @property-read stdClass $activityrecord The row from the activities own database table (for example
* the forum or quiz table) that this page belongs to. Will be null
* if this page is not within a module.
* @property-read activity_header $activityheader The activity header for the page, representing standard components
* displayed within the header
* @property-read array $alternativeversions Mime type => object with ->url and ->title.
* @property-read block_manager $blocks The blocks manager object for this page.
* @property-read array $blockmanipulations
@ -404,7 +408,7 @@ class moodle_page {
protected $_activenodeprimary = null;
/**
* @var \core\output\activity_header The default activity header for standardised.
* @var activity_header The activity header for the page.
*/
protected $_activityheader;
@ -835,12 +839,12 @@ class moodle_page {
/**
* Returns the activity header object
* @return secondary
* @return activity_header
*/
protected function magic_get_activityheader() {
protected function magic_get_activityheader(): activity_header {
global $USER;
if ($this->_activityheader === null) {
$class = 'core\output\activity_header';
$class = activity_header::class;
// Try and load a custom class first.
if (class_exists("mod_{$this->activityname}\\output\\activity_header")) {
$class = "mod_{$this->activityname}\\output\\activity_header";

View File

@ -21,16 +21,19 @@ namespace core\output;
*
* @package core
* @category test
* @coversDefaultClass \core\output\activity_header
* @copyright 2021 Peter
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class activity_header_test extends \advanced_testcase {
/**
* Test the title setter
*
* @dataProvider test_set_title_provider
* @param string $value
* @param string $expected
* @covers ::set_title
*/
public function test_set_title(string $value, string $expected): void {
global $PAGE, $DB;
@ -70,4 +73,54 @@ class activity_header_test extends \advanced_testcase {
],
];
}
/**
* Test setting multiple attributes
*
* @covers ::set_attrs
*/
public function test_set_attrs(): void {
global $DB, $PAGE;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course(['enablecompletion' => true]);
$assign = $this->getDataGenerator()->create_module('assign', [
'course' => $course->id,
'completion' => COMPLETION_TRACKING_AUTOMATIC,
'completionview' => 1
]);
$cm = $DB->get_record('course_modules', ['id' => $assign->cmid]);
$PAGE->set_cm($cm);
$PAGE->set_activity_record($assign);
$PAGE->activityheader->set_attrs([
'hidecompletion' => true,
'additionalnavitems' => new \url_select([]),
'hideoverflow' => true,
'title' => 'My title',
'description' => 'My description',
]);
$renderer = $PAGE->get_renderer('core');
$export = $PAGE->activityheader->export_for_template($renderer);
$this->assertEquals('My title', $export['title']);
$this->assertEquals('My description', $export['description']);
$this->assertEmpty($export['completion']); // Because hidecompletion = true.
$this->assertEmpty($export['additional_items']); // Because hideoverflow = true.
}
/**
* Test calling set_attrs with an invalid variable name
*
* @covers ::set_attrs
*/
public function test_set_attrs_invalid_variable(): void {
global $PAGE;
$PAGE->activityheader->set_attrs(['unknown' => true]);
$this->assertDebuggingCalledCount(1, ['Invalid class member variable: unknown']);
}
}