|
| 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 | +} |
0 commit comments