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 core_xapi\external;
  18  
  19  use core_external\external_api;
  20  use core_external\external_function_parameters;
  21  use core_external\external_single_structure;
  22  use core_external\external_value;
  23  use core_xapi\handler;
  24  use core_xapi\iri;
  25  use core_xapi\xapi_exception;
  26  
  27  /**
  28   * This is the external API for generic xAPI states deletion.
  29   *
  30   * @package    core_xapi
  31   * @since      Moodle 4.3
  32   * @copyright  2023 Laurent David <laurent.david@moodle.com>
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class delete_states extends external_api {
  36  
  37      use \core_xapi\local\helper\state_trait;
  38  
  39      /**
  40       * Process a state delete request.
  41       *
  42       * @param string $component The component name in frankenstyle.
  43       * @param string $activityiri The activity IRI.
  44       * @param string $agent The agent JSON.
  45       * @param string|null $registration The xAPI registration UUID.
  46       * @return void
  47       */
  48      public static function execute(
  49          string $component,
  50          string $activityiri,
  51          string $agent,
  52          ?string $registration = null,
  53      ): void {
  54          global $USER;
  55  
  56          [
  57              'component' => $component,
  58              'activityId' => $activityiri,
  59              'agent' => $agent,
  60              'registration' => $registration,
  61          ] = self::validate_parameters(self::execute_parameters(), [
  62              'component' => $component,
  63              'activityId' => $activityiri,
  64              'agent' => $agent,
  65              'registration' => $registration,
  66          ]);
  67  
  68          static::validate_component($component);
  69  
  70          $handler = handler::create($component);
  71  
  72          $activityid = iri::extract($activityiri, 'activity');
  73          $agent = self::get_agent_from_json($agent);
  74          $user = $agent->get_user();
  75          if ($user->id != $USER->id) {
  76              throw new xapi_exception('State agent is not the current user');
  77          }
  78          $handler->wipe_states($activityid, $user->id, null, $registration);
  79      }
  80  
  81      /**
  82       * Parameters for execute.
  83       *
  84       * @return external_function_parameters
  85       */
  86      public static function execute_parameters(): external_function_parameters {
  87          return new external_function_parameters([
  88              'component' => new external_value(PARAM_COMPONENT, 'Component name'),
  89              'activityId' => new external_value(PARAM_URL, 'xAPI activity ID IRI'),
  90              'agent' => new external_value(PARAM_RAW, 'The xAPI agent json'),
  91              'registration' => new external_value(PARAM_ALPHANUMEXT, 'The xAPI registration UUID', VALUE_DEFAULT, null)
  92          ]);
  93      }
  94  
  95      /**
  96       * Return for execute.
  97       */
  98      public static function execute_returns() {
  99          return null;
 100      }
 101  }