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.
   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_xapi\handler;
  20  use core_xapi\xapi_exception;
  21  use core_external\external_api;
  22  use core_external\external_function_parameters;
  23  use core_external\external_multiple_structure;
  24  use core_external\external_value;
  25  use core_xapi\iri;
  26  use core_xapi\local\statement\item_agent;
  27  
  28  /**
  29   * This is the external API for generic xAPI get all states ids.
  30   *
  31   * @package    core_xapi
  32   * @since      Moodle 4.2
  33   * @copyright  2023 Ferran Recio <ferran@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class get_states extends external_api {
  37  
  38      use \core_xapi\local\helper\state_trait;
  39  
  40      /**
  41       * Parameters for execute
  42       *
  43       * @return external_function_parameters
  44       */
  45      public static function execute_parameters(): external_function_parameters {
  46          return new external_function_parameters([
  47              'component' => new external_value(PARAM_COMPONENT, 'Component name'),
  48              'activityId' => new external_value(PARAM_URL, 'xAPI activity ID IRI'),
  49              'agent' => new external_value(PARAM_RAW, 'The xAPI agent json'),
  50              'registration' => new external_value(PARAM_ALPHANUMEXT, 'The xAPI registration UUID', VALUE_DEFAULT, null),
  51              'since' => new external_value(PARAM_TEXT, 'Filter ids stored since the timestamp (exclusive)', VALUE_DEFAULT, null),
  52          ]);
  53      }
  54  
  55      /**
  56       * Process a get states request.
  57       *
  58       * @param string $component The component name in frankenstyle.
  59       * @param string $activityiri The activity IRI.
  60       * @param string $agent The agent JSON.
  61       * @param string|null $registration The xAPI registration UUID.
  62       * @param string|null $since A ISO 8601 timestamps or a numeric timestamp.
  63       * @return array the list of the stored state ids
  64       */
  65      public static function execute(
  66          string $component,
  67          string $activityiri,
  68          string $agent,
  69          ?string $registration = null,
  70          ?string $since = null
  71      ): array {
  72          global $USER;
  73  
  74          [
  75              'component' => $component,
  76              'activityId' => $activityiri,
  77              'agent' => $agent,
  78              'registration' => $registration,
  79              'since' => $since,
  80          ] = self::validate_parameters(self::execute_parameters(), [
  81              'component' => $component,
  82              'activityId' => $activityiri,
  83              'agent' => $agent,
  84              'registration' => $registration,
  85              'since' => $since,
  86          ]);
  87  
  88          static::validate_component($component);
  89  
  90          $handler = handler::create($component);
  91  
  92          $agent = self::get_agent_from_json($agent);
  93          $user = $agent->get_user();
  94  
  95          if ($user->id !== $USER->id) {
  96              throw new xapi_exception('State agent is not the current user');
  97          }
  98  
  99          $activityid = iri::extract($activityiri, 'activity');
 100          $createdsince = self::convert_since_param_to_timestamp($since);
 101          $store = $handler->get_state_store();
 102  
 103          return $store->get_state_ids(
 104              $activityid,
 105              $user->id,
 106              $registration,
 107              $createdsince
 108          );
 109      }
 110  
 111      /**
 112       * Convert the xAPI since param into a Moodle integer timestamp.
 113       *
 114       * According to xAPI standard, the "since" param must follow the ISO 8601
 115       * format. However, because Moodle do not use this format, we accept both
 116       * numeric timestamp and ISO 8601.
 117       *
 118       * @param string|null $since A ISO 8601 timestamps or a numeric timestamp.
 119       * @return null|int the resulting timestamp or null if since is null.
 120       */
 121      private static function convert_since_param_to_timestamp(?string $since): ?int {
 122          if ($since === null) {
 123              return null;
 124          }
 125          if (is_numeric($since)) {
 126              return intval($since);
 127          }
 128          try {
 129              $datetime = new \DateTime($since);
 130              return $datetime->getTimestamp();
 131          } catch (\Exception $exception) {
 132              throw new xapi_exception("Since param '$since' is not in ISO 8601 or a numeric timestamp format");
 133          }
 134      }
 135  
 136      /**
 137       * Return for execute.
 138       *
 139       * @return external_multiple_structure
 140       */
 141      public static function execute_returns(): external_multiple_structure {
 142          return new external_multiple_structure(
 143              new external_value(PARAM_RAW, 'State ID'),
 144              'List of state Ids'
 145          );
 146      }
 147  }