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   * Notification viewed event.
  19   *
  20   * @package    core
  21   * @copyright  2018 Mark Nelson <markn@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core\event;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Notification viewed event class.
  31   *
  32   * @package    core
  33   * @copyright  2018 Mark Nelson <markn@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class notification_viewed extends base {
  37  
  38      /**
  39       * Create event using ids.
  40       *
  41       * @param int $userfromid
  42       * @param int $usertoid
  43       * @param int $notificationid
  44       * @return notification_viewed
  45       */
  46      public static function create_from_ids($userfromid, $usertoid, $notificationid) {
  47          // We may be sending a notification from the 'noreply' address, which means we are not actually sending a
  48          // notification from a valid user. In this case, we will set the userid to 0.
  49          // Check if the userid is valid.
  50          if (!\core_user::is_real_user($userfromid)) {
  51              $userfromid = 0;
  52          }
  53  
  54          // Get the context for the user who received the notification.
  55          $context = \context_user::instance($usertoid, IGNORE_MISSING);
  56          // If the user no longer exists the context value will be false, in this case use the system context.
  57          if ($context === false) {
  58              $context = \context_system::instance();
  59          }
  60  
  61          $event = self::create(
  62              [
  63                  'objectid' => $notificationid,
  64                  'userid' => $usertoid, // Using the user who read the notification as they are the ones performing the action.
  65                  'context' => $context,
  66                  'relateduserid' => $userfromid
  67              ]
  68          );
  69  
  70          return $event;
  71      }
  72  
  73      /**
  74       * Init method.
  75       */
  76      protected function init() {
  77          $this->data['objecttable'] = 'notifications';
  78          $this->data['crud'] = 'u';
  79          $this->data['edulevel'] = self::LEVEL_OTHER;
  80      }
  81  
  82      /**
  83       * Returns localised general event name.
  84       *
  85       * @return string
  86       */
  87      public static function get_name() {
  88          return get_string('eventnotificationviewed', 'message');
  89      }
  90  
  91      /**
  92       * Returns relevant URL.
  93       *
  94       * @return \moodle_url
  95       */
  96      public function get_url() {
  97          return new \moodle_url('/message/output/popup/notifications.php', array('notificationid' => $this->objectid));
  98      }
  99  
 100      /**
 101       * Returns description of what happened.
 102       *
 103       * @return string
 104       */
 105      public function get_description() {
 106          return "The user with id '$this->userid' read a notification from the user with id '$this->relateduserid'.";
 107      }
 108  
 109      /**
 110       * Custom validation.
 111       *
 112       * @throws \coding_exception
 113       * @return void
 114       */
 115      protected function validate_data() {
 116          parent::validate_data();
 117  
 118          if (!isset($this->relateduserid)) {
 119              throw new \coding_exception('The \'relateduserid\' must be set.');
 120          }
 121      }
 122  
 123      public static function get_objectid_mapping() {
 124          return array('db' => 'notifications', 'restore' => base::NOT_MAPPED);
 125      }
 126  }