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  /**
  18   * Statement verb object for xAPI structure checking and usage.
  19   *
  20   * @package    core_xapi
  21   * @copyright  2020 Ferran Recio
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_xapi\local\statement;
  26  
  27  use core_xapi\xapi_exception;
  28  use core_xapi\iri;
  29  use stdClass;
  30  
  31  defined('MOODLE_INTERNAL') || die();
  32  
  33  /**
  34   * Verb xAPI statement item.
  35   *
  36   * Verbs represent the interaction a user/group made inside a xAPI
  37   * compatible plugin. Internally a xAPI verb must be representad as
  38   * in a valid IRI format (which is a less restrictive version of a
  39   * regular URL so a moodle_url out is completelly fine). To make it
  40   * easy for plugins to generate valid IRI, a simple string van be
  41   * provided and the class will convert into a valid IRI internally.
  42   *
  43   * @copyright  2020 Ferran Recio
  44   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  45   */
  46  class item_verb extends item {
  47  
  48      /** @var string The statement. */
  49      protected $id;
  50  
  51      /**
  52       * An xAPI verb constructor based on xAPI data structure.
  53       *
  54       * @param stdClass $data from the specific xAPI element
  55       */
  56      protected function __construct(stdClass $data) {
  57          parent::__construct($data);
  58          $this->id = iri::extract($data->id, 'verb');
  59      }
  60  
  61      /**
  62       * Function to create an item from part of the xAPI statement.
  63       *
  64       * @param stdClass $data the original xAPI element
  65       * @return item item_verb xAPI generated
  66       */
  67      public static function create_from_data(stdClass $data): item {
  68          if (empty($data->id)) {
  69              throw new xapi_exception("missing verb id");
  70          }
  71          if (!iri::check($data->id)) {
  72              throw new xapi_exception("verb id $data->id is not a valid IRI");
  73          }
  74          return new self($data);
  75      }
  76  
  77      /**
  78       * Create a valid item_verb from a simple verb string.
  79       *
  80       * @param string $id string to convert to a valid IRI (or a valid IRI)
  81       * @return item_verb the resulting item_verb
  82       */
  83      public static function create_from_id(string $id): item_verb {
  84  
  85          $data = new stdClass();
  86          $data->id = iri::generate($id, 'verb');
  87  
  88          return new self($data);
  89      }
  90  
  91      /**
  92       * Return the id used in this item.
  93       *
  94       * Id will be extracted from the provided IRI. If it's a valid IRI
  95       * it will return all IRI value but if it is generate by the iri helper
  96       * from this library it will extract the original value.
  97       *
  98       * @return string the ID (extracted from IRI value)
  99       */
 100      public function get_id(): string {
 101          return $this->id;
 102      }
 103  }