Skip to content

Commit

Permalink
issue#178 refactor subplugins calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Nguyen committed Nov 17, 2023
1 parent 8e08f2d commit 66b8e09
Show file tree
Hide file tree
Showing 19 changed files with 493 additions and 27 deletions.
7 changes: 5 additions & 2 deletions classes/local/form/form_step_instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,11 @@ public function definition_after_data() {
$mform->setDefault('id', '');
$subpluginname = $this->subpluginname;
}
$mform->setDefault('subpluginnamestatic',
get_string('pluginname', 'lifecyclestep_' . $subpluginname));

if (isset($this->lib)) {
$mform->setDefault('subpluginnamestatic', $this->lib->get_plugin_description());
}

$mform->setDefault('subpluginname', $subpluginname);

// Setting the default values for the local step settings.
Expand Down
7 changes: 5 additions & 2 deletions classes/local/form/form_trigger_instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,11 @@ public function definition_after_data() {
$mform->setDefault('id', $this->trigger->id);
$mform->setDefault('instancename', $this->trigger->instancename);
}
$mform->setDefault('subpluginnamestatic',
get_string('pluginname', 'lifecycletrigger_' . $this->subpluginname));

if (isset($this->lib)) {
$mform->setDefault('subpluginnamestatic', $this->lib->get_plugin_description());
}

$mform->setDefault('subpluginname', $this->subpluginname);

// Setting the default values for the local trigger settings.
Expand Down
27 changes: 18 additions & 9 deletions classes/local/manager/lib_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,27 @@ public static function get_step_interactionlib($subpluginname) {
* @return null|base|libbase
*/
private static function get_lib($subpluginname, $subplugintype, $libsubtype = '') {
// Plugins defined in subplugins.json file.
$triggerlist = \core_component::get_plugin_list('lifecycle' . $subplugintype);
if (!array_key_exists($subpluginname, $triggerlist)) {
return null;
}
$filename = $triggerlist[$subpluginname].'/'.$libsubtype.'lib.php';
if (file_exists($filename)) {
require_once($filename);
$extendedclass = "tool_lifecycle\\$subplugintype\\$libsubtype$subpluginname";
if (class_exists($extendedclass)) {
return new $extendedclass();
if (array_key_exists($subpluginname, $triggerlist)) {
$filename = $triggerlist[$subpluginname].'/'.$libsubtype.'lib.php';
if (file_exists($filename)) {
require_once($filename);
$extendedclass = "tool_lifecycle\\$subplugintype\\$libsubtype$subpluginname";
if (class_exists($extendedclass)) {
return new $extendedclass();
}
}
}

// Plugins defined under "lifecycle" name space.
// The base class has already been checked by get_trigger_types or get_steps_types.
$classname = !$libsubtype ? $subplugintype : $libsubtype;
$classname = "$subpluginname\\lifecycle\\$classname";
if (class_exists($classname)) {
return new $classname();
}

return null;
}
}
16 changes: 16 additions & 0 deletions classes/local/manager/step_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,27 @@ public static function get_step_instances_by_subpluginname($subpluginname) {
* @throws \coding_exception
*/
public static function get_step_types() {
// Sub plugins in 'step' folder.
$subplugins = \core_component::get_plugin_list('lifecyclestep');
$result = array();
foreach (array_keys($subplugins) as $plugin) {
$result[$plugin] = get_string('pluginname', 'lifecyclestep_' . $plugin);
}

// Additional sub plugins defined under "lifecycle" name space, ie "local_newstep\lifecycle".
// The class name must be step (step.php) and placed under "classes/lifecycle" folder.
// The name space must be "local_newstep\lifecycle"
// The "local_newstep\lifecycle\step" class must extend the step base classes.
foreach (array_keys(\core_component::get_plugin_types()) as $plugintype) {
$potentialsteps = \core_component::get_plugin_list_with_class($plugintype, 'lifecycle\\step');
foreach ($potentialsteps as $plugin => $potentialstep) {
// Check if it implements the step base class.
if (is_a($potentialstep, \tool_lifecycle\step\libbase::class, true)) {
$result[$plugin] = get_string('pluginname', $plugin);
}
}
}

return $result;
}

Expand Down
15 changes: 15 additions & 0 deletions classes/local/manager/trigger_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,21 @@ public static function get_trigger_types() {
foreach (array_keys($subplugins) as $plugin) {
$result[$plugin] = get_string('pluginname', 'lifecycletrigger_' . $plugin);
}

// Additional sub plugins defined under "lifecycle" name space, ie "local_newtrigger\lifecycle".
// The class name must be trigger (trigger.php) and placed under "classes/lifecycle" folder.
// The name space must be "local_newtrigger\lifecycle"
// The "local_newtrigger\lifecycle\trigger" class must extend the trigger base classes (base_automatic or base_manual).
foreach (array_keys(\core_component::get_plugin_types()) as $plugintype) {
$potentialtriggers = \core_component::get_plugin_list_with_class($plugintype, 'lifecycle\\trigger');
foreach ($potentialtriggers as $plugin => $potentialtrigger) {
// Check if it implements the trigger base class.
if (is_a($potentialtrigger, \tool_lifecycle\trigger\base::class, true)) {
$result[$plugin] = get_string('pluginname', $plugin);
}
}
}

return $result;
}

Expand Down
26 changes: 13 additions & 13 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
* @copyright 2017 Tobias Reischmann WWU
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

use tool_lifecycle\local\manager\lib_manager;
use tool_lifecycle\local\manager\step_manager;
use tool_lifecycle\local\manager\trigger_manager;

defined('MOODLE_INTERNAL') || die;

if ($hassiteconfig) {
Expand Down Expand Up @@ -68,21 +73,16 @@
new moodle_url('/admin/tool/lifecycle/errors.php')));

if ($ADMIN->fulltree) {
$triggers = core_component::get_plugin_list('lifecycletrigger');
foreach ($triggers as $trigger => $path) {
if (file_exists($settingsfile = $path . '/settings.php')) {
$settings->add(new admin_setting_heading('lifecycletriggersetting'.$trigger,
get_string('trigger', 'tool_lifecycle') .
' - ' . get_string('pluginname', 'lifecycletrigger_' . $trigger), ''));
include($settingsfile);
}
$triggers = trigger_manager::get_trigger_types();
foreach ($triggers as $id => $trigger) {
$lib = lib_manager::get_trigger_lib($id);
$lib->get_plugin_settings();
}
}

$steps = core_component::get_plugin_list('lifecyclestep');
foreach ($steps as $step => $path) {
if (file_exists($settingsfile = $path . '/settings.php')) {
include($settingsfile);
}
$steps = step_manager::get_step_types();
foreach ($steps as $id => $step) {
$lib = lib_manager::get_step_lib($id);
$lib->get_plugin_settings();
}
}
24 changes: 24 additions & 0 deletions step/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,30 @@ public function extend_add_instance_form_definition_after_data($mform, $settings
public function abort_course($process) {
}

/**
* Define description of the step.
* Allow subplugins to have custom description.
*
* @return string description of the trigger.
*/
public function get_plugin_description() {
return get_string("pluginname", "lifecyclestep_" . $this->get_subpluginname());
}

/**
* Returns the settings of the step.
*
* @return void
*/
public function get_plugin_settings() {
$step = $this->get_subpluginname();
$file = __DIR__ . "/$step/settings.php";

if (file_exists($file)) {
include($file);
}
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace tool_samplestep\lifecycle;

global $CFG;
require_once($CFG->dirroot . '/admin/tool/lifecycle/step/interactionlib.php');

use tool_lifecycle\step\interactionlibbase;

defined('MOODLE_INTERNAL') || die();

class interaction extends interactionlibbase {

public function get_relevant_capability()
{
}

public function get_action_tools($process)
{
}

public function get_status_message($process)
{
}

public function get_action_string($action, $user)
{
}

public function handle_interaction($process, $step, $action = 'default')
{
}
}
27 changes: 27 additions & 0 deletions tests/fixtures/fakeplugins/samplestep/classes/lifecycle/step.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace tool_samplestep\lifecycle;

global $CFG;
require_once($CFG->dirroot . '/admin/tool/lifecycle/step/lib.php');

use tool_lifecycle\step\libbase;

defined('MOODLE_INTERNAL') || die();

class step extends libbase {
public function get_subpluginname()
{
return 'sample step';
}

public function get_plugin_description() {
return "Sample step plugin";
}

public function process_course($processid, $instanceid, $course)
{
return null;
}

}
38 changes: 38 additions & 0 deletions tests/fixtures/fakeplugins/samplestep/classes/privacy/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
// This file is part of 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_samplestep\privacy;

use core_privacy\local\metadata\null_provider;

/**
* Privacy subsystem implementation for tool_samplestep.
*
* @package tool_samplestep
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements null_provider {

/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string the reason
*/
public static function get_reason() : string {
return 'privacy:metadata';
}
}
18 changes: 18 additions & 0 deletions tests/fixtures/fakeplugins/samplestep/lang/en/tool_samplestep.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
// This file is part of 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/>.

$string['pluginname'] = 'Sample step';
$string['privacy:metadata'] = 'The plugin does not store any personal data.';
28 changes: 28 additions & 0 deletions tests/fixtures/fakeplugins/samplestep/version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
// This file is part of 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/>.

/**
* Fake component for testing
*
* @package core
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2023100400;
$plugin->requires = 2022041200;
$plugin->component = 'tool_samplestep';
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace tool_sampletrigger\lifecycle;

global $CFG;
require_once($CFG->dirroot . '/admin/tool/lifecycle/trigger/lib.php');

use tool_lifecycle\trigger\base_automatic;

defined('MOODLE_INTERNAL') || die();

class trigger extends base_automatic {

public function get_subpluginname()
{
return 'sample trigger';
}

public function get_plugin_description() {
return "Sample trigger";
}

public function check_course($course, $triggerid)
{
return null;
}

}
Loading

0 comments on commit 66b8e09

Please sign in to comment.