Skip to content

Commit 6d5e452

Browse files
author
Nathan Nguyen
committed
issue#178 refactor subplugins calls
1 parent 8e08f2d commit 6d5e452

File tree

17 files changed

+451
-13
lines changed

17 files changed

+451
-13
lines changed

classes/local/form/form_step_instance.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,11 @@ public function definition_after_data() {
161161
$mform->setDefault('id', '');
162162
$subpluginname = $this->subpluginname;
163163
}
164-
$mform->setDefault('subpluginnamestatic',
165-
get_string('pluginname', 'lifecyclestep_' . $subpluginname));
164+
165+
if (isset($this->lib)) {
166+
$mform->setDefault('subpluginnamestatic', $this->lib->get_plugin_description());
167+
}
168+
166169
$mform->setDefault('subpluginname', $subpluginname);
167170

168171
// Setting the default values for the local step settings.

classes/local/form/form_trigger_instance.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,11 @@ public function definition_after_data() {
162162
$mform->setDefault('id', $this->trigger->id);
163163
$mform->setDefault('instancename', $this->trigger->instancename);
164164
}
165-
$mform->setDefault('subpluginnamestatic',
166-
get_string('pluginname', 'lifecycletrigger_' . $this->subpluginname));
165+
166+
if (isset($this->lib)) {
167+
$mform->setDefault('subpluginnamestatic', $this->lib->get_plugin_description());
168+
}
169+
167170
$mform->setDefault('subpluginname', $this->subpluginname);
168171

169172
// Setting the default values for the local trigger settings.

classes/local/manager/lib_manager.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,27 @@ public static function get_step_interactionlib($subpluginname) {
102102
* @return null|base|libbase
103103
*/
104104
private static function get_lib($subpluginname, $subplugintype, $libsubtype = '') {
105+
// Plugins defined in subplugins.json file.
105106
$triggerlist = \core_component::get_plugin_list('lifecycle' . $subplugintype);
106-
if (!array_key_exists($subpluginname, $triggerlist)) {
107-
return null;
108-
}
109-
$filename = $triggerlist[$subpluginname].'/'.$libsubtype.'lib.php';
110-
if (file_exists($filename)) {
111-
require_once($filename);
112-
$extendedclass = "tool_lifecycle\\$subplugintype\\$libsubtype$subpluginname";
113-
if (class_exists($extendedclass)) {
114-
return new $extendedclass();
107+
if (array_key_exists($subpluginname, $triggerlist)) {
108+
$filename = $triggerlist[$subpluginname].'/'.$libsubtype.'lib.php';
109+
if (file_exists($filename)) {
110+
require_once($filename);
111+
$extendedclass = "tool_lifecycle\\$subplugintype\\$libsubtype$subpluginname";
112+
if (class_exists($extendedclass)) {
113+
return new $extendedclass();
114+
}
115115
}
116116
}
117+
118+
// Plugins defined under "lifecycle" name space.
119+
// The base class has already been checked by get_trigger_types or get_steps_types.
120+
$classname = !$libsubtype ? $subplugintype : $libsubtype;
121+
$classname = "$subpluginname\\lifecycle\\$classname";
122+
if (class_exists($classname)) {
123+
return new $classname();
124+
}
125+
117126
return null;
118127
}
119128
}

classes/local/manager/step_manager.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,27 @@ public static function get_step_instances_by_subpluginname($subpluginname) {
228228
* @throws \coding_exception
229229
*/
230230
public static function get_step_types() {
231+
// Sub plugins in 'step' folder.
231232
$subplugins = \core_component::get_plugin_list('lifecyclestep');
232233
$result = array();
233234
foreach (array_keys($subplugins) as $plugin) {
234235
$result[$plugin] = get_string('pluginname', 'lifecyclestep_' . $plugin);
235236
}
237+
238+
// Additional sub plugins defined under "lifecycle" name space, ie "local_newstep\lifecycle".
239+
// The class name must be step (step.php) and placed under "classes/lifecycle" folder.
240+
// The name space must be "local_newstep\lifecycle"
241+
// The "local_newstep\lifecycle\step" class must extend the step base classes.
242+
foreach (array_keys(\core_component::get_plugin_types()) as $plugintype) {
243+
$potentialsteps = \core_component::get_plugin_list_with_class($plugintype, 'lifecycle\\step');
244+
foreach ($potentialsteps as $plugin => $potentialstep) {
245+
// Check if it implements the step base class.
246+
if (is_a($potentialstep, \tool_lifecycle\step\libbase::class, true)) {
247+
$result[$plugin] = get_string('pluginname', $plugin);
248+
}
249+
}
250+
}
251+
236252
return $result;
237253
}
238254

classes/local/manager/trigger_manager.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,21 @@ public static function get_trigger_types() {
241241
foreach (array_keys($subplugins) as $plugin) {
242242
$result[$plugin] = get_string('pluginname', 'lifecycletrigger_' . $plugin);
243243
}
244+
245+
// Additional sub plugins defined under "lifecycle" name space, ie "local_newtrigger\lifecycle".
246+
// The class name must be trigger (trigger.php) and placed under "classes/lifecycle" folder.
247+
// The name space must be "local_newtrigger\lifecycle"
248+
// The "local_newtrigger\lifecycle\trigger" class must extend the trigger base classes (base_automatic or base_manual).
249+
foreach (array_keys(\core_component::get_plugin_types()) as $plugintype) {
250+
$potentialtriggers = \core_component::get_plugin_list_with_class($plugintype, 'lifecycle\\trigger');
251+
foreach ($potentialtriggers as $plugin => $potentialtrigger) {
252+
// Check if it implements the trigger base class.
253+
if (is_a($potentialtrigger, \tool_lifecycle\trigger\base::class, true)) {
254+
$result[$plugin] = get_string('pluginname', $plugin);
255+
}
256+
}
257+
}
258+
244259
return $result;
245260
}
246261

step/lib.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,16 @@ public function extend_add_instance_form_definition_after_data($mform, $settings
142142
public function abort_course($process) {
143143
}
144144

145+
/**
146+
* Define description of the step.
147+
* Allow subplugins to have custom description.
148+
*
149+
* @return string description of the trigger.
150+
*/
151+
public function get_plugin_description() {
152+
return get_string("pluginname", "lifecyclestep_" . $this->get_subpluginname());
153+
}
154+
145155
}
146156

147157
/**
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace tool_samplestep\lifecycle;
4+
5+
global $CFG;
6+
require_once($CFG->dirroot . '/admin/tool/lifecycle/step/interactionlib.php');
7+
8+
use tool_lifecycle\step\interactionlibbase;
9+
10+
defined('MOODLE_INTERNAL') || die();
11+
12+
class interaction extends interactionlibbase {
13+
14+
public function get_relevant_capability()
15+
{
16+
}
17+
18+
public function get_action_tools($process)
19+
{
20+
}
21+
22+
public function get_status_message($process)
23+
{
24+
}
25+
26+
public function get_action_string($action, $user)
27+
{
28+
}
29+
30+
public function handle_interaction($process, $step, $action = 'default')
31+
{
32+
}
33+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace tool_samplestep\lifecycle;
4+
5+
global $CFG;
6+
require_once($CFG->dirroot . '/admin/tool/lifecycle/step/lib.php');
7+
8+
use tool_lifecycle\step\libbase;
9+
10+
defined('MOODLE_INTERNAL') || die();
11+
12+
class step extends libbase {
13+
public function get_subpluginname()
14+
{
15+
return 'sample step';
16+
}
17+
18+
public function get_plugin_description() {
19+
return "Sample step plugin";
20+
}
21+
22+
public function process_course($processid, $instanceid, $course)
23+
{
24+
return null;
25+
}
26+
27+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace tool_samplestep\privacy;
18+
19+
use core_privacy\local\metadata\null_provider;
20+
21+
/**
22+
* Privacy subsystem implementation for tool_samplestep.
23+
*
24+
* @package tool_samplestep
25+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26+
*/
27+
class provider implements null_provider {
28+
29+
/**
30+
* Get the language string identifier with the component's language
31+
* file to explain why this plugin stores no data.
32+
*
33+
* @return string the reason
34+
*/
35+
public static function get_reason() : string {
36+
return 'privacy:metadata';
37+
}
38+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
$string['pluginname'] = 'Sample step';
18+
$string['privacy:metadata'] = 'The plugin does not store any personal data.';
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Fake component for testing
19+
*
20+
* @package core
21+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22+
*/
23+
24+
defined('MOODLE_INTERNAL') || die();
25+
26+
$plugin->version = 2023100400;
27+
$plugin->requires = 2022041200;
28+
$plugin->component = 'tool_samplestep';
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace tool_sampletrigger\lifecycle;
4+
5+
global $CFG;
6+
require_once($CFG->dirroot . '/admin/tool/lifecycle/trigger/lib.php');
7+
8+
use tool_lifecycle\trigger\base_automatic;
9+
10+
defined('MOODLE_INTERNAL') || die();
11+
12+
class trigger extends base_automatic {
13+
14+
public function get_subpluginname()
15+
{
16+
return 'sample trigger';
17+
}
18+
19+
public function get_plugin_description() {
20+
return "Sample trigger";
21+
}
22+
23+
public function check_course($course, $triggerid)
24+
{
25+
return null;
26+
}
27+
28+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace tool_sampletrigger\privacy;
18+
19+
use core_privacy\local\metadata\null_provider;
20+
21+
/**
22+
* Privacy subsystem implementation for tool_sampletrigger.
23+
*
24+
* @package tool_sampletrigger
25+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26+
*/
27+
class provider implements null_provider {
28+
29+
/**
30+
* Get the language string identifier with the component's language
31+
* file to explain why this plugin stores no data.
32+
*
33+
* @return string the reason
34+
*/
35+
public static function get_reason() : string {
36+
return 'privacy:metadata';
37+
}
38+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
$string['pluginname'] = 'Sample trigger';
18+
$string['privacy:metadata'] = 'The plugin does not store any personal data.';
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Fake component for testing
19+
*
20+
* @package core
21+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22+
*/
23+
24+
defined('MOODLE_INTERNAL') || die();
25+
26+
$plugin->version = 2023100400;
27+
$plugin->requires = 2022041200;
28+
$plugin->component = 'tool_sampletrigger';

0 commit comments

Comments
 (0)