Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 402] [Versions 310 and 403]

   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   * The core_xapi statement validation and tansformation.
  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  
  26  namespace core_xapi;
  27  
  28  use core_xapi\local\statement;
  29  use core_xapi\xapi_exception;
  30  use stdClass;
  31  
  32  defined('MOODLE_INTERNAL') || die();
  33  
  34  /**
  35   * Class handler handles basic xapi statements.
  36   *
  37   * @package core_xapi
  38   * @copyright  2020 Ferran Recio
  39   */
  40  abstract class handler {
  41  
  42      /** @var string component name in frankenstyle. */
  43      protected $component;
  44  
  45      /**
  46       * Constructor for a xAPI handler base class.
  47       *
  48       * @param string $component the component name
  49       */
  50      final protected function __construct(string $component) {
  51          $this->component = $component;
  52      }
  53  
  54      /**
  55       * Returns the xAPI handler of a specific component.
  56       *
  57       * @param string $component the component name in frankenstyle.
  58       * @return handler|null a handler object or null if none found.
  59       * @throws xapi_exception
  60       */
  61      final public static function create(string $component): self {
  62          $classname = "\\$component\\xapi\\handler";
  63          if (class_exists($classname)) {
  64              return new $classname($component);
  65          }
  66          throw new xapi_exception('Unknown handler');
  67      }
  68  
  69      /**
  70       * Convert a statement object into a Moodle xAPI Event.
  71       *
  72       * If a statement is accepted by validate_statement the component must provide a event
  73       * to handle that statement, otherwise the statement will be rejected.
  74       *
  75       * Note: this method must be overridden by the plugins which want to use xAPI.
  76       *
  77       * @param statement $statement
  78       * @return \core\event\base|null a Moodle event to trigger
  79       */
  80      abstract public function statement_to_event(statement $statement): ?\core\event\base;
  81  
  82      /**
  83       * Return true if group actor is enabled.
  84       *
  85       * Note: this method must be overridden by the plugins which want to
  86       * use groups in statements.
  87       *
  88       * @return bool
  89       */
  90      public function supports_group_actors(): bool {
  91          return false;
  92      }
  93  
  94      /**
  95       * Process a bunch of statements sended to a specific component.
  96       *
  97       * @param statement[] $statements an array with all statement to process.
  98       * @return int[] return an specifying what statements are being stored.
  99       */
 100      public function process_statements(array $statements): array {
 101          $result = [];
 102          foreach ($statements as $key => $statement) {
 103              try {
 104                  // Ask the plugin to convert into an event.
 105                  $event = $this->statement_to_event($statement);
 106                  if ($event) {
 107                      $event->trigger();
 108                      $result[$key] = true;
 109                  } else {
 110                      $result[$key] = false;
 111                  }
 112              } catch (\Exception $e) {
 113                  $result[$key] = false;
 114              }
 115          }
 116          return $result;
 117      }
 118  }