Merge branch 'MDL-63548_master' of git://github.com/markn86/moodle
This commit is contained in:
+11
-1
@@ -1059,7 +1059,17 @@ $functions = array(
|
||||
'classname' => 'core_message_external',
|
||||
'methodname' => 'mark_all_messages_as_read',
|
||||
'classpath' => 'message/externallib.php',
|
||||
'description' => 'Mark all messages as read for a given user',
|
||||
'description' => '** DEPRECATED ** Please do not call this function any more.
|
||||
Mark all messages as read for a given user',
|
||||
'type' => 'write',
|
||||
'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),
|
||||
|
||||
@@ -829,6 +829,34 @@ class api {
|
||||
return $DB->count_records_sql($sql, [$user->id, self::MESSAGE_ACTION_READ, $user->id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a user can mark all messages as read.
|
||||
*
|
||||
* @param int $userid The user id of who we want to mark the messages for
|
||||
* @param int $conversationid The id of the conversation
|
||||
* @return bool true if user is permitted, false otherwise
|
||||
* @since 3.6
|
||||
*/
|
||||
public static function can_mark_all_messages_as_read(int $userid, int $conversationid) : bool {
|
||||
global $USER;
|
||||
|
||||
$systemcontext = \context_system::instance();
|
||||
|
||||
if (has_capability('moodle/site:readallmessages', $systemcontext)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!self::is_user_in_conversation($userid, $conversationid)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($USER->id == $userid) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks all messages being sent to a user in a particular conversation.
|
||||
*
|
||||
|
||||
+75
-3
@@ -2414,6 +2414,7 @@ class core_message_external extends external_api {
|
||||
/**
|
||||
* Mark all messages as read parameters description.
|
||||
*
|
||||
* @deprecated since 3.6
|
||||
* @return external_function_parameters
|
||||
* @since 3.2
|
||||
*/
|
||||
@@ -2429,14 +2430,15 @@ class core_message_external extends external_api {
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark all notifications as read function.
|
||||
* Mark all messages as read function.
|
||||
*
|
||||
* @since 3.2
|
||||
* @deprecated since 3.6
|
||||
* @throws invalid_parameter_exception
|
||||
* @throws moodle_exception
|
||||
* @param int $useridto the user id who received the message
|
||||
* @param int $useridfrom the user id who send the message. -10 or -20 for no-reply or support user
|
||||
* @return external_description
|
||||
* @since 3.2
|
||||
*/
|
||||
public static function mark_all_messages_as_read($useridto, $useridfrom) {
|
||||
global $USER, $CFG;
|
||||
@@ -2492,8 +2494,9 @@ class core_message_external extends external_api {
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark all notifications as read return description.
|
||||
* Mark all messages as read return description.
|
||||
*
|
||||
* @deprecated since 3.6
|
||||
* @return external_single_structure
|
||||
* @since 3.2
|
||||
*/
|
||||
@@ -2501,6 +2504,75 @@ class core_message_external extends external_api {
|
||||
return new external_value(PARAM_BOOL, 'True if the messages were marked read, false otherwise');
|
||||
}
|
||||
|
||||
/**
|
||||
* Marking the method as deprecated.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function mark_all_messages_as_read_is_deprecated() {
|
||||
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.
|
||||
*
|
||||
|
||||
@@ -1434,6 +1434,43 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
|
||||
$this->assertFalse($profile->iscontact);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests checking if a user can mark all messages as read.
|
||||
*/
|
||||
public function test_can_mark_all_messages_as_read() {
|
||||
// Set as the admin.
|
||||
$this->setAdminUser();
|
||||
|
||||
// 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 = 1;
|
||||
$this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
|
||||
$this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
|
||||
$this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
|
||||
$this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
|
||||
|
||||
$conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
|
||||
|
||||
// The admin can do anything.
|
||||
$this->assertTrue(\core_message\api::can_mark_all_messages_as_read($user1->id, $conversationid));
|
||||
|
||||
// Set as the user 1.
|
||||
$this->setUser($user1);
|
||||
|
||||
// The user can mark the messages as he is in the conversation.
|
||||
$this->assertTrue(\core_message\api::can_mark_all_messages_as_read($user1->id, $conversationid));
|
||||
|
||||
// User 1 can not mark the messages read for user 2.
|
||||
$this->assertFalse(\core_message\api::can_mark_all_messages_as_read($user2->id, $conversationid));
|
||||
|
||||
// This user is not a part of the conversation.
|
||||
$this->assertFalse(\core_message\api::can_mark_all_messages_as_read($user3->id, $conversationid));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests checking if a user can delete a conversation.
|
||||
*/
|
||||
|
||||
@@ -3251,6 +3251,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.
|
||||
*/
|
||||
|
||||
@@ -38,6 +38,8 @@ information provided here is intended especially for developers.
|
||||
- core_message_external::unblock_contacts(), please use core_message_external::unblock_user() instead.
|
||||
- core_message_external::create_contacts(), please use core_message_external::create_contact_request() instead.
|
||||
- core_message_external::delete_conversation(), please use core_message_external::delete_conversations_by_id() instead.
|
||||
- core_message_external::core_message_mark_all_messages_as_read(), please use
|
||||
core_message_external::core_message_mark_all_conversation_messages_as_read() instead.
|
||||
* The following function has been added for getting the privacy messaging preference:
|
||||
- get_user_privacy_messaging_preference()
|
||||
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$version = 2018102200.00; // YYYYMMDD = weekly release date of this DEV branch.
|
||||
$version = 2018102300.00; // 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