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 * Specify with context levels this expired contexts manager is deleting.
59 abstract protected function get_context_levels();
62 * Flag expired contexts as expired.
64 * @return int The number of contexts flagged as expired.
66 public function flag_expired() {
68 if (!$this->check_requirements()) {
72 $contexts = $this->get_expired_contexts();
73 foreach ($contexts as $context) {
74 api
::create_expired_context($context->id
);
77 return count($contexts);
81 * Deletes the expired contexts.
83 * @return int The number of deleted contexts.
85 public function delete() {
89 if (!$this->check_requirements()) {
93 $privacymanager = new \core_privacy\
manager();
94 $privacymanager->set_observer(new \tool_dataprivacy\
manager_observer());
96 foreach ($this->get_context_levels() as $level) {
98 $expiredcontexts = expired_context
::get_records_by_contextlevel($level, expired_context
::STATUS_APPROVED
);
100 foreach ($expiredcontexts as $expiredctx) {
102 if (!$this->delete_expired_context($privacymanager, $expiredctx)) {
107 if ($numprocessed == self
::DELETE_LIMIT
) {
108 // Close the recordset.
109 $expiredcontexts->close();
115 return $numprocessed;
119 * Deletes user data from the provided context.
121 * @param \core_privacy\manager $privacymanager
122 * @param \tool_dataprivacy\expired_context $expiredctx
123 * @return \context|false
125 protected function delete_expired_context(\core_privacy\manager
$privacymanager, \tool_dataprivacy\expired_context
$expiredctx) {
127 $context = \context
::instance_by_id($expiredctx->get('contextid'), IGNORE_MISSING
);
129 api
::delete_expired_context($expiredctx->get('contextid'));
134 mtrace('Deleting context ' . $context->id
. ' - ' .
135 shorten_text($context->get_context_name(true, true)));
138 $privacymanager->delete_data_for_all_users_in_context($context);
139 api
::set_expired_context_status($expiredctx, expired_context
::STATUS_CLEANED
);
145 * Check that the requirements to start deleting contexts are satisified.
149 protected function check_requirements() {
150 api
::check_can_manage_data_registry(\context_system
::instance()->id
);
152 if (!data_registry
::defaults_set()) {