Skip to content

Commit a06b87e

Browse files
committed
MDL-75610 output: Fix title display logic in activity header
The display logic for including the title in the activity header was such that the title would only display if both the theme default and the layout option for 'notitle' were undefined or false. It was not possible to a theme to have 'notitle' default to true, but have a layout override that as false to display the title. This change re-writes the is_title_allowed method to encapsulte the new logic, first checking if the current layout has the option set and using that, and if not falling back to the theme default if that is set. If neither is set, the title is displayed. This also tweaks moodle_page::magic_get_layout_options to ensure the theme is initialised before trying to return the layout options.
1 parent cb13bba commit a06b87e

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

lib/classes/output/activity_header.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,10 @@ class activity_header implements renderable, templatable {
5656
public function __construct(moodle_page $page, \stdClass $user) {
5757
$this->page = $page;
5858
$this->user = $user;
59-
$pageoptions = $this->page->theme->activityheaderconfig ?? [];
6059
$layoutoptions = $this->page->layout_options['activityheader'] ?? [];
6160
// Do a basic setup for the header based on theme/page options.
6261
if ($page->activityrecord) {
63-
if (empty($pageoptions['notitle']) && empty($layoutoptions['notitle'])) {
62+
if ($this->is_title_allowed()) {
6463
$this->title = format_string($page->activityrecord->name);
6564
}
6665

@@ -79,10 +78,22 @@ public function __construct(moodle_page $page, \stdClass $user) {
7978
/**
8079
* Checks if the theme has specified titles to be displayed.
8180
*
81+
* First checks if the current layout has the notitle option set. If it is, uses that option to decide whether the title is
82+
* displayed. If not, then checks whether the theme has the notitle option set and uses that. If neither is set, the title
83+
* is allowed by default.
84+
*
8285
* @return bool
8386
*/
8487
public function is_title_allowed(): bool {
85-
return empty($this->page->theme->activityheaderconfig['notitle']);
88+
$layoutoptions = $this->page->layout_options['activityheader'] ?? [];
89+
$themeoptions = $this->page->theme->activityheaderconfig;
90+
if (isset($layoutoptions['notitle'])) {
91+
return !$layoutoptions['notitle'];
92+
} else if (isset($themeoptions['notitle'])) {
93+
return !$themeoptions['notitle'];
94+
} else {
95+
return true;
96+
}
8697
}
8798

8899
/**

lib/pagelib.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ protected function magic_get_pagelayout() {
617617
*/
618618
protected function magic_get_layout_options() {
619619
if (!is_array($this->_layout_options)) {
620-
$this->_layout_options = $this->_theme->pagelayout_options($this->pagelayout);
620+
$this->_layout_options = $this->theme->pagelayout_options($this->pagelayout);
621621
}
622622
return $this->_layout_options;
623623
}

lib/tests/output/activity_header_test.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,55 @@ public function test_get_heading_level(bool $allowtitle, string $title, int $exp
156156
$activityheaderstub->set_title($title);
157157
$this->assertEquals($expectedheadinglevel, $activityheaderstub->get_heading_level());
158158
}
159+
160+
/**
161+
* Tests that is_title_allowed returns correctly based on the theme default and current layout options.
162+
*
163+
* The current layout has precedence, if the notitle option is set, otherwise the theme default is used if set.
164+
*
165+
* @param array $themeoptions The activityheader options array set in the theme.
166+
* @param array $layoutoptions The activitityheader options array set in the layout.
167+
* @param bool $allowed The expected return value of is_title_allowed.
168+
* @covers ::is_title_allowed
169+
* @dataProvider get_title_options
170+
* @return void
171+
*/
172+
public function test_is_title_allowed(array $themeoptions, array $layoutoptions, bool $allowed): void {
173+
$themeconfig = $this->getMockBuilder(\theme_config::class)
174+
->disableOriginalConstructor()
175+
->getMock();
176+
$themeconfig->activityheaderconfig = $themeoptions;
177+
$page = $this->getMockBuilder(\moodle_page::class)
178+
->getMock();
179+
$page->expects($this->any())->method('__get')->willReturnCallback(fn($name) => match(true) {
180+
$name == 'layout_options' => ['activityheader' => $layoutoptions],
181+
$name == 'theme' => $themeconfig,
182+
default => null
183+
});
184+
$user = new \stdClass();
185+
186+
$activityheader = new activity_header($page, $user);
187+
$this->assertEquals($allowed, $activityheader->is_title_allowed());
188+
}
189+
190+
/**
191+
* Return scenarios for test_is_title_allowed.
192+
*
193+
* Test each combination of the 'notitle' option being unset, true and false in each of the theme and layout options.
194+
*
195+
* @return array[]
196+
*/
197+
public static function get_title_options(): array {
198+
return [
199+
'Undefined in theme, undefined in layout' => [[], [], true],
200+
'Undefined in theme, disallowed in layout' => [[], ['notitle' => true], false],
201+
'Undefined in theme, allowed in layout' => [[], ['notitle' => false], true],
202+
'Disallowed in theme, undefined in layout' => [['notitle' => true], [], false],
203+
'Disallowed in theme, disallowed in layout' => [['notitle' => true], ['notitle' => true], false],
204+
'Disallowed in theme, allowed in layout' => [['notitle' => true], ['notitle' => false], true],
205+
'Allowed in theme, undefined in layout' => [['notitle' => false], [], true],
206+
'Allowed in theme, disallowed in layout' => [['notitle' => false], ['notitle' => true], false],
207+
'Allowed in theme, allowed in layout' => [['notitle' => false], ['notitle' => false], true],
208+
];
209+
}
159210
}

0 commit comments

Comments
 (0)