MDL-62660 tool_dataprivacy: Add ability to expire data requests
[moodle.git] / admin / tool / dataprivacy / classes / data_request.php
blobc41db11e83988602bfe9570cca984d0bf8a1d303
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * Class for loading/storing data requests from the DB.
20 * @package tool_dataprivacy
21 * @copyright 2018 Jun Pataleta
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 namespace tool_dataprivacy;
25 defined('MOODLE_INTERNAL') || die();
27 use core\persistent;
29 /**
30 * Class for loading/storing competencies from the DB.
32 * @copyright 2018 Jun Pataleta
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class data_request extends persistent {
37 /** The table name this persistent object maps to. */
38 const TABLE = 'tool_dataprivacy_request';
40 /**
41 * Return the definition of the properties of this model.
43 * @return array
45 protected static function define_properties() {
46 return [
47 'type' => [
48 'choices' => [
49 api::DATAREQUEST_TYPE_EXPORT,
50 api::DATAREQUEST_TYPE_DELETE,
51 api::DATAREQUEST_TYPE_OTHERS,
53 'type' => PARAM_INT
55 'comments' => [
56 'type' => PARAM_TEXT,
57 'default' => ''
59 'commentsformat' => [
60 'choices' => [
61 FORMAT_HTML,
62 FORMAT_MOODLE,
63 FORMAT_PLAIN,
64 FORMAT_MARKDOWN
66 'type' => PARAM_INT,
67 'default' => FORMAT_PLAIN
69 'userid' => [
70 'default' => 0,
71 'type' => PARAM_INT
73 'requestedby' => [
74 'default' => 0,
75 'type' => PARAM_INT
77 'status' => [
78 'default' => api::DATAREQUEST_STATUS_PENDING,
79 'choices' => [
80 api::DATAREQUEST_STATUS_PENDING,
81 api::DATAREQUEST_STATUS_PREPROCESSING,
82 api::DATAREQUEST_STATUS_AWAITING_APPROVAL,
83 api::DATAREQUEST_STATUS_APPROVED,
84 api::DATAREQUEST_STATUS_PROCESSING,
85 api::DATAREQUEST_STATUS_COMPLETE,
86 api::DATAREQUEST_STATUS_CANCELLED,
87 api::DATAREQUEST_STATUS_REJECTED,
88 api::DATAREQUEST_STATUS_DOWNLOAD_READY,
89 api::DATAREQUEST_STATUS_EXPIRED,
90 api::DATAREQUEST_STATUS_DELETED,
92 'type' => PARAM_INT
94 'dpo' => [
95 'default' => 0,
96 'type' => PARAM_INT,
97 'null' => NULL_ALLOWED
99 'dpocomment' => [
100 'default' => '',
101 'type' => PARAM_TEXT,
102 'null' => NULL_ALLOWED
104 'dpocommentformat' => [
105 'choices' => [
106 FORMAT_HTML,
107 FORMAT_MOODLE,
108 FORMAT_PLAIN,
109 FORMAT_MARKDOWN
111 'type' => PARAM_INT,
112 'default' => FORMAT_PLAIN
118 * Determines whether a completed data export request has expired.
119 * The response will be valid regardless of the expiry scheduled task having run.
121 * @param data_request $request the data request object whose expiry will be checked.
122 * @return bool true if the request has expired.
124 public static function is_expired(data_request $request) {
125 $result = false;
127 // Only export requests expire.
128 if ($request->get('type') == api::DATAREQUEST_TYPE_EXPORT) {
129 switch ($request->get('status')) {
130 // Expired requests are obviously expired.
131 case api::DATAREQUEST_STATUS_EXPIRED:
132 $result = true;
133 break;
134 // Complete requests are expired if the expiry time has elapsed.
135 case api::DATAREQUEST_STATUS_DOWNLOAD_READY:
136 $expiryseconds = get_config('tool_dataprivacy', 'privacyrequestexpiry');
137 if ($expiryseconds > 0 && time() >= ($request->get('timemodified') + $expiryseconds)) {
138 $result = true;
140 break;
144 return $result;
150 * Fetch completed data requests which are due to expire.
152 * @param int $userid Optional user ID to filter by.
154 * @return array Details of completed requests which are due to expire.
156 public static function get_expired_requests($userid = 0) {
157 global $DB;
159 $expiryseconds = get_config('tool_dataprivacy', 'privacyrequestexpiry');
160 $expirytime = strtotime("-{$expiryseconds} second");
161 $table = data_request::TABLE;
162 $sqlwhere = 'type = :export_type AND status = :completestatus AND timemodified <= :expirytime';
163 $params = array(
164 'export_type' => api::DATAREQUEST_TYPE_EXPORT,
165 'completestatus' => api::DATAREQUEST_STATUS_DOWNLOAD_READY,
166 'expirytime' => $expirytime,
168 $sort = 'id';
169 $fields = 'id, userid';
171 // Filter by user ID if specified.
172 if ($userid > 0) {
173 $sqlwhere .= ' AND (userid = :userid OR requestedby = :requestedby)';
174 $params['userid'] = $userid;
175 $params['requestedby'] = $userid;
178 return $DB->get_records_select_menu($table, $sqlwhere, $params, $sort, $fields, 0, 2000);
182 * Expire a given set of data requests.
183 * Update request status and delete the files.
185 * @param array $expiredrequests [requestid => userid]
187 * @return void
189 public static function expire($expiredrequests) {
190 global $DB;
192 $ids = array_keys($expiredrequests);
194 if (count($ids) > 0) {
195 list($insql, $inparams) = $DB->get_in_or_equal($ids);
196 $initialparams = array(api::DATAREQUEST_STATUS_EXPIRED, time());
197 $params = array_merge($initialparams, $inparams);
199 $update = "UPDATE {" . data_request::TABLE . "}
200 SET status = ?, timemodified = ?
201 WHERE id $insql";
203 if ($DB->execute($update, $params)) {
204 $fs = get_file_storage();
206 foreach ($expiredrequests as $id => $userid) {
207 $usercontext = \context_user::instance($userid);
208 $fs->delete_area_files($usercontext->id, 'tool_dataprivacy', 'export', $id);