return true;
}
+ /**
+ * Returns description of method parameters.
+ *
+ * @return external_function_parameters
+ * @since 3.6
+ */
+ public static function delete_conversation_by_id_parameters() {
+ return new external_function_parameters(
+ array(
+ 'userid' => new external_value(PARAM_INT, 'The user id of who we want to delete the conversation for'),
+ 'conversationid' => new external_value(PARAM_INT, 'The id of the conversation'),
+ )
+ );
+ }
+
+ /**
+ * Deletes a conversation.
+ *
+ * @param int $userid The user id of who we want to delete the conversation for
+ * @param int $conversationid The id of the conversation
+ * @return array
+ * @throws moodle_exception
+ * @since 3.6
+ */
+ public static function delete_conversation_by_id($userid, $conversationid) {
+ global $CFG;
+
+ // Check if private messaging between users is allowed.
+ if (empty($CFG->messaging)) {
+ throw new moodle_exception('disabled', 'message');
+ }
+
+ // Validate params.
+ $params = [
+ 'userid' => $userid,
+ 'conversationid' => $conversationid,
+ ];
+ $params = self::validate_parameters(self::delete_conversation_by_id_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, $conversationid)) {
+ \core_message\api::delete_conversation_by_id($user->id, $conversationid);
+ } else {
+ throw new moodle_exception("You do not have permission to delete the conversation '$conversationid'");
+ }
+
+ return [];
+ }
+
+ /**
+ * Returns description of method result value.
+ *
+ * @return external_description
+ * @since 3.6
+ */
+ public static function delete_conversation_by_id_returns() {
+ return new external_warnings();
+ }
+
/**
* Returns description of method parameters
*
core_message_external::delete_conversation($user1->id, $user2->id);
}
+ /**
+ * Test deleting conversation.
+ */
+ public function test_delete_conversation_by_id() {
+ global $DB;
+
+ $this->resetAfterTest(true);
+
+ // Create some users.
+ $user1 = self::getDataGenerator()->create_user();
+ $user2 = self::getDataGenerator()->create_user();
+
+ // The person wanting to delete the conversation.
+ $this->setUser($user1);
+
+ // Send some messages back and forth.
+ $time = time();
+ $m1id = $this->send_message($user1, $user2, 'Yo!', 0, $time);
+ $m2id = $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
+ $m3id = $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
+ $m4id = $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
+
+ $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
+
+ // Delete the conversation.
+ core_message_external::delete_conversation_by_id($user1->id, $conversationid);
+
+ $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC');
+ $this->assertCount(4, $muas);
+ // Sort by id.
+ ksort($muas);
+
+ $mua1 = array_shift($muas);
+ $mua2 = array_shift($muas);
+ $mua3 = array_shift($muas);
+ $mua4 = array_shift($muas);
+
+ $this->assertEquals($user1->id, $mua1->userid);
+ $this->assertEquals($m1id, $mua1->messageid);
+ $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua1->action);
+
+ $this->assertEquals($user1->id, $mua2->userid);
+ $this->assertEquals($m2id, $mua2->messageid);
+ $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua2->action);
+
+ $this->assertEquals($user1->id, $mua3->userid);
+ $this->assertEquals($m3id, $mua3->messageid);
+ $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua3->action);
+
+ $this->assertEquals($user1->id, $mua4->userid);
+ $this->assertEquals($m4id, $mua4->messageid);
+ $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua4->action);
+ }
+
+ /**
+ * Test deleting conversation as other user.
+ */
+ public function test_delete_conversation_by_id_as_other_user() {
+ global $DB;
+
+ $this->resetAfterTest(true);
+
+ $this->setAdminUser();
+
+ // Create some users.
+ $user1 = self::getDataGenerator()->create_user();
+ $user2 = self::getDataGenerator()->create_user();
+
+ // Send some messages back and forth.
+ $time = time();
+ $m1id = $this->send_message($user1, $user2, 'Yo!', 0, $time);
+ $m2id = $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
+ $m3id = $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
+ $m4id = $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
+
+ $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
+
+ // Delete the conversation.
+ core_message_external::delete_conversation_by_id($user1->id, $conversationid);
+
+ $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC');
+ $this->assertCount(4, $muas);
+ // Sort by id.
+ ksort($muas);
+
+ $mua1 = array_shift($muas);
+ $mua2 = array_shift($muas);
+ $mua3 = array_shift($muas);
+ $mua4 = array_shift($muas);
+
+ $this->assertEquals($user1->id, $mua1->userid);
+ $this->assertEquals($m1id, $mua1->messageid);
+ $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua1->action);
+
+ $this->assertEquals($user1->id, $mua2->userid);
+ $this->assertEquals($m2id, $mua2->messageid);
+ $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua2->action);
+
+ $this->assertEquals($user1->id, $mua3->userid);
+ $this->assertEquals($m3id, $mua3->messageid);
+ $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua3->action);
+
+ $this->assertEquals($user1->id, $mua4->userid);
+ $this->assertEquals($m4id, $mua4->messageid);
+ $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua4->action);
+ }
+
+ /**
+ * Test deleting conversation as other user without proper capability.
+ */
+ public function test_delete_conversation_by_id_as_other_user_without_cap() {
+ $this->resetAfterTest(true);
+
+ // Create some users.
+ $user1 = self::getDataGenerator()->create_user();
+ $user2 = self::getDataGenerator()->create_user();
+ $user3 = self::getDataGenerator()->create_user();
+
+ // Send some messages back and forth.
+ $time = time();
+ $this->send_message($user1, $user2, 'Yo!', 0, $time);
+ $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
+ $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
+ $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
+
+ $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
+
+ // The person wanting to delete the conversation.
+ $this->setUser($user3);
+
+ // Ensure an exception is thrown.
+ $this->expectException('moodle_exception');
+ core_message_external::delete_conversation_by_id($user1->id, $conversationid);
+ }
+
+ /**
+ * Test deleting conversation with messaging disabled.
+ */
+ public function test_delete_conversation_by_id_messaging_disabled() {
+ global $CFG;
+
+ $this->resetAfterTest(true);
+
+ // Create some users.
+ $user1 = self::getDataGenerator()->create_user();
+ $user2 = self::getDataGenerator()->create_user();
+
+ // Send some messages back and forth.
+ $time = time();
+ $this->send_message($user1, $user2, 'Yo!', 0, $time);
+ $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
+ $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
+ $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
+
+ $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
+
+ // The person wanting to delete the conversation.
+ $this->setUser($user1);
+
+ // Disable messaging.
+ $CFG->messaging = 0;
+
+ // Ensure an exception is thrown.
+ $this->expectException('moodle_exception');
+ core_message_external::delete_conversation_by_id($user1->id, $conversationid);
+ }
+
/**
* Test get message processor.
*/