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.
   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  namespace factor_token\event;
  18  
  19  use stdClass;
  20  
  21  /**
  22   * Event for a token being created for a user.
  23   *
  24   * @package     factor_token
  25   * @author      Peter Burnett <peterburnett@catalyst-au.net>
  26   * @copyright   Catalyst IT
  27   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  class token_created extends \core\event\base {
  30  
  31      /**
  32       * Create instance of event.
  33       *
  34       * @param stdClass $user the User object of the User who had the token creeated.
  35       * @param array $state an array of the state of the token.
  36       *
  37       * @return \core\event\base the token_created_event event
  38       *
  39       * @throws \coding_exception
  40       */
  41      public static function token_created_event(stdClass $user, array $state): \core\event\base {
  42          $data = [
  43              'relateduserid' => $user->id,
  44              'context' => \context_user::instance($user->id),
  45              'other' => [
  46                  'userid' => $user->id,
  47                  'state' => json_encode($state),
  48              ],
  49          ];
  50  
  51          return self::create($data);
  52      }
  53  
  54      /**
  55       * Init method.
  56       *
  57       * @return void
  58       */
  59      protected function init(): void {
  60          $this->data['crud'] = 'c';
  61          $this->data['edulevel'] = self::LEVEL_OTHER;
  62      }
  63  
  64      /**
  65       * Returns description of what happened.
  66       *
  67       * @return string
  68       */
  69      public function get_description(): string {
  70          $info = json_decode($this->other['state']);
  71          $string = '<br>';
  72          foreach ($info as $name => $value) {
  73              if ($name === 'expiry') {
  74                  $value = userdate($value);
  75              }
  76  
  77              $string .= ucwords($name) . ': ' . $value . '<br>';
  78          }
  79  
  80          $data = new stdClass();
  81          $data->string = $string;
  82          $data->userid = $this->other['userid'];
  83          return get_string('tokenstoredindevice', 'factor_token', $data);
  84      }
  85  
  86      /**
  87       * Return localised event name.
  88       *
  89       * @return string
  90       * @throws \coding_exception
  91       */
  92      public static function get_name(): string {
  93          return get_string('event:token_created', 'factor_token');
  94      }
  95  }