Skip to content
Snippets Groups Projects
Unverified Commit 11605f38 authored by Tobias Reischmann's avatar Tobias Reischmann
Browse files

Added a trigger for delaying courses after rollback

parent 996b67cb
No related branches found
No related tags found
No related merge requests found
<?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/>.
/**
* Manager for Delayed Courses
* Each entry tells that the trigger-check for a certain course is delayed until a certain timestamp.
*
* @package tool_cleanupcourses
* @copyright 2017 Tobias Reischmann WWU
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_cleanupcourses\manager;
defined('MOODLE_INTERNAL') || die();
class delayed_courses_manager {
/**
* Creates an instance of a delayed course.
* @param int $courseid id of the course
* @param int $duration number of seconds
*/
public static function set_course_delayed($courseid, $duration) {
global $DB;
$delayeduntil = time() + $duration;
$record = $DB->get_record('tool_cleanupcourses_delayed', array('courseid' => $courseid));
if (!$record) {
$record = new \stdClass();
$record->courseid = $courseid;
$record->delayeduntil = $delayeduntil;
$DB->insert_record('tool_cleanupcourses_delayed', $record);
} else {
if ($record['delayeduntil'] < $delayeduntil) {
$record = (object) $record;
$record->delayeduntil = $delayeduntil;
$DB->update_record('tool_cleanupcourses_delayed', $record);
}
}
}
/**
* Queries if a course was delayed.
* @param int $courseid id of the course
* @return null|int timestamp until when the course is delayed (null if no entry exists).
*/
public static function get_course_delayed($courseid) {
global $DB;
$record = $DB->get_record('tool_cleanupcourses_delayed', array('courseid' => $courseid));
if ($record) {
return $record->delayeduntil;
} else {
return null;
}
}
/**
* Deletes the delay entry for a course.
* @param int $courseid id of the course
*/
public static function remove_delay_entry($courseid) {
global $DB;
$DB->delete_records('tool_cleanupcourses_delayed', array('courseid' => $courseid));
}
}
......@@ -25,6 +25,7 @@ namespace tool_cleanupcourses\manager;
use tool_cleanupcourses\entity\process;
use tool_cleanupcourses\entity\trigger_subplugin;
use tool_cleanupcourses\manager\delayed_courses_manager;
defined('MOODLE_INTERNAL') || die();
......@@ -119,6 +120,7 @@ class process_manager {
*/
public static function rollback_process($process) {
// TODO: Add logic to revert changes made by steps.
delayed_courses_manager::set_course_delayed($process->courseid, 60);
self::remove_process($process);
}
......@@ -130,5 +132,6 @@ class process_manager {
global $DB;
$DB->delete_records('tool_cleanupcourses_procdata', array('processid' => $process->id));
$DB->delete_records('tool_cleanupcourses_process', (array) $process);
delayed_courses_manager::remove_delay_entry($process->courseid);
}
}
......@@ -13,7 +13,18 @@
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="courseid_fk" TYPE="unique" FIELDS="courseid" COMMENT="Foreign key on course table"/>
<KEY NAME="courseid_fk" TYPE="foreign" FIELDS="courseid" REFTABLE="course" REFFIELDS="id" COMMENT="Foreign key on course table"/>
</KEYS>
</TABLE>
<TABLE NAME="tool_cleanupcourses_delayed" COMMENT="List of courses, for which the next check is delayed until a certain timestamp.">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="courseid" TYPE="int" LENGTH="20" NOTNULL="true" SEQUENCE="false" COMMENT="id of the course"/>
<FIELD NAME="delayeduntil" TYPE="int" LENGTH="11" NOTNULL="true" SEQUENCE="false" COMMENT="The timestamp till when the course is delayed"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="courseid_fk" TYPE="foreign" FIELDS="courseid" REFTABLE="course" REFFIELDS="id" COMMENT="Foreign key on course table"/>
</KEYS>
</TABLE>
<TABLE NAME="tool_cleanupcourses_trigger" COMMENT="Trigger subplugins for the cleanup courses">
......
<?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/>.
/**
* Install script for course cleanup subplugin
*
* @package tool_cleanupcourses_trigger
* @subpackage delayedcourses
* @copyright 2017 Tobias Reischmann WWU
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use tool_cleanupcourses\manager\trigger_manager;
defined('MOODLE_INTERNAL') || die();
function xmldb_cleanupcoursestrigger_delayedcourses_install() {
trigger_manager::register('delayedcourses');
}
\ No newline at end of file
<?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/>.
/**
* Uninstall script for course cleanup subplugin
*
* @package tool_cleanupcourses_trigger
* @subpackage delayedcourses
* @copyright 2017 Tobias Reischmann WWU
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use tool_cleanupcourses\manager\trigger_manager;
defined('MOODLE_INTERNAL') || die();
function xmldb_cleanupcoursestrigger_delayedcourses_uninstall() {
trigger_manager::deregister('delayedcourses');
}
\ No newline at end of file
<?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/>.
/**
* Lang strings for site course trigger
*
* @package tool_cleanupcourses_trigger
* @subpackage delayedcourses
* @copyright 2017 Tobias Reischmann WWU
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['pluginname'] = 'Exclude delayed courses trigger';
$string['delay'] = 'This module will exclude all courses, which were delayed by user interaction';
<?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/>.
/**
* Interface for the subplugintype trigger
* It has to be implemented by all subplugins.
*
* @package tool_cleanupcourses
* @copyright 2017 Tobias Reischmann WWU
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_cleanupcourses\trigger;
use tool_cleanupcourses\response\trigger_response;
use tool_cleanupcourses\manager\delayed_courses_manager;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/../lib.php');
/**
* Class which implements the basic methods necessary for a cleanup courses trigger subplugin
* @package tool_cleanupcourses\trigger
*/
class delayedcourses implements base {
/**
* Checks the course and returns a repsonse, which tells if the course should be further processed.
* @param $course object to be processed.
* @return trigger_response
*/
public function check_course($course) {
$delayeduntil = delayed_courses_manager::get_course_delayed($course->id);
if ($delayeduntil !== null && time() < $delayeduntil) {
return trigger_response::exclude();
}
return trigger_response::next();
}
}
<?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_cleanupcourses\trigger;
use tool_cleanupcourses\response\trigger_response;
use tool_cleanupcourses\manager\delayed_courses_manager;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/../lib.php');
/**
* Trigger test for delayed courses trigger.
*
* @package tool_cleanupcourses_trigger
* @category delayedcourses
* @group tool_cleanupcourses_trigger
* @copyright 2017 Tobias Reischmann WWU
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_cleanupcourses_trigger_delayedcourses_testcase extends \advanced_testcase {
/**
* Tests that a course is not excluded by this plugin, when there exists no dalayed entry, yet.
*/
public function test_course_not_delayed() {
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$trigger = new delayedcourses();
$response = $trigger->check_course($course);
$this->assertEquals($response, trigger_response::next());
}
/**
* Tests that a course is excluded by this plugin, when there exists a dalayed entry.
*/
public function test_course_delayed() {
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
delayed_courses_manager::set_course_delayed($course->id, 2000);
$trigger = new delayedcourses();
$response = $trigger->check_course($course);
$this->assertEquals($response, trigger_response::exclude());
}
/**
* Tests that a course is not excluded by this plugin, when there exists a dalayed entry, but it is expired.
*/
public function test_course_delay_expired() {
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
delayed_courses_manager::set_course_delayed($course->id, -2000);
$trigger = new delayedcourses();
$response = $trigger->check_course($course);
$this->assertEquals($response, trigger_response::next());
}
}
\ No newline at end of file
<?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/>.
/**
* Cleanup Courses Site Course Trigger
*
* @package tool_cleanupcourses_trigger
* @subpackage delayedcourses
* @copyright 2017 Tobias Reischmann WWU
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
$plugin->version = 2017061900;
$plugin->component = 'cleanupcoursestrigger_delayedcourses';
\ No newline at end of file
......@@ -23,5 +23,5 @@
defined('MOODLE_INTERNAL') || die;
$plugin->version = 2017061601;
$plugin->version = 2017061901;
$plugin->component = 'tool_cleanupcourses';
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment