Skip to content

Commit

Permalink
MDL-36941 core: alter events to use new table structure
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Mar 23, 2018
1 parent ee14412 commit 4cd4398
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 32 deletions.
20 changes: 9 additions & 11 deletions lib/classes/event/message_deleted.php
Expand Up @@ -32,7 +32,6 @@
* @property-read array $other {
* Extra information about event.
*
* - string $messagetable: the table we marked the message as deleted from (message/message_read).
* - int messageid: the id of the message.
* - int useridfrom: the id of the user who received the message.
* - int useridto: the id of the user who sent the message.
Expand All @@ -51,11 +50,11 @@ class message_deleted extends base {
* @param int $userfromid the user who the message was from.
* @param int $usertoid the user who the message was sent to.
* @param int $userdeleted the user who deleted it.
* @param string $messagetable the table we are marking the message as deleted in.
* @param int $messageid the id of the message that was deleted.
* @param int $muaid The id in the message_user_actions table
* @return message_deleted
*/
public static function create_from_ids($userfromid, $usertoid, $userdeleted, $messagetable, $messageid) {
public static function create_from_ids($userfromid, $usertoid, $userdeleted, $messageid, $muaid) {
// Check who was deleting the message.
if ($userdeleted == $userfromid) {
$relateduserid = $usertoid;
Expand All @@ -66,11 +65,11 @@ public static function create_from_ids($userfromid, $usertoid, $userdeleted, $me
// We set the userid to the user who deleted the message, nothing to do
// with whether or not they sent or received the message.
$event = self::create(array(
'objectid' => $muaid,
'userid' => $userdeleted,
'context' => \context_system::instance(),
'relateduserid' => $relateduserid,
'other' => array(
'messagetable' => $messagetable,
'messageid' => $messageid,
'useridfrom' => $userfromid,
'useridto' => $usertoid
Expand All @@ -84,7 +83,8 @@ public static function create_from_ids($userfromid, $usertoid, $userdeleted, $me
* Init method.
*/
protected function init() {
$this->data['crud'] = 'u';
$this->data['objecttable'] = 'message_user_actions';
$this->data['crud'] = 'c';
$this->data['edulevel'] = self::LEVEL_OTHER;
}

Expand Down Expand Up @@ -126,10 +126,6 @@ protected function validate_data() {
throw new \coding_exception('The \'relateduserid\' must be set.');
}

if (!isset($this->other['messagetable'])) {
throw new \coding_exception('The \'messagetable\' value must be set in other.');
}

if (!isset($this->other['messageid'])) {
throw new \coding_exception('The \'messageid\' value must be set in other.');
}
Expand All @@ -143,11 +139,13 @@ protected function validate_data() {
}
}

public static function get_objectid_mapping() {
return array('db' => 'message_user_actions', 'restore' => base::NOT_MAPPED);
}

public static function get_other_mapping() {
// Messages are not backed up, so no need to map them on restore.
$othermapped = array();
// The messageid table varies so it cannot be mapped.
$othermapped['messageid'] = array('db' => base::NOT_MAPPED, 'restore' => base::NOT_MAPPED);
$othermapped['useridfrom'] = array('db' => 'user', 'restore' => base::NOT_MAPPED);
$othermapped['useridto'] = array('db' => 'user', 'restore' => base::NOT_MAPPED);
return $othermapped;
Expand Down
22 changes: 6 additions & 16 deletions lib/classes/event/message_sent.php
Expand Up @@ -32,7 +32,6 @@
* @property-read array $other {
* Extra information about event.
*
* - int messageid: the id of the message.
* - int courseid: the id of the related course.
* }
*
Expand Down Expand Up @@ -69,14 +68,11 @@ public static function create_from_ids($userfromid, $usertoid, $messageid, $cour
}

$event = self::create(array(
'objectid' => $messageid,
'userid' => $userfromid,
'context' => \context_system::instance(),
'relateduserid' => $usertoid,
'other' => array(
// In earlier versions it can either be the id in the 'message_read' or 'message' table.
// Now it is always the id from 'message' table. Please note that the record is still moved
// to the 'message_read' table later when message marked as read.
'messageid' => $messageid,
'courseid' => $courseid
)
));
Expand All @@ -88,6 +84,7 @@ public static function create_from_ids($userfromid, $usertoid, $messageid, $cour
* Init method.
*/
protected function init() {
$this->data['objecttable'] = 'messages';
$this->data['crud'] = 'c';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
Expand Down Expand Up @@ -133,8 +130,9 @@ protected function get_legacy_logdata() {
// The add_to_log function was only ever called when we sent a message from one user to another. We do not want
// to return the legacy log data if we are sending a system message, so check that the userid is valid.
if (\core_user::is_real_user($this->userid)) {
$messageid = $this->other['messageid'] ?? $this->objectid; // For BC we may have 'messageid' in other.
return array(SITEID, 'message', 'write', 'index.php?user=' . $this->userid . '&id=' . $this->relateduserid .
'&history=1#m' . $this->other['messageid'], $this->userid);
'&history=1#m' . $messageid, $this->userid);
}

return null;
Expand All @@ -153,26 +151,18 @@ protected function validate_data() {
throw new \coding_exception('The \'relateduserid\' must be set.');
}

if (!isset($this->other['messageid'])) {
throw new \coding_exception('The \'messageid\' value must be set in other.');
}

if (!isset($this->other['courseid'])) {
throw new \coding_exception('The \'courseid\' value must be set in other.');
}
}

public static function get_objectid_mapping() {
// Messages are not backed up, so no need to map them.
return false;
return array('db' => 'messages', 'restore' => base::NOT_MAPPED);
}

public static function get_other_mapping() {
// Messages are not backed up, so no need to map them on restore.
$othermapped = array();
// The messages table could vary for older events - so cannot be mapped.
$othermapped['messageid'] = array('db' => base::NOT_MAPPED, 'restore' => base::NOT_MAPPED);
$othermapped['courseid'] = array('db' => base::NOT_MAPPED, 'restore' => base::NOT_MAPPED);
$othermapped['courseid'] = array('db' => 'course', 'restore' => base::NOT_MAPPED);
return $othermapped;
}
}
8 changes: 3 additions & 5 deletions lib/classes/event/message_viewed.php
Expand Up @@ -46,7 +46,7 @@ class message_viewed extends base {
* Init method.
*/
protected function init() {
$this->data['objecttable'] = 'message_read';
$this->data['objecttable'] = 'message_user_actions';
$this->data['crud'] = 'c';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
Expand Down Expand Up @@ -97,15 +97,13 @@ protected function validate_data() {
}

public static function get_objectid_mapping() {
// Messages are not backed up, so no need to map them.
return array('db' => 'message_read', 'restore' => base::NOT_MAPPED);
return array('db' => 'message_user_actions', 'restore' => base::NOT_MAPPED);
}

public static function get_other_mapping() {
// Messages are not backed up, so no need to map them on restore.
$othermapped = array();
// The messages table could vary for older events - so cannot be mapped.
$othermapped['messageid'] = array('db' => base::NOT_MAPPED, 'restore' => base::NOT_MAPPED);
$othermapped['messageid'] = array('db' => 'messages', 'restore' => base::NOT_MAPPED);
return $othermapped;
}
}

0 comments on commit 4cd4398

Please sign in to comment.