Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [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 = ['data-action' => 'view', 'data-requestid' => $requestid];
 228          $actiontext = get_string('viewrequest', 'tool_dataprivacy');
 229          $actions[] = new action_menu_link_secondary($actionurl, null, $actiontext, $actiondata);
 230  
 231          switch ($status) {
 232              case api::DATAREQUEST_STATUS_PENDING:
 233                  // Add action to mark a general enquiry request as complete.
 234                  if ($data->type == api::DATAREQUEST_TYPE_OTHERS) {
 235                      $actiondata['data-action'] = 'complete';
 236                      $nameemail = (object)[
 237                          'name' => $data->foruser->fullname,
 238                          'email' => $data->foruser->email
 239                      ];
 240                      $actiondata['data-requestid'] = $data->id;
 241                      $actiondata['data-replytoemail'] = get_string('nameemail', 'tool_dataprivacy', $nameemail);
 242                      $actiontext = get_string('markcomplete', 'tool_dataprivacy');
 243                      $actions[] = new action_menu_link_secondary($actionurl, null, $actiontext, $actiondata);
 244                  }
 245                  break;
 246              case api::DATAREQUEST_STATUS_AWAITING_APPROVAL:
 247                  // Only show "Approve" and "Deny" button for deletion request if current user has permission.
 248                  if ($persistent->get('type') == api::DATAREQUEST_TYPE_DELETE &&
 249                      !api::can_create_data_deletion_request_for_other()) {
 250                      break;
 251                  }
 252                  // Approve.
 253                  $actiondata['data-action'] = 'approve';
 254                  $actiontext = get_string('approverequest', 'tool_dataprivacy');
 255                  $actions[] = new action_menu_link_secondary($actionurl, null, $actiontext, $actiondata);
 256  
 257                  // Deny.
 258                  $actiondata['data-action'] = 'deny';
 259                  $actiontext = get_string('denyrequest', 'tool_dataprivacy');
 260                  $actions[] = new action_menu_link_secondary($actionurl, null, $actiontext, $actiondata);
 261                  break;
 262              case api::DATAREQUEST_STATUS_DOWNLOAD_READY:
 263                  $userid = $data->foruser->id;
 264                  $usercontext = \context_user::instance($userid, IGNORE_MISSING);
 265                  // If user has permission to view download link, show relevant action item.
 266                  if ($usercontext && api::can_download_data_request_for_user($userid, $data->requestedbyuser->id)) {
 267                      $actions[] = api::get_download_link($usercontext, $requestid);
 268                  }
 269                  break;
 270          }
 271  
 272          if ($this->manage) {
 273              $canreset = $persistent->is_active() || empty($this->ongoingrequests[$data->foruser->id]->{$data->type});
 274              $canreset = $canreset && $persistent->is_resettable();
 275              // Prevent re-submmit deletion request if current user don't have permission.
 276              $canreset = $canreset && ($persistent->get('type') != api::DATAREQUEST_TYPE_DELETE ||
 277                      api::can_create_data_deletion_request_for_other());
 278              if ($canreset) {
 279                  $reseturl = new moodle_url('/admin/tool/dataprivacy/resubmitrequest.php', [
 280                          'requestid' => $requestid,
 281                      ]);
 282                  $actiondata = ['data-action' => 'reset', 'data-requestid' => $requestid];
 283                  $actiontext = get_string('resubmitrequestasnew', 'tool_dataprivacy');
 284                  $actions[] = new action_menu_link_secondary($reseturl, null, $actiontext, $actiondata);
 285              }
 286          }
 287  
 288          $actionsmenu = new action_menu($actions);
 289          $actionsmenu->set_menu_trigger(get_string('actions'));
 290          $actionsmenu->set_owner_selector('request-actions-' . $requestid);
 291          $actionsmenu->set_boundary('window');
 292  
 293          return $OUTPUT->render($actionsmenu);
 294      }
 295  
 296      /**
 297       * Query the database for results to display in the table.
 298       *
 299       * @param int $pagesize size of page for paginated displayed table.
 300       * @param bool $useinitialsbar do you want to use the initials bar.
 301       * @throws dml_exception
 302       * @throws coding_exception
 303       */
 304      public function query_db($pagesize, $useinitialsbar = true) {
 305          global $PAGE;
 306  
 307          // Set dummy page total until we fetch full result set.
 308          $this->pagesize($pagesize, $pagesize + 1);
 309  
 310          $sort = $this->get_sql_sort();
 311  
 312          // Get data requests from the given conditions.
 313          $datarequests = api::get_data_requests($this->userid, $this->statuses, $this->types,
 314                  $this->creationmethods, $sort, $this->get_page_start(), $this->get_page_size());
 315  
 316          // Count data requests from the given conditions.
 317          $total = api::get_data_requests_count($this->userid, $this->statuses, $this->types,
 318                  $this->creationmethods);
 319          $this->pagesize($pagesize, $total);
 320  
 321          $this->rawdata = [];
 322          $context = \context_system::instance();
 323          $renderer = $PAGE->get_renderer('tool_dataprivacy');
 324  
 325          $forusers = [];
 326          foreach ($datarequests as $persistent) {
 327              $this->datarequests[$persistent->get('id')] = $persistent;
 328              $exporter = new data_request_exporter($persistent, ['context' => $context]);
 329              $this->rawdata[] = $exporter->export($renderer);
 330              $forusers[] = $persistent->get('userid');
 331          }
 332  
 333          // Fetch the list of all ongoing requests for the users currently shown.
 334          // This is used to determine whether any non-active request can be resubmitted.
 335          // There can only be one ongoing request of a type for each user.
 336          $this->ongoingrequests = api::find_ongoing_request_types_for_users($forusers);
 337  
 338          // Set initial bars.
 339          if ($useinitialsbar) {
 340              $this->initialbars($total > $pagesize);
 341          }
 342      }
 343  
 344      /**
 345       * Override default implementation to display a more meaningful information to the user.
 346       */
 347      public function print_nothing_to_display() {
 348          global $OUTPUT;
 349          echo $this->render_reset_button();
 350          $this->print_initials_bar();
 351          if (!empty($this->statuses) || !empty($this->types)) {
 352              $message = get_string('nodatarequestsmatchingfilter', 'tool_dataprivacy');
 353          } else {
 354              $message = get_string('nodatarequests', 'tool_dataprivacy');
 355          }
 356          echo $OUTPUT->notification($message, 'warning');
 357      }
 358  
 359      /**
 360       * Override the table's show_hide_link method to prevent the show/hide links from rendering.
 361       *
 362       * @param string $column the column name, index into various names.
 363       * @param int $index numerical index of the column.
 364       * @return string HTML fragment.
 365       */
 366      protected function show_hide_link($column, $index) {
 367          return '';
 368      }
 369  
 370      /**
 371       * Override the table's wrap_html_finish method in order to render the bulk actions and
 372       * records per page options.
 373       */
 374      public function wrap_html_finish() {
 375          global $OUTPUT;
 376  
 377          $data = new stdClass();
 378          $data->options = [
 379              [
 380                  'value' => 0,
 381                  'name' => ''
 382              ],
 383              [
 384                  'value' => \tool_dataprivacy\api::DATAREQUEST_ACTION_APPROVE,
 385                  'name' => get_string('approve', 'tool_dataprivacy')
 386              ],
 387              [
 388                  'value' => \tool_dataprivacy\api::DATAREQUEST_ACTION_REJECT,
 389                  'name' => get_string('deny', 'tool_dataprivacy')
 390              ]
 391          ];
 392  
 393          $perpageoptions = array_combine($this->perpageoptions, $this->perpageoptions);
 394          $perpageselect = new \single_select(new moodle_url(''), 'perpage',
 395                  $perpageoptions, get_user_preferences('tool_dataprivacy_request-perpage'), null, 'selectgroup');
 396          $perpageselect->label = get_string('perpage', 'moodle');
 397          $data->perpage = $OUTPUT->render($perpageselect);
 398  
 399          echo $OUTPUT->render_from_template('tool_dataprivacy/data_requests_bulk_actions', $data);
 400      }
 401  
 402      /**
 403       * Set the number of data request records to be displayed per page.
 404       *
 405       * @param int $perpage The number of data request records.
 406       */
 407      public function set_requests_per_page(int $perpage) {
 408          $this->perpage = $perpage;
 409      }
 410  
 411      /**
 412       * Get the number of data request records to be displayed per page.
 413       *
 414       * @return int The number of data request records.
 415       */
 416      public function get_requests_per_page() : int {
 417          return $this->perpage;
 418      }
 419  
 420      /**
 421       * Set the available options for the number of data request to be displayed per page.
 422       *
 423       * @param array $perpageoptions The available options for the number of data request to be displayed per page.
 424       */
 425      public function set_requests_per_page_options(array $perpageoptions) {
 426          $this->$perpageoptions = $perpageoptions;
 427      }
 428  
 429      /**
 430       * Get the available options for the number of data request to be displayed per page.
 431       *
 432       * @return array The available options for the number of data request to be displayed per page.
 433       */
 434      public function get_requests_per_page_options() : array {
 435          return $this->perpageoptions;
 436      }
 437  }