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  /**
  18   * Group message sent event.
  19   *
  20   * @package    core
  21   * @copyright  2018 Jake Dallimore <jrhdallimore@gmail.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   * Group 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   *      - int conversationid: the id of the conversation in which the message was sent.
  37   * }
  38   *
  39   * @package    core
  40   * @copyright  2018 Jake Dallimore <jrhdallimore@gmail.com>
  41   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class group_message_sent extends base {
  44      /**
  45       * Create event using ids.
  46       * @param int $userfromid
  47       * @param int $conversationid
  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 $conversationid, 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([
  61              'objectid' => $messageid,
  62              'userid' => $userfromid,
  63              'context' => \context_system::instance(),
  64              'other' => [
  65                  'courseid' => $courseid,
  66                  'conversationid' => $conversationid
  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('eventgroupmessagesent', 'message');
  89      }
  90  
  91      /**
  92       * Returns relevant URL.
  93       *
  94       * @return \moodle_url
  95       */
  96      public function get_url() {
  97          // There currently isn't a way to link back from a 'group message sent' event to a conversation.
  98          // So, just return the user to the index page.
  99          return new \moodle_url('/message/index.php');
 100      }
 101  
 102      /**
 103       * Returns description of what happened.
 104       *
 105       * @return string
 106       */
 107      public function get_description() {
 108          $conversationid = $this->other['conversationid'];
 109  
 110          // Check if we are sending from a valid user.
 111          if (\core_user::is_real_user($this->userid)) {
 112  
 113              return "The user with id '$this->userid' sent a message with id '$this->objectid' to the conversation " .
 114                     "with id '$conversationid'.";
 115          }
 116  
 117          return "A message with id '$this->objectid' was sent by the system to the conversation with id '$conversationid'.";
 118      }
 119  
 120      /**
 121       * Custom validation.
 122       *
 123       * @throws \coding_exception
 124       * @return void
 125       */
 126      protected function validate_data() {
 127          parent::validate_data();
 128  
 129          if (!isset($this->other['courseid'])) {
 130              throw new \coding_exception('The \'courseid\' value must be set in other.');
 131          }
 132          if (!isset($this->other['conversationid'])) {
 133              throw new \coding_exception('The \'conversationid\' value must be set in other.');
 134          }
 135      }
 136  
 137      /**
 138       * Get the object this event maps to.
 139       *
 140       * @return array|string object id mapping.
 141       */
 142      public static function get_objectid_mapping() {
 143          return ['db' => 'messages', 'restore' => base::NOT_MAPPED];
 144      }
 145  
 146      /**
 147       * Get the item mappings for the 'other' fields for this event.
 148       *
 149       * @return array the array of other fields, mapped.
 150       */
 151      public static function get_other_mapping() {
 152          $othermapped = [];
 153          $othermapped['courseid'] = ['db' => 'course', 'restore' => base::NOT_MAPPED];
 154          $othermapped['conversationid'] = ['db' => 'message_conversations', 'restore' => base::NOT_MAPPED];
 155          return $othermapped;
 156      }
 157  }