Skip to content

Commit

Permalink
Separate out the two cron tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
sk-unikent committed Nov 3, 2015
1 parent 9c3ef37 commit deb545c
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 30 deletions.
65 changes: 65 additions & 0 deletions classes/task/cleanup_activities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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/>.

/**
* Recycle bin cron task.
*
* @package local_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace local_recyclebin\task;

/**
* This task deletes expired course recyclebin items.
*
* @package local_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cleanup_activities extends \core\task\scheduled_task {
/**
* Task name.
*/
public function get_name() {
return get_string('cleancourserecyclebin', 'local_recyclebin');
}

/**
* Delete all expired items.
*/
public function execute() {
global $DB;

// Delete mods.
$lifetime = get_config('local_recyclebin', 'expiry');
if ($lifetime <= 0) {
return true;
}

$items = $DB->get_recordset_select('local_recyclebin_course', 'deleted < ?', array(time() - (86400 * $lifetime)));
foreach ($items as $item) {
mtrace("[RecycleBin] Deleting item {$item->id}...");

$bin = new \local_recyclebin\course($item->course);
$bin->delete_item($item);
}
$items->close();

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@
namespace local_recyclebin\task;

/**
* This task deleted expired recyclebin items.
* This task deletes expired category recyclebin items.
*
* @package local_recyclebin
* @copyright 2015 University of Kent
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class clean_recyclebin extends \core\task\scheduled_task {
class cleanup_courses extends \core\task\scheduled_task {
/**
* Task name.
*/
public function get_name() {
return get_string('cleanrecyclebin', 'local_recyclebin');
return get_string('cleancategoryrecyclebin', 'local_recyclebin');
}

/**
Expand All @@ -45,35 +45,20 @@ public function get_name() {
public function execute() {
global $DB;

// Delete mods.
$lifetime = get_config('local_recyclebin', 'expiry');
if ($lifetime > 0) {
$deletefrom = time() - (86400 * $lifetime);

$items = $DB->get_recordset_select('local_recyclebin_course', 'deleted < ?', array($deletefrom));
foreach ($items as $item) {
mtrace("[RecycleBin] Deleting item {$item->id}...");

$bin = new \local_recyclebin\course($item->course);
$bin->delete_item($item);
}
$items->close();
}

// Delete courses.
$lifetime = get_config('local_recyclebin', 'course_expiry');
if ($lifetime > 0) {
$deletefrom = time() - (86400 * $lifetime);
if ($lifetime <= 0) {
return true;
}

$items = $DB->get_recordset_select('local_recyclebin_category', 'deleted < ?', array($deletefrom));
foreach ($items as $item) {
mtrace("[RecycleBin] Deleting course {$item->id}...");
$items = $DB->get_recordset_select('local_recyclebin_category', 'deleted < ?', array(time() - (86400 * $lifetime)));
foreach ($items as $item) {
mtrace("[RecycleBin] Deleting course {$item->id}...");

$bin = new \local_recyclebin\category($item->category);
$bin->delete_item($item);
}
$items->close();
$bin = new \local_recyclebin\category($item->category);
$bin->delete_item($item);
}
$items->close();

return true;
}
Expand Down
11 changes: 10 additions & 1 deletion db/tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@

$tasks = array(
array(
'classname' => 'local_recyclebin\task\clean_recyclebin',
'classname' => 'local_recyclebin\task\cleanup_activities',
'blocking' => 0,
'minute' => '*/30',
'hour' => '*',
'day' => '*',
'dayofweek' => '*',
'month' => '*'
),
array(
'classname' => 'local_recyclebin\task\cleanup_courses',
'blocking' => 0,
'minute' => '*/30',
'hour' => '*',
Expand Down
4 changes: 3 additions & 1 deletion lang/en/local_recyclebin.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
defined('MOODLE_INTERNAL') || die();

$string['pluginname'] = 'Recycle bin';
$string['cleanrecyclebin'] = 'Clean recycle bin';

$string['cleancourserecyclebin'] = 'Clean course recycle bins';
$string['cleancategoryrecyclebin'] = 'Clean category recycle bins';

$string['expiry'] = 'Mod lifetime';
$string['expiry_desc'] = 'How long should a deleted mod remain in the recycle bin?';
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2015082800;
$plugin->version = 2015110300;
$plugin->component = 'local_recyclebin';
$plugin->requires = 2014051200;
$plugin->maturity = MATURITY_STABLE;
Expand Down

0 comments on commit deb545c

Please sign in to comment.