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.
   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  namespace tool_dataprivacy\task;
  18  
  19  use coding_exception;
  20  use core\task\adhoc_task;
  21  use tool_dataprivacy\api;
  22  use tool_dataprivacy\contextlist_context;
  23  use tool_dataprivacy\data_request;
  24  
  25  /**
  26   * Class that processes a data request and prepares the user's relevant contexts for review.
  27   *
  28   * Custom data accepted:
  29   * - requestid -> The ID of the data request to be processed.
  30   *
  31   * @package   tool_dataprivacy
  32   * @copyright 2021 The Open University
  33   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   * @since     Moodle 4.3
  35   */
  36  class initiate_data_request_task extends adhoc_task {
  37  
  38      /**
  39       * Run the task to initiate the data request process.
  40       */
  41      public function execute(): void {
  42          if (!isset($this->get_custom_data()->requestid)) {
  43              throw new coding_exception('The custom data \'requestid\' is required.');
  44          }
  45          $requestid = $this->get_custom_data()->requestid;
  46  
  47          $datarequest = new data_request($requestid);
  48  
  49          // Check if this request still needs to be processed. e.g. The user might have cancelled it before this task has run.
  50          $status = $datarequest->get('status');
  51          if (!api::is_active($status)) {
  52              mtrace('Request ' . $requestid . ' with status ' . $status . ' doesn\'t need to be processed. Skipping...');
  53              return;
  54          }
  55  
  56          // Update the status of this request as pre-processing.
  57          mtrace('Generating the contexts containing personal data for the user...');
  58          api::update_request_status($requestid, api::DATAREQUEST_STATUS_PREPROCESSING);
  59  
  60          // Add the list of relevant contexts to the request, and mark all as pending approval.
  61          $privacymanager = new \core_privacy\manager();
  62          $privacymanager->set_observer(new \tool_dataprivacy\manager_observer());
  63  
  64          $contextlistcollection = $privacymanager->get_contexts_for_userid($datarequest->get('userid'));
  65          api::add_request_contexts_with_status($contextlistcollection, $requestid, contextlist_context::STATUS_PENDING);
  66  
  67          // When the preparation of the contexts finishes, update the request status to awaiting approval.
  68          api::update_request_status($requestid, api::DATAREQUEST_STATUS_AWAITING_APPROVAL);
  69          mtrace('Context generation complete...');
  70  
  71          // Get the list of the site Data Protection Officers.
  72          $dpos = api::get_site_dpos();
  73  
  74          // Email the data request to the Data Protection Officer(s)/Admin(s).
  75          foreach ($dpos as $dpo) {
  76              $dponame = fullname($dpo);
  77              if (api::notify_dpo($dpo, $datarequest)) {
  78                  mtrace('Message sent to DPO: ' . $dponame);
  79              } else {
  80                  mtrace('A problem was encountered while sending the message to the DPO: ' . $dponame);
  81              }
  82          }
  83      }
  84  }