[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/classes/event/ -> message_sent.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 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 messageid: the id of the message.
  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       * @return message_sent
  50       */
  51      public static function create_from_ids($userfromid, $usertoid, $messageid) {
  52          // We may be sending a message from the 'noreply' address, which means we are not actually sending a
  53          // message from a valid user. In this case, we will set the userid to 0.
  54          // Check if the userid is valid.
  55          if (!\core_user::is_real_user($userfromid)) {
  56              $userfromid = 0;
  57          }
  58  
  59          $event = self::create(array(
  60              'userid' => $userfromid,
  61              'context' => \context_system::instance(),
  62              'relateduserid' => $usertoid,
  63              'other' => array(
  64                  // In earlier versions it can either be the id in the 'message_read' or 'message' table.
  65                  // Now it is always the id from 'message' table. Please note that the record is still moved
  66                  // to the 'message_read' table later when message marked as read.
  67                  'messageid' => $messageid
  68              )
  69          ));
  70  
  71          return $event;
  72      }
  73  
  74      /**
  75       * Init method.
  76       */
  77      protected function init() {
  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       * Return legacy data for add_to_log().
 116       *
 117       * @return array
 118       */
 119      protected function get_legacy_logdata() {
 120          // The add_to_log function was only ever called when we sent a message from one user to another. We do not want
 121          // to return the legacy log data if we are sending a system message, so check that the userid is valid.
 122          if (\core_user::is_real_user($this->userid)) {
 123              return array(SITEID, 'message', 'write', 'index.php?user=' . $this->userid . '&id=' . $this->relateduserid .
 124                  '&history=1#m' . $this->other['messageid'], $this->userid);
 125          }
 126  
 127          return null;
 128      }
 129  
 130      /**
 131       * Custom validation.
 132       *
 133       * @throws \coding_exception
 134       * @return void
 135       */
 136      protected function validate_data() {
 137          parent::validate_data();
 138  
 139          if (!isset($this->relateduserid)) {
 140              throw new \coding_exception('The \'relateduserid\' must be set.');
 141          }
 142  
 143          if (!isset($this->other['messageid'])) {
 144              throw new \coding_exception('The \'messageid\' value must be set in other.');
 145          }
 146      }
 147  
 148      public static function get_objectid_mapping() {
 149          // Messages are not backed up, so no need to map them.
 150          return false;
 151      }
 152  
 153      public static function get_other_mapping() {
 154          // Messages are not backed up, so no need to map them on restore.
 155          $othermapped = array();
 156          // The messages table could vary for older events - so cannot be mapped.
 157          $othermapped['messageid'] = array('db' => base::NOT_MAPPED, 'restore' => base::NOT_MAPPED);
 158          return $othermapped;
 159      }
 160  }


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