From 7f3d270a370fef9b335048e8bae3340a3b73721c Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Wed, 2 Mar 2022 16:58:15 +0000 Subject: [PATCH] MDL-74071 output: code docs for activity header API to aid IDE hints. --- lib/classes/output/activity_header.php | 9 +++- lib/outputlib.php | 6 +++ lib/pagelib.php | 12 +++-- lib/tests/output/activity_header_test.php | 53 +++++++++++++++++++++++ 4 files changed, 75 insertions(+), 5 deletions(-) diff --git a/lib/classes/output/activity_header.php b/lib/classes/output/activity_header.php index 7af3340b0da..2a3f27597b0 100644 --- a/lib/classes/output/activity_header.php +++ b/lib/classes/output/activity_header.php @@ -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); } } } diff --git a/lib/outputlib.php b/lib/outputlib.php index a99c7cdca6a..2cdf6caf0ad 100644 --- a/lib/outputlib.php +++ b/lib/outputlib.php @@ -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.) diff --git a/lib/pagelib.php b/lib/pagelib.php index 98801ad2237..b27463aa949 100644 --- a/lib/pagelib.php +++ b/lib/pagelib.php @@ -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"; diff --git a/lib/tests/output/activity_header_test.php b/lib/tests/output/activity_header_test.php index c0dbe4e1356..13b84c2096f 100644 --- a/lib/tests/output/activity_header_test.php +++ b/lib/tests/output/activity_header_test.php @@ -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']); + } }