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.
*
$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.
*/