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 Oct 6, 2023
1 parent 8e08f2d commit 6d5e452
Show file tree
Hide file tree
Showing 17 changed files with 451 additions and 13 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
10 changes: 10 additions & 0 deletions step/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ 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());
}

}

/**
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;
}

}
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_sampletrigger\privacy;

use core_privacy\local\metadata\null_provider;

/**
* Privacy subsystem implementation for tool_sampletrigger.
*
* @package tool_sampletrigger
* @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';
}
}
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 trigger';
$string['privacy:metadata'] = 'The plugin does not store any personal data.';
28 changes: 28 additions & 0 deletions tests/fixtures/fakeplugins/sampletrigger/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_sampletrigger';
Loading

0 comments on commit 6d5e452

Please sign in to comment.