Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions classes/element.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,19 @@ public static function instance(int $id = 0, ?\stdClass $obj = null): element {
* Helper method to create an instance from persistent
*
* @param \tool_certificate\persistent\element $persistent
* @return element
* @return element|null
*/
protected static function instance_from_persistent(\tool_certificate\persistent\element $persistent): ?element {
// Get the class name.
$classname = '\\certificateelement_' . $persistent->get('element') . '\\element';
$elementclasses = element_helper::get_element_classes();

// Ensure the necessary class exists.
if (!class_exists($classname) || !is_subclass_of($classname, self::class)) {
$shortname = $persistent->get('element');
if (!isset($elementclasses[$shortname])) {
return null;
}

/** @var self $el */
/** @var class-string<element> $classname */
$classname = $elementclasses[$shortname];

$el = new $classname($persistent);
$el->persistent = $persistent;
return $el;
Expand Down
55 changes: 42 additions & 13 deletions classes/element_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,29 +446,58 @@ public static function get_element_sequence($pageid) {
return $sequence;
}

/**
* Returns all enabled element classes.
*
* @return array<string, class-string<\tool_certificate\element>>
*/
public static function get_element_classes(): array {
$plugins = self::get_enabled_plugins();

if (!class_exists(\core\hook\manager::class)) {
// Legacy code for 4.2.x and older.
$options = [];
foreach ($plugins as $plugin) {
/** @var element $classname */
$classname = '\\certificateelement_' . $plugin. '\\element';
// Ensure the necessary class exists.
if (class_exists($classname) && is_subclass_of($classname, element::class)) {
$options[$plugin] = $classname;
}
}
return $options;
}

$hook = new hook\element_classes();

// Loop through the enabled plugins.
foreach ($plugins as $plugin) {
$classname = 'certificateelement_' . $plugin. '\\element';
$hook->add_class($plugin, $classname);
}

// Ask other plugins if they define more elements.
\core\hook\manager::get_instance()->dispatch($hook);

return $hook->get_classes();
}

/**
* Return the list of possible elements to add.
*
* @return array the list of element types that can be used.
*/
public static function get_available_element_types() {
global $CFG;

public static function get_available_element_types(): array {
// Array to store the element types.
$options = [];

$plugins = self::get_enabled_plugins();
$classes = self::get_element_classes();

// Loop through the enabled plugins.
foreach ($plugins as $plugin) {
/** @var element $classname */
$classname = '\\certificateelement_' . $plugin. '\\element';
// Ensure the necessary class exists.
if (class_exists($classname) && is_subclass_of($classname, element::class)) {
// Additionally, check if the user is allowed to add the element at all.
if ($classname::can_add()) {
$options[$plugin] = $classname::get_element_type_name();
}
foreach ($classes as $shortname => $classname) {
// Additionally, check if the user is allowed to add the element at all.
if ($classname::can_add()) {
$options[$shortname] = $classname::get_element_type_name();
}
}

Expand Down
76 changes: 76 additions & 0 deletions classes/hook/element_classes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
// This file is part of the tool_certificate plugin for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace tool_certificate\hook;

/**
* Certification element classes discovery hook.
*
* @package tool_certificate
* @copyright 2024 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class element_classes implements \core\hook\described_hook {
/**
* @var array<string, class-string<\tool_certificate\element>>
*/
protected $classes = [];

/**
* Add known enabled element class.
*
* @param string $shortname name of certificateelement sub-plugin or other unique name
* @param string $classname \tool_certificate\element class
*/
public function add_class(string $shortname, string $classname): void {
if (!class_exists($classname) || !is_subclass_of($classname, \tool_certificate\element::class)) {
debugging('Invalid certificate element class: ' . $classname, DEBUG_DEVELOPER);
return;
}
if (isset($this->classes[$shortname])) {
debugging('Duplicate certificate element short name detected: ' . $shortname, DEBUG_DEVELOPER);
// Override previous in case admins forgot to uninstall element add-on.
}
$this->classes[$shortname] = $classname;
}

/**
* Returns known enabled element classes indexed with their short names.
*
* @return array<string, class-string<\tool_certificate\element>>
*/
public function get_classes(): array {
return $this->classes;
}

/**
* Hook description.
*
* @return string
*/
public static function get_hook_description(): string {
return 'Certificate element class discovery';
}

/**
* Hook tags.
*
* @return array
*/
public static function get_hook_tags(): array {
return [];
}
}
3 changes: 1 addition & 2 deletions classes/output/element.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,10 @@ protected static function define_other_properties(): array {
*/
protected function get_other_values(\renderer_base $output): array {
$element = $this->get_element();
$pluginname = 'certificateelement_' . $this->persistent->get('element');
return [
'displayname' => $element->get_display_name(),
'editablename' => $element->get_inplace_editable()->export_for_template($output),
'elementtype' => get_string('pluginname', $pluginname),
'elementtype' => $element::get_element_type_name(),
'movetitle' => get_string('changeelementsequence', 'tool_certificate'),
'icon' => $output->render($this->get_element()->get_element_type_image(true)),
'html' => $this->get_element()->render_html(),
Expand Down