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\local\helper;
  18  
  19  use core_component;
  20  use core_xapi\local\state;
  21  use core_xapi\local\statement\item_agent;
  22  use core_xapi\xapi_exception;
  23  use JsonException;
  24  use stdClass;
  25  
  26  /**
  27   * State trait helper, with common methods.
  28   *
  29   * @package    core_xapi
  30   * @since      Moodle 4.2
  31   * @copyright  2023 Sara Arjona (sara@moodle.com)
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  trait state_trait {
  35      /**
  36       * Check component name.
  37       *
  38       * Note: this function is separated mainly for testing purposes to
  39       * be overridden to fake components.
  40       *
  41       * @throws xapi_exception if component is not available
  42       * @param string $component component name
  43       */
  44      protected static function validate_component(string $component): void {
  45          // Check that $component is a real component name.
  46          $dir = core_component::get_component_directory($component);
  47          if (!$dir) {
  48              throw new xapi_exception("Component $component not available.");
  49          }
  50      }
  51  
  52      /**
  53       * Convert a JSON agent into a valid item_agent.
  54       *
  55       * @throws xapi_exception if JSON cannot be parsed
  56       * @param string $agentjson JSON encoded agent structure
  57       * @return item_agent the agent
  58       */
  59      private static function get_agent_from_json(string $agentjson): item_agent {
  60          try {
  61              $agentdata = json_decode($agentjson, null, 512, JSON_THROW_ON_ERROR);
  62          } catch (JsonException $e) {
  63              throw new xapi_exception('No agent detected');
  64          }
  65          return item_agent::create_from_data($agentdata);
  66      }
  67  
  68      /**
  69       * Check that $USER is actor in state.
  70       *
  71       * @param state $state The state
  72       * @return bool if $USER is actor of the state
  73       */
  74      private static function check_state_user(state $state): bool {
  75          global $USER;
  76          $user = $state->get_user();
  77          if ($user->id != $USER->id) {
  78              return false;
  79          }
  80          return true;
  81      }
  82  
  83      /**
  84       * Convert the state data JSON into valid object.
  85       *
  86       * @throws xapi_exception if JSON cannot be parsed
  87       * @param string $statedatajson JSON encoded structure
  88       * @return stdClass the state data structure
  89       */
  90      private static function get_statedata_from_json(string $statedatajson): stdClass {
  91          try {
  92              // Force it to be an object, because some statedata might be sent as array instead of JSON.
  93              $statedata = json_decode($statedatajson, false, 512, JSON_THROW_ON_ERROR);
  94          } catch (JsonException $e) {
  95              throw new xapi_exception('Invalid state data format');
  96          }
  97          return $statedata;
  98      }
  99  }