Skip to content

Commit bb366ac

Browse files
authored
new trigger depending on enddate (#272)
1 parent 9430b81 commit bb366ac

File tree

8 files changed

+442
-0
lines changed

8 files changed

+442
-0
lines changed

trigger/enddate/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# moodle-lifecycletrigger_enddate
2+
3+
This subplugin adds a trigger for the tool lifecycle that depends on the course enddate.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 lifecycletrigger_enddate\privacy;
18+
19+
use core_privacy\local\metadata\null_provider;
20+
21+
/**
22+
* Privacy subsystem implementation for lifecycletrigger_enddate.
23+
*
24+
* @package lifecycletrigger_enddate
25+
* @copyright 2022 ISB Bayern
26+
* @author Philipp Memmel
27+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28+
*/
29+
class provider implements null_provider {
30+
31+
/**
32+
* Get the language string identifier with the component's language
33+
* file to explain why this plugin stores no data.
34+
*
35+
* @return string the reason
36+
*/
37+
public static function get_reason(): string {
38+
return 'privacy:metadata';
39+
}
40+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
* Lang strings for start date delay trigger
19+
*
20+
* @package lifecycletrigger_enddate
21+
* @copyright 2025 Ostfalia
22+
* @copyright 2017 Tobias Reischmann WWU
23+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24+
*/
25+
26+
$string['delay'] = 'Zeit seit Kursendedatum, bis ein Prozess gestartet wird';
27+
$string['delay_help'] = 'Dieser Trigger wird ausgeführt, sobald die Zeit, die seit dem Ende des Kurses vergangen ist, größer ist als der angegebene Zeitraum.';
28+
$string['plugindescription'] = 'Trigger des Lifecycle-Tools, der bestimmt, wieviel Zeit nach dem Endedatum des Kurses verstreichen muss, ehe dieser Workflow startet.';
29+
$string['pluginname'] = 'Endedatumsabstand - Trigger';
30+
$string['privacy:metadata'] = 'Dieses Subplugin speichert keine persönlichen Daten.';
31+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
* Lang strings for start date delay trigger
19+
*
20+
* @package lifecycletrigger_enddate
21+
* @copyright 2025 Ostfalia
22+
* @copyright 2017 Tobias Reischmann WWU
23+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24+
*/
25+
26+
$string['delay'] = 'Delay from end of course until starting a process';
27+
$string['delay_help'] = 'The trigger will be invoked if the time passed since the course has ended is longer than this delay.';
28+
$string['plugindescription'] = 'Defines the time that has to pass after the course end date has been reached before this workflow starts.';
29+
$string['pluginname'] = 'End date trigger';
30+
$string['privacy:metadata'] = 'This subplugin does not store any personal data.';
31+

trigger/enddate/lib.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
* Subplugin for the start date delay.
19+
*
20+
* @package lifecycletrigger_enddate
21+
* @copyright 2025 Ostfalia
22+
* @copyright 2017 Tobias Reischmann WWU
23+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24+
*/
25+
namespace tool_lifecycle\trigger;
26+
27+
use tool_lifecycle\local\manager\settings_manager;
28+
use tool_lifecycle\local\response\trigger_response;
29+
use tool_lifecycle\settings_type;
30+
31+
defined('MOODLE_INTERNAL') || die();
32+
require_once(__DIR__ . '/../lib.php');
33+
require_once(__DIR__ . '/../../lib.php');
34+
35+
/**
36+
* Class which implements the basic methods necessary for a cleanyp courses trigger subplugin
37+
*
38+
* @package lifecycletrigger_enddate
39+
* @copyright 2025 Ostfalia
40+
* @copyright 2017 Tobias Reischmann WWU
41+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42+
*/
43+
class enddate extends base_automatic {
44+
45+
/**
46+
* If check_course_code() returns true, code to check the given course is placed here
47+
* @param object $course
48+
* @param int $triggerid
49+
* @return trigger_response
50+
*/
51+
public function check_course($course, $triggerid) {
52+
return trigger_response::trigger();
53+
}
54+
55+
/**
56+
* Add sql comparing the current date to the start date of a course in combination with the specified delay.
57+
* @param int $triggerid Id of the trigger.
58+
* @return array A list containing the constructed sql fragment and an array of parameters.
59+
* @throws \coding_exception
60+
* @throws \dml_exception
61+
*/
62+
public function get_course_recordset_where($triggerid) {
63+
$delay = settings_manager::get_settings($triggerid, settings_type::TRIGGER)['delay'];
64+
$where = "c.enddate > 0 AND c.enddate < :enddatedelay";
65+
$params = [
66+
"enddatedelay" => time() - $delay,
67+
];
68+
return [$where, $params];
69+
}
70+
71+
/**
72+
* The return value should be equivalent with the name of the subplugin folder.
73+
* @return string technical name of the subplugin
74+
*/
75+
public function get_subpluginname() {
76+
return 'enddate';
77+
}
78+
79+
/**
80+
* Defines which settings each instance of the subplugin offers for the user to define.
81+
* @return instance_setting[] containing settings keys and PARAM_TYPES
82+
*/
83+
public function instance_settings() {
84+
return [
85+
new instance_setting('delay', PARAM_INT, true),
86+
];
87+
}
88+
89+
/**
90+
* At the delay since the start date of a course.
91+
* @param \MoodleQuickForm $mform
92+
* @throws \coding_exception
93+
*/
94+
public function extend_add_instance_form_definition($mform) {
95+
$mform->addElement('duration', 'delay', get_string('delay', 'lifecycletrigger_enddate'));
96+
$mform->addHelpButton('delay', 'delay', 'lifecycletrigger_enddate');
97+
}
98+
99+
/**
100+
* Reset the delay at the add instance form initializiation.
101+
* @param \MoodleQuickForm $mform
102+
* @param array $settings array containing the settings from the db.
103+
*/
104+
public function extend_add_instance_form_definition_after_data($mform, $settings) {
105+
if (is_array($settings) && array_key_exists('delay', $settings)) {
106+
$default = $settings['delay'];
107+
} else {
108+
$default = 24 * 60 * 60; // One day.
109+
}
110+
$mform->setDefault('delay', $default);
111+
}
112+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
* lifecycletrigger_enddate generator tests
19+
*
20+
* @package lifecycletrigger_enddate
21+
* @category test
22+
* @copyright 2025 Ostfalia
23+
* @copyright 2018 Tobias Reischmann WWU
24+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25+
*/
26+
27+
use tool_lifecycle\local\entity\trigger_subplugin;
28+
use tool_lifecycle\local\entity\workflow;
29+
use tool_lifecycle\local\manager\settings_manager;
30+
use tool_lifecycle\local\manager\trigger_manager;
31+
use tool_lifecycle\local\manager\workflow_manager;
32+
use tool_lifecycle\settings_type;
33+
34+
/**
35+
* lifecycletrigger_enddate generator tests
36+
*
37+
* @package lifecycletrigger_enddate
38+
* @category test
39+
* @copyright 2025 Ostfalia
40+
* @copyright 2018 Tobias Reischmann WWU
41+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42+
*/
43+
class tool_lifecycle_trigger_enddate_generator extends testing_module_generator {
44+
45+
/**
46+
* Creates a trigger enddatedelay for an artificial workflow without steps.
47+
* @return trigger_subplugin the created enddatedelay trigger.
48+
* @throws moodle_exception
49+
*/
50+
public static function create_trigger_with_workflow() {
51+
// Create Workflow.
52+
$record = new stdClass();
53+
$record->id = null;
54+
$record->title = 'myworkflow';
55+
$workflow = workflow::from_record($record);
56+
workflow_manager::insert_or_update($workflow);
57+
// Create trigger.
58+
$record = new stdClass();
59+
$record->subpluginname = 'enddate';
60+
$record->instancename = 'enddate';
61+
$record->workflowid = $workflow->id;
62+
$trigger = trigger_subplugin::from_record($record);
63+
trigger_manager::insert_or_update($trigger);
64+
// Set delay setting.
65+
$settings = new stdClass();
66+
$settings->delay = 10 * DAYSECS; // Trigger when 10 days have passed.
67+
settings_manager::save_settings($trigger->id, settings_type::TRIGGER, $trigger->subpluginname, $settings);
68+
69+
return $trigger;
70+
}
71+
}

0 commit comments

Comments
 (0)