. namespace core_courseformat; use section_info; /** * Section delegate base class. * * Plugins using delegate sections must extend this class into * their PLUGINNAME\courseformat\sectiondelegate class. * * @package core_courseformat * @copyright 2023 Ferran Recio * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ abstract class sectiondelegate { /** * Constructor. * @param section_info $sectioninfo */ public function __construct( protected section_info $sectioninfo ) { } /** * Get the section info instance if available. * * @param section_info $sectioninfo * @return section_info|null */ public static function instance(section_info $sectioninfo): ?self { if (empty($sectioninfo->component)) { return null; } $classname = self::get_delegate_class_name($sectioninfo->component); if ($classname === null) { return null; } return new $classname($sectioninfo); } /** * Return the delgate class name of a plugin, if any. * @param string $pluginname * @return string|null the delegate class name or null if not found. */ protected static function get_delegate_class_name(string $pluginname): ?string { $classname = $pluginname . '\courseformat\sectiondelegate'; if (!class_exists($classname)) { return null; } return $classname; } /** * Check if a plugin has a delegate class. * @param string $pluginname * @return bool */ public static function has_delegate_class(string $pluginname): bool { return self::get_delegate_class_name($pluginname) !== null; } }