Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.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   * Moodle net user profile class.
  19   *
  20   * @package    tool_moodlenet
  21   * @copyright  2020 Adrian Greeve <adrian@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_moodlenet;
  26  
  27  /**
  28   * A class to represent the moodlenet profile.
  29   *
  30   * @package    tool_moodlenet
  31   * @copyright  2020 Adrian Greeve <adrian@moodle.com>
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class moodlenet_user_profile {
  35  
  36      /** @var string $profile The full profile name. */
  37      protected $profile;
  38  
  39      /** @var int $userid The user ID that this profile belongs to. */
  40      protected $userid;
  41  
  42      /** @var string $username The username from $userprofile */
  43      protected $username;
  44  
  45      /** @var string $domain The domain from $domain */
  46      protected $domain;
  47  
  48      /**
  49       * Constructor method.
  50       *
  51       * @param string $userprofile The moodle net user profile string.
  52       * @param int $userid The user ID that this profile belongs to.
  53       */
  54      public function __construct(string $userprofile, int $userid) {
  55          $this->profile = $userprofile;
  56          $this->userid = $userid;
  57  
  58          $explodedprofile = explode('@', $this->profile);
  59          if (count($explodedprofile) === 2) {
  60              // It'll either be an email or WebFinger entry.
  61              $this->username = $explodedprofile[0];
  62              $this->domain = $explodedprofile[1];
  63          } else if (count($explodedprofile) === 3) {
  64              // We may have a profile link as MoodleNet gives to the user.
  65              $this->username = $explodedprofile[1];
  66              $this->domain = $explodedprofile[2];
  67          } else {
  68              throw new \moodle_exception('invalidmoodlenetprofile', 'tool_moodlenet');
  69          }
  70      }
  71  
  72      /**
  73       * Get the full moodle net profile.
  74       *
  75       * @return string The moodle net profile.
  76       */
  77      public function get_profile_name(): string {
  78          return $this->profile;
  79      }
  80  
  81      /**
  82       * Get the user ID that this profile belongs to.
  83       *
  84       * @return int The user ID.
  85       */
  86      public function get_userid(): int {
  87          return $this->userid;
  88      }
  89  
  90      /**
  91       * Get the username for this profile.
  92       *
  93       * @return string The username.
  94       */
  95      public function get_username(): string {
  96          return $this->username;
  97      }
  98  
  99      /**
 100       * Get the domain for this profile.
 101       *
 102       * @return string The domain.
 103       */
 104      public function get_domain(): string {
 105          return $this->domain;
 106      }
 107  }