mirrored from git://git.moodle.org/moodle.git
/
expired_contexts_manager.php
156 lines (128 loc) · 4.35 KB
/
expired_contexts_manager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?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/>.
/**
* Expired contexts manager.
*
* @package tool_dataprivacy
* @copyright 2018 David Monllao
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_dataprivacy;
use tool_dataprivacy\api;
use tool_dataprivacy\purpose;
use tool_dataprivacy\context_instance;
use tool_dataprivacy\data_registry;
use tool_dataprivacy\expired_context;
defined('MOODLE_INTERNAL') || die();
/**
* Expired contexts manager.
*
* @copyright 2018 David Monllao
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class expired_contexts_manager {
/**
* Number of deleted contexts for each scheduled task run.
*/
const DELETE_LIMIT = 200;
/**
* Returns the list of expired context instances.
*
* @return \stdClass[]
*/
abstract protected function get_expired_contexts();
/**
* Specify with context levels this expired contexts manager is deleting.
*
* @return int[]
*/
abstract protected function get_context_levels();
/**
* Flag expired contexts as expired.
*
* @return int The number of contexts flagged as expired.
*/
public function flag_expired() {
if (!$this->check_requirements()) {
return 0;
}
$contexts = $this->get_expired_contexts();
foreach ($contexts as $context) {
api::create_expired_context($context->id);
}
return count($contexts);
}
/**
* Deletes the expired contexts.
*
* @return int The number of deleted contexts.
*/
public function delete() {
$numprocessed = 0;
if (!$this->check_requirements()) {
return $numprocessed;
}
$privacymanager = new manager();
foreach ($this->get_context_levels() as $level) {
$expiredcontexts = expired_context::get_records_by_contextlevel($level, expired_context::STATUS_APPROVED);
foreach ($expiredcontexts as $expiredctx) {
if (!$this->delete_expired_context($privacymanager, $expiredctx)) {
continue;
}
$numprocessed += 1;
if ($numprocessed == self::DELETE_LIMIT) {
// Close the recordset.
$expiredcontexts->close();
break 2;
}
}
}
return $numprocessed;
}
/**
* Deletes user data from the provided context.
*
* @param \core_privacy\manager $privacymanager
* @param \tool_dataprivacy\expired_context $expiredctx
* @return \context|false
*/
protected function delete_expired_context(\core_privacy\manager $privacymanager, \tool_dataprivacy\expired_context $expiredctx) {
$context = \context::instance_by_id($expiredctx->get('contextid'), IGNORE_MISSING);
if (!$context) {
api::delete_expired_context($expiredctx->get('contextid'));
return false;
}
if (!PHPUNIT_TEST) {
mtrace('Deleting context ' . $context->id . ' - ' .
shorten_text($context->get_context_name(true, true)));
}
$privacymanager->delete_data_for_all_users_in_context($context);
api::set_expired_context_status($expiredctx, expired_context::STATUS_CLEANED);
return $context;
}
/**
* Check that the requirements to start deleting contexts are satisified.
*
* @return bool
*/
protected function check_requirements() {
api::check_can_manage_data_registry(\context_system::instance()->id);
if (!data_registry::defaults_set()) {
return false;
}
return true;
}
}