Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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