Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.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   * Message deleted event.
  19   *
  20   * @package    core
  21   * @copyright  2015 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 deleted event class.
  31   *
  32   * @property-read array $other {
  33   *      Extra information about event.
  34   *
  35   *      - int messageid: the id of the message.
  36   * }
  37   *
  38   * @package    core
  39   * @since      Moodle 3.0
  40   * @copyright  2015 Mark Nelson <markn@moodle.com>
  41   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class message_deleted extends base {
  44  
  45      /**
  46       * Create event using ids.
  47       *
  48       * @param int $userid the user who the we are deleting the message for.
  49       * @param int $userdeleting the user who deleted it (it's possible that an admin may delete a message on someones behalf)
  50       * @param int $messageid the id of the message that was deleted.
  51       * @param int $muaid The id in the message_user_actions table.
  52       * @return message_deleted
  53       */
  54      public static function create_from_ids(int $userid, int $userdeleting, int $messageid, int $muaid) : message_deleted {
  55          // We set the userid to the user who deleted the message, nothing to do
  56          // with whether or not they sent or received the message.
  57          $event = self::create(array(
  58              'objectid' => $muaid,
  59              'userid' => $userdeleting,
  60              'context' => \context_system::instance(),
  61              'relateduserid' => $userid,
  62              'other' => array(
  63                  'messageid' => $messageid,
  64              )
  65          ));
  66  
  67          return $event;
  68      }
  69  
  70      /**
  71       * Init method.
  72       */
  73      protected function init() {
  74          $this->data['objecttable'] = 'message_user_actions';
  75          $this->data['crud'] = 'c';
  76          $this->data['edulevel'] = self::LEVEL_OTHER;
  77      }
  78  
  79      /**
  80       * Returns localised general event name.
  81       *
  82       * @return string
  83       */
  84      public static function get_name() {
  85          return get_string('eventmessagedeleted', 'message');
  86      }
  87  
  88      /**
  89       * Returns description of what happened.
  90       *
  91       * @return string
  92       */
  93      public function get_description() {
  94          // This is for BC when the event used to take this value into account before group conversations.
  95          // We still want the same message to display for older events.
  96          if (isset($this->other['useridto'])) {
  97              // Check if the person who deleted the message received or sent it.
  98              if ($this->userid == $this->other['useridto']) {
  99                  $str = 'from';
 100              } else {
 101                  $str = 'to';
 102              }
 103  
 104              return "The user with id '$this->userid' deleted a message sent $str the user with id '$this->relateduserid'.";
 105          }
 106  
 107          $messageid = $this->other['messageid'];
 108  
 109          // Check if the user deleting the message was not the actual user we are deleting for.
 110          $str = "The user with id '$this->userid' deleted a message with id '$messageid'";
 111          if ($this->userid != $this->relateduserid) {
 112              $str .= " for the user with id '$this->relateduserid'";
 113          }
 114  
 115          return $str;
 116      }
 117  
 118      /**
 119       * Custom validation.
 120       *
 121       * @throws \coding_exception
 122       * @return void
 123       */
 124      protected function validate_data() {
 125          parent::validate_data();
 126  
 127          if (!isset($this->relateduserid)) {
 128              throw new \coding_exception('The \'relateduserid\' must be set.');
 129          }
 130  
 131          if (!isset($this->other['messageid'])) {
 132              throw new \coding_exception('The \'messageid\' value must be set in other.');
 133          }
 134      }
 135  
 136      public static function get_objectid_mapping() {
 137          return array('db' => 'message_user_actions', 'restore' => base::NOT_MAPPED);
 138      }
 139  
 140      public static function get_other_mapping() {
 141          // Messages are not backed up, so no need to map them on restore.
 142          $othermapped = [];
 143          $othermapped['messageid'] = ['db' => 'messages', 'restore' => base::NOT_MAPPED];
 144  
 145          return $othermapped;
 146      }
 147  }