From 26dca05d02bbdf2aed9d8c11eaa39ff686c4f088 Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Tue, 15 Nov 2016 17:24:29 +0800 Subject: [PATCH] MDL-56407 message: Indicate user is blocked in message area --- lib/deprecatedlib.php | 6 +++- message/classes/api.php | 24 +++++++------ .../classes/output/messagearea/messages.php | 3 ++ message/externallib.php | 3 +- .../message_area_messages_area.mustache | 36 +++++++++++++++++++ message/tests/api_test.php | 6 ++-- 6 files changed, 62 insertions(+), 16 deletions(-) diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index 0c24640bab2..79775c89f73 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -5616,7 +5616,11 @@ function message_is_user_blocked($recipient, $sender = null) { debugging('message_is_user_blocked() is deprecated and is no longer used, please use \core_message\api::is_user_blocked() instead.', DEBUG_DEVELOPER); - return \core_message\api::is_user_blocked($recipient, $sender); + $senderid = null; + if ($sender !== null && isset($sender->id)) { + $senderid = $sender->id; + } + return \core_message\api::is_user_blocked($recipient->id, $senderid); } /** diff --git a/message/classes/api.php b/message/classes/api.php index eabe7d7379b..0d90dae5f71 100644 --- a/message/classes/api.php +++ b/message/classes/api.php @@ -601,8 +601,12 @@ class api { return false; } + $senderid = null; + if ($sender !== null && isset($sender->id)) { + $senderid = $sender->id; + } // The recipient has specifically blocked this sender. - if (self::is_user_blocked($recipient, $sender)) { + if (self::is_user_blocked($recipient->id, $senderid)) { return false; } @@ -648,27 +652,25 @@ class api { * Note: This function will always return false if the sender has the * readallmessages capability at the system context level. * - * @param object $recipient User object. - * @param object $sender User object. + * @param int $recipientid User ID of the recipient. + * @param int $senderid User ID of the sender. * @return bool true if $sender is blocked, false otherwise. */ - public static function is_user_blocked($recipient, $sender = null) { + public static function is_user_blocked($recipientid, $senderid = null) { global $USER, $DB; - if (is_null($sender)) { + if (is_null($senderid)) { // The message is from the logged in user, unless otherwise specified. - $sender = $USER; + $senderid = $USER->id; } $systemcontext = \context_system::instance(); - if (has_capability('moodle/site:readallmessages', $systemcontext, $sender)) { + if (has_capability('moodle/site:readallmessages', $systemcontext, $senderid)) { return false; } - if ($contact = $DB->get_record('message_contacts', array('userid' => $recipient->id, 'contactid' => $sender->id))) { - if ($contact->blocked) { - return true; - } + if ($DB->get_field('message_contacts', 'blocked', ['userid' => $recipientid, 'contactid' => $senderid])) { + return true; } return false; diff --git a/message/classes/output/messagearea/messages.php b/message/classes/output/messagearea/messages.php index 368d1d08ec6..e967ec15dcd 100644 --- a/message/classes/output/messagearea/messages.php +++ b/message/classes/output/messagearea/messages.php @@ -26,6 +26,7 @@ namespace core_message\output\messagearea; defined('MOODLE_INTERNAL') || die(); +use core_message\api; use renderable; use templatable; @@ -95,6 +96,8 @@ class messages implements templatable, renderable { $data->messages[] = $message->export_for_template($output); } + $data->isblocked = api::is_user_blocked($this->currentuserid, $this->otheruserid); + return $data; } } diff --git a/message/externallib.php b/message/externallib.php index 2ecaf66b101..1d394bcac98 100644 --- a/message/externallib.php +++ b/message/externallib.php @@ -970,7 +970,8 @@ class core_message_external extends external_api { 'isonline' => new external_value(PARAM_BOOL, 'The user\'s online status'), 'messages' => new external_multiple_structure( self::get_messagearea_message_structure() - ) + ), + 'isblocked' => new external_value(PARAM_BOOL, 'Is this user blocked by the current user?', VALUE_DEFAULT, false), ) ); } diff --git a/message/templates/message_area_messages_area.mustache b/message/templates/message_area_messages_area.mustache index acfa4494a37..ebafaec1c79 100644 --- a/message/templates/message_area_messages_area.mustache +++ b/message/templates/message_area_messages_area.mustache @@ -14,6 +14,37 @@ You should have received a copy of the GNU General Public License along with Moodle. If not, see . }} +{{! + @template core_message/message_area_messages_area + + Messages area template. + + Classes required for JS: + * none + + Data attributes required for JS: + * none + + Context variables required for this template: + * isonline - boolean + * isblocked - boolean + * otheruserid - int + * otheruserfullname - string + * messages - array of messages + + Example context (json): + { + "isonline": true, + "isblocked": true, + "otheruserid": 1, + "otheruserfullname": "Sam Student", + "messages": [ + { + "text": "Hello there!" + } + ] + } +}} {{#otheruserid}}
@@ -25,6 +56,11 @@
+ {{#isblocked}} + + {{#pix}} t/block, core, {{#str}} contactblocked, message {{/str}} {{/pix}} + + {{/isblocked}}
{{#str}} offline, message {{/str}} diff --git a/message/tests/api_test.php b/message/tests/api_test.php index 67d898ed569..803ae64b30c 100644 --- a/message/tests/api_test.php +++ b/message/tests/api_test.php @@ -839,13 +839,13 @@ class core_message_api_testcase extends core_message_messagelib_testcase { $this->setUser($user1); // User shouldn't be blocked. - $this->assertFalse(\core_message\api::is_user_blocked($user1, $user2)); + $this->assertFalse(\core_message\api::is_user_blocked($user1->id, $user2->id)); // Block the user. message_block_contact($user2->id); // User should be blocked. - $this->assertTrue(\core_message\api::is_user_blocked($user1, $user2)); + $this->assertTrue(\core_message\api::is_user_blocked($user1->id, $user2->id)); } /** @@ -865,7 +865,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase { $this->setAdminUser(); // As the admin you should still be able to send messages to the user. - $this->assertFalse(\core_message\api::is_user_blocked($user1)); + $this->assertFalse(\core_message\api::is_user_blocked($user1->id)); } /*