MDL-62660 tool_dataprivacy: Add ability to expire data requests
[moodle.git] / admin / tool / dataprivacy / classes / output / my_data_requests_page.php
blob729a7fe17104e09f9e7a712efd7269d5018b5a04
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 containing data for a user's data requests.
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\output;
25 defined('MOODLE_INTERNAL') || die();
27 use action_menu;
28 use action_menu_link_secondary;
29 use coding_exception;
30 use context_user;
31 use moodle_exception;
32 use moodle_url;
33 use renderable;
34 use renderer_base;
35 use stdClass;
36 use templatable;
37 use tool_dataprivacy\api;
38 use tool_dataprivacy\data_request;
39 use tool_dataprivacy\external\data_request_exporter;
41 /**
42 * Class containing data for a user's data requests.
44 * @copyright 2018 Jun Pataleta
45 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
47 class my_data_requests_page implements renderable, templatable {
49 /** @var array $requests List of data requests. */
50 protected $requests = [];
52 /**
53 * Construct this renderable.
55 * @param data_request[] $requests
57 public function __construct($requests) {
58 $this->requests = $requests;
61 /**
62 * Export this data so it can be used as the context for a mustache template.
64 * @param renderer_base $output
65 * @return stdClass
66 * @throws coding_exception
67 * @throws moodle_exception
69 public function export_for_template(renderer_base $output) {
70 global $USER;
72 $data = new stdClass();
73 $data->newdatarequesturl = new moodle_url('/admin/tool/dataprivacy/createdatarequest.php');
75 if (!is_https()) {
76 $httpwarningmessage = get_string('httpwarning', 'tool_dataprivacy');
77 $data->httpsite = array('message' => $httpwarningmessage, 'announce' => 1);
80 $requests = [];
81 foreach ($this->requests as $request) {
82 $requestid = $request->get('id');
83 $status = $request->get('status');
84 $userid = $request->get('userid');
85 $type = $request->get('type');
87 $usercontext = context_user::instance($userid, IGNORE_MISSING);
88 if (!$usercontext) {
89 // Use the context system.
90 $outputcontext = \context_system::instance();
91 } else {
92 $outputcontext = $usercontext;
95 $requestexporter = new data_request_exporter($request, ['context' => $outputcontext]);
96 $item = $requestexporter->export($output);
98 $self = $request->get('userid') == $USER->id;
99 if (!$self) {
100 // Append user name if it differs from $USER.
101 $a = (object)['typename' => $item->typename, 'user' => $item->foruser->fullname];
102 $item->typename = get_string('requesttypeuser', 'tool_dataprivacy', $a);
105 $candownload = false;
106 $cancancel = true;
107 switch ($status) {
108 case api::DATAREQUEST_STATUS_COMPLETE:
109 $item->statuslabelclass = 'label-success';
110 $item->statuslabel = get_string('statuscomplete', 'tool_dataprivacy');
111 $cancancel = false;
112 break;
113 case api::DATAREQUEST_STATUS_DOWNLOAD_READY:
114 $item->statuslabelclass = 'label-success';
115 $item->statuslabel = get_string('statusready', 'tool_dataprivacy');
116 $cancancel = false;
117 $candownload = true;
119 if ($usercontext) {
120 $candownload = api::can_download_data_request_for_user(
121 $request->get('userid'), $request->get('requestedby'));
123 break;
124 case api::DATAREQUEST_STATUS_DELETED:
125 $item->statuslabelclass = 'label-success';
126 $item->statuslabel = get_string('statusdeleted', 'tool_dataprivacy');
127 $cancancel = false;
128 break;
129 case api::DATAREQUEST_STATUS_EXPIRED:
130 $item->statuslabelclass = 'label-default';
131 $item->statuslabel = get_string('statusexpired', 'tool_dataprivacy');
132 $item->statuslabeltitle = get_string('downloadexpireduser', 'tool_dataprivacy');
133 $cancancel = false;
134 break;
135 case api::DATAREQUEST_STATUS_CANCELLED:
136 case api::DATAREQUEST_STATUS_REJECTED:
137 $cancancel = false;
138 break;
141 // Prepare actions.
142 $actions = [];
143 if ($cancancel) {
144 $cancelurl = new moodle_url('#');
145 $canceldata = ['data-action' => 'cancel', 'data-requestid' => $requestid];
146 $canceltext = get_string('cancelrequest', 'tool_dataprivacy');
147 $actions[] = new action_menu_link_secondary($cancelurl, null, $canceltext, $canceldata);
149 if ($candownload && $usercontext) {
150 $actions[] = api::get_download_link($usercontext, $requestid);
152 if (!empty($actions)) {
153 $actionsmenu = new action_menu($actions);
154 $actionsmenu->set_menu_trigger(get_string('actions'));
155 $actionsmenu->set_owner_selector('request-actions-' . $requestid);
156 $actionsmenu->set_alignment(\action_menu::TL, \action_menu::BL);
157 $item->actions = $actionsmenu->export_for_template($output);
160 $requests[] = $item;
162 $data->requests = $requests;
163 return $data;