Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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_alignment(\action_menu::TL, \action_menu::BL);
 289          $actionsmenu->set_constraint('[data-region=data-requests-table] > .no-overflow');
 290  
 291          return $OUTPUT->render($actionsmenu);
 292      }
 293  
 294      /**
 295       * Query the database for results to display in the table.
 296       *
 297       * @param int $pagesize size of page for paginated displayed table.
 298       * @param bool $useinitialsbar do you want to use the initials bar.
 299       * @throws dml_exception
 300       * @throws coding_exception
 301       */
 302      public function query_db($pagesize, $useinitialsbar = true) {
 303          global $PAGE;
 304  
 305          // Set dummy page total until we fetch full result set.
 306          $this->pagesize($pagesize, $pagesize + 1);
 307  
 308          $sort = $this->get_sql_sort();
 309  
 310          // Get data requests from the given conditions.
 311          $datarequests = api::get_data_requests($this->userid, $this->statuses, $this->types,
 312                  $this->creationmethods, $sort, $this->get_page_start(), $this->get_page_size());
 313  
 314          // Count data requests from the given conditions.
 315          $total = api::get_data_requests_count($this->userid, $this->statuses, $this->types,
 316                  $this->creationmethods);
 317          $this->pagesize($pagesize, $total);
 318  
 319          $this->rawdata = [];
 320          $context = \context_system::instance();
 321          $renderer = $PAGE->get_renderer('tool_dataprivacy');
 322  
 323          $forusers = [];
 324          foreach ($datarequests as $persistent) {
 325              $this->datarequests[$persistent->get('id')] = $persistent;
 326              $exporter = new data_request_exporter($persistent, ['context' => $context]);
 327              $this->rawdata[] = $exporter->export($renderer);
 328              $forusers[] = $persistent->get('userid');
 329          }
 330  
 331          // Fetch the list of all ongoing requests for the users currently shown.
 332          // This is used to determine whether any non-active request can be resubmitted.
 333          // There can only be one ongoing request of a type for each user.
 334          $this->ongoingrequests = api::find_ongoing_request_types_for_users($forusers);
 335  
 336          // Set initial bars.
 337          if ($useinitialsbar) {
 338              $this->initialbars($total > $pagesize);
 339          }
 340      }
 341  
 342      /**
 343       * Override default implementation to display a more meaningful information to the user.
 344       */
 345      public function print_nothing_to_display() {
 346          global $OUTPUT;
 347          echo $this->render_reset_button();
 348          $this->print_initials_bar();
 349          if (!empty($this->statuses) || !empty($this->types)) {
 350              $message = get_string('nodatarequestsmatchingfilter', 'tool_dataprivacy');
 351          } else {
 352              $message = get_string('nodatarequests', 'tool_dataprivacy');
 353          }
 354          echo $OUTPUT->notification($message, 'warning');
 355      }
 356  
 357      /**
 358       * Override the table's show_hide_link method to prevent the show/hide links from rendering.
 359       *
 360       * @param string $column the column name, index into various names.
 361       * @param int $index numerical index of the column.
 362       * @return string HTML fragment.
 363       */
 364      protected function show_hide_link($column, $index) {
 365          return '';
 366      }
 367  
 368      /**
 369       * Override the table's wrap_html_finish method in order to render the bulk actions and
 370       * records per page options.
 371       */
 372      public function wrap_html_finish() {
 373          global $OUTPUT;
 374  
 375          $data = new stdClass();
 376          $data->options = [
 377              [
 378                  'value' => 0,
 379                  'name' => ''
 380              ],
 381              [
 382                  'value' => \tool_dataprivacy\api::DATAREQUEST_ACTION_APPROVE,
 383                  'name' => get_string('approve', 'tool_dataprivacy')
 384              ],
 385              [
 386                  'value' => \tool_dataprivacy\api::DATAREQUEST_ACTION_REJECT,
 387                  'name' => get_string('deny', 'tool_dataprivacy')
 388              ]
 389          ];
 390  
 391          $perpageoptions = array_combine($this->perpageoptions, $this->perpageoptions);
 392          $perpageselect = new \single_select(new moodle_url(''), 'perpage',
 393                  $perpageoptions, get_user_preferences('tool_dataprivacy_request-perpage'), null, 'selectgroup');
 394          $perpageselect->label = get_string('perpage', 'moodle');
 395          $data->perpage = $OUTPUT->render($perpageselect);
 396  
 397          echo $OUTPUT->render_from_template('tool_dataprivacy/data_requests_bulk_actions', $data);
 398      }
 399  
 400      /**
 401       * Set the number of data request records to be displayed per page.
 402       *
 403       * @param int $perpage The number of data request records.
 404       */
 405      public function set_requests_per_page(int $perpage) {
 406          $this->perpage = $perpage;
 407      }
 408  
 409      /**
 410       * Get the number of data request records to be displayed per page.
 411       *
 412       * @return int The number of data request records.
 413       */
 414      public function get_requests_per_page() : int {
 415          return $this->perpage;
 416      }
 417  
 418      /**
 419       * Set the available options for the number of data request to be displayed per page.
 420       *
 421       * @param array $perpageoptions The available options for the number of data request to be displayed per page.
 422       */
 423      public function set_requests_per_page_options(array $perpageoptions) {
 424          $this->$perpageoptions = $perpageoptions;
 425      }
 426  
 427      /**
 428       * Get the available options for the number of data request to be displayed per page.
 429       *
 430       * @return array The available options for the number of data request to be displayed per page.
 431       */
 432      public function get_requests_per_page_options() : array {
 433          return $this->perpageoptions;
 434      }
 435  }