Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.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 agent (user) 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_user;
  29  use stdClass;
  30  
  31  defined('MOODLE_INTERNAL') || die();
  32  
  33  /**
  34   * Agent xAPI statement element representing a Moodle user.
  35   *
  36   * Agents can be used either as actor or object in a statement.
  37   *
  38   * @copyright  2020 Ferran Recio
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class item_agent extends item_actor {
  42  
  43      /** @var stdClass The user record of this actor. */
  44      protected $user;
  45  
  46      /**
  47       * Function to create an agent (user) from part of the xAPI statement.
  48       *
  49       * @param stdClass $data the original xAPI element
  50       * @param stdClass $user user record
  51       */
  52      protected function __construct(stdClass $data, stdClass $user) {
  53          parent::__construct($data);
  54          $this->user = $user;
  55      }
  56  
  57      /**
  58       * Function to create an item from part of the xAPI statement.
  59       *
  60       * @param stdClass $data the original xAPI element
  61       * @return item item_agentxAPI generated
  62       */
  63      public static function create_from_data(stdClass $data): item {
  64          global $CFG;
  65          if (!isset($data->objectType)) {
  66              throw new xapi_exception('Missing agent objectType');
  67          }
  68          if ($data->objectType != 'Agent') {
  69              throw new xapi_exception("Agent objectType must be 'Agent'");
  70          }
  71          if (isset($data->account) && isset($data->mbox)) {
  72              throw new xapi_exception("Agent cannot have more than one identifier");
  73          }
  74          $user = null;
  75          if (!empty($data->account)) {
  76              if ($data->account->homePage != $CFG->wwwroot) {
  77                  throw new xapi_exception("Invalid agent homePage '{$data->account->homePage}'");
  78              }
  79              if (!is_numeric($data->account->name)) {
  80                  throw new xapi_exception("Agent account name must be integer '{$data->account->name}' found");
  81              }
  82              $user = core_user::get_user($data->account->name);
  83              if (empty($user)) {
  84                  throw new xapi_exception("Inexistent agent '{$data->account->name}'");
  85              }
  86          }
  87          if (!empty($data->mbox)) {
  88              $mbox = str_replace('mailto:', '', $data->mbox);
  89              $user = core_user::get_user_by_email($mbox);
  90              if (empty($user)) {
  91                  throw new xapi_exception("Inexistent agent '{$data->mbox}'");
  92              }
  93          }
  94          if (empty($user)) {
  95              throw new xapi_exception("Unsupported agent definition");
  96          }
  97          return new self($data, $user);
  98      }
  99  
 100      /**
 101       * Create a item_agent from a existing user.
 102       *
 103       * @param stdClass $user A user record.
 104       * @return item_agent
 105       */
 106      public static function create_from_user(stdClass $user): item_agent {
 107          global $CFG;
 108  
 109          if (!isset($user->id)) {
 110              throw new xapi_exception("Missing user id");
 111          }
 112          $data = (object) [
 113              'objectType' => 'Agent',
 114              'account' => (object) [
 115                  'homePage' => $CFG->wwwroot,
 116                  'name' => $user->id,
 117              ],
 118          ];
 119          return new self($data, $user);
 120      }
 121  
 122      /**
 123       * Returns the moodle user represented by this item.
 124       *
 125       * @return stdClass user record
 126       */
 127      public function get_user(): stdClass {
 128          return $this->user;
 129      }
 130  
 131      /**
 132       * Return all users represented by this item.
 133       *
 134       * In this case the item is an agent so a single element array
 135       * will be returned always.
 136       *
 137       * @return array list of users
 138       */
 139      public function get_all_users(): array {
 140          return [$this->user->id => $this->user];
 141      }
 142  }