MDL-56498 message: Ensure clicked notifications are marked read

This commit is contained in:
Michael Hawkins 2018-05-16 14:59:06 +08:00
parent dc71a8b50e
commit 105974cd08
3 changed files with 56 additions and 22 deletions

File diff suppressed because one or more lines are too long

View File

@ -237,6 +237,12 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/str', 'core/url',
offset: offset,
});
// Link to mark read page before loading the actual link.
notification.contexturl = URL.relativeUrl('message/output/popup/mark_notification_read.php', {
redirecturl: notification.contexturl,
notificationid: notification.id,
});
var promise = Templates.render('message_popup/notification_content_item', notification)
.then(function(html, js) {
return {html: html, js: js};
@ -321,26 +327,6 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/str', 'core/url',
}.bind(this));
};
/**
* Send a request to the server to mark a single notification as read and update
* the unread count and unread notification elements appropriately.
*
* @param {jQuery} element
* @return {Promise|boolean}
* @method markAllAsRead
*/
NotificationPopoverController.prototype.markNotificationAsRead = function(element) {
if (!element.hasClass('unread')) {
return false;
}
return NotificationRepo.markAsRead(element.attr('data-id'))
.then(function() {
this.unreadCount--;
element.removeClass('unread');
}.bind(this));
};
/**
* Add all of the required event listeners for this notification popover.
*
@ -361,7 +347,12 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/str', 'core/url',
// Mark individual notification read if the user activates it.
this.root.on(CustomEvents.events.activate, SELECTORS.NOTIFICATION_LINK, function(e) {
var element = $(e.target).closest(SELECTORS.NOTIFICATION);
this.markNotificationAsRead(element);
if (element.hasClass('unread')) {
this.unreadCount--;
element.removeClass('unread');
}
e.stopPropagation();
}.bind(this));

View File

@ -0,0 +1,43 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Mark a notification read and redirect to the relevant content.
*
* @package message_popup
* @copyright 2018 Michael Hawkins
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../../config.php');
require_login(null, false);
if (isguestuser()) {
redirect($CFG->wwwroot);
}
$notificationid = required_param('notificationid', PARAM_INT);
$redirecturl = optional_param('redirecturl', $CFG->wwwroot, PARAM_LOCALURL);
$notification = $DB->get_record('notifications', array('id' => $notificationid));
// Check notification belongs to this user.
if ($USER->id != $notification->useridto) {
redirect($CFG->wwwroot);
}
\core_message\api::mark_notification_as_read($notification);
redirect($redirecturl);