Merge branch 'MDL-56407-master-2' of git://github.com/junpataleta/moodle

This commit is contained in:
Andrew Nicols 2016-11-22 09:06:09 +08:00
commit d10ca75b8c
6 changed files with 62 additions and 16 deletions

View file

@ -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 debugging('message_is_user_blocked() is deprecated and is no longer used, please use
\core_message\api::is_user_blocked() instead.', DEBUG_DEVELOPER); \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);
} }
/** /**

View file

@ -616,8 +616,12 @@ class api {
return false; return false;
} }
$senderid = null;
if ($sender !== null && isset($sender->id)) {
$senderid = $sender->id;
}
// The recipient has specifically blocked this sender. // The recipient has specifically blocked this sender.
if (self::is_user_blocked($recipient, $sender)) { if (self::is_user_blocked($recipient->id, $senderid)) {
return false; return false;
} }
@ -663,27 +667,25 @@ class api {
* Note: This function will always return false if the sender has the * Note: This function will always return false if the sender has the
* readallmessages capability at the system context level. * readallmessages capability at the system context level.
* *
* @param object $recipient User object. * @param int $recipientid User ID of the recipient.
* @param object $sender User object. * @param int $senderid User ID of the sender.
* @return bool true if $sender is blocked, false otherwise. * @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; global $USER, $DB;
if (is_null($sender)) { if (is_null($senderid)) {
// The message is from the logged in user, unless otherwise specified. // The message is from the logged in user, unless otherwise specified.
$sender = $USER; $senderid = $USER->id;
} }
$systemcontext = \context_system::instance(); $systemcontext = \context_system::instance();
if (has_capability('moodle/site:readallmessages', $systemcontext, $sender)) { if (has_capability('moodle/site:readallmessages', $systemcontext, $senderid)) {
return false; return false;
} }
if ($contact = $DB->get_record('message_contacts', array('userid' => $recipient->id, 'contactid' => $sender->id))) { if ($DB->get_field('message_contacts', 'blocked', ['userid' => $recipientid, 'contactid' => $senderid])) {
if ($contact->blocked) { return true;
return true;
}
} }
return false; return false;

View file

@ -26,6 +26,7 @@ namespace core_message\output\messagearea;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
use core_message\api;
use renderable; use renderable;
use templatable; use templatable;
@ -95,6 +96,8 @@ class messages implements templatable, renderable {
$data->messages[] = $message->export_for_template($output); $data->messages[] = $message->export_for_template($output);
} }
$data->isblocked = api::is_user_blocked($this->currentuserid, $this->otheruserid);
return $data; return $data;
} }
} }

View file

@ -996,7 +996,8 @@ class core_message_external extends external_api {
'isonline' => new external_value(PARAM_BOOL, 'The user\'s online status'), 'isonline' => new external_value(PARAM_BOOL, 'The user\'s online status'),
'messages' => new external_multiple_structure( 'messages' => new external_multiple_structure(
self::get_messagearea_message_structure() self::get_messagearea_message_structure()
) ),
'isblocked' => new external_value(PARAM_BOOL, 'Is this user blocked by the current user?', VALUE_DEFAULT, false),
) )
); );
} }

View file

@ -14,6 +14,37 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with Moodle. If not, see <http://www.gnu.org/licenses/>. along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}} }}
{{!
@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}} {{#otheruserid}}
<div class="messages-header"> <div class="messages-header">
<div class="view-toggle btn-container"> <div class="view-toggle btn-container">
@ -25,6 +56,11 @@
<div class="name-container"> <div class="name-container">
<div class="name"> <div class="name">
<button class="btn btn-link" data-action="view-contact-profile" data-userid="{{otheruserid}}">{{otheruserfullname}}</button> <button class="btn btn-link" data-action="view-contact-profile" data-userid="{{otheruserid}}">{{otheruserfullname}}</button>
{{#isblocked}}
<span data-region="contact-icon-blocked">
{{#pix}} t/block, core, {{#str}} contactblocked, message {{/str}} {{/pix}}
</span>
{{/isblocked}}
</div> </div>
<div class="status {{#isonline}}online{{/isonline}}"> <div class="status {{#isonline}}online{{/isonline}}">
<span class="offline-text">{{#str}} offline, message {{/str}}</span> <span class="offline-text">{{#str}} offline, message {{/str}}</span>

View file

@ -839,13 +839,13 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
$this->setUser($user1); $this->setUser($user1);
// User shouldn't be blocked. // 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. // Block the user.
message_block_contact($user2->id); message_block_contact($user2->id);
// User should be blocked. // 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(); $this->setAdminUser();
// As the admin you should still be able to send messages to the user. // 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));
} }
/* /*