Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]

   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   * Contains the class used for the displaying the data requests table.
  19   *
  20   * @package    tool_dataprivacy
  21   * @copyright  2018 Jun Pataleta <jun@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace tool_dataprivacy\output;
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once($CFG->libdir . '/tablelib.php');
  28  
  29  use action_menu;
  30  use action_menu_link_secondary;
  31  use coding_exception;
  32  use dml_exception;
  33  use html_writer;
  34  use moodle_url;
  35  use stdClass;
  36  use table_sql;
  37  use tool_dataprivacy\api;
  38  use tool_dataprivacy\external\data_request_exporter;
  39  
  40  defined('MOODLE_INTERNAL') || die;
  41  
  42  /**
  43   * The class for displaying the data requests table.
  44   *
  45   * @copyright  2018 Jun Pataleta <jun@moodle.com>
  46   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  47   */
  48  class data_requests_table extends table_sql {
  49  
  50      /** @var int The user ID. */
  51      protected $userid = 0;
  52  
  53      /** @var int[] The status filters. */
  54      protected $statuses = [];
  55  
  56      /** @var int[] The request type filters.  */
  57      protected $types = [];
  58  
  59      /** @var bool Whether this table is being rendered for managing data requests. */
  60      protected $manage = false;
  61  
  62      /** @var \tool_dataprivacy\data_request[] Array of data request persistents. */
  63      protected $datarequests = [];
  64  
  65      /** @var \stdClass[] List of userids and whether they have any ongoing active requests. */
  66      protected $ongoingrequests = [];
  67  
  68      /** @var int The number of data request to be displayed per page. */
  69      protected $perpage;
  70  
  71      /** @var int[] The available options for the number of data request to be displayed per page. */
  72      protected $perpageoptions = [25, 50, 100, 250];
  73  
  74      /**
  75       * data_requests_table constructor.
  76       *
  77       * @param int $userid The user ID
  78       * @param int[] $statuses
  79       * @param int[] $types
  80       * @param int[] $creationmethods
  81       * @param bool $manage
  82       * @throws coding_exception
  83       */
  84      public function __construct($userid = 0, $statuses = [], $types = [], $creationmethods = [], $manage = false) {
  85          parent::__construct('data-requests-table');
  86  
  87          $this->userid = $userid;
  88          $this->statuses = $statuses;
  89          $this->types = $types;
  90          $this->creationmethods = $creationmethods;
  91          $this->manage = $manage;
  92  
  93          $checkboxattrs = [
  94              'title' => get_string('selectall'),
  95              'data-action' => 'selectall'
  96          ];
  97  
  98          $columnheaders = [
  99              'select' => html_writer::checkbox('selectall', 1, false, null, $checkboxattrs),
 100              'type' => get_string('requesttype', 'tool_dataprivacy'),
 101              'userid' => get_string('user', 'tool_dataprivacy'),
 102              'timecreated' => get_string('daterequested', 'tool_dataprivacy'),
 103              'requestedby' => get_string('requestby', 'tool_dataprivacy'),
 104              'status' => get_string('requeststatus', 'tool_dataprivacy'),
 105              'comments' => get_string('message', 'tool_dataprivacy'),
 106              'actions' => '',
 107          ];
 108  
 109          $this->define_columns(array_keys($columnheaders));
 110          $this->define_headers(array_values($columnheaders));
 111          $this->no_sorting('select', 'actions');
 112      }
 113  
 114      /**
 115       * The select column.
 116       *
 117       * @param stdClass $data The row data.
 118       * @return string
 119       * @throws \moodle_exception
 120       * @throws coding_exception
 121       */
 122      public function col_select($data) {
 123          if ($data->status == \tool_dataprivacy\api::DATAREQUEST_STATUS_AWAITING_APPROVAL) {
 124              if ($data->type == \tool_dataprivacy\api::DATAREQUEST_TYPE_DELETE
 125                  && !api::can_create_data_deletion_request_for_other()) {
 126                  // Don't show checkbox if request's type is delete and user don't have permission.
 127                  return false;
 128              }
 129  
 130              $stringdata = [
 131                  'username' => $data->foruser->fullname,
 132                  'requesttype' => \core_text::strtolower($data->typenameshort)
 133              ];
 134  
 135              return \html_writer::checkbox('requestids[]', $data->id, false, '',
 136                      ['class' => 'selectrequests', 'title' => get_string('selectuserdatarequest',
 137                      'tool_dataprivacy', $stringdata)]);
 138          }
 139      }
 140  
 141      /**
 142       * The type column.
 143       *
 144       * @param stdClass $data The row data.
 145       * @return string
 146       */
 147      public function col_type($data) {
 148          if ($this->manage) {
 149              return $data->typenameshort;
 150          }
 151          return $data->typename;
 152      }
 153  
 154      /**
 155       * The user column.
 156       *
 157       * @param stdClass $data The row data.
 158       * @return mixed
 159       */
 160      public function col_userid($data) {
 161          $user = $data->foruser;
 162          return html_writer::link($user->profileurl, $user->fullname, ['title' => get_string('viewprofile')]);
 163      }
 164  
 165      /**
 166       * The context information column.
 167       *
 168       * @param stdClass $data The row data.
 169       * @return string
 170       */
 171      public function col_timecreated($data) {
 172          return userdate($data->timecreated);
 173      }
 174  
 175      /**
 176       * The requesting user's column.
 177       *
 178       * @param stdClass $data The row data.
 179       * @return mixed
 180       */
 181      public function col_requestedby($data) {
 182          $user = $data->requestedbyuser;
 183          return html_writer::link($user->profileurl, $user->fullname, ['title' => get_string('viewprofile')]);
 184      }
 185  
 186      /**
 187       * The status column.
 188       *
 189       * @param stdClass $data The row data.
 190       * @return mixed
 191       */
 192      public function col_status($data) {
 193          return html_writer::span($data->statuslabel, 'badge ' . $data->statuslabelclass);
 194      }
 195  
 196      /**
 197       * The comments column.
 198       *
 199       * @param stdClass $data The row data.
 200       * @return string
 201       */
 202      public function col_comments($data) {
 203          return shorten_text($data->comments, 60);
 204      }
 205  
 206      /**
 207       * The actions column.
 208       *
 209       * @param stdClass $data The row data.
 210       * @return string
 211       */
 212      public function col_actions($data) {
 213          global $OUTPUT;
 214  
 215          $requestid = $data->id;
 216          $status = $data->status;
 217          $persistent = $this->datarequests[$requestid];
 218  
 219          // Prepare actions.
 220          $actions = [];
 221  
 222          // View action.
 223          $actionurl = new moodle_url('#');
 224          $actiondata = ['data-action' => 'view', 'data-requestid' => $requestid];
 225          $actiontext = get_string('viewrequest', 'tool_dataprivacy');
 226          $actions[] = new action_menu_link_secondary($actionurl, null, $actiontext, $actiondata);
 227  
 228          switch ($status) {
 229              case api::DATAREQUEST_STATUS_PENDING:
 230                  // Add action to mark a general enquiry request as complete.
 231                  if ($data->type == api::DATAREQUEST_TYPE_OTHERS) {
 232                      $actiondata['data-action'] = 'complete';
 233                      $nameemail = (object)[
 234                          'name' => $data->foruser->fullname,
 235                          'email' => $data->foruser->email
 236                      ];
 237                      $actiondata['data-requestid'] = $data->id;
 238                      $actiondata['data-replytoemail'] = get_string('nameemail', 'tool_dataprivacy', $nameemail);
 239                      $actiontext = get_string('markcomplete', 'tool_dataprivacy');
 240                      $actions[] = new action_menu_link_secondary($actionurl, null, $actiontext, $actiondata);
 241                  }
 242                  break;
 243              case api::DATAREQUEST_STATUS_AWAITING_APPROVAL:
 244                  // Only show "Approve" and "Deny" button for deletion request if current user has permission.
 245                  if ($persistent->get('type') == api::DATAREQUEST_TYPE_DELETE &&
 246                      !api::can_create_data_deletion_request_for_other()) {
 247                      break;
 248                  }
 249                  // Approve.
 250                  $actiondata['data-action'] = 'approve';
 251                  $actiontext = get_string('approverequest', 'tool_dataprivacy');
 252                  $actions[] = new action_menu_link_secondary($actionurl, null, $actiontext, $actiondata);
 253  
 254                  // Deny.
 255                  $actiondata['data-action'] = 'deny';
 256                  $actiontext = get_string('denyrequest', 'tool_dataprivacy');
 257                  $actions[] = new action_menu_link_secondary($actionurl, null, $actiontext, $actiondata);
 258                  break;
 259              case api::DATAREQUEST_STATUS_DOWNLOAD_READY:
 260                  $userid = $data->foruser->id;
 261                  $usercontext = \context_user::instance($userid, IGNORE_MISSING);
 262                  // If user has permission to view download link, show relevant action item.
 263                  if ($usercontext && api::can_download_data_request_for_user($userid, $data->requestedbyuser->id)) {
 264                      $actions[] = api::get_download_link($usercontext, $requestid);
 265                  }
 266                  break;
 267          }
 268  
 269          if ($this->manage) {
 270              $canreset = $persistent->is_active() || empty($this->ongoingrequests[$data->foruser->id]->{$data->type});
 271              $canreset = $canreset && $persistent->is_resettable();
 272              // Prevent re-submmit deletion request if current user don't have permission.
 273              $canreset = $canreset && ($persistent->get('type') != api::DATAREQUEST_TYPE_DELETE ||
 274                      api::can_create_data_deletion_request_for_other());
 275              if ($canreset) {
 276                  $reseturl = new moodle_url('/admin/tool/dataprivacy/resubmitrequest.php', [
 277                          'requestid' => $requestid,
 278                      ]);
 279                  $actiondata = ['data-action' => 'reset', 'data-requestid' => $requestid];
 280                  $actiontext = get_string('resubmitrequestasnew', 'tool_dataprivacy');
 281                  $actions[] = new action_menu_link_secondary($reseturl, null, $actiontext, $actiondata);
 282              }
 283          }
 284  
 285          $actionsmenu = new action_menu($actions);
 286          $actionsmenu->set_menu_trigger(get_string('actions'));
 287          $actionsmenu->set_owner_selector('request-actions-' . $requestid);
 288          $actionsmenu->set_boundary('window');
 289  
 290          return $OUTPUT->render($actionsmenu);
 291      }
 292  
 293      /**
 294       * Query the database for results to display in the table.
 295       *
 296       * @param int $pagesize size of page for paginated displayed table.
 297       * @param bool $useinitialsbar do you want to use the initials bar.
 298       * @throws dml_exception
 299       * @throws coding_exception
 300       */
 301      public function query_db($pagesize, $useinitialsbar = true) {
 302          global $PAGE;
 303  
 304          // Set dummy page total until we fetch full result set.
 305          $this->pagesize($pagesize, $pagesize + 1);
 306  
 307          $sort = $this->get_sql_sort();
 308  
 309          // Get data requests from the given conditions.
 310          $datarequests = api::get_data_requests($this->userid, $this->statuses, $this->types,
 311                  $this->creationmethods, $sort, $this->get_page_start(), $this->get_page_size());
 312  
 313          // Count data requests from the given conditions.
 314          $total = api::get_data_requests_count($this->userid, $this->statuses, $this->types,
 315                  $this->creationmethods);
 316          $this->pagesize($pagesize, $total);
 317  
 318          $this->rawdata = [];
 319          $context = \context_system::instance();
 320          $renderer = $PAGE->get_renderer('tool_dataprivacy');
 321  
 322          $forusers = [];
 323          foreach ($datarequests as $persistent) {
 324              $this->datarequests[$persistent->get('id')] = $persistent;
 325              $exporter = new data_request_exporter($persistent, ['context' => $context]);
 326              $this->rawdata[] = $exporter->export($renderer);
 327              $forusers[] = $persistent->get('userid');
 328          }
 329  
 330          // Fetch the list of all ongoing requests for the users currently shown.
 331          // This is used to determine whether any non-active request can be resubmitted.
 332          // There can only be one ongoing request of a type for each user.
 333          $this->ongoingrequests = api::find_ongoing_request_types_for_users($forusers);
 334  
 335          // Set initial bars.
 336          if ($useinitialsbar) {
 337              $this->initialbars($total > $pagesize);
 338          }
 339      }
 340  
 341      /**
 342       * Override default implementation to display a more meaningful information to the user.
 343       */
 344      public function print_nothing_to_display() {
 345          global $OUTPUT;
 346          echo $this->render_reset_button();
 347          $this->print_initials_bar();
 348          if (!empty($this->statuses) || !empty($this->types)) {
 349              $message = get_string('nodatarequestsmatchingfilter', 'tool_dataprivacy');
 350          } else {
 351              $message = get_string('nodatarequests', 'tool_dataprivacy');
 352          }
 353          echo $OUTPUT->notification($message, 'warning');
 354      }
 355  
 356      /**
 357       * Override the table's show_hide_link method to prevent the show/hide links from rendering.
 358       *
 359       * @param string $column the column name, index into various names.
 360       * @param int $index numerical index of the column.
 361       * @return string HTML fragment.
 362       */
 363      protected function show_hide_link($column, $index) {
 364          return '';
 365      }
 366  
 367      /**
 368       * Override the table's wrap_html_finish method in order to render the bulk actions and
 369       * records per page options.
 370       */
 371      public function wrap_html_finish() {
 372          global $OUTPUT;
 373  
 374          $data = new stdClass();
 375          $data->options = [
 376              [
 377                  'value' => 0,
 378                  'name' => ''
 379              ],
 380              [
 381                  'value' => \tool_dataprivacy\api::DATAREQUEST_ACTION_APPROVE,
 382                  'name' => get_string('approve', 'tool_dataprivacy')
 383              ],
 384              [
 385                  'value' => \tool_dataprivacy\api::DATAREQUEST_ACTION_REJECT,
 386                  'name' => get_string('deny', 'tool_dataprivacy')
 387              ]
 388          ];
 389  
 390          $perpageoptions = array_combine($this->perpageoptions, $this->perpageoptions);
 391          $perpageselect = new \single_select(new moodle_url(''), 'perpage',
 392                  $perpageoptions, get_user_preferences('tool_dataprivacy_request-perpage'), null, 'selectgroup');
 393          $perpageselect->label = get_string('perpage', 'moodle');
 394          $data->perpage = $OUTPUT->render($perpageselect);
 395  
 396          echo $OUTPUT->render_from_template('tool_dataprivacy/data_requests_bulk_actions', $data);
 397      }
 398  
 399      /**
 400       * Set the number of data request records to be displayed per page.
 401       *
 402       * @param int $perpage The number of data request records.
 403       */
 404      public function set_requests_per_page(int $perpage) {
 405          $this->perpage = $perpage;
 406      }
 407  
 408      /**
 409       * Get the number of data request records to be displayed per page.
 410       *
 411       * @return int The number of data request records.
 412       */
 413      public function get_requests_per_page() : int {
 414          return $this->perpage;
 415      }
 416  
 417      /**
 418       * Set the available options for the number of data request to be displayed per page.
 419       *
 420       * @param array $perpageoptions The available options for the number of data request to be displayed per page.
 421       */
 422      public function set_requests_per_page_options(array $perpageoptions) {
 423          $this->$perpageoptions = $perpageoptions;
 424      }
 425  
 426      /**
 427       * Get the available options for the number of data request to be displayed per page.
 428       *
 429       * @return array The available options for the number of data request to be displayed per page.
 430       */
 431      public function get_requests_per_page_options() : array {
 432          return $this->perpageoptions;
 433      }
 434  }