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 * 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();
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';
41 * Return the definition of the properties of this model.
45 protected static function define_properties() {
49 api
::DATAREQUEST_TYPE_EXPORT
,
50 api
::DATAREQUEST_TYPE_DELETE
,
51 api
::DATAREQUEST_TYPE_OTHERS
,
67 'default' => FORMAT_PLAIN
78 'default' => api
::DATAREQUEST_STATUS_PENDING
,
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
,
97 'null' => NULL_ALLOWED
101 'type' => PARAM_TEXT
,
102 'null' => NULL_ALLOWED
104 'dpocommentformat' => [
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) {
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
:
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)) {
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) {
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';
164 'export_type' => api
::DATAREQUEST_TYPE_EXPORT
,
165 'completestatus' => api
::DATAREQUEST_STATUS_DOWNLOAD_READY
,
166 'expirytime' => $expirytime,
169 $fields = 'id, userid';
171 // Filter by user ID if specified.
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]
189 public static function expire($expiredrequests) {
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 = ?
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);