Skip to content

Commit

Permalink
MDL-56292 message: move popover code into output plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanwyllie authored and mdjnelson committed Oct 11, 2016
1 parent f8e4145 commit 7d69958
Show file tree
Hide file tree
Showing 28 changed files with 979 additions and 735 deletions.
16 changes: 0 additions & 16 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -732,22 +732,6 @@
'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',
Expand Down
49 changes: 13 additions & 36 deletions lib/outputrenderers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3193,47 +3193,24 @@ public function search_box($id = false) {
}

/**
* 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;

$processor = $DB->get_record('message_processors', array('name' => 'popup'));
public function navbar_plugin_output() {
$output = '';

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;
}

/**
Expand Down
79 changes: 0 additions & 79 deletions message/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,85 +525,6 @@ public static function mark_all_read_for_user($touserid, $fromuserid = 0, $type
$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.
*
Expand Down
194 changes: 0 additions & 194 deletions message/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1487,138 +1487,6 @@ public static function get_messages_returns() {
);
}

/**
* 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.
*
Expand Down Expand Up @@ -1698,68 +1566,6 @@ public static function mark_all_notifications_as_read_returns() {
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.
*
Expand Down
10 changes: 10 additions & 0 deletions message/output/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ public function get_default_messaging_settings() {
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;
}
}


Expand Down
Loading

0 comments on commit 7d69958

Please sign in to comment.