Skip to content

Commit

Permalink
MDL-54687 core_message: added ability to delete messages
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Oct 7, 2016
1 parent c6e97f5 commit 3090f52
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 59 deletions.
1 change: 1 addition & 0 deletions lang/en/message.php
Expand Up @@ -131,6 +131,7 @@
$string['searchforperson'] = 'Search for a person';
$string['searchmessages'] = 'Search messages';
$string['searchcombined'] = 'Search people and messages';
$string['selectmessagestodelete'] = 'Select messages to delete';
$string['send'] = 'Send';
$string['sendingvia'] = 'Sending "{$a->provider}" via "{$a->processor}"';
$string['sendingviawhen'] = 'Sending "{$a->provider}" via "{$a->processor}" when {$a->state}';
Expand Down
1 change: 1 addition & 0 deletions lib/db/services.php
Expand Up @@ -629,6 +629,7 @@
'description' => 'Deletes a message.',
'type' => 'write',
'capabilities' => 'moodle/site:deleteownmessage',
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_message_get_blocked_users' => array(
Expand Down
76 changes: 76 additions & 0 deletions message/amd/src/message-area.js
Expand Up @@ -48,6 +48,9 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/str'
this._node.on('click', '.unblockcontactbtn', this._unblockContact.bind(this));
this._node.on('click', '.addcontactbtn', this._addContact.bind(this));
this._node.on('click', '.removecontactbtn', this._removeContact.bind(this));
this._node.on('click', '.choosemessagestodeletebtn', this._chooseMessagesToDelete.bind(this));
this._node.on('click', '.deletemessagesbtn', this._deleteMessages.bind(this));
this._node.on('click', '.canceldeletemessagesbtn', this._cancelDeleteMessages.bind(this));
};

Messagearea.prototype._loadConversations = function() {
Expand Down Expand Up @@ -277,6 +280,75 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/str'
}.bind(this)).fail(notification.exception);
};

Messagearea.prototype._chooseMessagesToDelete = function() {
// Toggle the checkboxes.
var checkboxes = this.find('.deletemessagecheckbox').toggle();

// Check if we didn't toggle to delete the messages.
if (checkboxes.is(':hidden')) {
// Only show a response field if we are viewing the logged in user's messages.
this.find('.response').empty();
if (this._getLoggedInUserId() == this._getCurrentUserId()) {
templates.render('core_message/message_response', {}).then(function (html, js) {
this.find('.response').append(html);
templates.runTemplateJS(js);
}.bind(this));
}
} else {
templates.render('core_message/message_delete_message', {}).then(function(html, js) {
this.find('.response').empty().append(html);
templates.runTemplateJS(js);
}.bind(this));
}
};

Messagearea.prototype._deleteMessages = function() {
var userid = this._getCurrentUserId();
var checkboxes = this.find('.deletemessagecheckbox input:checked');
var requests = [];
var messagestoremove = [];

// Go through all the checked checkboxes and prepare them for deletion.
checkboxes.each(function(id, element) {
var node = $(element);
var messageid = node.data('messageid');
var isread = node.data('messageread') ? 1 : 0;
var message = this.find('#message-' + messageid + '' + isread);
messagestoremove.push(message);
requests.push({
methodname: 'core_message_delete_message',
args: {
messageid: messageid,
userid: userid,
read: isread
}
});
}.bind(this));

ajax.call(requests)[requests.length - 1].then(function() {
// Remove the messages from the DOM.
$.each(messagestoremove, function(key, message) {
// Remove the message.
message.remove();
}.bind(this));
// Now we have removed all the messages from the DOM lets remove any block times we may need to as well.
$.each(messagestoremove, function(key, message) {
// First - let's make sure there are no more messages in that time block.
var blocktime = message.data('blocktime');
if (this.find('div.message[data-blocktime=\'' + blocktime + '\']').length == 0) {
this.find('div.blocktime[data-blocktime=\'' + blocktime + '\']').remove();
}
}.bind(this));
// Simple toggle the delete button action.
this._chooseMessagesToDelete();
}.bind(this), notification.exception);
};

Messagearea.prototype._cancelDeleteMessages = function() {
// Simple toggle the delete button action.
this._chooseMessagesToDelete();
};

Messagearea.prototype._loadMessages = function(userid) {
// Show loading template.
templates.render('core_message/loading', {}).done(function(html) {
Expand Down Expand Up @@ -317,6 +389,10 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/str'
button.prop('id', newid);
};

Messagearea.prototype._getLoggedInUserId = function() {
return this._node.data('loggedinuserid');
};

Messagearea.prototype._getCurrentUserId = function() {
return this._node.data('userid');
};
Expand Down
8 changes: 5 additions & 3 deletions message/classes/helper.php
Expand Up @@ -84,22 +84,24 @@ public static function create_messages($userid, $messages) {
$year = '';
foreach ($messages as $message) {
// Check if we are now viewing a different block period.
$blocktime = null;
$displayblocktime = false;
$date = usergetdate($message->timecreated);
if ($day != $date['mday'] || $month != $date['month'] || $year != $date['year']) {
$day = $date['mday'];
$month = $date['month'];
$year = $date['year'];
$blocktime = userdate($message->timecreated, get_string('strftimedaydate'));
$displayblocktime = true;
}
// Store the message to pass to the renderable.
$msg = new \stdClass();
$msg->id = $message->id;
$msg->text = message_format_message_text($message);
$msg->currentuserid = $userid;
$msg->useridfrom = $message->useridfrom;
$msg->useridto = $message->useridto;
$msg->blocktime = $blocktime;
$msg->displayblocktime = $displayblocktime;
$msg->timecreated = $message->timecreated;
$msg->timeread = $message->timeread;
$arrmessages[] = new \core_message\output\message($msg);
}

Expand Down
5 changes: 4 additions & 1 deletion message/classes/output/message.php
Expand Up @@ -52,13 +52,16 @@ public function __construct($message) {

public function export_for_template(\renderer_base $output) {
$message = new \stdClass();
$message->id = $this->message->id;
$message->text = $this->message->text;
$message->blocktime = $this->message->blocktime;
$message->displayblocktime = $this->message->displayblocktime;
$message->blocktime = userdate($this->message->timecreated, get_string('strftimedaydate'));
$message->position = 'left';
if ($this->message->currentuserid == $this->message->useridfrom) {
$message->position = 'right';
}
$message->timesent = userdate($this->message->timecreated, get_string('strftimetime'));
$message->isread = !empty($this->message->timeread) ? 1 : 0;

return $message;
}
Expand Down
3 changes: 3 additions & 0 deletions message/classes/output/message_area_page.php
Expand Up @@ -65,7 +65,10 @@ public function __construct($userid, $contacts, $messages) {
}

public function export_for_template(\renderer_base $output) {
global $USER;

$data = new \stdClass();
$data->loggedinuserid = $USER->id;
$data->userid = $this->userid;
$data->contacts = $this->contacts->export_for_template($output);
if ($this->messages) {
Expand Down
6 changes: 6 additions & 0 deletions message/externallib.php
Expand Up @@ -606,10 +606,13 @@ public static function data_for_messagearea_messages_returns() {
'messages' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'The id of the message'),
'text' => new external_value(PARAM_RAW, 'The text of the message'),
'displayblocktime' => new external_value(PARAM_BOOL, 'Should we display the block time?'),
'blocktime' => new external_value(PARAM_NOTAGS, 'The time to display above the message'),
'position' => new external_value(PARAM_ALPHA, 'The position of the text'),
'timesent' => new external_value(PARAM_NOTAGS, 'The time the message was sent'),
'isread' => new external_value(PARAM_INT, 'Determines if the message was read or not'),
)
)
)
Expand Down Expand Up @@ -668,10 +671,13 @@ public static function data_for_messagearea_get_most_recent_message($currentuser
public static function data_for_messagearea_get_most_recent_message_returns() {
return new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'The id of the message'),
'text' => new external_value(PARAM_RAW, 'The text of the message'),
'displayblocktime' => new external_value(PARAM_BOOL, 'Should we display the block time?'),
'blocktime' => new external_value(PARAM_NOTAGS, 'The time to display above the message'),
'position' => new external_value(PARAM_ALPHA, 'The position of the text'),
'timesent' => new external_value(PARAM_NOTAGS, 'The time the message was sent'),
'isread' => new external_value(PARAM_INT, 'Determines if the message was read or not'),
)
);
}
Expand Down
23 changes: 14 additions & 9 deletions message/templates/message.mustache
@@ -1,11 +1,16 @@
{{#blocktime}}
<div class="blocktime">
{{.}}
{{#displayblocktime}}
<div id="blocktime-{{id}}{{isread}}" class="blocktime" data-blocktime="{{blocktime}}">
{{blocktime}}
</div>
{{/displayblocktime}}
<div id="message-{{id}}{{isread}}" class="message row-fluid" data-blocktime="{{blocktime}}">
<div class="span1 deletemessagecheckbox">
<input type="checkbox" data-messageid="{{id}}" data-messageread="{{isread}}"/>
</div>
<div class="span11 content {{position}}">
{{{text}}}
<span class="timesent">
{{timesent}}
</span>
</div>
{{/blocktime}}
<div class="message {{position}}">
{{{text}}}
<span class="timesent">
{{timesent}}
</span>
</div>
27 changes: 15 additions & 12 deletions message/templates/message_area.mustache
@@ -1,19 +1,22 @@
<div class="messaging-area" data-userid="{{userid}}">
<div class="row-fluid">
<div class="span4 contacts-area">
{{#contacts}}
{{> core_message/contacts }}
{{/contacts}}
</div>
<div class="span8 messages-area">
{{#messages}}
{{> core_message/messages }}
{{/messages}}
<div class="messaging-area-all" data-loggedinuserid="{{loggedinuserid}}" data-userid="{{userid}}">
<input class="choosemessagestodeletebtn" type="button" value="{{#str}}delete{{/str}}"/>
<div class="messaging-area">
<div class="row-fluid">
<div class="span4 contacts-area">
{{#contacts}}
{{> core_message/contacts }}
{{/contacts}}
</div>
<div class="span8 messages-area">
{{#messages}}
{{> core_message/messages }}
{{/messages}}
</div>
</div>
</div>
</div>
{{#js}}
require(['core_message/message-area'], function(Messagearea) {
var messageArea = new Messagearea('.messaging-area');
var messageArea = new Messagearea('.messaging-area-all');
});
{{/js}}
3 changes: 3 additions & 0 deletions message/templates/message_delete_message.mustache
@@ -0,0 +1,3 @@
<strong>{{#str}}selectmessagestodelete, message{{/str}}</strong>
<input class="canceldeletemessagesbtn" type="button" value="{{#str}}cancel{{/str}}" />
<input class="deletemessagesbtn" type="button" value="{{#str}}delete{{/str}}" />
2 changes: 2 additions & 0 deletions message/templates/message_response.mustache
@@ -0,0 +1,2 @@
<textarea id="sendmessagetxt"></textarea>
<input class="sendmessagebtn" type="button" value="{{#str}}send, message{{/str}}" />
11 changes: 5 additions & 6 deletions message/templates/messages.mustache
Expand Up @@ -6,9 +6,8 @@
{{> core_message/message }}
{{/messages}}
</div>
{{#iscurrentuser}}
<div class="response">
<textarea id="sendmessagetxt"></textarea>
<input class="sendmessagebtn" type="button" value="{{#str}}send, message{{/str}}">
</div>
{{/iscurrentuser}}
<div class="response">
{{#iscurrentuser}}
{{> core_message/message_response }}
{{/iscurrentuser}}
</div>
37 changes: 20 additions & 17 deletions theme/bootstrapbase/less/moodle/message.less
Expand Up @@ -108,25 +108,28 @@
text-align: center;
}

.left {
float: left;
width: 80%;
}
.message {

.right {
float: right;
padding-right: 5px;
margin-right: 5px;
max-width: 80%;
}
.deletemessagecheckbox {
display: none;
}

.message {
border: 1px solid black;
padding-left: 5px;
margin-bottom: 5px;
clear: both;
font-size: 10px;
word-wrap: break-word;
.content {
border: 1px solid black;
padding-left: 5px;
margin-bottom: 5px;
font-size: 10px;
word-wrap: break-word;
width: 80%;
}

.content.left {
float: left;
}

.content.right {
float: right;
}
}
}

Expand Down
21 changes: 10 additions & 11 deletions theme/bootstrapbase/style/moodle.css
Expand Up @@ -5844,23 +5844,22 @@ a.ygtvspacer:hover {
font-weight: bold;
text-align: center;
}
.messaging-area .messages-area .messages .left {
float: left;
width: 80%;
}
.messaging-area .messages-area .messages .right {
float: right;
padding-right: 5px;
margin-right: 5px;
max-width: 80%;
.messaging-area .messages-area .messages .message .deletemessagecheckbox {
display: none;
}
.messaging-area .messages-area .messages .message {
.messaging-area .messages-area .messages .message .content {
border: 1px solid black;
padding-left: 5px;
margin-bottom: 5px;
clear: both;
font-size: 10px;
word-wrap: break-word;
width: 80%;
}
.messaging-area .messages-area .messages .message .content.left {
float: left;
}
.messaging-area .messages-area .messages .message .content.right {
float: right;
}
.messaging-area .messages-area .response textarea {
width: 80%;
Expand Down

0 comments on commit 3090f52

Please sign in to comment.