From f696590f0aafdff24cb1b54040934a38e650ce3b Mon Sep 17 00:00:00 2001 From: sam marshall Date: Mon, 27 Jun 2022 16:42:43 +0100 Subject: [PATCH] MDL-75074 core_courseformat: Output classes are not inherited If you have one course format that extends another one, any course format output classes that are not defined in the child format will now inherit from the parent (instead of from the base class as before). --- course/format/classes/base.php | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/course/format/classes/base.php b/course/format/classes/base.php index 9523522a5f9..ad2eec1cfbc 100644 --- a/course/format/classes/base.php +++ b/course/format/classes/base.php @@ -1273,16 +1273,28 @@ abstract class base { public function get_output_classname(string $outputname): string { // The core output class. $baseclass = "core_courseformat\\output\\local\\$outputname"; - // Check if there is a specific format class. - $component = 'format_'. $this->get_format(); - $outputclass = "$component\\output\\courseformat\\$outputname"; - if (class_exists($outputclass)) { - // Check that the outputclass is a subclass of the base class. - if (!is_subclass_of($outputclass, $baseclass)) { - throw new coding_exception("The \"$outputclass\" must extend \"$baseclass\""); + + // Look in this format and any parent formats before we get to the base one. + $classes = array_merge([get_class($this)], class_parents($this)); + foreach ($classes as $component) { + if ($component === self::class) { + break; + } + + // Because course formats are in the root namespace, there is no need to process the + // class name - it is already a Frankenstyle component name beginning 'format_'. + + // Check if there is a specific class in this format. + $outputclass = "$component\\output\\courseformat\\$outputname"; + if (class_exists($outputclass)) { + // Check that the outputclass is a subclass of the base class. + if (!is_subclass_of($outputclass, $baseclass)) { + throw new coding_exception("The \"$outputclass\" must extend \"$baseclass\""); + } + return $outputclass; } - return $outputclass; } + return $baseclass; }