Skip to content

Commit

Permalink
MDL-55637 core_message: display icon for blocked users
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Oct 7, 2016
1 parent a3e3a3a commit dd0c140
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 19 deletions.
1 change: 1 addition & 0 deletions lang/en/message.php
Expand Up @@ -39,6 +39,7 @@
$string['blockedusers'] = 'Blocked users ({$a})';
$string['blocknoncontacts'] = 'Prevent non-contacts from messaging me';
$string['collapsenotification'] = 'Collapse notification';
$string['contactblocked'] = 'Contact blocked';
$string['contactlistempty'] = 'Contact list empty';
$string['contacts'] = 'Contacts';
$string['context'] = 'context';
Expand Down
2 changes: 2 additions & 0 deletions message/amd/src/message_area.js
Expand Up @@ -41,6 +41,8 @@ define(['jquery', 'core_message/message_area_contacts', 'core_message/message_ar
BLOCKTIME: "[data-region='blocktime']",
CANCELDELETEMESSAGES: "[data-action='cancel-delete-messages']",
CONTACT: "[data-region='contact']",
CONTACTICONBLOCKED: "[data-region='contact-icon-blocked']",
CONTACTICONONLINE: "[data-region='contact-icon-online']",
CONTACTS: "[data-region='contacts'][data-region-content='contacts']",
CONTACTSAREA: "[data-region='contacts-area']",
CONTACTSPANELS: "[data-region='contacts']",
Expand Down
56 changes: 48 additions & 8 deletions message/amd/src/message_area_contacts.js
Expand Up @@ -86,6 +86,12 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust
this.messageArea.onCustomEvent(this.messageArea.EVENTS.CONTACTADDED, function(e, userid) {
this._addContact(userid);
}.bind(this));
this.messageArea.onCustomEvent(this.messageArea.EVENTS.CONTACTBLOCKED, function(e, userid) {
this._blockContact(userid);
}.bind(this));
this.messageArea.onCustomEvent(this.messageArea.EVENTS.CONTACTUNBLOCKED, function(e, userid) {
this._unblockContact(userid);
}.bind(this));
this.messageArea.onCustomEvent(this.messageArea.EVENTS.CHOOSEMESSAGESTODELETE,
this._chooseConversationsToDelete.bind(this));
this.messageArea.onCustomEvent(this.messageArea.EVENTS.CANCELDELETEMESSAGES,
Expand Down Expand Up @@ -474,27 +480,61 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust
};

/**
* Handles retrieving a user node from a list.
* Handles removing a contact from the list.
*
* @param {String} selector
* @param {int} userid
* @private
*/
Contacts.prototype._getUserNode = function(selector, userid) {
return this.messageArea.find(selector + " " + this.messageArea.SELECTORS.CONTACT +
"[data-userid='" + userid + "']");
Contacts.prototype._removeContact = function(selector, userid) {
this._getUserNode(selector, userid).remove();
this._numContactsDisplayed--;
};

/**
* Handles removing a contact from the list.
* Handles marking a contact as blocked on the list.
*
* @param {int} userid
* @private
*/
Contacts.prototype._blockContact = function(userid) {
var user = this._getUserNode(this.messageArea.SELECTORS.CONTACTS, userid);
user.find(this.messageArea.SELECTORS.CONTACTICONBLOCKED).removeClass('hidden');

user = this._getUserNode(this.messageArea.SELECTORS.CONVERSATIONS, userid);
user.find(this.messageArea.SELECTORS.CONTACTICONBLOCKED).removeClass('hidden');

user = this._getUserNode(this.messageArea.SELECTORS.SEARCHRESULTSAREA, userid);
user.find(this.messageArea.SELECTORS.CONTACTICONBLOCKED).removeClass('hidden');
};

/**
* Handles marking a contact as unblocked on the list.
*
* @param {int} userid
* @private
*/
Contacts.prototype._unblockContact = function(userid) {
var user = this._getUserNode(this.messageArea.SELECTORS.CONTACTS, userid);
user.find(this.messageArea.SELECTORS.CONTACTICONBLOCKED).addClass('hidden');

user = this._getUserNode(this.messageArea.SELECTORS.CONVERSATIONS, userid);
user.find(this.messageArea.SELECTORS.CONTACTICONBLOCKED).addClass('hidden');

user = this._getUserNode(this.messageArea.SELECTORS.SEARCHRESULTSAREA, userid);
user.find(this.messageArea.SELECTORS.CONTACTICONBLOCKED).addClass('hidden');
};

/**
* Handles retrieving a user node from a list.
*
* @param {String} selector
* @param {int} userid
* @private
*/
Contacts.prototype._removeContact = function(selector, userid) {
this._getUserNode(selector, userid).remove();
this._numContactsDisplayed--;
Contacts.prototype._getUserNode = function(selector, userid) {
return this.messageArea.find(selector + " " + this.messageArea.SELECTORS.CONTACT +
"[data-userid='" + userid + "']");
};

/**
Expand Down
38 changes: 28 additions & 10 deletions message/classes/api.php
Expand Up @@ -54,12 +54,17 @@ public static function search_messages($userid, $search, $limitfrom = 0, $limitn

// Get all the messages for the user.
$sql = "SELECT m.id, m.useridfrom, m.useridto, m.subject, m.fullmessage, m.fullmessagehtml, m.fullmessageformat,
m.smallmessage, m.notification, m.timecreated, 0 as isread, $ufields, $ufields2
m.smallmessage, m.notification, m.timecreated, 0 as isread, $ufields, mc.blocked as userfrom_blocked,
$ufields2, mc2.blocked as userto_blocked
FROM {message} m
JOIN {user} u
ON m.useridfrom = u.id
LEFT JOIN {message_contacts} mc
ON (mc.contactid = u.id AND mc.userid = ?)
JOIN {user} u2
ON m.useridto = u2.id
LEFT JOIN {message_contacts} mc2
ON (mc2.contactid = u2.id AND mc2.userid = ?)
WHERE ((useridto = ? AND timeusertodeleted = 0)
OR (useridfrom = ? AND timeuserfromdeleted = 0))
AND notification = 0
Expand All @@ -68,21 +73,26 @@ public static function search_messages($userid, $search, $limitfrom = 0, $limitn
AND " . $DB->sql_like('smallmessage', '?', false) . "
UNION ALL
SELECT mr.id, mr.useridfrom, mr.useridto, mr.subject, mr.fullmessage, mr.fullmessagehtml, mr.fullmessageformat,
mr.smallmessage, mr.notification, mr.timecreated, 1 as isread, $ufields, $ufields2
mr.smallmessage, mr.notification, mr.timecreated, 1 as isread, $ufields, mc.blocked as userfrom_blocked,
$ufields2, mc2.blocked as userto_blocked
FROM {message_read} mr
JOIN {user} u
ON mr.useridfrom = u.id
LEFT JOIN {message_contacts} mc
ON (mc.contactid = u.id AND mc.userid = ?)
JOIN {user} u2
ON mr.useridto = u2.id
LEFT JOIN {message_contacts} mc2
ON (mc2.contactid = u2.id AND mc2.userid = ?)
WHERE ((useridto = ? AND timeusertodeleted = 0)
OR (useridfrom = ? AND timeuserfromdeleted = 0))
AND notification = 0
AND u.deleted = 0
AND u2.deleted = 0
AND " . $DB->sql_like('smallmessage', '?', false) . "
ORDER BY timecreated DESC";
$params = array($userid, $userid, '%' . $search . '%',
$userid, $userid, '%' . $search . '%');
$params = array($userid, $userid, $userid, $userid, '%' . $search . '%',
$userid, $userid, $userid, $userid, '%' . $search . '%');

// Convert the messages into searchable contacts with their last message being the message that was searched.
$contacts = array();
Expand All @@ -94,6 +104,9 @@ public static function search_messages($userid, $search, $limitfrom = 0, $limitn
// If it from the user, then mark it as read, even if it wasn't by the receiver.
$message->isread = true;
}
$blockedcol = $prefix . 'blocked';
$message->blocked = $message->$blockedcol;

$message->messageid = $message->id;
$contacts[] = \core_message\helper::create_contact($message, $prefix);
}
Expand All @@ -117,16 +130,19 @@ public static function search_people_in_course($userid, $courseid, $search, $lim

// Get all the users in the course.
list($esql, $params) = get_enrolled_sql(\context_course::instance($courseid), '', 0, true);
$sql = "SELECT u.*
$sql = "SELECT u.*, mc.blocked
FROM {user} u
JOIN ($esql) je ON je.id = u.id
JOIN ($esql) je
ON je.id = u.id
LEFT JOIN {message_contacts} mc
ON (mc.contactid = u.id AND mc.userid = :userid)
WHERE u.deleted = 0";
// Add more conditions.
$fullname = $DB->sql_fullname();
$sql .= " AND u.id != :userid
$sql .= " AND u.id != :userid2
AND " . $DB->sql_like($fullname, ':search', false) . "
ORDER BY " . $DB->sql_fullname();
$params = array_merge(array('userid' => $userid, 'search' => '%' . $search . '%'), $params);
$params = array_merge(array('userid' => $userid, 'userid2' => $userid, 'search' => '%' . $search . '%'), $params);


// Convert all the user records into contacts.
Expand Down Expand Up @@ -163,7 +179,7 @@ public static function search_people($userid, $search, $limitnum = 0) {

// Ok, let's search for contacts first.
$contacts = array();
$sql = "SELECT $ufields
$sql = "SELECT $ufields, mc.blocked
FROM {user} u
JOIN {message_contacts} mc
ON u.id = mc.contactid
Expand Down Expand Up @@ -193,6 +209,8 @@ public static function search_people($userid, $search, $limitnum = 0) {
}

// Let's get those non-contacts. Toast them gears boi.
// Note - you can only block contacts, so these users will not be blocked, so no need to get that
// extra detail from the database.
$noncontacts = array();
$sql = "SELECT $ufields
FROM {user} u
Expand Down Expand Up @@ -246,7 +264,7 @@ public static function get_contacts($userid, $limitfrom = 0, $limitnum = 0) {
global $DB;

$arrcontacts = array();
$sql = "SELECT u.*
$sql = "SELECT u.*, mc.blocked
FROM {message_contacts} mc
JOIN {user} u
ON mc.contactid = u.id
Expand Down
1 change: 1 addition & 0 deletions message/classes/helper.php
Expand Up @@ -150,6 +150,7 @@ public static function create_contact($contact, $prefix = '') {
}
// Check if the user is online.
$data->isonline = \core_message\helper::is_online($userfields->lastaccess);
$data->isblocked = isset($contact->blocked) ? $contact->blocked : 0;
$data->isread = isset($contact->isread) ? $contact->isread : 0;
$data->unreadcount = isset($contact->unreadcount) ? $contact->unreadcount : null;

Expand Down
1 change: 1 addition & 0 deletions message/classes/output/messagearea/contact.php
Expand Up @@ -75,6 +75,7 @@ public function export_for_template(\renderer_base $output) {
$contact->lastmessage = null;
}
$contact->isonline = $this->contact->isonline;
$contact->isblocked = $this->contact->isblocked;
$contact->isread = $this->contact->isread;
$contact->unreadcount = $this->contact->unreadcount;

Expand Down
1 change: 1 addition & 0 deletions message/externallib.php
Expand Up @@ -437,6 +437,7 @@ private static function get_messagearea_contact_structure() {
'messageid' => new external_value(PARAM_INT, 'The unique search message id', VALUE_DEFAULT, null),
'isonline' => new external_value(PARAM_BOOL, 'The user\'s online status'),
'isread' => new external_value(PARAM_BOOL, 'If the user has read the message'),
'isblocked' => new external_value(PARAM_BOOL, 'If the user has been blocked'),
'unreadcount' => new external_value(PARAM_INT, 'The number of unread messages in this conversation',
VALUE_DEFAULT, null),
)
Expand Down
10 changes: 9 additions & 1 deletion message/templates/message_area_contact.mustache
Expand Up @@ -5,7 +5,15 @@
<img src="{{profileimageurl}}" alt="" />
</div>
<div class="span9 information">
<div class="name">{{fullname}} {{#isonline}}{{#pix}} t/go, core, {{#str}} online, message {{/str}} {{/pix}}{{/isonline}}</div>
<div class="name">
{{fullname}}
<span {{^isonline}}class="hidden"{{/isonline}} data-region="contact-icon-online">
{{#pix}} t/go, core, {{#str}} online, message {{/str}} {{/pix}}
</span>
<span {{^isblocked}}class="hidden"{{/isblocked}} data-region="contact-icon-blocked">
{{#pix}} t/block, core, {{#str}} contactblocked, message {{/str}} {{/pix}}
</span>
</div>
<p class="lastmessage" data-region="lastmessage">
{{#lastmessage}}
{{.}}
Expand Down

0 comments on commit dd0c140

Please sign in to comment.