MDL-64506 templates: Move BS2 labels to BS4 badges
[moodle.git] / admin / tool / dataprivacy / classes / output / my_data_requests_page.php
CommitLineData
5efc1f9e
DM
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.
13//
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/>.
16
17/**
18 * Class containing data for a user's data requests.
19 *
20 * @package tool_dataprivacy
21 * @copyright 2018 Jun Pataleta
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24namespace tool_dataprivacy\output;
25defined('MOODLE_INTERNAL') || die();
26
27use action_menu;
28use action_menu_link_secondary;
29use coding_exception;
30use context_user;
31use moodle_exception;
32use moodle_url;
33use renderable;
34use renderer_base;
35use stdClass;
36use templatable;
37use tool_dataprivacy\api;
38use tool_dataprivacy\data_request;
39use tool_dataprivacy\external\data_request_exporter;
40
41/**
42 * Class containing data for a user's data requests.
43 *
44 * @copyright 2018 Jun Pataleta
45 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
46 */
47class my_data_requests_page implements renderable, templatable {
48
49 /** @var array $requests List of data requests. */
50 protected $requests = [];
51
52 /**
53 * Construct this renderable.
54 *
55 * @param data_request[] $requests
56 */
57 public function __construct($requests) {
58 $this->requests = $requests;
59 }
60
61 /**
62 * Export this data so it can be used as the context for a mustache template.
63 *
64 * @param renderer_base $output
65 * @return stdClass
66 * @throws coding_exception
67 * @throws moodle_exception
68 */
69 public function export_for_template(renderer_base $output) {
a319808c
DM
70 global $USER;
71
5efc1f9e
DM
72 $data = new stdClass();
73 $data->newdatarequesturl = new moodle_url('/admin/tool/dataprivacy/createdatarequest.php');
74
be5cc0ba
MH
75 if (!is_https()) {
76 $httpwarningmessage = get_string('httpwarning', 'tool_dataprivacy');
77 $data->httpsite = array('message' => $httpwarningmessage, 'announce' => 1);
78 }
79
5efc1f9e
DM
80 $requests = [];
81 foreach ($this->requests as $request) {
82 $requestid = $request->get('id');
83 $status = $request->get('status');
84 $userid = $request->get('userid');
ef7f7cfd 85 $type = $request->get('type');
a319808c
DM
86
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;
93 }
94
95 $requestexporter = new data_request_exporter($request, ['context' => $outputcontext]);
5efc1f9e
DM
96 $item = $requestexporter->export($output);
97
635c7b29 98 $self = $request->get('userid') == $USER->id;
99 if (!$self) {
a319808c
DM
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);
103 }
104
5efc1f9e
DM
105 $candownload = false;
106 $cancancel = true;
107 switch ($status) {
108 case api::DATAREQUEST_STATUS_COMPLETE:
3f2c68f1 109 $item->statuslabelclass = 'badge-success';
5efc1f9e
DM
110 $item->statuslabel = get_string('statuscomplete', 'tool_dataprivacy');
111 $cancancel = false;
693f690c
MH
112 break;
113 case api::DATAREQUEST_STATUS_DOWNLOAD_READY:
3f2c68f1 114 $item->statuslabelclass = 'badge-success';
693f690c
MH
115 $item->statuslabel = get_string('statusready', 'tool_dataprivacy');
116 $cancancel = false;
117 $candownload = true;
118
635c7b29 119 if ($usercontext) {
120 $candownload = api::can_download_data_request_for_user(
121 $request->get('userid'), $request->get('requestedby'));
122 }
5efc1f9e 123 break;
693f690c 124 case api::DATAREQUEST_STATUS_DELETED:
3f2c68f1 125 $item->statuslabelclass = 'badge-success';
693f690c
MH
126 $item->statuslabel = get_string('statusdeleted', 'tool_dataprivacy');
127 $cancancel = false;
128 break;
129 case api::DATAREQUEST_STATUS_EXPIRED:
3f2c68f1 130 $item->statuslabelclass = 'badge-secondary';
693f690c
MH
131 $item->statuslabel = get_string('statusexpired', 'tool_dataprivacy');
132 $item->statuslabeltitle = get_string('downloadexpireduser', 'tool_dataprivacy');
133 $cancancel = false;
134 break;
5efc1f9e
DM
135 case api::DATAREQUEST_STATUS_CANCELLED:
136 case api::DATAREQUEST_STATUS_REJECTED:
137 $cancancel = false;
138 break;
139 }
140
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);
148 }
a319808c 149 if ($candownload && $usercontext) {
635c7b29 150 $actions[] = api::get_download_link($usercontext, $requestid);
5efc1f9e
DM
151 }
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);
158 }
159
160 $requests[] = $item;
161 }
162 $data->requests = $requests;
163 return $data;
164 }
165}