2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Expired contexts manager.
20 * @package tool_dataprivacy
21 * @copyright 2018 David Monllao
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 namespace tool_dataprivacy
;
26 use tool_dataprivacy\api
;
27 use tool_dataprivacy\purpose
;
28 use tool_dataprivacy\context_instance
;
29 use tool_dataprivacy\data_registry
;
30 use tool_dataprivacy\expired_context
;
32 defined('MOODLE_INTERNAL') ||
die();
35 * Expired contexts manager.
37 * @copyright 2018 David Monllao
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 abstract class expired_contexts_manager
{
43 * Number of deleted contexts for each scheduled task run.
45 const DELETE_LIMIT
= 200;
48 * Returns the list of expired context instances.
52 abstract protected function get_expired_contexts();
55 * Flag expired contexts as expired.
57 * @return int The number of contexts flagged as expired.
59 public function flag_expired() {
61 if (!$this->check_requirements()) {
65 $contexts = $this->get_expired_contexts();
66 foreach ($contexts as $context) {
67 api
::create_expired_context($context->id
);
70 return count($contexts);
74 * Deletes the expired contexts.
76 * @return int The number of deleted contexts.
78 public function delete() {
82 if (!$this->check_requirements()) {
86 $privacymanager = new \core_privacy\
manager();
88 $levels = [CONTEXT_USER
, CONTEXT_MODULE
, CONTEXT_BLOCK
, CONTEXT_COURSE
];
89 foreach ($levels as $level) {
91 $expiredcontexts = expired_context
::get_records_by_contextlevel($level, expired_context
::STATUS_APPROVED
);
93 foreach ($expiredcontexts as $expiredctx) {
95 $context = \context
::instance_by_id($expiredctx->get('contextid'), IGNORE_MISSING
);
97 api
::delete_expired_context($expiredctx->get('contextid'));
102 mtrace('Deleting context ' . $context->id
. ' - ' .
103 shorten_text($context->get_context_name(true, true)));
106 $privacymanager->delete_data_for_all_users_in_context($context);
108 api
::set_expired_context_status($expiredctx, expired_context
::STATUS_CLEANED
);
111 if ($numprocessed == self
::DELETE_LIMIT
) {
112 // Close the recordset.
113 $expiredcontexts->close();
119 return $numprocessed;
123 * Check that the requirements to start deleting contexts are satisified.
127 protected function check_requirements() {
128 api
::check_can_manage_data_registry(\context_system
::instance()->id
);
130 if (!data_registry
::defaults_set()) {