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 test class for xAPI statements.
  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 fake_component\xapi;
  27  
  28  use core_xapi\local\statement;
  29  use core_xapi\handler as handler_base;
  30  use core_xapi\event\xapi_test_statement_post;
  31  use context_system;
  32  use core\event\base;
  33  
  34  defined('MOODLE_INTERNAL') || die();
  35  
  36  /**
  37   * Class xapi_handler testing dummie class.
  38   *
  39   * @package core_xapi
  40   * @since      Moodle 3.9
  41   * @copyright  2020 Ferran Recio
  42   */
  43  class handler extends handler_base {
  44  
  45      /**
  46       * Convert statements to event.
  47       *
  48       * Convert a statement object into a Moodle xAPI Event. If a statement is accepted
  49       * by validate_statement the component must provide a event to handle that statement,
  50       * otherwise the statement will be rejected.
  51       *
  52       * @param statement $statement the statement object
  53       * @return \core\event\base|null a Moodle event to trigger
  54       */
  55      public function statement_to_event(statement $statement): ?base {
  56          global $USER;
  57  
  58          // Validate verb.
  59          $validvalues = [
  60                  'cook',
  61                  'http://adlnet.gov/expapi/verbs/answered'
  62              ];
  63          $verbid = $statement->get_verb_id();
  64          if (!in_array($verbid, $validvalues)) {
  65              return null;
  66          }
  67          // Validate object.
  68          $validvalues = [
  69                  'paella',
  70                  'http://adlnet.gov/expapi/activities/example'
  71              ];
  72          $activityid = $statement->get_activity_id();
  73          if (!in_array($activityid, $validvalues)) {
  74              return null;
  75          }
  76  
  77          if ($this->supports_group_actors()) {
  78              $users = $statement->get_all_users();
  79              // In most cases we can use $USER->id as the event userid but because
  80              // this is just a test class it checks first for $USER and, if not
  81              // present just pick the first one.
  82              $user = $users[$USER->id] ?? array_shift($users);
  83          } else {
  84              $user = $statement->get_user();
  85          }
  86  
  87          // Convert into a Moodle event.
  88          $minstatement = $statement->minify();
  89          $params = array(
  90              'other' => $minstatement,
  91              'context' => context_system::instance(),
  92              'userid' => $user->id,
  93          );
  94          return xapi_test_statement_post::create($params);
  95      }
  96  
  97      /**
  98       * Return true if group actor is enabled.
  99       *
 100       * NOTE: the use of a global is only for testing. We need to change
 101       * the behaviour from the PHPUnitTest to test all possible scenarios.
 102       *
 103       * Note: this method must be overridden by the plugins which want to
 104       * use groups in statements.
 105       *
 106       * @return bool
 107       */
 108      public function supports_group_actors(): bool {
 109          global $CFG;
 110          if (isset($CFG->xapitestforcegroupactors)) {
 111              return $CFG->xapitestforcegroupactors;
 112          }
 113          return parent::supports_group_actors();
 114      }
 115  }