Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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   * Message sent event.
  19   *
  20   * @package    core
  21   * @copyright  2014 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   * Message 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   * @since      Moodle 2.7
  40   * @copyright  2014 Mark Nelson <markn@moodle.com>
  41   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class message_sent extends base {
  44      /**
  45       * Create event using ids.
  46       * @param int $userfromid
  47       * @param int $usertoid
  48       * @param int $messageid
  49       * @param int $courseid course id the event is related with.
  50       * @return message_sent
  51       */
  52      public static function create_from_ids(int $userfromid, int $usertoid, int $messageid, int $courseid) {
  53          // We may be sending a message from the 'noreply' address, which means we are not actually sending a
  54          // message from a valid user. In this case, we will set the userid to 0.
  55          // Check if the userid is valid.
  56          if (!\core_user::is_real_user($userfromid)) {
  57              $userfromid = 0;
  58          }
  59  
  60          $event = self::create(array(
  61              'objectid' => $messageid,
  62              'userid' => $userfromid,
  63              'context' => \context_system::instance(),
  64              'relateduserid' => $usertoid,
  65              'other' => array(
  66                  'courseid' => $courseid
  67              )
  68          ));
  69  
  70          return $event;
  71      }
  72  
  73      /**
  74       * Init method.
  75       */
  76      protected function init() {
  77          $this->data['objecttable'] = 'messages';
  78          $this->data['crud'] = 'c';
  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('eventmessagesent', '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/index.php', array('user1' => $this->userid, 'user2' => $this->relateduserid));
  98      }
  99  
 100      /**
 101       * Returns description of what happened.
 102       *
 103       * @return string
 104       */
 105      public function get_description() {
 106          // Check if we are sending from a valid user.
 107          if (\core_user::is_real_user($this->userid)) {
 108              return "The user with id '$this->userid' sent a message to the user with id '$this->relateduserid'.";
 109          }
 110  
 111          return "A message was sent by the system to the user with id '$this->relateduserid'.";
 112      }
 113  
 114      /**
 115       * Custom validation.
 116       *
 117       * @throws \coding_exception
 118       * @return void
 119       */
 120      protected function validate_data() {
 121          parent::validate_data();
 122  
 123          if (!isset($this->relateduserid)) {
 124              throw new \coding_exception('The \'relateduserid\' must be set.');
 125          }
 126  
 127          if (!isset($this->other['courseid'])) {
 128              throw new \coding_exception('The \'courseid\' value must be set in other.');
 129          }
 130      }
 131  
 132      public static function get_objectid_mapping() {
 133          return array('db' => 'messages', 'restore' => base::NOT_MAPPED);
 134      }
 135  
 136      public static function get_other_mapping() {
 137          $othermapped = array();
 138          $othermapped['courseid'] = array('db' => 'course', 'restore' => base::NOT_MAPPED);
 139          return $othermapped;
 140      }
 141  }