/** * Deletes a message. * * This function does not verify any permissions. * * @param stdClass $message the message to delete * @param string $userid the user id of who we want to delete the message for (this may be done by the admin * but will still seem as if it was by the user) * @return bool */ function message_delete_message($message, $userid) { global $DB; // The column we want to alter. if ($message->useridfrom == $userid) { $coltimedeleted = 'timeuserfromdeleted'; } else if ($message->useridto == $userid) { $coltimedeleted = 'timeusertodeleted'; } else { return false; } // Don't update it if it's already been deleted. if ($message->$coltimedeleted > 0) { return false; } // Get the table we want to update. if (isset($message->timeread)) { $messagetable = 'message_read'; } else { $messagetable = 'message'; } // Mark the message as deleted. $updatemessage = new stdClass(); $updatemessage->id = $message->id; $updatemessage->$coltimedeleted = time(); $success = $DB->update_record($messagetable, $updatemessage); if ($success) { // Trigger event for deleting a message. \core\event\message_deleted::create_from_ids($message->useridfrom, $message->useridto, $userid, $messagetable, $message->id)->trigger(); } return $success; }
/** * Deletes a conversation. * * This function does not verify any permissions. * * @param int $userid The user id of who we want to delete the messages for (this may be done by the admin * but will still seem as if it was by the user) * @param int $otheruserid The id of the other user in the conversation * @return bool */ public static function delete_conversation($userid, $otheruserid) { global $DB; // We need to update the tables to mark all messages as deleted from and to the other user. This seems worse than it // is, that's because our DB structure splits messages into two tables (great idea, huh?) which causes code like this. // This won't be a particularly heavily used function (at least I hope not), so let's hope MDL-36941 gets worked on // soon for the sake of any developers' sanity when dealing with the messaging system. $now = time(); $sql = "UPDATE {message}\n SET timeuserfromdeleted = :time\n WHERE useridfrom = :userid\n AND useridto = :otheruserid\n AND notification = 0"; $DB->execute($sql, array('time' => $now, 'userid' => $userid, 'otheruserid' => $otheruserid)); $sql = "UPDATE {message}\n SET timeusertodeleted = :time\n WHERE useridto = :userid\n AND useridfrom = :otheruserid\n AND notification = 0"; $DB->execute($sql, array('time' => $now, 'userid' => $userid, 'otheruserid' => $otheruserid)); $sql = "UPDATE {message_read}\n SET timeuserfromdeleted = :time\n WHERE useridfrom = :userid\n AND useridto = :otheruserid\n AND notification = 0"; $DB->execute($sql, array('time' => $now, 'userid' => $userid, 'otheruserid' => $otheruserid)); $sql = "UPDATE {message_read}\n SET timeusertodeleted = :time\n WHERE useridto = :userid\n AND useridfrom = :otheruserid\n AND notification = 0"; $DB->execute($sql, array('time' => $now, 'userid' => $userid, 'otheruserid' => $otheruserid)); // Now we need to trigger events for these. if ($messages = helper::get_messages($userid, $otheruserid, $now)) { // Loop through and trigger a deleted event. foreach ($messages as $message) { $messagetable = 'message'; if (!empty($message->timeread)) { $messagetable = 'message_read'; } // Trigger event for deleting the message. \core\event\message_deleted::create_from_ids($message->useridfrom, $message->useridto, $userid, $messagetable, $message->id)->trigger(); } } return true; }