MDL-56292 message: move popover code into output plugin

This commit is contained in:
Ryan Wyllie
2016-10-11 14:39:23 +08:00
committed by Mark Nelson
parent f8e4145feb
commit 7d69958e6b
28 changed files with 979 additions and 735 deletions
-16
View File
@@ -732,22 +732,6 @@ $functions = array(
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
'ajax' => true,
),
'core_message_get_popup_notifications' => array(
'classname' => 'core_message_external',
'methodname' => 'get_popup_notifications',
'classpath' => 'message/externallib.php',
'description' => 'Retrieve a list of popup notifications for a user',
'type' => 'read',
'ajax' => true,
),
'core_message_get_unread_popup_notification_count' => array(
'classname' => 'core_message_external',
'methodname' => 'get_unread_popup_notification_count',
'classpath' => 'message/externallib.php',
'description' => 'Retrieve the count of unread popup notifications for a given user',
'type' => 'read',
'ajax' => true,
),
'core_message_get_unread_conversations_count' => array(
'classname' => 'core_message_external',
'methodname' => 'get_unread_conversations_count',
+13 -36
View File
@@ -3193,47 +3193,24 @@ EOD;
}
/**
* Returns the notification menu
* Allow plugins to provide some content to be rendered in the navbar.
* The plugin must define a PLUGIN_render_navbar_output function that returns
* the HTML they wish to add to the navbar.
*
* @return string HTML for the notification menu
* @return string HTML for the navbar
*/
public function notification_menu() {
global $USER, $DB;
public function navbar_plugin_output() {
$output = '';
$processor = $DB->get_record('message_processors', array('name' => 'popup'));
if (isloggedin() && $processor->enabled && !isguestuser() && !user_not_fully_set_up($USER)) {
$context = [
'userid' => $USER->id,
'urls' => [
'preferences' => (new moodle_url('/message/notificationpreferences.php', ['userid' => $USER->id]))->out(),
],
];
return $this->render_from_template('message/notification_popover', $context);
} else {
return '';
if ($pluginsfunction = get_plugins_with_function('render_navbar_output')) {
foreach ($pluginsfunction as $plugintype => $plugins) {
foreach ($plugins as $pluginfunction) {
$output .= $pluginfunction($this);
}
}
}
}
/**
* Returns the message menu
*
* @return string HTML for the message menu
*/
public function message_menu() {
global $CFG, $USER;
if (!empty($CFG->messaging) && isloggedin() && !isguestuser() && !user_not_fully_set_up($USER)) {
$context = [
'userid' => $USER->id,
'urls' => [
'preferences' => (new moodle_url('/message/edit.php', ['id' => $USER->id]))->out(),
],
];
return $this->render_from_template('message/message_popover', $context);
} else {
return '';
}
return $output;
}
/**
-79
View File
@@ -525,85 +525,6 @@ class api {
$messages->close();
}
/**
* Get popup notifications for the specified users.
*
* @param int $useridto the user id who received the notification
* @param string $status MESSAGE_READ for retrieving read notifications, MESSAGE_UNREAD for unread, empty for both
* @param bool $embeduserto embed the to user details in the notification response
* @param bool $embeduserfrom embed the from user details in the notification response
* @param string $sort the column name to order by including optionally direction
* @param int $limit limit the number of result returned
* @param int $offset offset the result set by this amount
* @return array notification records
* @throws \moodle_exception
* @since 3.2
*/
public static function get_popup_notifications($useridto = 0, $sort = 'DESC', $limit = 0, $offset = 0) {
global $DB, $USER;
$sort = strtoupper($sort);
if ($sort != 'DESC' && $sort != 'ASC') {
throw new \moodle_exception('invalid parameter: sort: must be "DESC" or "ASC"');
}
if (empty($useridto)) {
$useridto = $USER->id;
}
$params = [
'useridto1' => $useridto,
'useridto2' => $useridto,
];
$sql = "SELECT * FROM (
SELECT concat('r', r.id) as uniqueid, r.id, r.useridfrom, r.useridto,
r.subject, r.fullmessage, r.fullmessageformat,
r.fullmessagehtml, r.smallmessage, r.notification, r.contexturl,
r.contexturlname, r.timecreated, r.timeuserfromdeleted, r.timeusertodeleted,
r.component, r.eventtype, r.timeread
FROM {message_read} r
WHERE r.notification = 1
AND r.id IN (SELECT messageid FROM {message_popup} WHERE isread = 1)
AND r.useridto = :useridto1
UNION
SELECT concat('u', u.id) as uniqueid, u.id, u.useridfrom, u.useridto,
u.subject, u.fullmessage, u.fullmessageformat,
u.fullmessagehtml, u.smallmessage, u.notification, u.contexturl,
u.contexturlname, u.timecreated, u.timeuserfromdeleted, u.timeusertodeleted,
u.component, u.eventtype, 0 as timeread
FROM {message} u
WHERE u.notification = 1
AND u.id IN (SELECT messageid FROM {message_popup} WHERE isread = 0)
AND u.useridto = :useridto2
) f ORDER BY timecreated $sort, timeread $sort, id $sort";
return array_values($DB->get_records_sql($sql, $params, $offset, $limit));
}
/**
* Count the unread notifications for a user.
*
* @param int $useridto the user id who received the notification
* @return int count of the unread notifications
* @since 3.2
*/
public static function count_unread_popup_notifications($useridto = 0) {
global $USER, $DB;
if (empty($useridto)) {
$useridto = $USER->id;
}
return $DB->count_records_sql(
"SELECT count(id)
FROM {message}
WHERE id IN (SELECT messageid FROM {message_popup} WHERE isread = 0)
AND useridto = ?",
[$useridto]
);
}
/**
* Returns message preferences.
*
-194
View File
@@ -1487,138 +1487,6 @@ class core_message_external extends external_api {
);
}
/**
* Get popup notifications parameters description.
*
* @return external_function_parameters
* @since 3.2
*/
public static function get_popup_notifications_parameters() {
return new external_function_parameters(
array(
'useridto' => new external_value(PARAM_INT, 'the user id who received the message, 0 for current user', VALUE_REQUIRED),
'newestfirst' => new external_value(
PARAM_BOOL, 'true for ordering by newest first, false for oldest first',
VALUE_DEFAULT, true),
'limit' => new external_value(PARAM_INT, 'the number of results to return', VALUE_DEFAULT, 0),
'offset' => new external_value(PARAM_INT, 'offset the result set by a given amount', VALUE_DEFAULT, 0)
)
);
}
/**
* Get notifications function.
*
* @since 3.2
* @throws invalid_parameter_exception
* @throws moodle_exception
* @param int $useridto the user id who received the message
* @param bool $newestfirst true for ordering by newest first, false for oldest first
* @param int $limit the number of results to return
* @param int $offset offset the result set by a given amount
* @return external_description
*/
public static function get_popup_notifications($useridto, $newestfirst, $limit, $offset) {
global $USER, $PAGE;
$params = self::validate_parameters(
self::get_popup_notifications_parameters(),
array(
'useridto' => $useridto,
'newestfirst' => $newestfirst,
'limit' => $limit,
'offset' => $offset,
)
);
$context = context_system::instance();
self::validate_context($context);
$useridto = $params['useridto'];
$newestfirst = $params['newestfirst'];
$limit = $params['limit'];
$offset = $params['offset'];
$issuperuser = has_capability('moodle/site:readallmessages', $context);
$renderer = $PAGE->get_renderer('core_message');
if (empty($useridto)) {
$useridto = $USER->id;
}
// Check if the current user is the sender/receiver or just a privileged user.
if ($useridto != $USER->id and !$issuperuser) {
throw new moodle_exception('accessdenied', 'admin');
}
if (!empty($useridto)) {
if (!core_user::is_real_user($useridto)) {
throw new moodle_exception('invaliduser');
}
}
$sort = $newestfirst ? 'DESC' : 'ASC';
$notifications = \core_message\api::get_popup_notifications($useridto, $sort, $limit, $offset);
$notificationcontexts = [];
if ($notifications) {
foreach ($notifications as $notification) {
$notificationoutput = new \core_message\output\popup_notification($notification);
$notificationcontext = $notificationoutput->export_for_template($renderer);
$notificationcontexts[] = $notificationcontext;
}
}
return array(
'notifications' => $notificationcontexts,
'unreadcount' => \core_message\api::count_unread_popup_notifications($useridto),
);
}
/**
* Get notifications return description.
*
* @return external_single_structure
* @since 3.2
*/
public static function get_popup_notifications_returns() {
return new external_single_structure(
array(
'notifications' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'Notification id (this is not guaranteed to be unique
within this result set)'),
'useridfrom' => new external_value(PARAM_INT, 'User from id'),
'useridto' => new external_value(PARAM_INT, 'User to id'),
'subject' => new external_value(PARAM_TEXT, 'The notification subject'),
'shortenedsubject' => new external_value(PARAM_TEXT, 'The notification subject shortened
with ellipsis'),
'text' => new external_value(PARAM_RAW, 'The message text formated'),
'fullmessage' => new external_value(PARAM_RAW, 'The message'),
'fullmessageformat' => new external_format_value('fullmessage'),
'fullmessagehtml' => new external_value(PARAM_RAW, 'The message in html'),
'smallmessage' => new external_value(PARAM_RAW, 'The shorten message'),
'contexturl' => new external_value(PARAM_RAW, 'Context URL'),
'contexturlname' => new external_value(PARAM_TEXT, 'Context URL link name'),
'timecreated' => new external_value(PARAM_INT, 'Time created'),
'timecreatedpretty' => new external_value(PARAM_TEXT, 'Time created in a pretty format'),
'timeread' => new external_value(PARAM_INT, 'Time read'),
'read' => new external_value(PARAM_BOOL, 'notification read status'),
'deleted' => new external_value(PARAM_BOOL, 'notification deletion status'),
'iconurl' => new external_value(PARAM_URL, 'URL for notification icon'),
'component' => new external_value(PARAM_TEXT, 'The component that generated the notification',
VALUE_OPTIONAL),
'eventtype' => new external_value(PARAM_TEXT, 'The type of notification', VALUE_OPTIONAL),
), 'message'
)
),
'unreadcount' => new external_value(PARAM_INT, 'the number of unread message for the given user'),
)
);
}
/**
* Mark all notifications as read parameters description.
*
@@ -1698,68 +1566,6 @@ class core_message_external extends external_api {
return new external_value(PARAM_BOOL, 'True if the messages were marked read, false otherwise');
}
/**
* Get unread notification count parameters description.
*
* @return external_function_parameters
* @since 3.2
*/
public static function get_unread_popup_notification_count_parameters() {
return new external_function_parameters(
array(
'useridto' => new external_value(PARAM_INT, 'the user id who received the message, 0 for any user', VALUE_REQUIRED),
)
);
}
/**
* Get unread notification count function.
*
* @since 3.2
* @throws invalid_parameter_exception
* @throws moodle_exception
* @param int $useridto the user id who received the message
* @return external_description
*/
public static function get_unread_popup_notification_count($useridto) {
global $USER;
$params = self::validate_parameters(
self::get_unread_popup_notification_count_parameters(),
array('useridto' => $useridto)
);
$context = context_system::instance();
self::validate_context($context);
$useridto = $params['useridto'];
if (!empty($useridto)) {
if (core_user::is_real_user($useridto)) {
$userto = core_user::get_user($useridto, '*', MUST_EXIST);
} else {
throw new moodle_exception('invaliduser');
}
}
// Check if the current user is the sender/receiver or just a privileged user.
if ($useridto != $USER->id and !has_capability('moodle/site:readallmessages', $context)) {
throw new moodle_exception('accessdenied', 'admin');
}
return \core_message\api::count_unread_popup_notifications($useridto);
}
/**
* Get unread popup notification count return description.
*
* @return external_single_structure
* @since 3.2
*/
public static function get_unread_popup_notification_count_returns() {
return new external_value(PARAM_INT, 'The count of unread popup notifications');
}
/**
* Get unread conversations count parameters description.
*
+10
View File
@@ -102,6 +102,16 @@ abstract class message_output {
public function can_send_to_any_users() {
return false;
}
/**
* Returns true if this processor has configurable message preferences. This is
* distinct from notification preferences.
*
* @return bool
*/
public function has_message_preferences() {
return true;
}
}
@@ -16,11 +16,11 @@
/**
* Controls the message popover in the nav bar.
*
* See template: message/message_menu
* See template: message_popup/message_popover
*
* @module core_message/message_popover_controller
* @module message_popup/message_popover_controller
* @class message_popover_controller
* @package message
* @package message_popup
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@@ -180,7 +180,7 @@ define(['jquery', 'theme_bootstrapbase/bootstrap', 'core/ajax', 'core/templates'
id: message.userid,
});
var promise = Templates.render('message/message_content_item', message);
var promise = Templates.render('message_popup/message_content_item', message);
promises.push(promise);
promise.then(function(html, js) {
@@ -16,11 +16,11 @@
/**
* Controls the notification popover in the nav bar.
*
* See template: message/notification_menu
* See template: message_popup/notification_popover
*
* @module core_message/notification_popover_controller
* @module message_popup/notification_popover_controller
* @class notification_popover_controller
* @package message
* @package message_popup
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@@ -241,7 +241,7 @@ define(['jquery', 'theme_bootstrapbase/bootstrap', 'core/ajax', 'core/templates'
offset: offset,
}, true);
var promise = Templates.render('message/notification_content_item', notification);
var promise = Templates.render('message_popup/notification_content_item', notification);
promises.push(promise);
promise.then(function(html, js) {
@@ -16,13 +16,13 @@
/**
* Retrieves notifications from the server.
*
* @module core_message/notification_repository
* @module message_popup/notification_repository
* @class notification_repository
* @package message
* @copyright 2015 Ryan Wyllie <ryan@moodle.com>
* @package message_popup
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(['core/ajax', 'core/notification'], function(ajax, notification) {
define(['core/ajax', 'core/notification'], function(Ajax, Notification) {
/**
* Retrieve a list of notifications from the server.
*
@@ -39,13 +39,13 @@ define(['core/ajax', 'core/notification'], function(ajax, notification) {
}
var request = {
methodname: 'core_message_get_popup_notifications',
methodname: 'message_popup_get_popup_notifications',
args: args
};
var promise = ajax.call([request])[0];
var promise = Ajax.call([request])[0];
promise.fail(notification.exception);
promise.fail(Notification.exception);
return promise;
};
@@ -58,13 +58,13 @@ define(['core/ajax', 'core/notification'], function(ajax, notification) {
*/
var countUnread = function(args) {
var request = {
methodname: 'core_message_get_unread_popup_notification_count',
methodname: 'message_popup_get_unread_popup_notification_count',
args: args
};
var promise = ajax.call([request])[0];
var promise = Ajax.call([request])[0];
promise.fail(notification.exception);
promise.fail(Notification.exception);
return promise;
};
@@ -81,9 +81,9 @@ define(['core/ajax', 'core/notification'], function(ajax, notification) {
args: args
};
var promise = ajax.call([request])[0];
var promise = Ajax.call([request])[0];
promise.fail(notification.exception);
promise.fail(Notification.exception);
return promise;
};
@@ -109,9 +109,9 @@ define(['core/ajax', 'core/notification'], function(ajax, notification) {
args: args
};
var promise = ajax.call([request])[0];
var promise = Ajax.call([request])[0];
promise.fail(notification.exception);
promise.fail(Notification.exception);
return promise;
};
+111
View File
@@ -0,0 +1,111 @@
<?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/>.
/**
* Contains class used to return information to display for the message popup.
*
* @package message_popup
* @copyright 2016 Ryan Wyllie <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace message_popup;
defined('MOODLE_INTERNAL') || die();
/**
* Class used to return information to display for the message popup.
*
* @copyright 2016 Ryan Wyllie <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class api {
/**
* Get popup notifications for the specified users.
*
* @param int $useridto the user id who received the notification
* @param string $sort the column name to order by including optionally direction
* @param int $limit limit the number of result returned
* @param int $offset offset the result set by this amount
* @return array notification records
* @throws \moodle_exception
* @since 3.2
*/
public static function get_popup_notifications($useridto = 0, $sort = 'DESC', $limit = 0, $offset = 0) {
global $DB, $USER;
$sort = strtoupper($sort);
if ($sort != 'DESC' && $sort != 'ASC') {
throw new \moodle_exception('invalid parameter: sort: must be "DESC" or "ASC"');
}
if (empty($useridto)) {
$useridto = $USER->id;
}
$params = [
'useridto1' => $useridto,
'useridto2' => $useridto,
];
$sql = "SELECT * FROM (
SELECT concat('r', r.id) as uniqueid, r.id, r.useridfrom, r.useridto,
r.subject, r.fullmessage, r.fullmessageformat,
r.fullmessagehtml, r.smallmessage, r.notification, r.contexturl,
r.contexturlname, r.timecreated, r.timeuserfromdeleted, r.timeusertodeleted,
r.component, r.eventtype, r.timeread
FROM {message_read} r
WHERE r.notification = 1
AND r.id IN (SELECT messageid FROM {message_popup} WHERE isread = 1)
AND r.useridto = :useridto1
UNION
SELECT concat('u', u.id) as uniqueid, u.id, u.useridfrom, u.useridto,
u.subject, u.fullmessage, u.fullmessageformat,
u.fullmessagehtml, u.smallmessage, u.notification, u.contexturl,
u.contexturlname, u.timecreated, u.timeuserfromdeleted, u.timeusertodeleted,
u.component, u.eventtype, 0 as timeread
FROM {message} u
WHERE u.notification = 1
AND u.id IN (SELECT messageid FROM {message_popup} WHERE isread = 0)
AND u.useridto = :useridto2
) f ORDER BY timecreated $sort, timeread $sort, id $sort";
return array_values($DB->get_records_sql($sql, $params, $offset, $limit));
}
/**
* Count the unread notifications for a user.
*
* @param int $useridto the user id who received the notification
* @return int count of the unread notifications
* @since 3.2
*/
public static function count_unread_popup_notifications($useridto = 0) {
global $USER, $DB;
if (empty($useridto)) {
$useridto = $USER->id;
}
return $DB->count_records_sql(
"SELECT count(id)
FROM {message}
WHERE id IN (SELECT messageid FROM {message_popup} WHERE isread = 0)
AND useridto = ?",
[$useridto]
);
}
}
@@ -17,12 +17,12 @@
/**
* Contains class used to prepare a popup notification for display.
*
* @package core_message
* @package message_popup
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_message\output;
namespace message_popup\output;
defined('MOODLE_INTERNAL') || die();
@@ -36,7 +36,7 @@ use core_user;
/**
* Class to prepare a popup notification for display.
*
* @package core_message
* @package message_popup
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@@ -51,9 +51,6 @@ class popup_notification implements templatable, renderable {
* Constructor.
*
* @param \stdClass $notification
* @param \stdClass $embeduserto
* @param \stdClass $embeduserfrom
* @param string $usertofullname
*/
public function __construct($notification) {
$this->notification = $notification;
+43
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/>.
defined('MOODLE_INTERNAL') || die();
/**
* External functions and service definitions.
*
* @package message_popup
* @copyright 2016 Ryan Wyllie <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$functions = array(
'message_popup_get_popup_notifications' => array(
'classname' => 'message_popup_external',
'methodname' => 'get_popup_notifications',
'classpath' => 'message/output/popup/externallib.php',
'description' => 'Retrieve a list of popup notifications for a user',
'type' => 'read',
'ajax' => true,
),
'message_popup_get_unread_popup_notification_count' => array(
'classname' => 'message_popup_external',
'methodname' => 'get_unread_popup_notification_count',
'classpath' => 'message/output/popup/externallib.php',
'description' => 'Retrieve the count of unread popup notifications for a given user',
'type' => 'read',
'ajax' => true,
),
);
+234
View File
@@ -0,0 +1,234 @@
<?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/>.
defined('MOODLE_INTERNAL') || die();
/**
* External message popup API
*
* @package message_popup
* @category external
* @copyright 2016 Ryan Wyllie <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once("$CFG->libdir/externallib.php");
require_once($CFG->dirroot . "/message/lib.php");
/**
* Message external functions
*
* @package message_popup
* @category external
* @copyright 2016 Ryan Wyllie <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class message_popup_external extends external_api {
/**
* Get popup notifications parameters description.
*
* @return external_function_parameters
* @since 3.2
*/
public static function get_popup_notifications_parameters() {
return new external_function_parameters(
array(
'useridto' => new external_value(PARAM_INT, 'the user id who received the message, 0 for current user'),
'newestfirst' => new external_value(
PARAM_BOOL, 'true for ordering by newest first, false for oldest first',
VALUE_DEFAULT, true),
'limit' => new external_value(PARAM_INT, 'the number of results to return', VALUE_DEFAULT, 0),
'offset' => new external_value(PARAM_INT, 'offset the result set by a given amount', VALUE_DEFAULT, 0)
)
);
}
/**
* Get notifications function.
*
* @since 3.2
* @throws invalid_parameter_exception
* @throws moodle_exception
* @param int $useridto the user id who received the message
* @param bool $newestfirst true for ordering by newest first, false for oldest first
* @param int $limit the number of results to return
* @param int $offset offset the result set by a given amount
* @return external_description
*/
public static function get_popup_notifications($useridto, $newestfirst, $limit, $offset) {
global $USER, $PAGE;
$params = self::validate_parameters(
self::get_popup_notifications_parameters(),
array(
'useridto' => $useridto,
'newestfirst' => $newestfirst,
'limit' => $limit,
'offset' => $offset,
)
);
$context = context_system::instance();
self::validate_context($context);
$useridto = $params['useridto'];
$newestfirst = $params['newestfirst'];
$limit = $params['limit'];
$offset = $params['offset'];
$issuperuser = has_capability('moodle/site:readallmessages', $context);
$renderer = $PAGE->get_renderer('core_message');
if (empty($useridto)) {
$useridto = $USER->id;
}
// Check if the current user is the sender/receiver or just a privileged user.
if ($useridto != $USER->id and !$issuperuser) {
throw new moodle_exception('accessdenied', 'admin');
}
if (!empty($useridto)) {
if (!core_user::is_real_user($useridto)) {
throw new moodle_exception('invaliduser');
}
}
$sort = $newestfirst ? 'DESC' : 'ASC';
$notifications = \message_popup\api::get_popup_notifications($useridto, $sort, $limit, $offset);
$notificationcontexts = [];
if ($notifications) {
foreach ($notifications as $notification) {
$notificationoutput = new \message_popup\output\popup_notification($notification);
$notificationcontext = $notificationoutput->export_for_template($renderer);
$notificationcontexts[] = $notificationcontext;
}
}
return array(
'notifications' => $notificationcontexts,
'unreadcount' => \message_popup\api::count_unread_popup_notifications($useridto),
);
}
/**
* Get notifications return description.
*
* @return external_single_structure
* @since 3.2
*/
public static function get_popup_notifications_returns() {
return new external_single_structure(
array(
'notifications' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'Notification id (this is not guaranteed to be unique
within this result set)'),
'useridfrom' => new external_value(PARAM_INT, 'User from id'),
'useridto' => new external_value(PARAM_INT, 'User to id'),
'subject' => new external_value(PARAM_TEXT, 'The notification subject'),
'shortenedsubject' => new external_value(PARAM_TEXT, 'The notification subject shortened
with ellipsis'),
'text' => new external_value(PARAM_RAW, 'The message text formated'),
'fullmessage' => new external_value(PARAM_RAW, 'The message'),
'fullmessageformat' => new external_format_value('fullmessage'),
'fullmessagehtml' => new external_value(PARAM_RAW, 'The message in html'),
'smallmessage' => new external_value(PARAM_RAW, 'The shorten message'),
'contexturl' => new external_value(PARAM_RAW, 'Context URL'),
'contexturlname' => new external_value(PARAM_TEXT, 'Context URL link name'),
'timecreated' => new external_value(PARAM_INT, 'Time created'),
'timecreatedpretty' => new external_value(PARAM_TEXT, 'Time created in a pretty format'),
'timeread' => new external_value(PARAM_INT, 'Time read'),
'read' => new external_value(PARAM_BOOL, 'notification read status'),
'deleted' => new external_value(PARAM_BOOL, 'notification deletion status'),
'iconurl' => new external_value(PARAM_URL, 'URL for notification icon'),
'component' => new external_value(PARAM_TEXT, 'The component that generated the notification',
VALUE_OPTIONAL),
'eventtype' => new external_value(PARAM_TEXT, 'The type of notification', VALUE_OPTIONAL),
), 'message'
)
),
'unreadcount' => new external_value(PARAM_INT, 'the number of unread message for the given user'),
)
);
}
/**
* Get unread notification count parameters description.
*
* @return external_function_parameters
* @since 3.2
*/
public static function get_unread_popup_notification_count_parameters() {
return new external_function_parameters(
array(
'useridto' => new external_value(PARAM_INT, 'the user id who received the message, 0 for any user', VALUE_REQUIRED),
)
);
}
/**
* Get unread notification count function.
*
* @since 3.2
* @throws invalid_parameter_exception
* @throws moodle_exception
* @param int $useridto the user id who received the message
* @return external_description
*/
public static function get_unread_popup_notification_count($useridto) {
global $USER;
$params = self::validate_parameters(
self::get_unread_popup_notification_count_parameters(),
array('useridto' => $useridto)
);
$context = context_system::instance();
self::validate_context($context);
$useridto = $params['useridto'];
if (!empty($useridto)) {
if (core_user::is_real_user($useridto)) {
$userto = core_user::get_user($useridto, '*', MUST_EXIST);
} else {
throw new moodle_exception('invaliduser');
}
}
// Check if the current user is the sender/receiver or just a privileged user.
if ($useridto != $USER->id and !has_capability('moodle/site:readallmessages', $context)) {
throw new moodle_exception('accessdenied', 'admin');
}
return \message_popup\api::count_unread_popup_notifications($useridto);
}
/**
* Get unread popup notification count return description.
*
* @return external_single_structure
* @since 3.2
*/
public static function get_unread_popup_notification_count_returns() {
return new external_value(PARAM_INT, 'The count of unread popup notifications');
}
}
+67
View File
@@ -0,0 +1,67 @@
<?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/>.
defined('MOODLE_INTERNAL') || die();
/**
* Contains standard functions for message_popup.
*
* @package message_popup
* @copyright 2016 Ryan Wyllie <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Renders the popup.
*
* @param renderer_base $renderer
* @return string The HTML
*/
function message_popup_render_navbar_output(\renderer_base $renderer) {
global $USER, $DB, $CFG;
// Early bail out conditions.
if (!isloggedin() || isguestuser() || user_not_fully_set_up($USER)) {
return '';
}
$output = '';
// Add the messages popover.
if (!empty($CFG->messaging)) {
$context = [
'userid' => $USER->id,
'urls' => [
'preferences' => (new moodle_url('/message/edit.php', ['id' => $USER->id]))->out(),
],
];
$output .= $renderer->render_from_template('message_popup/message_popover', $context);
}
// Add the notifications popover.
$processor = $DB->get_record('message_processors', array('name' => 'popup'));
if ($processor && $processor->enabled) {
$context = [
'userid' => $USER->id,
'urls' => [
'preferences' => (new moodle_url('/message/notificationpreferences.php', ['userid' => $USER->id]))->out(),
],
];
$output .= $renderer->render_from_template('message_popup/notification_popover', $context);
}
return $output;
}
@@ -103,6 +103,16 @@ class message_output_popup extends message_output {
return true;
}
/**
* Don't show this processor on the message preferences page. The user can't disable
* the notifications for user-to-user messaging.
*
* @return bool
*/
public function has_message_preferences() {
return false;
}
/**
* Handles the message_viewed event to keep data in sync.
*
@@ -15,7 +15,7 @@
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template message/message_content_item
@template message_output_popup/message_content_item
This template will render the message content item for the
navigation bar message menu.
@@ -15,7 +15,7 @@
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template message/message_popover
@template message_output_popup/message_popover
This template will render the message popover for the navigation bar.
@@ -71,7 +71,7 @@
{{/content}}
{{/ core/popover_region }}
{{#js}}
require(['jquery', 'core_message/message_popover_controller'], function($, controller) {
require(['jquery', 'message_popup/message_popover_controller'], function($, controller) {
var container = $('#nav-message-popover-container');
var controller = new controller(container);
controller.registerEventListeners();
@@ -15,7 +15,7 @@
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template message/notification_content_item
@template message_output_popup/notification_content_item
This template will render the notification content item for the
navigation bar notification menu.
@@ -15,7 +15,7 @@
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template message/notification_popover
@template message_output_popup/notification_popover
This template will render the notification popover for the navigation bar.
@@ -76,7 +76,7 @@
{{/content}}
{{/ core/popover_region }}
{{#js}}
require(['jquery', 'core_message/notification_popover_controller'], function($, controller) {
require(['jquery', 'message_popup/notification_popover_controller'], function($, controller) {
var container = $('#nav-notification-popover-container');
var controller = new controller(container);
controller.registerEventListeners();
+130
View File
@@ -0,0 +1,130 @@
<?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/>.
/**
* Test message popup API.
*
* @package message_popup
* @category test
* @copyright 2016 Ryan Wyllie <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
require_once($CFG->dirroot . '/message/output/popup/tests/base.php');
/**
* Test message popup API.
*
* @package message_popup
* @category test
* @copyright 2016 Ryan Wyllie <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class message_popup_api_testcase extends advanced_testcase {
use message_popup_test_helper;
/**
* Test set up.
*
* This is executed before running any test in this file.
*/
public function setUp() {
$this->preventResetByRollback(); // Messaging is not compatible with transactions.
$this->messagesink = $this->redirectMessages();
$this->resetAfterTest();
}
/**
* Test that the get_popup_notifications function will return the correct notifications.
*/
public function test_message_get_popup_notifications() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
$this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
$this->send_fake_read_popup_notification($sender, $recipient, 'Message 3', 3, 1);
$this->send_fake_read_popup_notification($sender, $recipient, 'Message 4', 3, 2);
$this->send_fake_unread_popup_notification($sender, $recipient, 'Message 5', 4);
$notifications = \message_popup\api::get_popup_notifications($recipient->id);
$this->assertEquals($notifications[0]->fullmessage, 'Message 5');
$this->assertEquals($notifications[1]->fullmessage, 'Message 4');
$this->assertEquals($notifications[2]->fullmessage, 'Message 3');
$this->assertEquals($notifications[3]->fullmessage, 'Message 2');
$this->assertEquals($notifications[4]->fullmessage, 'Message 1');
}
/**
* Test that the get_popup_notifications function works correctly with limiting and offsetting
* the result set if requested.
*/
public function test_message_get_popup_notifications_all_limit_and_offset() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
$this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
$this->send_fake_read_popup_notification($sender, $recipient, 'Message 3', 3, 1);
$this->send_fake_read_popup_notification($sender, $recipient, 'Message 4', 3, 2);
$this->send_fake_unread_popup_notification($sender, $recipient, 'Message 5', 4);
$this->send_fake_unread_popup_notification($sender, $recipient, 'Message 6', 5);
$notifications = \message_popup\api::get_popup_notifications($recipient->id, 'DESC', 2, 0);
$this->assertEquals($notifications[0]->fullmessage, 'Message 6');
$this->assertEquals($notifications[1]->fullmessage, 'Message 5');
$notifications = \message_popup\api::get_popup_notifications($recipient->id, 'DESC', 2, 2);
$this->assertEquals($notifications[0]->fullmessage, 'Message 4');
$this->assertEquals($notifications[1]->fullmessage, 'Message 3');
$notifications = \message_popup\api::get_popup_notifications($recipient->id, 'DESC', 0, 3);
$this->assertEquals($notifications[0]->fullmessage, 'Message 3');
$this->assertEquals($notifications[1]->fullmessage, 'Message 2');
$this->assertEquals($notifications[2]->fullmessage, 'Message 1');
}
/**
* Test count_unread_popup_notifications.
*/
public function test_message_count_unread_popup_notifications() {
$sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$recipient1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
$recipient2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test4', 'lastname' => 'User4'));
$this->send_fake_unread_popup_notification($sender1, $recipient1);
$this->send_fake_unread_popup_notification($sender1, $recipient1);
$this->send_fake_unread_popup_notification($sender2, $recipient1);
$this->send_fake_unread_popup_notification($sender1, $recipient2);
$this->send_fake_unread_popup_notification($sender2, $recipient2);
$this->send_fake_unread_popup_notification($sender2, $recipient2);
$this->send_fake_unread_popup_notification($sender2, $recipient2);
$this->send_fake_unread_popup_notification($sender2, $recipient2);
$this->assertEquals(\message_popup\api::count_unread_popup_notifications($recipient1->id), 3);
$this->assertEquals(\message_popup\api::count_unread_popup_notifications($recipient2->id), 5);
}
}
+102
View File
@@ -0,0 +1,102 @@
<?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/>.
/**
* Base trait for message popup tests.
*
* @package message_popup
* @copyright 2016 Ryan Wyllie <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
trait message_popup_test_helper {
/**
* Send a fake unread popup notification.
*
* {@link message_send()} does not support transaction, this function will simulate a message
* sent from a user to another. We should stop using it once {@link message_send()} will support
* transactions. This is not clean at all, this is just used to add rows to the table.
*
* @param stdClass $userfrom user object of the one sending the message.
* @param stdClass $userto user object of the one receiving the message.
* @param string $message message to send.
* @param int $timecreated time the message was created.
* @return int the id of the message
*/
protected function send_fake_unread_popup_notification($userfrom, $userto, $message = 'Hello world!', $timecreated = 0) {
global $DB;
$record = new stdClass();
$record->useridfrom = $userfrom->id;
$record->useridto = $userto->id;
$record->notification = 1;
$record->subject = 'No subject';
$record->fullmessage = $message;
$record->smallmessage = $message;
$record->timecreated = $timecreated ? $timecreated : time();
$id = $DB->insert_record('message', $record);
$popup = new stdClass();
$popup->messageid = $id;
$popup->isread = 0;
$DB->insert_record('message_popup', $popup);
return $id;
}
/**
* Send a fake read popup notification.
*
* {@link message_send()} does not support transaction, this function will simulate a message
* sent from a user to another. We should stop using it once {@link message_send()} will support
* transactions. This is not clean at all, this is just used to add rows to the table.
*
* @param stdClass $userfrom user object of the one sending the message.
* @param stdClass $userto user object of the one receiving the message.
* @param string $message message to send.
* @param int $timecreated time the message was created.
* @param int $timeread the the message was read
* @return int the id of the message
*/
protected function send_fake_read_popup_notification($userfrom, $userto, $message = 'Hello world!',
$timecreated = 0, $timeread = 0) {
global $DB;
$record = new stdClass();
$record->useridfrom = $userfrom->id;
$record->useridto = $userto->id;
$record->notification = 1;
$record->subject = 'No subject';
$record->fullmessage = $message;
$record->smallmessage = $message;
$record->timecreated = $timecreated ? $timecreated : time();
$record->timeread = $timeread ? $timeread : time();
$id = $DB->insert_record('message_read', $record);
$popup = new stdClass();
$popup->messageid = $id;
$popup->isread = 1;
$DB->insert_record('message_popup', $popup);
return $id;
}
}
@@ -0,0 +1,188 @@
<?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/>.
/**
* External message popup functions unit tests
*
* @package message_popup
* @copyright 2016 Ryan Wyllie <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
require_once($CFG->dirroot . '/message/output/popup/externallib.php');
require_once($CFG->dirroot . '/message/output/popup/tests/base.php');
/**
* Class for external message popup functions unit tests.
*
* @copyright 2016 Ryan Wyllie <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class message_popup_externallib_testcase extends advanced_testcase {
use message_popup_test_helper;
/**
* Test set up.
*
* This is executed before running any test in this file.
*/
public function setUp() {
$this->preventResetByRollback(); // Messaging is not compatible with transactions.
$this->messagesink = $this->redirectMessages();
$this->resetAfterTest();
}
/**
* Test that get_popup_notifications throws an exception if the user
* doesn't exist.
*/
public function test_get_popup_notifications_no_user_exception() {
$this->resetAfterTest(true);
$this->setExpectedException('moodle_exception');
$result = message_popup_external::get_popup_notifications(-2132131, false, 0, 0);
}
/**
* get_popup_notifications should throw exception if user isn't logged in
* user.
*/
public function test_get_popup_notifications_access_denied_exception() {
$this->resetAfterTest(true);
$sender = $this->getDataGenerator()->create_user();
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->setExpectedException('moodle_exception');
$result = message_popup_external::get_popup_notifications($sender->id, false, 0, 0);
}
/**
* get_popup_notifications should return notifications for the recipient.
*/
public function test_get_popup_notifications_as_recipient() {
$this->resetAfterTest(true);
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Sendy', 'lastname' => 'Sender'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Recipy', 'lastname' => 'Recipient'));
$notificationids = array(
$this->send_fake_unread_popup_notification($sender, $recipient),
$this->send_fake_unread_popup_notification($sender, $recipient),
$this->send_fake_read_popup_notification($sender, $recipient),
$this->send_fake_read_popup_notification($sender, $recipient),
);
// Confirm that admin has super powers to retrieve any notifications.
$this->setAdminUser();
$result = message_popup_external::get_popup_notifications($recipient->id, false, 0, 0);
$this->assertCount(4, $result['notifications']);
$this->setUser($recipient);
$result = message_popup_external::get_popup_notifications($recipient->id, false, 0, 0);
$this->assertCount(4, $result['notifications']);
}
/**
* get_popup_notifications result set should work with limit and offset.
*/
public function test_get_popup_notification_limit_offset() {
$this->resetAfterTest(true);
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Sendy', 'lastname' => 'Sender'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Recipy', 'lastname' => 'Recipient'));
$this->setUser($recipient);
$notificationids = array(
$this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 1', 1),
$this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 2', 2),
$this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 3', 3),
$this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 4', 4),
$this->send_fake_read_popup_notification($sender, $recipient, 'Notification 5', 5),
$this->send_fake_read_popup_notification($sender, $recipient, 'Notification 6', 6),
$this->send_fake_read_popup_notification($sender, $recipient, 'Notification 7', 7),
$this->send_fake_read_popup_notification($sender, $recipient, 'Notification 8', 8),
);
$result = message_popup_external::get_popup_notifications($recipient->id, true, 2, 0);
$this->assertEquals($result['notifications'][0]->id, $notificationids[7]);
$this->assertEquals($result['notifications'][1]->id, $notificationids[6]);
$result = message_popup_external::get_popup_notifications($recipient->id, true, 2, 2);
$this->assertEquals($result['notifications'][0]->id, $notificationids[5]);
$this->assertEquals($result['notifications'][1]->id, $notificationids[4]);
}
/**
* get_unread_popup_notification should throw an exception for an invalid user.
*/
public function test_get_unread_popup_notification_count_invalid_user_exception() {
$this->resetAfterTest(true);
$this->setExpectedException('moodle_exception');
$result = message_popup_external::get_unread_popup_notification_count(-2132131, 0);
}
/**
* get_unread_popup_notification_count should throw exception if being requested for
* non-logged in user.
*/
public function test_get_unread_popup_notification_count_access_denied_exception() {
$this->resetAfterTest(true);
$sender = $this->getDataGenerator()->create_user();
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->setExpectedException('moodle_exception');
$result = message_popup_external::get_unread_popup_notification_count($sender->id, 0);
}
/**
* Test get_unread_popup_notification_count.
*/
public function test_get_unread_popup_notification_count() {
$this->resetAfterTest(true);
$sender1 = $this->getDataGenerator()->create_user();
$sender2 = $this->getDataGenerator()->create_user();
$sender3 = $this->getDataGenerator()->create_user();
$recipient = $this->getDataGenerator()->create_user();
$this->setUser($recipient);
$notificationids = array(
$this->send_fake_unread_popup_notification($sender1, $recipient, 'Notification', 1),
$this->send_fake_unread_popup_notification($sender1, $recipient, 'Notification', 2),
$this->send_fake_unread_popup_notification($sender2, $recipient, 'Notification', 3),
$this->send_fake_unread_popup_notification($sender2, $recipient, 'Notification', 4),
$this->send_fake_unread_popup_notification($sender3, $recipient, 'Notification', 5),
$this->send_fake_unread_popup_notification($sender3, $recipient, 'Notification', 6),
);
$count = message_popup_external::get_unread_popup_notification_count($recipient->id);
$this->assertEquals($count, 6);
}
}
+2 -3
View File
@@ -244,9 +244,8 @@ class core_message_renderer extends plugin_renderer_base {
return $processor->enabled &&
$processor->configured &&
$processor->object->is_user_configured() &&
// Filter out the popup processor because the notification preferences aren't
// considered for that.
$processor->name != 'popup';
// Filter out processors that don't have and message preferences to configure.
$processor->object->has_message_preferences();
});
$providers = array_filter(message_get_providers_for_user($user->id), function($provider) {
+12 -88
View File
@@ -43,9 +43,9 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$this->send_fake_unread_popup_notification($sender, $recipient);
$this->send_fake_unread_popup_notification($sender, $recipient);
$this->send_fake_unread_popup_notification($sender, $recipient);
$this->send_fake_message($sender, $recipient, 'Notification', 1);
$this->send_fake_message($sender, $recipient, 'Notification', 1);
$this->send_fake_message($sender, $recipient, 'Notification', 1);
$this->send_fake_message($sender, $recipient);
$this->send_fake_message($sender, $recipient);
$this->send_fake_message($sender, $recipient);
@@ -59,15 +59,15 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
$sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$this->send_fake_unread_popup_notification($sender1, $recipient);
$this->send_fake_unread_popup_notification($sender1, $recipient);
$this->send_fake_unread_popup_notification($sender1, $recipient);
$this->send_fake_message($sender1, $recipient, 'Notification', 1);
$this->send_fake_message($sender1, $recipient, 'Notification', 1);
$this->send_fake_message($sender1, $recipient, 'Notification', 1);
$this->send_fake_message($sender1, $recipient);
$this->send_fake_message($sender1, $recipient);
$this->send_fake_message($sender1, $recipient);
$this->send_fake_unread_popup_notification($sender2, $recipient);
$this->send_fake_unread_popup_notification($sender2, $recipient);
$this->send_fake_unread_popup_notification($sender2, $recipient);
$this->send_fake_message($sender2, $recipient, 'Notification', 1);
$this->send_fake_message($sender2, $recipient, 'Notification', 1);
$this->send_fake_message($sender2, $recipient, 'Notification', 1);
$this->send_fake_message($sender2, $recipient);
$this->send_fake_message($sender2, $recipient);
$this->send_fake_message($sender2, $recipient);
@@ -80,9 +80,9 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$this->send_fake_unread_popup_notification($sender, $recipient);
$this->send_fake_unread_popup_notification($sender, $recipient);
$this->send_fake_unread_popup_notification($sender, $recipient);
$this->send_fake_message($sender, $recipient, 'Notification', 1);
$this->send_fake_message($sender, $recipient, 'Notification', 1);
$this->send_fake_message($sender, $recipient, 'Notification', 1);
$this->send_fake_message($sender, $recipient);
$this->send_fake_message($sender, $recipient);
$this->send_fake_message($sender, $recipient);
@@ -94,82 +94,6 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
$this->assertEquals(message_count_unread_messages($recipient), 0);
}
/**
* Test that the get_popup_notifications function will return the correct notifications.
*/
public function test_message_get_popup_notifications() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
$this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
$this->send_fake_read_popup_notification($sender, $recipient, 'Message 3', 3, 1);
$this->send_fake_read_popup_notification($sender, $recipient, 'Message 4', 3, 2);
$this->send_fake_unread_popup_notification($sender, $recipient, 'Message 5', 4);
$notifications = \core_message\api::get_popup_notifications($recipient->id);
$this->assertEquals($notifications[0]->fullmessage, 'Message 5');
$this->assertEquals($notifications[1]->fullmessage, 'Message 4');
$this->assertEquals($notifications[2]->fullmessage, 'Message 3');
$this->assertEquals($notifications[3]->fullmessage, 'Message 2');
$this->assertEquals($notifications[4]->fullmessage, 'Message 1');
}
/**
* Test that the get_popup_notifications function works correctly with limiting and offsetting
* the result set if requested.
*/
public function test_message_get_popup_notifications_all_limit_and_offset() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
$this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
$this->send_fake_read_popup_notification($sender, $recipient, 'Message 3', 3, 1);
$this->send_fake_read_popup_notification($sender, $recipient, 'Message 4', 3, 2);
$this->send_fake_unread_popup_notification($sender, $recipient, 'Message 5', 4);
$this->send_fake_unread_popup_notification($sender, $recipient, 'Message 6', 5);
$notifications = \core_message\api::get_popup_notifications($recipient->id, 'DESC', 2, 0);
$this->assertEquals($notifications[0]->fullmessage, 'Message 6');
$this->assertEquals($notifications[1]->fullmessage, 'Message 5');
$notifications = \core_message\api::get_popup_notifications($recipient->id, 'DESC', 2, 2);
$this->assertEquals($notifications[0]->fullmessage, 'Message 4');
$this->assertEquals($notifications[1]->fullmessage, 'Message 3');
$notifications = \core_message\api::get_popup_notifications($recipient->id, 'DESC', 0, 3);
$this->assertEquals($notifications[0]->fullmessage, 'Message 3');
$this->assertEquals($notifications[1]->fullmessage, 'Message 2');
$this->assertEquals($notifications[2]->fullmessage, 'Message 1');
}
/**
* Test count_unread_popup_notifications.
*/
public function test_message_count_unread_popup_notifications() {
$sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$recipient1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
$recipient2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test4', 'lastname' => 'User4'));
$this->send_fake_unread_popup_notification($sender1, $recipient1);
$this->send_fake_unread_popup_notification($sender1, $recipient1);
$this->send_fake_unread_popup_notification($sender2, $recipient1);
$this->send_fake_unread_popup_notification($sender1, $recipient2);
$this->send_fake_unread_popup_notification($sender2, $recipient2);
$this->send_fake_unread_popup_notification($sender2, $recipient2);
$this->send_fake_unread_popup_notification($sender2, $recipient2);
$this->send_fake_unread_popup_notification($sender2, $recipient2);
$this->assertEquals(\core_message\api::count_unread_popup_notifications($recipient1->id), 3);
$this->assertEquals(\core_message\api::count_unread_popup_notifications($recipient2->id), 5);
}
/**
* Test count_blocked_users.
*
+19 -202
View File
@@ -51,8 +51,9 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
* @param stdClass $userfrom user object of the one sending the message.
* @param stdClass $userto user object of the one receiving the message.
* @param string $message message to send.
* @param int $notification is the message a notification.
*/
protected function send_message($userfrom, $userto, $message = 'Hello world!') {
protected function send_message($userfrom, $userto, $message = 'Hello world!', $notification = 0) {
global $DB;
$record = new stdClass();
$record->useridfrom = $userfrom->id;
@@ -60,84 +61,10 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
$record->subject = 'No subject';
$record->fullmessage = $message;
$record->timecreated = time();
$record->notification = $notification;
$insert = $DB->insert_record('message', $record);
}
/**
* Send a fake unread popup notification.
*
* {@link message_send()} does not support transaction, this function will simulate a message
* sent from a user to another. We should stop using it once {@link message_send()} will support
* transactions. This is not clean at all, this is just used to add rows to the table.
*
* @param stdClass $userfrom user object of the one sending the message.
* @param stdClass $userto user object of the one receiving the message.
* @param string $message message to send.
* @param int $timecreated time the message was created.
* @return int the id of the message
*/
protected function send_fake_unread_popup_notification($userfrom, $userto, $message = 'Hello world!', $timecreated = 0) {
global $DB;
$record = new stdClass();
$record->useridfrom = $userfrom->id;
$record->useridto = $userto->id;
$record->notification = 1;
$record->subject = 'No subject';
$record->fullmessage = $message;
$record->smallmessage = $message;
$record->timecreated = $timecreated ? $timecreated : time();
$id = $DB->insert_record('message', $record);
$popup = new stdClass();
$popup->messageid = $id;
$popup->isread = 0;
$DB->insert_record('message_popup', $popup);
return $id;
}
/**
* Send a fake read popup notification.
*
* {@link message_send()} does not support transaction, this function will simulate a message
* sent from a user to another. We should stop using it once {@link message_send()} will support
* transactions. This is not clean at all, this is just used to add rows to the table.
*
* @param stdClass $userfrom user object of the one sending the message.
* @param stdClass $userto user object of the one receiving the message.
* @param string $message message to send.
* @param int $timecreated time the message was created.
* @param int $timeread the the message was read
* @return int the id of the message
*/
protected function send_fake_read_popup_notification($userfrom, $userto, $message = 'Hello world!',
$timecreated = 0, $timeread = 0) {
global $DB;
$record = new stdClass();
$record->useridfrom = $userfrom->id;
$record->useridto = $userto->id;
$record->notification = 1;
$record->subject = 'No subject';
$record->fullmessage = $message;
$record->smallmessage = $message;
$record->timecreated = $timecreated ? $timecreated : time();
$record->timeread = $timeread ? $timeread : time();
$id = $DB->insert_record('message_read', $record);
$popup = new stdClass();
$popup->messageid = $id;
$popup->isread = 1;
$DB->insert_record('message_popup', $popup);
return $id;
}
/**
* Test send_instant_messages
*/
@@ -896,77 +823,6 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
}
public function test_get_popup_notifications_no_user_exception() {
$this->resetAfterTest(true);
$this->setExpectedException('moodle_exception');
$result = core_message_external::get_popup_notifications(-2132131, false, 0, 0);
}
public function test_get_popup_notifications_access_denied_exception() {
$this->resetAfterTest(true);
$sender = $this->getDataGenerator()->create_user();
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->setExpectedException('moodle_exception');
$result = core_message_external::get_popup_notifications($sender->id, false, 0, 0);
}
public function test_get_popup_notifications_as_recipient() {
$this->resetAfterTest(true);
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Sendy', 'lastname' => 'Sender'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Recipy', 'lastname' => 'Recipient'));
$notificationids = array(
$this->send_fake_unread_popup_notification($sender, $recipient),
$this->send_fake_unread_popup_notification($sender, $recipient),
$this->send_fake_read_popup_notification($sender, $recipient),
$this->send_fake_read_popup_notification($sender, $recipient),
);
// Confirm that admin has super powers to retrieve any notifications.
$this->setAdminUser();
$result = core_message_external::get_popup_notifications($recipient->id, false, 0, 0);
$this->assertCount(4, $result['notifications']);
$this->setUser($recipient);
$result = core_message_external::get_popup_notifications($recipient->id, false, 0, 0);
$this->assertCount(4, $result['notifications']);
}
public function test_get_popup_notification_limit_offset() {
$this->resetAfterTest(true);
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Sendy', 'lastname' => 'Sender'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Recipy', 'lastname' => 'Recipient'));
$this->setUser($recipient);
$notificationids = array(
$this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 1', 1),
$this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 2', 2),
$this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 3', 3),
$this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 4', 4),
$this->send_fake_read_popup_notification($sender, $recipient, 'Notification 5', 5),
$this->send_fake_read_popup_notification($sender, $recipient, 'Notification 6', 6),
$this->send_fake_read_popup_notification($sender, $recipient, 'Notification 7', 7),
$this->send_fake_read_popup_notification($sender, $recipient, 'Notification 8', 8),
);
$result = core_message_external::get_popup_notifications($recipient->id, true, 2, 0);
$this->assertEquals($result['notifications'][0]->id, $notificationids[7]);
$this->assertEquals($result['notifications'][1]->id, $notificationids[6]);
$result = core_message_external::get_popup_notifications($recipient->id, true, 2, 2);
$this->assertEquals($result['notifications'][0]->id, $notificationids[5]);
$this->assertEquals($result['notifications'][1]->id, $notificationids[4]);
}
public function test_mark_all_notifications_as_read_invalid_user_exception() {
$this->resetAfterTest(true);
@@ -996,6 +852,8 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
}
public function test_mark_all_notifications_as_read() {
global $DB;
$this->resetAfterTest(true);
$sender1 = $this->getDataGenerator()->create_user();
@@ -1006,67 +864,26 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
$this->setUser($recipient);
$notificationids = array(
$this->send_fake_unread_popup_notification($sender1, $recipient, 'Notification', 1),
$this->send_fake_unread_popup_notification($sender1, $recipient, 'Notification', 2),
$this->send_fake_unread_popup_notification($sender2, $recipient, 'Notification', 3),
$this->send_fake_unread_popup_notification($sender2, $recipient, 'Notification', 4),
$this->send_fake_unread_popup_notification($sender3, $recipient, 'Notification', 5),
$this->send_fake_unread_popup_notification($sender3, $recipient, 'Notification', 6),
$this->send_message($sender1, $recipient, 'Notification', 1),
$this->send_message($sender1, $recipient, 'Notification', 1),
$this->send_message($sender2, $recipient, 'Notification', 1),
$this->send_message($sender2, $recipient, 'Notification', 1),
$this->send_message($sender3, $recipient, 'Notification', 1),
$this->send_message($sender3, $recipient, 'Notification', 1),
);
core_message_external::mark_all_notifications_as_read($recipient->id, $sender1->id);
$result = core_message_external::get_popup_notifications($recipient->id, false, 0, 0);
$notifications = $result['notifications'];
$readnotifications = $DB->get_recordset('message_read', ['useridto' => $recipient->id]);
$unreadnotifications = $DB->get_recordset('message', ['useridto' => $recipient->id]);
$this->assertCount(2, array_filter($notifications, function($a) { return $a->read; }));
$this->assertCount(4, array_filter($notifications, function($a) { return !$a->read; }));
$this->assertCount(2, $readnotifications);
$this->assertCount(4, $unreadnotifications);
core_message_external::mark_all_notifications_as_read($recipient->id, 0);
$result = core_message_external::get_popup_notifications($recipient->id, false, 0, 0);
$notifications = $result['notifications'];
$readnotifications = $DB->get_recordset('message_read', ['useridto' => $recipient->id]);
$unreadnotifications = $DB->get_recordset('message', ['useridto' => $recipient->id]);
$this->assertCount(6, array_filter($notifications, function($a) { return $a->read; }));
$this->assertCount(0, array_filter($notifications, function($a) { return !$a->read; }));
}
public function test_get_unread_popup_notification_count_invalid_user_exception() {
$this->resetAfterTest(true);
$this->setExpectedException('moodle_exception');
$result = core_message_external::get_unread_popup_notification_count(-2132131, 0);
}
public function test_get_unread_popup_notification_count_access_denied_exception() {
$this->resetAfterTest(true);
$sender = $this->getDataGenerator()->create_user();
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->setExpectedException('moodle_exception');
$result = core_message_external::get_unread_popup_notification_count($sender->id, 0);
}
public function test_get_unread_popup_notification_count() {
$this->resetAfterTest(true);
$sender1 = $this->getDataGenerator()->create_user();
$sender2 = $this->getDataGenerator()->create_user();
$sender3 = $this->getDataGenerator()->create_user();
$recipient = $this->getDataGenerator()->create_user();
$this->setUser($recipient);
$notificationids = array(
$this->send_fake_unread_popup_notification($sender1, $recipient, 'Notification', 1),
$this->send_fake_unread_popup_notification($sender1, $recipient, 'Notification', 2),
$this->send_fake_unread_popup_notification($sender2, $recipient, 'Notification', 3),
$this->send_fake_unread_popup_notification($sender2, $recipient, 'Notification', 4),
$this->send_fake_unread_popup_notification($sender3, $recipient, 'Notification', 5),
$this->send_fake_unread_popup_notification($sender3, $recipient, 'Notification', 6),
);
$count = core_message_external::get_unread_popup_notification_count($recipient->id);
$this->assertEquals($count, 6);
$this->assertCount(6, $readnotifications);
$this->assertCount(0, $unreadnotifications);
}
}
+4 -77
View File
@@ -62,9 +62,10 @@ class core_message_messagelib_testcase extends advanced_testcase {
* @param stdClass $userfrom user object of the one sending the message.
* @param stdClass $userto user object of the one receiving the message.
* @param string $message message to send.
* @param int $notification if the message is a notification.
* @return int the id of the message
*/
protected function send_fake_message($userfrom, $userto, $message = 'Hello world!') {
protected function send_fake_message($userfrom, $userto, $message = 'Hello world!', $notification = 0) {
global $DB;
$record = new stdClass();
@@ -74,85 +75,11 @@ class core_message_messagelib_testcase extends advanced_testcase {
$record->fullmessage = $message;
$record->smallmessage = $message;
$record->timecreated = time();
$record->notification = $notification;
return $DB->insert_record('message', $record);
}
/**
* Send a fake unread popup notification.
*
* {@link message_send()} does not support transaction, this function will simulate a message
* sent from a user to another. We should stop using it once {@link message_send()} will support
* transactions. This is not clean at all, this is just used to add rows to the table.
*
* @param stdClass $userfrom user object of the one sending the message.
* @param stdClass $userto user object of the one receiving the message.
* @param string $message message to send.
* @param int $timecreated time the message was created.
* @return int the id of the message
*/
protected function send_fake_unread_popup_notification($userfrom, $userto, $message = 'Hello world!', $timecreated = 0) {
global $DB;
$record = new stdClass();
$record->useridfrom = $userfrom->id;
$record->useridto = $userto->id;
$record->notification = 1;
$record->subject = 'No subject';
$record->fullmessage = $message;
$record->smallmessage = $message;
$record->timecreated = $timecreated ? $timecreated : time();
$id = $DB->insert_record('message', $record);
$popup = new stdClass();
$popup->messageid = $id;
$popup->isread = 0;
$DB->insert_record('message_popup', $popup);
return $id;
}
/**
* Send a fake read popup notification.
*
* {@link message_send()} does not support transaction, this function will simulate a message
* sent from a user to another. We should stop using it once {@link message_send()} will support
* transactions. This is not clean at all, this is just used to add rows to the table.
*
* @param stdClass $userfrom user object of the one sending the message.
* @param stdClass $userto user object of the one receiving the message.
* @param string $message message to send.
* @param int $timecreated time the message was created.
* @param int $timeread the the message was read
* @return int the id of the message
*/
protected function send_fake_read_popup_notification($userfrom, $userto, $message = 'Hello world!',
$timecreated = 0, $timeread = 0) {
global $DB;
$record = new stdClass();
$record->useridfrom = $userfrom->id;
$record->useridto = $userto->id;
$record->notification = 1;
$record->subject = 'No subject';
$record->fullmessage = $message;
$record->smallmessage = $message;
$record->timecreated = $timecreated ? $timecreated : time();
$record->timeread = $timeread ? $timeread : time();
$id = $DB->insert_record('message_read', $record);
$popup = new stdClass();
$popup->messageid = $id;
$popup->isread = 1;
$DB->insert_record('message_popup', $popup);
return $id;
}
/**
* Test message_get_blocked_users.
*/
@@ -651,7 +578,7 @@ class core_message_messagelib_testcase extends advanced_testcase {
if (isset($messagedata['state']) && $messagedata['state'] == 'unread') {
$table = 'message';
$messageid = $this->send_fake_message($from, $to, $subject, FORMAT_PLAIN);
$messageid = $this->send_fake_message($from, $to, $subject);
} else {
// If there is no state, or the state is not 'unread', assume the message is read.
$table = 'message_read';
+1 -2
View File
@@ -44,8 +44,7 @@ echo $OUTPUT->doctype() ?>
<?php echo $OUTPUT->navbar_home(); ?>
<?php echo $OUTPUT->navbar_button(); ?>
<?php echo $OUTPUT->user_menu(); ?>
<?php echo $OUTPUT->message_menu(); ?>
<?php echo $OUTPUT->notification_menu(); ?>
<?php echo $OUTPUT->navbar_plugin_output(); ?>
<?php echo $OUTPUT->search_box(); ?>
<div class="nav-collapse collapse">
<?php echo $OUTPUT->custom_menu(); ?>
+1 -2
View File
@@ -47,8 +47,7 @@ echo $OUTPUT->doctype() ?>
<?php echo $OUTPUT->navbar_home(); ?>
<?php echo $OUTPUT->navbar_button(); ?>
<?php echo $OUTPUT->user_menu(); ?>
<?php echo $OUTPUT->message_menu(); ?>
<?php echo $OUTPUT->notification_menu(); ?>
<?php echo $OUTPUT->navbar_plugin_output(); ?>
<?php echo $OUTPUT->search_box(); ?>
<div class="nav-collapse collapse">
<?php echo $OUTPUT->custom_menu(); ?>
+1 -2
View File
@@ -55,8 +55,7 @@ echo $OUTPUT->doctype() ?>
<?php echo $OUTPUT->navbar_home(); ?>
<?php echo $OUTPUT->navbar_button(); ?>
<?php echo $OUTPUT->user_menu(); ?>
<?php echo $OUTPUT->message_menu(); ?>
<?php echo $OUTPUT->notification_menu(); ?>
<?php echo $OUTPUT->navbar_plugin_output(); ?>
<?php echo $OUTPUT->search_box(); ?>
<div class="nav-collapse collapse">
<?php echo $OUTPUT->custom_menu(); ?>