MDL-64180 core_message: change stdClass to int for count requests param
authorMark Nelson <markn@moodle.com>
Fri, 23 Nov 2018 02:22:51 +0000 (10:22 +0800)
committerMark Nelson <markn@moodle.com>
Fri, 23 Nov 2018 03:34:40 +0000 (11:34 +0800)
This will be used by a new web service and we do not want to fetch
the user unnecessarily in the call.

Also renamed the method for consistency.

message/classes/api.php
message/lib.php
message/tests/api_test.php

index 86a22f8..13c1894 100644 (file)
@@ -2473,14 +2473,14 @@ class api {
     }
 
     /**
-     * Count how many contact requests the user has received.
+     * Returns the number of contact requests the user has received.
      *
-     * @param \stdClass $user The user to fetch contact requests for
+     * @param int $userid The ID of the user we want to return the number of received contact requests for
      * @return int The count
      */
-    public static function count_received_contact_requests(\stdClass $user) : int {
+    public static function get_received_contact_requests_count(int $userid) : int {
         global $DB;
-        return $DB->count_records('message_contact_requests', ['requesteduserid' => $user->id]);
+        return $DB->count_records('message_contact_requests', ['requesteduserid' => $userid]);
     }
 
     /**
index 53487a7..269bdb6 100644 (file)
@@ -805,7 +805,7 @@ function core_message_render_navbar_output(\renderer_base $renderer) {
     // Add the messages popover.
     if (!empty($CFG->messaging)) {
         $unreadcount = \core_message\api::count_unread_conversations($USER);
-        $requestcount = \core_message\api::count_received_contact_requests($USER);
+        $requestcount = \core_message\api::get_received_contact_requests_count($USER->id);
         $context = [
             'userid' => $USER->id,
             'unreadcount' => $unreadcount + $requestcount
@@ -848,7 +848,7 @@ function core_message_standard_after_main_region_html() {
     $usercontext = \context_user::instance($USER->id);
     $ufservice = \core_favourites\service_factory::get_service_for_user_context($usercontext);
     $favouriteconversationcount = $ufservice->count_favourites_by_type('core_message', 'message_conversations', $systemcontext);
-    $requestcount = \core_message\api::count_received_contact_requests($USER);
+    $requestcount = \core_message\api::get_received_contact_requests_count($USER->id);
     $contactscount = \core_message\api::count_contacts($USER->id);
 
     // Get the privacy settings options for being messaged.
index 9e29bd1..f868371 100644 (file)
@@ -4832,27 +4832,27 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
     }
 
     /**
-     * Test the count_received_contact_requests() function.
+     * Test the get_received_contact_requests_count() function.
      */
-    public function test_count_received_contact_requests() {
+    public function test_get_received_contact_requests_count() {
         $user1 = self::getDataGenerator()->create_user();
         $user2 = self::getDataGenerator()->create_user();
         $user3 = self::getDataGenerator()->create_user();
         $user4 = self::getDataGenerator()->create_user();
 
-        $this->assertEquals(0, \core_message\api::count_received_contact_requests($user1));
+        $this->assertEquals(0, \core_message\api::get_received_contact_requests_count($user1->id));
 
         \core_message\api::create_contact_request($user2->id, $user1->id);
 
-        $this->assertEquals(1, \core_message\api::count_received_contact_requests($user1));
+        $this->assertEquals(1, \core_message\api::get_received_contact_requests_count($user1->id));
 
         \core_message\api::create_contact_request($user3->id, $user1->id);
 
-        $this->assertEquals(2, \core_message\api::count_received_contact_requests($user1));
+        $this->assertEquals(2, \core_message\api::get_received_contact_requests_count($user1->id));
 
         \core_message\api::create_contact_request($user1->id, $user4->id);
         // Function should ignore sent requests.
-        $this->assertEquals(2, \core_message\api::count_received_contact_requests($user1));
+        $this->assertEquals(2, \core_message\api::get_received_contact_requests_count($user1->id));
     }
 
     /**