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  /**
  18   * Class for submit selected courses from form.
  19   *
  20   * @package    tool_dataprivacy
  21   * @copyright  2021 The Open University.
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace tool_dataprivacy\external;
  25  
  26  use core_external\external_api;
  27  use core_external\external_function_parameters;
  28  use core_external\external_single_structure;
  29  use core_external\external_value;
  30  use core_external\external_warnings;
  31  use core\notification;
  32  use context_system;
  33  
  34  /**
  35   * Class for submit selected courses from form.
  36   *
  37   * @copyright  2021 The Open University.
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   * @since Moodle 4.3
  40   */
  41  class submit_selected_courses_form extends external_api {
  42      /**
  43       * Parameter description for get_data_request().
  44       *
  45       * @return external_function_parameters
  46       */
  47      public static function execute_parameters(): external_function_parameters {
  48          return new external_function_parameters([
  49              'requestid' => new external_value(PARAM_INT, 'The id of data request'),
  50              'jsonformdata' => new external_value(PARAM_RAW, 'The data of selected courses form, encoded as a json array'),
  51          ]);
  52      }
  53  
  54      /**
  55       * Fetch the list of course which user can select to export data.
  56       *
  57       * @param int $requestid The request ID.
  58       * @param string $jsonformdata The data of selected courses form.
  59       * @return array
  60       */
  61      public static function execute(int $requestid, string $jsonformdata): array {
  62  
  63          $warnings = [];
  64          $result = false;
  65          $params = self::validate_parameters(self::execute_parameters(), [
  66              'requestid' => $requestid,
  67              'jsonformdata' => $jsonformdata,
  68          ]);
  69  
  70          $context = context_system::instance();
  71          self::validate_context($context);
  72  
  73          // Make sure the user has the proper capability.
  74          require_capability('tool/dataprivacy:managedatarequests', $context);
  75  
  76          $requestid = $params['requestid'];
  77          $serialiseddata = json_decode($params['jsonformdata']);
  78          $data = array();
  79          parse_str($serialiseddata, $data);
  80  
  81          $mform = new \tool_dataprivacy\form\exportfilter_form(null, ['requestid' => $requestid], 'post', '', null, true, $data);
  82  
  83          if (PHPUNIT_TEST) {
  84              $validateddata = $mform->mock_ajax_submit($data);
  85          } else {
  86              $validateddata = $mform->get_data();
  87          }
  88          if ($validateddata) {
  89              // Ensure the request exists.
  90              $requestexists = \tool_dataprivacy\data_request::record_exists($requestid);
  91  
  92              if ($requestexists) {
  93                  $coursecontextids = [];
  94                  if (!empty($validateddata->coursecontextids)) {
  95                      $coursecontextids = $validateddata->coursecontextids;
  96                  }
  97                  if (PHPUNIT_TEST) {
  98                      if (!empty($validateddata['coursecontextids'])) {
  99                          $coursecontextids = $validateddata['coursecontextids'];
 100                      }
 101                  }
 102                  $result = \tool_dataprivacy\api::approve_data_request($requestid, $coursecontextids);
 103  
 104                  // Add notification in the session to be shown when the page is reloaded on the JS side.
 105                  notification::success(get_string('requestapproved', 'tool_dataprivacy'));
 106              } else {
 107                  $warnings = [
 108                      'item' => $requestid,
 109                      'warningcode' => 'errorrequestnotfound',
 110                      'message' => get_string('errorrequestnotfound', 'tool_dataprivacy'),
 111                  ];
 112              }
 113          }
 114          return [
 115              'result' => $result,
 116              'warnings' => $warnings,
 117          ];
 118      }
 119  
 120      /**
 121       * Parameter description for submit_selected_courses_form().
 122       *
 123       * @return external_single_structure
 124       */
 125      public static function execute_returns(): external_single_structure {
 126          return new external_single_structure([
 127              'result' => new external_value(PARAM_BOOL, 'The processing result'),
 128              'warnings' => new external_warnings(),
 129          ]);
 130      }
 131  }