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.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 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   * Statement group 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 stdClass;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  /**
  33   * Group item inside a xAPI statement.
  34   *
  35   * Only named groups are accepted (all groups must be real groups in the
  36   * platform) so anonymous groups will be rejected on creation. Groups can
  37   * be used as actor or as object inside a xAPI statement.
  38   *
  39   * @copyright  2020 Ferran Recio
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class item_group extends item_actor {
  43  
  44      /** @var array */
  45      protected $users;
  46  
  47      /** @var stdClass */
  48      protected $group;
  49  
  50      /**
  51       * Function to create an group from part of the xAPI statement.
  52       *
  53       * @param stdClass $data the original xAPI element
  54       * @param stdClass $group group record
  55       */
  56      protected function __construct(stdClass $data, stdClass $group) {
  57          parent::__construct($data);
  58          $this->group = $group;
  59          $this->users = groups_get_members($group->id);
  60          if (!$this->users) {
  61              $this->users = [];
  62          }
  63      }
  64  
  65      /**
  66       * Function to create an item from part of the xAPI statement.
  67       *
  68       * @param stdClass $data the original xAPI element
  69       * @return item item_group xAPI item generated
  70       */
  71      public static function create_from_data(stdClass $data): item {
  72          global $CFG;
  73          if (!isset($data->objectType)) {
  74              throw new xapi_exception('Missing group objectType');
  75          }
  76          if ($data->objectType != 'Group') {
  77              throw new xapi_exception("Group objectType must be 'Group'");
  78          }
  79          if (!isset($data->account)) {
  80              throw new xapi_exception("Missing Group account");
  81          }
  82          if ($data->account->homePage != $CFG->wwwroot) {
  83              throw new xapi_exception("Invalid group homePage '{$data->account->homePage}'");
  84          }
  85          if (!is_numeric($data->account->name)) {
  86              throw new xapi_exception("Agent account name must be integer '{$data->account->name}' found");
  87          }
  88          $group = groups_get_group($data->account->name);
  89          if (empty($group)) {
  90              throw new xapi_exception("Inexistent group '{$data->account->name}'");
  91          }
  92          return new self($data, $group);
  93      }
  94  
  95      /**
  96       * Create a item_group from a existing group.
  97       *
  98       * @param stdClass $group A group record.
  99       * @return item_group
 100       */
 101      public static function create_from_group(stdClass $group): item_group {
 102          global $CFG;
 103  
 104          if (!isset($group->id)) {
 105              throw new xapi_exception("Missing group id");
 106          }
 107          $data = (object) [
 108              'objectType' => 'Group',
 109              'account' => (object) [
 110                  'homePage' => $CFG->wwwroot,
 111                  'name' => $group->id,
 112              ],
 113          ];
 114          return new self($data, $group);
 115      }
 116  
 117      /**
 118       * Returns the moodle user represented by this item.
 119       *
 120       * This is a group item. To avoid security problems this method
 121       * thorws an exception when is called from a item_group class.
 122       *
 123       * @throws xapi_exception get_user must not be called from an item_group
 124       * @return stdClass user record
 125       */
 126      public function get_user(): stdClass {
 127          throw new xapi_exception("Group statements cannot be used as a individual user");
 128      }
 129  
 130      /**
 131       * Return all users from the group represented by this item.
 132       *
 133       * @return array group users
 134       */
 135      public function get_all_users(): array {
 136          return $this->users;
 137      }
 138  
 139      /**
 140       * Return the moodle group represented by this item.
 141       *
 142       * @return stdClass a group record
 143       */
 144      public function get_group(): stdClass {
 145          return $this->group;
 146      }
 147  }