Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.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   * xAPI LRS IRI values generator.
  19   *
  20   * @package    core_xapi
  21   * @since      Moodle 3.9
  22   * @copyright  2020 Ferran Recio
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  namespace core_xapi;
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use stdClass;
  29  use moodle_url;
  30  
  31  /**
  32   * Class to translate Moodle objects to xAPI elements.
  33   *
  34   * @copyright  2020 Ferran Recio
  35   * @since      Moodle 3.9
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class iri {
  39  
  40      /**
  41       * Generate a valid IRI element from a $value and an optional $type.
  42       *
  43       * Verbs and Objects in xAPI are in IRI format. This function could get
  44       * a valid IRI value (and will return without modifiyng it) or a simple
  45       * string and a type and generate a fake IRI valir for any xAPI statement.
  46       *
  47       * @param string $value a valid IRI value or any string
  48       * @param string|null $type if none passed $type will be 'element'
  49       * @return string a valid IRI value
  50       */
  51      public static function generate(string $value, string $type = null): string {
  52          if (self::check($value)) {
  53              return $value;
  54          }
  55          if (empty($type)) {
  56              $type = 'element';
  57          }
  58          return (new moodle_url("/xapi/$type/$value"))->out(false);
  59      }
  60  
  61      /**
  62       * Try to extract the original value from an IRI.
  63       *
  64       * If a real IRI value is passed, it will return it without any change. If a
  65       * fake IRI is passed (generated by iri::generate)
  66       * it will try to extract the original value.
  67       *
  68       * @param string $value the currewnt IRI value.
  69       * @param string|null $type if $value is a fake IRI, the $type must be provided.
  70       * @return string the original value used in iri::generate.
  71       */
  72      public static function extract(string $value, string $type = null): string {
  73          if (empty($type)) {
  74              $type = 'element';
  75          }
  76          $xapibase = (new moodle_url("/xapi/$type/"))->out(false);
  77          if (strpos($value, $xapibase) === 0) {
  78              return substr($value, strlen($xapibase));
  79          }
  80          return $value;
  81      }
  82  
  83      /**
  84       * Check if a $value could be a valid IRI or not.
  85       *
  86       * @param string $value the currewnt IRI value.
  87       * @return bool if the $value could be an IRI.
  88       */
  89      public static function check(string $value): bool {
  90          $iri = new moodle_url($value);
  91          return in_array($iri->get_scheme(), ['http', 'https']);
  92      }
  93  }