[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/classes/event/ -> message_deleted.php (source)

   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   *      - string $messagetable: the table we marked the message as deleted from (message/message_read).
  36   *      - int messageid: the id of the message.
  37   *      - int useridfrom: the id of the user who received the message.
  38   *      - int useridto: the id of the user who sent the message.
  39   * }
  40   *
  41   * @package    core
  42   * @since      Moodle 3.0
  43   * @copyright  2015 Mark Nelson <markn@moodle.com>
  44   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  45   */
  46  class message_deleted extends base {
  47  
  48      /**
  49       * Create event using ids.
  50       *
  51       * @param int $userfromid the user who the message was from.
  52       * @param int $usertoid the user who the message was sent to.
  53       * @param int $userdeleted the user who deleted it.
  54       * @param string $messagetable the table we are marking the message as deleted in.
  55       * @param int $messageid the id of the message that was deleted.
  56       * @return message_deleted
  57       */
  58      public static function create_from_ids($userfromid, $usertoid, $userdeleted, $messagetable, $messageid) {
  59          // Check who was deleting the message.
  60          if ($userdeleted == $userfromid) {
  61              $relateduserid = $usertoid;
  62          } else {
  63              $relateduserid = $userfromid;
  64          }
  65  
  66          // We set the userid to the user who deleted the message, nothing to do
  67          // with whether or not they sent or received the message.
  68          $event = self::create(array(
  69              'userid' => $userdeleted,
  70              'context' => \context_system::instance(),
  71              'relateduserid' => $relateduserid,
  72              'other' => array(
  73                  'messagetable' => $messagetable,
  74                  'messageid' => $messageid,
  75                  'useridfrom' => $userfromid,
  76                  'useridto' => $usertoid
  77              )
  78          ));
  79  
  80          return $event;
  81      }
  82  
  83      /**
  84       * Init method.
  85       */
  86      protected function init() {
  87          $this->data['crud'] = 'u';
  88          $this->data['edulevel'] = self::LEVEL_OTHER;
  89      }
  90  
  91      /**
  92       * Returns localised general event name.
  93       *
  94       * @return string
  95       */
  96      public static function get_name() {
  97          return get_string('eventmessagedeleted', 'message');
  98      }
  99  
 100      /**
 101       * Returns description of what happened.
 102       *
 103       * @return string
 104       */
 105      public function get_description() {
 106          // Check if the person who deleted the message received or sent it.
 107          if ($this->userid == $this->other['useridto']) {
 108              $str = 'from';
 109          } else {
 110              $str = 'to';
 111          }
 112  
 113          return "The user with id '$this->userid' deleted a message sent $str the user with id '$this->relateduserid'.";
 114      }
 115  
 116      /**
 117       * Custom validation.
 118       *
 119       * @throws \coding_exception
 120       * @return void
 121       */
 122      protected function validate_data() {
 123          parent::validate_data();
 124  
 125          if (!isset($this->relateduserid)) {
 126              throw new \coding_exception('The \'relateduserid\' must be set.');
 127          }
 128  
 129          if (!isset($this->other['messagetable'])) {
 130              throw new \coding_exception('The \'messagetable\' value must be set in other.');
 131          }
 132  
 133          if (!isset($this->other['messageid'])) {
 134              throw new \coding_exception('The \'messageid\' value must be set in other.');
 135          }
 136  
 137          if (!isset($this->other['useridfrom'])) {
 138              throw new \coding_exception('The \'useridfrom\' value must be set in other.');
 139          }
 140  
 141          if (!isset($this->other['useridto'])) {
 142              throw new \coding_exception('The \'useridto\' value must be set in other.');
 143          }
 144      }
 145  
 146      public static function get_other_mapping() {
 147          // Messages are not backed up, so no need to map them on restore.
 148          $othermapped = array();
 149          // The messageid table varies so it cannot be mapped.
 150          $othermapped['messageid'] = array('db' => base::NOT_MAPPED, 'restore' => base::NOT_MAPPED);
 151          $othermapped['useridfrom'] = array('db' => 'user', 'restore' => base::NOT_MAPPED);
 152          $othermapped['useridto'] = array('db' => 'user', 'restore' => base::NOT_MAPPED);
 153          return $othermapped;
 154      }
 155  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1