MDL-63548 core_message: added mark_all_conversation_messages_as_read WS
This commit is contained in:
@@ -1065,6 +1065,15 @@ $functions = array(
|
||||
'ajax' => true,
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
|
||||
),
|
||||
'core_message_mark_all_conversation_messages_as_read' => array(
|
||||
'classname' => 'core_message_external',
|
||||
'methodname' => 'mark_all_conversation_messages_as_read',
|
||||
'classpath' => 'message/externallib.php',
|
||||
'description' => 'Mark all conversation messages as read for a given user',
|
||||
'type' => 'write',
|
||||
'ajax' => true,
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
|
||||
),
|
||||
'core_message_mark_message_read' => array(
|
||||
'classname' => 'core_message_external',
|
||||
'methodname' => 'mark_message_read',
|
||||
|
||||
@@ -2513,6 +2513,66 @@ class core_message_external extends external_api {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark all conversation messages as read parameters description.
|
||||
*
|
||||
* @return external_function_parameters
|
||||
* @since 3.6
|
||||
*/
|
||||
public static function mark_all_conversation_messages_as_read_parameters() {
|
||||
return new external_function_parameters(
|
||||
array(
|
||||
'userid' => new external_value(PARAM_INT, 'The user id who who we are marking the messages as read for'),
|
||||
'conversationid' =>
|
||||
new external_value(PARAM_INT, 'The conversation id who who we are marking the messages as read for')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark all conversation messages as read function.
|
||||
*
|
||||
* @param int $userid The user id of who we want to delete the conversation for
|
||||
* @param int $conversationid The id of the conversations
|
||||
* @since 3.6
|
||||
*/
|
||||
public static function mark_all_conversation_messages_as_read(int $userid, int $conversationid) {
|
||||
global $CFG;
|
||||
|
||||
// Check if messaging is enabled.
|
||||
if (empty($CFG->messaging)) {
|
||||
throw new moodle_exception('disabled', 'message');
|
||||
}
|
||||
|
||||
$params = array(
|
||||
'userid' => $userid,
|
||||
'conversationid' => $conversationid,
|
||||
);
|
||||
$params = self::validate_parameters(self::mark_all_conversation_messages_as_read_parameters(), $params);
|
||||
|
||||
$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_mark_all_messages_as_read($userid, $conversationid)) {
|
||||
\core_message\api::mark_all_messages_as_read($userid, $conversationid);
|
||||
} else {
|
||||
throw new moodle_exception('accessdenied', 'admin');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark all conversation messages as read return description.
|
||||
*
|
||||
* @return external_warnings
|
||||
* @since 3.6
|
||||
*/
|
||||
public static function mark_all_conversation_messages_as_read_returns() {
|
||||
return new external_warnings();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method parameters.
|
||||
*
|
||||
|
||||
@@ -3245,6 +3245,128 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
|
||||
$this->assertEquals(6, $DB->count_records('message_user_actions'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test marking all conversation messages as read with an invalid user.
|
||||
*/
|
||||
public function test_mark_all_conversation_messages_as_read_invalid_user_exception() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$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]);
|
||||
|
||||
$this->expectException('moodle_exception');
|
||||
core_message_external::mark_all_conversation_messages_as_read(-2132131, $conversationid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test marking all conversation messages as read without proper access.
|
||||
*/
|
||||
public function test_mark_all_conversation_messages_as_read_access_denied_exception() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$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]);
|
||||
|
||||
// User 3 is not in the conversation.
|
||||
$this->expectException('moodle_exception');
|
||||
core_message_external::mark_all_conversation_messages_as_read($user3->id, $conversationid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test marking all conversation messages as read for another user.
|
||||
*/
|
||||
public function test_mark_all_conversation_messages_as_read_wrong_user() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$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]);
|
||||
|
||||
// Can't mark the messages as read for user 2.
|
||||
$this->setUser($user1);
|
||||
$this->expectException('moodle_exception');
|
||||
core_message_external::mark_all_conversation_messages_as_read($user2->id, $conversationid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test marking all conversation messages as admin.
|
||||
*/
|
||||
public function test_mark_all_conversation_messages_as_admin() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$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]);
|
||||
|
||||
// Admin can do anything.
|
||||
$this->setAdminUser();
|
||||
core_message_external::mark_all_conversation_messages_as_read($user2->id, $conversationid);
|
||||
$this->assertEquals(2, $DB->count_records('message_user_actions'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test marking all conversation messages.
|
||||
*/
|
||||
public function test_mark_all_conversation_messages_as_read() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$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]);
|
||||
|
||||
// We are the user we want to mark the messages for and we are in the conversation, all good.
|
||||
$this->setUser($user1);
|
||||
core_message_external::mark_all_conversation_messages_as_read($user1->id, $conversationid);
|
||||
$this->assertEquals(2, $DB->count_records('message_user_actions'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting unread conversation count.
|
||||
*/
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$version = 2018101900.00; // YYYYMMDD = weekly release date of this DEV branch.
|
||||
$version = 2018101900.01; // YYYYMMDD = weekly release date of this DEV branch.
|
||||
// RR = release increments - 00 in DEV branches.
|
||||
// .XX = incremental changes.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user