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 sent 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 sent event class.
  31   *
  32   * @property-read array $other {
  33   *      Extra information about event.
  34   *
  35   *      - int courseid: the id of the related course.
  36   * }
  37   *
  38   * @package    core
  39   * @copyright  2018 Mark Nelson <markn@moodle.com>
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class notification_sent extends base {
  43  
  44      /**
  45       * Create event using ids.
  46       *
  47       * @param int $userfromid
  48       * @param int $usertoid
  49       * @param int $notificationid
  50       * @param int $courseid course id the event is related with - SITEID if no relation exists.
  51       * @return notification_sent
  52       */
  53      public static function create_from_ids($userfromid, $usertoid, $notificationid, $courseid) {
  54          // We may be sending a notification from the 'noreply' address, which means we are not actually sending a
  55          // notification from a valid user. In this case, we will set the userid to 0.
  56          // Check if the userid is valid.
  57          if (!\core_user::is_real_user($userfromid)) {
  58              $userfromid = 0;
  59          }
  60  
  61          // If the courseid is null, then set it to the site id.
  62          if (is_null($courseid)) {
  63              $courseid = SITEID;
  64          }
  65  
  66          $event = self::create(
  67              [
  68                  'objectid' => $notificationid,
  69                  'userid' => $userfromid,
  70                  'context' => \context_system::instance(),
  71                  'relateduserid' => $usertoid,
  72                  'other' => [
  73                      'courseid' => $courseid
  74                  ]
  75              ]
  76          );
  77  
  78          return $event;
  79      }
  80  
  81      /**
  82       * Init method.
  83       */
  84      protected function init() {
  85          $this->data['objecttable'] = 'notifications';
  86          $this->data['crud'] = 'c';
  87          $this->data['edulevel'] = self::LEVEL_OTHER;
  88      }
  89  
  90      /**
  91       * Returns localised general event name.
  92       *
  93       * @return string
  94       */
  95      public static function get_name() {
  96          return get_string('eventnotificationsent', 'message');
  97      }
  98  
  99      /**
 100       * Returns relevant URL.
 101       *
 102       * @return \moodle_url
 103       */
 104      public function get_url() {
 105          return new \moodle_url('/message/output/popup/notifications.php', array('notificationid' => $this->objectid));
 106      }
 107  
 108      /**
 109       * Returns description of what happened.
 110       *
 111       * @return string
 112       */
 113      public function get_description() {
 114          // Check if we are sending from a valid user.
 115          if (\core_user::is_real_user($this->userid)) {
 116              return "The user with id '$this->userid' sent a notification to the user with id '$this->relateduserid'.";
 117          }
 118  
 119          return "A notification was sent by the system to the user with id '$this->relateduserid'.";
 120      }
 121  
 122      /**
 123       * Custom validation.
 124       *
 125       * @throws \coding_exception
 126       * @return void
 127       */
 128      protected function validate_data() {
 129          parent::validate_data();
 130  
 131          if (!isset($this->relateduserid)) {
 132              throw new \coding_exception('The \'relateduserid\' must be set.');
 133          }
 134  
 135          if (!isset($this->other['courseid'])) {
 136              throw new \coding_exception('The \'courseid\' value must be set in other.');
 137          }
 138      }
 139  
 140      public static function get_objectid_mapping() {
 141          return array('db' => 'notifications', 'restore' => base::NOT_MAPPED);
 142      }
 143  
 144      public static function get_other_mapping() {
 145          $othermapped = array();
 146          $othermapped['courseid'] = array('db' => 'course', 'restore' => base::NOT_MAPPED);
 147          return $othermapped;
 148      }
 149  }