MDL-63548 core_message: added api::can_mark_all_messages_as_read
authorMark Nelson <markn@moodle.com>
Mon, 15 Oct 2018 12:15:04 +0000 (20:15 +0800)
committerMark Nelson <markn@moodle.com>
Tue, 23 Oct 2018 01:52:14 +0000 (09:52 +0800)
message/classes/api.php
message/tests/api_test.php

index d9b507e..90e28c9 100644 (file)
@@ -767,6 +767,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.
      *
index 8be0f3f..907346e 100644 (file)
@@ -1136,6 +1136,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.
      */