Skip to content

Commit

Permalink
Merge branch 'MDL-56498-master' of git://github.com/mickhawkins/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed May 29, 2018
2 parents 77e7c45 + 105974c commit f9be034
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 22 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 12 additions & 21 deletions message/output/popup/amd/src/notification_popover_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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.
*
Expand All @@ -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));

Expand Down
43 changes: 43 additions & 0 deletions message/output/popup/mark_notification_read.php
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit f9be034

Please sign in to comment.