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  /**
  18   * Statement definition 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   * Validation and usage of xAPI definition.
  35   *
  36   * Definition contains extra information about user interaction with
  37   * questions and other activities inside a xAPI statement. For now
  38   * it performs a basic validation on the provided data.
  39   *
  40   * @copyright  2020 Ferran Recio
  41   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class item_definition extends item {
  44  
  45      /** @var string The statement. */
  46      protected $interactiontype;
  47  
  48      /**
  49       * Function to create a definition from part of the xAPI statement.
  50       *
  51       * @param stdClass $data the original xAPI element.
  52       */
  53      protected function __construct(stdClass $data) {
  54          parent::__construct($data);
  55          $this->interactiontype = $data->interactionType ?? null;
  56      }
  57  
  58      /**
  59       * Function to create an item from part of the xAPI statement.
  60       *
  61       * @param stdClass $data the original xAPI element
  62       * @return item item_definition xAPI generated
  63       */
  64      public static function create_from_data(stdClass $data): item {
  65          // Interaction Type is a optopnal param.
  66          if (!empty($data->interactionType)) {
  67              $posiblevalues = [
  68                  'choice' => true,
  69                  'fill-in' => true,
  70                  'long-fill-in' => true,
  71                  'true-false' => true,
  72                  'matching' => true,
  73                  'performance' => true,
  74                  'sequencing' => true,
  75                  'likert' => true,
  76                  'numeric' => true,
  77                  'other' => true,
  78                  'compound' => true,
  79              ];
  80              if (!isset($posiblevalues[$data->interactionType])) {
  81                  throw new xapi_exception("Invalid definition \"{$data->interactionType}\"");
  82              }
  83          }
  84          return new self($data);
  85      }
  86  
  87      /**
  88       * Return the definition interaction type.
  89       */
  90      public function get_interactiontype(): ?string {
  91          return $this->interactiontype;
  92      }
  93  }