/** * Tests checking if a user can delete a conversation. */ public function test_can_delete_conversation() { // Set as the admin. $this->setAdminUser(); // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); // The admin can do anything. $this->assertTrue(\core_message\api::can_delete_conversation($user1->id)); // Set as the user 1. $this->setUser($user1); // They can delete their own messages. $this->assertTrue(\core_message\api::can_delete_conversation($user1->id)); // They can't delete someone elses. $this->assertFalse(\core_message\api::can_delete_conversation($user2->id)); }
/** * Deletes a conversation. * * @param int $userid The user id of who we want to delete the conversation for * @param int $otheruserid The user id of the other user in the conversation * @return array * @throws moodle_exception * @since 3.2 */ public static function delete_conversation($userid, $otheruserid) { global $CFG; // Check if private messaging between users is allowed. if (empty($CFG->messaging)) { throw new moodle_exception('disabled', 'message'); } // Warnings array, it can be empty at the end but is mandatory. $warnings = array(); // Validate params. $params = array('userid' => $userid, 'otheruserid' => $otheruserid); $params = self::validate_parameters(self::delete_conversation_parameters(), $params); // Validate context. $context = context_system::instance(); self::validate_context($context); $user = core_user::get_user($params['userid'], '*', MUST_EXIST); core_user::require_active_user($user); if (\core_message\api::can_delete_conversation($user->id)) { $status = \core_message\api::delete_conversation($user->id, $otheruserid); } else { throw new moodle_exception('You do not have permission to delete messages'); } $results = array('status' => $status, 'warnings' => $warnings); return $results; }