mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 00:16:46 +02:00
MDL-55942 core_message: moved added functionality from message/lib.php
This commit is contained in:
parent
c598f278ca
commit
79f6c36c12
9 changed files with 629 additions and 556 deletions
|
@ -4176,7 +4176,7 @@ EOD;
|
|||
'sesskey' => sesskey())
|
||||
),
|
||||
'image' => $contactimage,
|
||||
'linkattributes' => message_togglecontact_link_params($user, $iscontact),
|
||||
'linkattributes' => \core_message\helper::togglecontact_link_params($user, $iscontact),
|
||||
'page' => $this->page
|
||||
),
|
||||
);
|
||||
|
@ -4238,7 +4238,7 @@ EOD;
|
|||
if (!isset($button->page)) {
|
||||
// Include js for messaging.
|
||||
if ($button['buttontype'] === 'togglecontact') {
|
||||
message_togglecontact_requirejs();
|
||||
\core_message\helper::togglecontact_requirejs();
|
||||
}
|
||||
$image = $this->pix_icon($button['formattedimage'], $button['title'], 'moodle', array(
|
||||
'class' => 'iconsmall',
|
||||
|
|
|
@ -462,4 +462,213 @@ class api {
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the count of unread conversations (collection of messages from a single user) for
|
||||
* the given user.
|
||||
*
|
||||
* @param \stdClass $user the user who's conversations should be counted
|
||||
* @return int the count of the user's unread conversations
|
||||
*/
|
||||
public static function count_unread_conversations($user = null) {
|
||||
global $USER, $DB;
|
||||
|
||||
if (empty($user)) {
|
||||
$user = $USER;
|
||||
}
|
||||
|
||||
return $DB->count_records_select(
|
||||
'message',
|
||||
'useridto = ? AND timeusertodeleted = 0 AND notification = 0',
|
||||
[$user->id],
|
||||
"COUNT(DISTINCT(useridfrom))");
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks ALL messages being sent from $fromuserid to $touserid as read.
|
||||
*
|
||||
* Can be filtered by type.
|
||||
*
|
||||
* @param int $touserid the id of the message recipient
|
||||
* @param int $fromuserid the id of the message sender
|
||||
* @param string $type filter the messages by type, either MESSAGE_TYPE_NOTIFICATION, MESSAGE_TYPE_MESSAGE or '' for all.
|
||||
* @return void
|
||||
*/
|
||||
public static function mark_all_read_for_user($touserid, $fromuserid = 0, $type = '') {
|
||||
global $DB;
|
||||
|
||||
$params = array();
|
||||
|
||||
if (!empty($touserid)) {
|
||||
$params['useridto'] = $touserid;
|
||||
}
|
||||
|
||||
if (!empty($fromuserid)) {
|
||||
$params['useridfrom'] = $fromuserid;
|
||||
}
|
||||
|
||||
if (!empty($type)) {
|
||||
if (strtolower($type) == MESSAGE_TYPE_NOTIFICATION) {
|
||||
$params['notification'] = 1;
|
||||
} else if (strtolower($type) == MESSAGE_TYPE_MESSAGE) {
|
||||
$params['notification'] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
$sql = sprintf('SELECT m.* FROM {message} m WHERE m.%s = ?', implode('= ? AND m.', array_keys($params)));
|
||||
$messages = $DB->get_recordset_sql($sql, array_values($params));
|
||||
|
||||
foreach ($messages as $message) {
|
||||
message_mark_message_read($message, time());
|
||||
}
|
||||
|
||||
$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, $status = '', $embeduserto = false, $embeduserfrom = false,
|
||||
$sort = 'DESC', $limit = 0, $offset = 0) {
|
||||
global $DB, $USER;
|
||||
|
||||
if (!empty($status) && $status != MESSAGE_READ && $status != MESSAGE_UNREAD) {
|
||||
throw new \moodle_exception(sprintf('invalid parameter: status: must be "%s" or "%s"',
|
||||
MESSAGE_READ, MESSAGE_UNREAD));
|
||||
}
|
||||
|
||||
$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 = array();
|
||||
|
||||
$buildtablesql = function($table, $prefix, $additionalfields, $messagestatus)
|
||||
use ($status, $useridto, $embeduserto, $embeduserfrom) {
|
||||
|
||||
$joinsql = '';
|
||||
$fields = "concat('$prefix', $prefix.id) as uniqueid, $prefix.id, $prefix.useridfrom, $prefix.useridto,
|
||||
$prefix.subject, $prefix.fullmessage, $prefix.fullmessageformat,
|
||||
$prefix.fullmessagehtml, $prefix.smallmessage, $prefix.notification, $prefix.contexturl,
|
||||
$prefix.contexturlname, $prefix.timecreated, $prefix.timeuserfromdeleted, $prefix.timeusertodeleted,
|
||||
$prefix.component, $prefix.eventtype, $additionalfields";
|
||||
$where = " AND $prefix.useridto = :{$prefix}useridto";
|
||||
$params = ["{$prefix}useridto" => $useridto];
|
||||
|
||||
if ($embeduserto) {
|
||||
$embedprefix = "{$prefix}ut";
|
||||
$fields .= ", " . get_all_user_name_fields(true, $embedprefix, '', 'userto');
|
||||
$joinsql .= " LEFT JOIN {user} $embedprefix ON $embedprefix.id = $prefix.useridto";
|
||||
}
|
||||
|
||||
if ($embeduserfrom) {
|
||||
$embedprefix = "{$prefix}uf";
|
||||
$fields .= ", " . get_all_user_name_fields(true, $embedprefix, '', 'userfrom');
|
||||
$joinsql .= " LEFT JOIN {user} $embedprefix ON $embedprefix.id = $prefix.useridfrom";
|
||||
}
|
||||
|
||||
if ($messagestatus == MESSAGE_READ) {
|
||||
$isread = '1';
|
||||
} else {
|
||||
$isread = '0';
|
||||
}
|
||||
|
||||
return array(
|
||||
sprintf(
|
||||
"SELECT %s
|
||||
FROM %s %s %s
|
||||
WHERE %s.notification = 1
|
||||
AND %s.id IN (SELECT messageid FROM {message_popup} WHERE isread = %s)
|
||||
%s",
|
||||
$fields, $table, $prefix, $joinsql, $prefix, $prefix, $isread, $where
|
||||
),
|
||||
$params
|
||||
);
|
||||
};
|
||||
|
||||
switch ($status) {
|
||||
case MESSAGE_READ:
|
||||
list($sql, $readparams) = $buildtablesql('{message_read}', 'r', 'r.timeread', MESSAGE_READ);
|
||||
$params = array_merge($params, $readparams);
|
||||
break;
|
||||
case MESSAGE_UNREAD:
|
||||
list($sql, $unreadparams) = $buildtablesql('{message}', 'u', '0 as timeread', MESSAGE_UNREAD);
|
||||
$params = array_merge($params, $unreadparams);
|
||||
break;
|
||||
default:
|
||||
list($readsql, $readparams) = $buildtablesql('{message_read}', 'r', 'r.timeread', MESSAGE_READ);
|
||||
list($unreadsql, $unreadparams) = $buildtablesql('{message}', 'u', '0 as timeread', MESSAGE_UNREAD);
|
||||
$sql = sprintf("SELECT * FROM (%s UNION %s) f", $readsql, $unreadsql);
|
||||
$params = array_merge($params, $readparams, $unreadparams);
|
||||
}
|
||||
|
||||
$sql .= " 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.
|
||||
*
|
||||
* @param array $processors
|
||||
* @param array $providers
|
||||
* @param \stdClass $user
|
||||
* @return \stdClass
|
||||
* @since 3.2
|
||||
*/
|
||||
public static function get_all_message_preferences($processors, $providers, $user) {
|
||||
$preferences = helper::get_providers_preferences($providers, $user->id);
|
||||
$preferences->userdefaultemail = $user->email; // May be displayed by the email processor.
|
||||
|
||||
// For every processors put its options on the form (need to get function from processor's lib.php).
|
||||
foreach ($processors as $processor) {
|
||||
$processor->object->load_data($preferences, $user->id);
|
||||
}
|
||||
|
||||
// Load general messaging preferences.
|
||||
$preferences->blocknoncontacts = get_user_preferences('message_blocknoncontacts', '', $user->id);
|
||||
$preferences->mailformat = $user->mailformat;
|
||||
$preferences->mailcharset = get_user_preferences('mailcharset', '', $user->id);
|
||||
|
||||
return $preferences;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -179,4 +179,69 @@ class helper {
|
|||
|
||||
return $lastaccess >= $time;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get providers preferences.
|
||||
*
|
||||
* @param array $providers
|
||||
* @param int $userid
|
||||
* @return \stdClass
|
||||
*/
|
||||
public static function get_providers_preferences($providers, $userid) {
|
||||
$preferences = new \stdClass();
|
||||
|
||||
// Get providers preferences.
|
||||
foreach ($providers as $provider) {
|
||||
foreach (array('loggedin', 'loggedoff') as $state) {
|
||||
$linepref = get_user_preferences('message_provider_' . $provider->component . '_' . $provider->name
|
||||
. '_' . $state, '', $userid);
|
||||
if ($linepref == '') {
|
||||
continue;
|
||||
}
|
||||
$lineprefarray = explode(',', $linepref);
|
||||
$preferences->{$provider->component.'_'.$provider->name.'_'.$state} = array();
|
||||
foreach ($lineprefarray as $pref) {
|
||||
$preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$pref] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $preferences;
|
||||
}
|
||||
|
||||
/**
|
||||
* Requires the JS libraries for the toggle contact button.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function togglecontact_requirejs() {
|
||||
global $PAGE;
|
||||
|
||||
static $done = false;
|
||||
if ($done) {
|
||||
return;
|
||||
}
|
||||
|
||||
$PAGE->requires->js_call_amd('core_message/toggle_contact_button', 'enhance', array('#toggle-contact-button'));
|
||||
$done = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the attributes to place on a contact button.
|
||||
*
|
||||
* @param object $user User object.
|
||||
* @param bool $iscontact
|
||||
* @return array
|
||||
*/
|
||||
public static function togglecontact_link_params($user, $iscontact = false) {
|
||||
$params = array(
|
||||
'data-userid' => $user->id,
|
||||
'data-is-contact' => $iscontact,
|
||||
'id' => 'toggle-contact-button',
|
||||
'role' => 'button',
|
||||
'class' => 'ajax-contact-button',
|
||||
);
|
||||
|
||||
return $params;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1581,7 +1581,7 @@ class core_message_external extends external_api {
|
|||
}
|
||||
|
||||
$sort = $newestfirst ? 'DESC' : 'ASC';
|
||||
$notifications = message_get_popup_notifications($useridto, $status, $embeduserto, $embeduserfrom, $sort, $limit, $offset);
|
||||
$notifications = \core_message\api::get_popup_notifications($useridto, $status, $embeduserto, $embeduserfrom, $sort, $limit, $offset);
|
||||
$notificationcontexts = [];
|
||||
|
||||
if ($notifications) {
|
||||
|
@ -1609,7 +1609,7 @@ class core_message_external extends external_api {
|
|||
|
||||
return array(
|
||||
'notifications' => $notificationcontexts,
|
||||
'unreadcount' => message_count_unread_popup_notifications($useridto),
|
||||
'unreadcount' => \core_message\api::count_unread_popup_notifications($useridto),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -1720,7 +1720,7 @@ class core_message_external extends external_api {
|
|||
throw new moodle_exception('accessdenied', 'admin');
|
||||
}
|
||||
|
||||
message_mark_all_read_for_user($useridto, $useridfrom, MESSAGE_TYPE_NOTIFICATION);
|
||||
\core_message\api::mark_all_read_for_user($useridto, $useridfrom, MESSAGE_TYPE_NOTIFICATION);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -1784,7 +1784,7 @@ class core_message_external extends external_api {
|
|||
throw new moodle_exception('accessdenied', 'admin');
|
||||
}
|
||||
|
||||
return message_count_unread_popup_notifications($useridto);
|
||||
return \core_message\api::count_unread_popup_notifications($useridto);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1848,7 +1848,7 @@ class core_message_external extends external_api {
|
|||
throw new moodle_exception('accessdenied', 'admin');
|
||||
}
|
||||
|
||||
return message_count_unread_conversations($userto);
|
||||
return \core_message\api::count_unread_conversations($userto);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -2109,7 +2109,7 @@ class core_message_external extends external_api {
|
|||
throw new moodle_exception('accessdenied', 'admin');
|
||||
}
|
||||
|
||||
message_mark_all_read_for_user($useridto, $useridfrom, MESSAGE_TYPE_MESSAGE);
|
||||
\core_message\api::mark_all_read_for_user($useridto, $useridfrom, MESSAGE_TYPE_MESSAGE);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -257,7 +257,7 @@ if (!$user2realuser) {
|
|||
// Mark the conversation as read.
|
||||
if ($currentuser) {
|
||||
$contact->isread = 1;
|
||||
message_mark_all_read_for_user($user1->id, $otheruserid);
|
||||
\core_message\api::mark_all_read_for_user($user1->id, $otheruserid);
|
||||
}
|
||||
|
||||
$messages = \core_message\api::get_messages($user1->id, $otheruserid, 0, 20, 'timecreated DESC');
|
||||
|
@ -267,7 +267,7 @@ if (!$user2realuser) {
|
|||
} else {
|
||||
// Mark the conversation as read.
|
||||
if ($currentuser) {
|
||||
message_mark_all_read_for_user($user1->id, $user2->id);
|
||||
\core_message\api::mark_all_read_for_user($user1->id, $user2->id);
|
||||
}
|
||||
|
||||
$conversations = \core_message\api::get_conversations($user1->id, $user2->id, 0, 20);
|
||||
|
|
261
message/lib.php
261
message/lib.php
|
@ -289,27 +289,6 @@ function message_count_unread_messages($user1=null, $user2=null) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the count of unread conversations (collection of messages from a single user) for
|
||||
* the given user.
|
||||
*
|
||||
* @param object $user the user who's conversations should be counted
|
||||
* @return in the count of $user's unread conversations
|
||||
*/
|
||||
function message_count_unread_conversations($user = null) {
|
||||
global $USER, $DB;
|
||||
|
||||
if (empty($user)) {
|
||||
$user = $USER;
|
||||
}
|
||||
|
||||
return $DB->count_records_select(
|
||||
'message',
|
||||
'useridto = ? AND timeusertodeleted = 0 AND notification = 0',
|
||||
[$user->id],
|
||||
"COUNT(DISTINCT(useridfrom))");
|
||||
}
|
||||
|
||||
/**
|
||||
* Count the number of users blocked by $user1
|
||||
*
|
||||
|
@ -1519,49 +1498,9 @@ function message_move_userfrom_unread2read($userid) {
|
|||
* @return void
|
||||
*/
|
||||
function message_mark_messages_read($touserid, $fromuserid) {
|
||||
return message_mark_all_read_for_user($touserid, $fromuserid);
|
||||
return \core_message\api::mark_all_read_for_user($touserid, $fromuserid);
|
||||
}
|
||||
|
||||
/**
|
||||
* marks ALL messages being sent from $fromuserid to $touserid as read. Can
|
||||
* be filtered by type.
|
||||
*
|
||||
* @param int $touserid the id of the message recipient
|
||||
* @param int $fromuserid the id of the message sender
|
||||
* @param string $type filter the messages by type, either MESSAGE_TYPE_NOTIFICATION, MESSAGE_TYPE_MESSAGE or '' for all.
|
||||
* @return void
|
||||
*/
|
||||
function message_mark_all_read_for_user($touserid, $fromuserid = 0, $type = '') {
|
||||
global $DB;
|
||||
|
||||
$params = array();
|
||||
$where = '';
|
||||
|
||||
if (!empty($touserid)) {
|
||||
$params['useridto'] = $touserid;
|
||||
}
|
||||
|
||||
if (!empty($fromuserid)) {
|
||||
$params['useridfrom'] = $fromuserid;
|
||||
}
|
||||
|
||||
if (!empty($type)) {
|
||||
if (strtolower($type) == MESSAGE_TYPE_NOTIFICATION) {
|
||||
$params['notification'] = 1;
|
||||
} else if (strtolower($type) == MESSAGE_TYPE_MESSAGE) {
|
||||
$params['notification'] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
$sql = sprintf('SELECT m.* FROM {message} m WHERE m.%s = ?', implode('= ? AND m.', array_keys($params)));
|
||||
$messages = $DB->get_recordset_sql($sql, array_values($params));
|
||||
|
||||
foreach ($messages as $message) {
|
||||
message_mark_message_read($message, time());
|
||||
}
|
||||
|
||||
$messages->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark a single message as read
|
||||
|
@ -1854,162 +1793,11 @@ function message_get_messages($useridto, $useridfrom = 0, $notifications = -1, $
|
|||
return $messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get popup notifications for the specified users.
|
||||
*
|
||||
* @param int $useridto the user id who received the notification
|
||||
* @param bool $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 array of notification records
|
||||
* @since 3.2
|
||||
*/
|
||||
function message_get_popup_notifications($useridto = 0, $status = '',
|
||||
$embeduserto = false, $embeduserfrom = false, $sort = 'DESC', $limit = 0, $offset = 0) {
|
||||
global $DB;
|
||||
|
||||
if (!empty($status) && $status != MESSAGE_READ && $status != MESSAGE_UNREAD) {
|
||||
throw new moodle_exception(sprintf('invalid parameter: status: must be "%s" or "%s"',
|
||||
MESSAGE_READ, MESSAGE_UNREAD));
|
||||
}
|
||||
|
||||
$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 = array();
|
||||
|
||||
$buildtablesql = function($table, $prefix, $additionalfields, $messagestatus)
|
||||
use ($status, $useridto, $embeduserto, $embeduserfrom) {
|
||||
|
||||
$joinsql = '';
|
||||
$fields = "concat('$prefix', $prefix.id) as uniqueid, $prefix.id, $prefix.useridfrom, $prefix.useridto,
|
||||
$prefix.subject, $prefix.fullmessage, $prefix.fullmessageformat,
|
||||
$prefix.fullmessagehtml, $prefix.smallmessage, $prefix.notification, $prefix.contexturl,
|
||||
$prefix.contexturlname, $prefix.timecreated, $prefix.timeuserfromdeleted, $prefix.timeusertodeleted,
|
||||
$prefix.component, $prefix.eventtype, $additionalfields";
|
||||
$where = " AND $prefix.useridto = :{$prefix}useridto";
|
||||
$params = ["{$prefix}useridto" => $useridto];
|
||||
|
||||
if ($embeduserto) {
|
||||
$embedprefix = "{$prefix}ut";
|
||||
$fields .= ", " . get_all_user_name_fields(true, $embedprefix, '', 'userto');
|
||||
$joinsql .= " LEFT JOIN {user} $embedprefix ON $embedprefix.id = $prefix.useridto";
|
||||
}
|
||||
|
||||
if ($embeduserfrom) {
|
||||
$embedprefix = "{$prefix}uf";
|
||||
$fields .= ", " . get_all_user_name_fields(true, $embedprefix, '', 'userfrom');
|
||||
$joinsql .= " LEFT JOIN {user} $embedprefix ON $embedprefix.id = $prefix.useridfrom";
|
||||
}
|
||||
|
||||
if ($messagestatus == MESSAGE_READ) {
|
||||
$isread = '1';
|
||||
} else {
|
||||
$isread = '0';
|
||||
}
|
||||
|
||||
return array(
|
||||
sprintf(
|
||||
"SELECT %s
|
||||
FROM %s %s %s
|
||||
WHERE %s.notification = 1
|
||||
AND %s.id IN (SELECT messageid FROM {message_popup} WHERE isread = %s)
|
||||
%s",
|
||||
$fields, $table, $prefix, $joinsql, $prefix, $prefix, $isread, $where
|
||||
),
|
||||
$params
|
||||
);
|
||||
};
|
||||
|
||||
$sql = '';
|
||||
switch ($status) {
|
||||
case MESSAGE_READ:
|
||||
list($sql, $readparams) = $buildtablesql('{message_read}', 'r', 'r.timeread', MESSAGE_READ);
|
||||
$params = array_merge($params, $readparams);
|
||||
break;
|
||||
case MESSAGE_UNREAD:
|
||||
list($sql, $unreadparams) = $buildtablesql('{message}', 'u', '0 as timeread', MESSAGE_UNREAD);
|
||||
$params = array_merge($params, $unreadparams);
|
||||
break;
|
||||
default:
|
||||
list($readsql, $readparams) = $buildtablesql('{message_read}', 'r', 'r.timeread', MESSAGE_READ);
|
||||
list($unreadsql, $unreadparams) = $buildtablesql('{message}', 'u', '0 as timeread', MESSAGE_UNREAD);
|
||||
$sql = sprintf("SELECT * FROM (%s UNION %s) f", $readsql, $unreadsql);
|
||||
$params = array_merge($params, $readparams, $unreadparams);
|
||||
}
|
||||
|
||||
$sql .= " 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
|
||||
*/
|
||||
function message_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]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Requires the JS libraries for the toggle contact button.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function message_togglecontact_requirejs() {
|
||||
global $PAGE;
|
||||
|
||||
static $done = false;
|
||||
if ($done) {
|
||||
return;
|
||||
}
|
||||
|
||||
$PAGE->requires->js_call_amd('core_message/toggle_contact_button', 'enhance', array('#toggle-contact-button'));
|
||||
$done = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the attributes to place on a contact button.
|
||||
*
|
||||
* @param object $user User object.
|
||||
* @param bool $iscontact
|
||||
* @return void
|
||||
*/
|
||||
function message_togglecontact_link_params($user, $iscontact = false) {
|
||||
$params = array(
|
||||
'data-userid' => $user->id,
|
||||
'data-is-contact' => $iscontact,
|
||||
'id' => 'toggle-contact-button',
|
||||
'role' => 'button',
|
||||
'class' => 'ajax-contact-button',
|
||||
);
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if a user is permitted to send another user a private message.
|
||||
|
@ -2110,44 +1898,13 @@ function message_is_user_blocked($recipient, $sender = null) {
|
|||
return false;
|
||||
}
|
||||
|
||||
function get_providers_preferences($providers, $userid) {
|
||||
$preferences = new stdClass();
|
||||
|
||||
/// Get providers preferences
|
||||
foreach ($providers as $provider) {
|
||||
foreach (array('loggedin', 'loggedoff') as $state) {
|
||||
$linepref = get_user_preferences('message_provider_'.$provider->component.'_'.$provider->name.'_'.$state, '', $userid);
|
||||
if ($linepref == ''){
|
||||
continue;
|
||||
}
|
||||
$lineprefarray = explode(',', $linepref);
|
||||
$preferences->{$provider->component.'_'.$provider->name.'_'.$state} = array();
|
||||
foreach ($lineprefarray as $pref) {
|
||||
$preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$pref] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $preferences;
|
||||
}
|
||||
|
||||
function get_all_message_preferences($processors, $providers, $user) {
|
||||
$preferences = get_providers_preferences($providers, $user->id);
|
||||
$preferences->userdefaultemail = $user->email;//may be displayed by the email processor
|
||||
|
||||
/// For every processors put its options on the form (need to get function from processor's lib.php)
|
||||
foreach ($processors as $processor) {
|
||||
$processor->object->load_data($preferences, $user->id);
|
||||
}
|
||||
|
||||
//load general messaging preferences
|
||||
$preferences->blocknoncontacts = get_user_preferences('message_blocknoncontacts', '', $user->id);
|
||||
$preferences->mailformat = $user->mailformat;
|
||||
$preferences->mailcharset = get_user_preferences('mailcharset', '', $user->id);
|
||||
|
||||
return $preferences;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles displaying processor settings in a fragment.
|
||||
*
|
||||
* @param array $args
|
||||
* @return bool|string
|
||||
* @throws moodle_exception
|
||||
*/
|
||||
function message_output_fragment_processor_settings($args = []) {
|
||||
global $PAGE;
|
||||
|
||||
|
@ -2167,7 +1924,7 @@ function message_output_fragment_processor_settings($args = []) {
|
|||
$providers = message_get_providers_for_user($userid);
|
||||
$processorwrapper = new stdClass();
|
||||
$processorwrapper->object = $processor;
|
||||
$preferences = get_all_message_preferences([$processorwrapper], $providers, $user);
|
||||
$preferences = \core_message\api::get_all_message_preferences([$processorwrapper], $providers, $user);
|
||||
|
||||
$processoroutput = new \core_message\output\preferences\processor($processor, $preferences, $user, $type);
|
||||
$renderer = $PAGE->get_renderer('core', 'message');
|
||||
|
|
|
@ -224,7 +224,7 @@ class core_message_renderer extends plugin_renderer_base {
|
|||
public function render_user_notification_preferences($user) {
|
||||
$processors = get_message_processors();
|
||||
$providers = message_get_providers_for_user($user->id);
|
||||
$preferences = get_all_message_preferences($processors, $providers, $user);
|
||||
$preferences = \core_message\api::get_all_message_preferences($processors, $providers, $user);
|
||||
$notificationlistoutput = new \core_message\output\preferences\notification_list($processors, $providers, $preferences, $user);
|
||||
|
||||
return $this->render_from_template('message/preferences_notifications_list', $notificationlistoutput->export_for_template($this));
|
||||
|
@ -250,7 +250,7 @@ class core_message_renderer extends plugin_renderer_base {
|
|||
$providers = array_filter(message_get_providers_for_user($user->id), function($provider) {
|
||||
return $provider->component === 'moodle';
|
||||
});
|
||||
$preferences = get_all_message_preferences($readyprocessors, $providers, $user);
|
||||
$preferences = \core_message\api::get_all_message_preferences($readyprocessors, $providers, $user);
|
||||
$notificationlistoutput = new \core_message\output\preferences\message_notification_list($readyprocessors, $providers, $preferences, $user);
|
||||
$context = $notificationlistoutput->export_for_template($this);
|
||||
$context['blocknoncontacts'] = get_user_preferences('message_blocknoncontacts', '', $user->id) ? true : false;
|
||||
|
|
334
message/tests/api_test.php
Normal file
334
message/tests/api_test.php
Normal file
|
@ -0,0 +1,334 @@
|
|||
<?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 API.
|
||||
*
|
||||
* @package core_message
|
||||
* @category test
|
||||
* @copyright 2016 Mark Nelson <markn@moodle.com>
|
||||
* @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');
|
||||
|
||||
/**
|
||||
* Test message API.
|
||||
*
|
||||
* @package core_message
|
||||
* @category test
|
||||
* @copyright 2016 Mark Nelson <markn@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class core_message_api_testcase extends core_message_messagelib_testcase {
|
||||
|
||||
public function test_message_mark_all_read_for_user_touser() {
|
||||
$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);
|
||||
$this->send_fake_message($sender, $recipient);
|
||||
$this->send_fake_message($sender, $recipient);
|
||||
|
||||
\core_message\api::mark_all_read_for_user($recipient->id);
|
||||
$this->assertEquals(message_count_unread_messages($recipient), 0);
|
||||
}
|
||||
|
||||
public function test_message_mark_all_read_for_user_touser_with_fromuser() {
|
||||
$sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
|
||||
$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);
|
||||
$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);
|
||||
$this->send_fake_message($sender2, $recipient);
|
||||
$this->send_fake_message($sender2, $recipient);
|
||||
|
||||
\core_message\api::mark_all_read_for_user($recipient->id, $sender1->id);
|
||||
$this->assertEquals(message_count_unread_messages($recipient), 6);
|
||||
}
|
||||
|
||||
public function test_message_mark_all_read_for_user_touser_with_type() {
|
||||
$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);
|
||||
$this->send_fake_message($sender, $recipient);
|
||||
$this->send_fake_message($sender, $recipient);
|
||||
|
||||
\core_message\api::mark_all_read_for_user($recipient->id, 0, MESSAGE_TYPE_NOTIFICATION);
|
||||
$this->assertEquals(message_count_unread_messages($recipient), 3);
|
||||
|
||||
\core_message\api::mark_all_read_for_user($recipient->id, 0, MESSAGE_TYPE_MESSAGE);
|
||||
$this->assertEquals(message_count_unread_messages($recipient), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the get_popup_notifications function will return only read notifications if requested.
|
||||
*/
|
||||
public function test_message_get_popup_notifications_read_only() {
|
||||
$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', 2);
|
||||
$this->send_fake_read_popup_notification($sender, $recipient, 'Message 2', 4);
|
||||
|
||||
$notifications = \core_message\api::get_popup_notifications($recipient->id, MESSAGE_READ);
|
||||
|
||||
$this->assertEquals($notifications[0]->fullmessage, 'Message 2');
|
||||
$this->assertEquals($notifications[1]->fullmessage, 'Message 1');
|
||||
|
||||
// Check if we request read and unread but there are only read messages, it should
|
||||
// still return those correctly.
|
||||
$notifications = \core_message\api::get_popup_notifications($recipient->id, '');
|
||||
|
||||
$this->assertEquals($notifications[0]->fullmessage, 'Message 2');
|
||||
$this->assertEquals($notifications[1]->fullmessage, 'Message 1');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the get_popup_notifications function will return only unread notifications if requested.
|
||||
*/
|
||||
public function test_message_get_popup_notifications_unread_only() {
|
||||
$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, 'Message 1', 2);
|
||||
$this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 4);
|
||||
|
||||
$notifications = \core_message\api::get_popup_notifications($recipient->id, MESSAGE_UNREAD);
|
||||
|
||||
$this->assertEquals($notifications[0]->fullmessage, 'Message 2');
|
||||
$this->assertEquals($notifications[1]->fullmessage, 'Message 1');
|
||||
|
||||
// Check if we request read and unread but there are only read messages, it should
|
||||
// still return those correctly.
|
||||
$notifications = \core_message\api::get_popup_notifications($recipient->id, '');
|
||||
|
||||
$this->assertEquals($notifications[0]->fullmessage, 'Message 2');
|
||||
$this->assertEquals($notifications[1]->fullmessage, 'Message 1');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the get_popup_notifications function will return the correct notifications when both
|
||||
* read and unread notifications are included.
|
||||
*/
|
||||
public function test_message_get_popup_notifications_mixed() {
|
||||
$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');
|
||||
|
||||
$notifications = \core_message\api::get_popup_notifications($recipient->id, MESSAGE_READ);
|
||||
|
||||
$this->assertEquals($notifications[0]->fullmessage, 'Message 4');
|
||||
$this->assertEquals($notifications[1]->fullmessage, 'Message 3');
|
||||
$this->assertEquals($notifications[2]->fullmessage, 'Message 1');
|
||||
|
||||
$notifications = \core_message\api::get_popup_notifications($recipient->id, MESSAGE_UNREAD);
|
||||
|
||||
$this->assertEquals($notifications[0]->fullmessage, 'Message 5');
|
||||
$this->assertEquals($notifications[1]->fullmessage, 'Message 2');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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_with_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, '', false, false, '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, '', false, false, '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, '', false, false, '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 that the get_popup_notifications function returns embedded user details for the
|
||||
* sender if requested.
|
||||
*/
|
||||
public function test_message_get_popup_notifications_embed_sender() {
|
||||
$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);
|
||||
|
||||
$notifications = \core_message\api::get_popup_notifications($recipient->id, '', false, true, 'DESC');
|
||||
|
||||
$func = function($type) {
|
||||
return function($notification) use ($type) {
|
||||
$user = new stdClass();
|
||||
$user = username_load_fields_from_object($user, $notification, $type);
|
||||
return $user;
|
||||
};
|
||||
};
|
||||
$senders = array_map($func('userfrom'), $notifications);
|
||||
$recipients = array_map($func('userto'), $notifications);
|
||||
|
||||
$this->assertEquals($senders[0]->firstname, 'Test1');
|
||||
$this->assertEquals($senders[0]->lastname, 'User1');
|
||||
$this->assertEquals($senders[1]->firstname, 'Test1');
|
||||
$this->assertEquals($senders[1]->lastname, 'User1');
|
||||
|
||||
// Make sure we didn't get recipient details when they weren't requested.
|
||||
$this->assertEmpty($recipients[0]->firstname);
|
||||
$this->assertEmpty($recipients[0]->lastname);
|
||||
$this->assertEmpty($recipients[1]->firstname);
|
||||
$this->assertEmpty($recipients[1]->lastname);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the get_popup_notifications function returns embedded user details for the
|
||||
* recipient if requested.
|
||||
*/
|
||||
public function test_message_get_popup_notifications_embed_recipient() {
|
||||
$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);
|
||||
|
||||
$notifications = \core_message\api::get_popup_notifications($recipient->id, '', true, false, 'DESC');
|
||||
|
||||
$func = function($type) {
|
||||
return function($notification) use ($type) {
|
||||
$user = new stdClass();
|
||||
$user = username_load_fields_from_object($user, $notification, $type);
|
||||
return $user;
|
||||
};
|
||||
};
|
||||
$senders = array_map($func('userfrom'), $notifications);
|
||||
$recipients = array_map($func('userto'), $notifications);
|
||||
|
||||
$this->assertEquals($recipients[0]->firstname, 'Test2');
|
||||
$this->assertEquals($recipients[0]->lastname, 'User2');
|
||||
$this->assertEquals($recipients[1]->firstname, 'Test2');
|
||||
$this->assertEquals($recipients[1]->lastname, 'User2');
|
||||
|
||||
// Make sure we didn't get sender details when they weren't requested.
|
||||
$this->assertEmpty($senders[0]->firstname);
|
||||
$this->assertEmpty($senders[0]->lastname);
|
||||
$this->assertEmpty($senders[1]->firstname);
|
||||
$this->assertEmpty($senders[1]->lastname);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the get_popup_notifications function returns embedded all user details.
|
||||
*/
|
||||
public function test_message_get_popup_notifications_embed_both() {
|
||||
$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);
|
||||
|
||||
$notifications = \core_message\api::get_popup_notifications($recipient->id, '', true, true, 'DESC');
|
||||
|
||||
$func = function($type) {
|
||||
return function($notification) use ($type) {
|
||||
$user = new stdClass();
|
||||
$user = username_load_fields_from_object($user, $notification, $type);
|
||||
return $user;
|
||||
};
|
||||
};
|
||||
$senders = array_map($func('userfrom'), $notifications);
|
||||
$recipients = array_map($func('userto'), $notifications);
|
||||
|
||||
$this->assertEquals($recipients[0]->firstname, 'Test2');
|
||||
$this->assertEquals($recipients[0]->lastname, 'User2');
|
||||
$this->assertEquals($recipients[1]->firstname, 'Test2');
|
||||
$this->assertEquals($recipients[1]->lastname, 'User2');
|
||||
|
||||
// Make sure we didn't get sender details when they weren't requested.
|
||||
$this->assertEquals($senders[0]->firstname, 'Test1');
|
||||
$this->assertEquals($senders[0]->lastname, 'User1');
|
||||
$this->assertEquals($senders[1]->firstname, 'Test1');
|
||||
$this->assertEquals($senders[1]->lastname, 'User1');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
|
@ -944,297 +944,5 @@ class core_message_messagelib_testcase extends advanced_testcase {
|
|||
$this->assertTrue(message_is_user_blocked($recipient, $sender));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the message_get_popup_notifications function will return only read notifications if requested.
|
||||
*/
|
||||
public function test_message_get_popup_notifications_read_only() {
|
||||
$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', 2);
|
||||
$this->send_fake_read_popup_notification($sender, $recipient, 'Message 2', 4);
|
||||
|
||||
$notifications = message_get_popup_notifications($recipient->id, MESSAGE_READ);
|
||||
|
||||
$this->assertEquals($notifications[0]->fullmessage, 'Message 2');
|
||||
$this->assertEquals($notifications[1]->fullmessage, 'Message 1');
|
||||
|
||||
// Check if we request read and unread but there are only read messages, it should
|
||||
// still return those correctly.
|
||||
$notifications = message_get_popup_notifications($recipient->id, '');
|
||||
|
||||
$this->assertEquals($notifications[0]->fullmessage, 'Message 2');
|
||||
$this->assertEquals($notifications[1]->fullmessage, 'Message 1');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the message_get_popup_notifications function will return only unread notifications if requested.
|
||||
*/
|
||||
public function test_message_get_popup_notifications_unread_only() {
|
||||
$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, 'Message 1', 2);
|
||||
$this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 4);
|
||||
|
||||
$notifications = message_get_popup_notifications($recipient->id, MESSAGE_UNREAD);
|
||||
|
||||
$this->assertEquals($notifications[0]->fullmessage, 'Message 2');
|
||||
$this->assertEquals($notifications[1]->fullmessage, 'Message 1');
|
||||
|
||||
// Check if we request read and unread but there are only read messages, it should
|
||||
// still return those correctly.
|
||||
$notifications = message_get_popup_notifications($recipient->id, '');
|
||||
|
||||
$this->assertEquals($notifications[0]->fullmessage, 'Message 2');
|
||||
$this->assertEquals($notifications[1]->fullmessage, 'Message 1');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the message_get_popup_notifications function will return the correct notifications when both
|
||||
* read and unread notifications are included.
|
||||
*/
|
||||
public function test_message_get_popup_notifications_mixed() {
|
||||
$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_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');
|
||||
|
||||
$notifications = message_get_popup_notifications($recipient->id, MESSAGE_READ);
|
||||
|
||||
$this->assertEquals($notifications[0]->fullmessage, 'Message 4');
|
||||
$this->assertEquals($notifications[1]->fullmessage, 'Message 3');
|
||||
$this->assertEquals($notifications[2]->fullmessage, 'Message 1');
|
||||
|
||||
$notifications = message_get_popup_notifications($recipient->id, MESSAGE_UNREAD);
|
||||
|
||||
$this->assertEquals($notifications[0]->fullmessage, 'Message 5');
|
||||
$this->assertEquals($notifications[1]->fullmessage, 'Message 2');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the message_get_popup_notifications function works correctly with limiting and offsetting
|
||||
* the result set if requested.
|
||||
*/
|
||||
public function test_message_get_popup_notifications_all_with_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_get_popup_notifications($recipient->id, '', false, false, 'DESC', 2, 0);
|
||||
|
||||
$this->assertEquals($notifications[0]->fullmessage, 'Message 6');
|
||||
$this->assertEquals($notifications[1]->fullmessage, 'Message 5');
|
||||
|
||||
$notifications = message_get_popup_notifications($recipient->id, '', false, false, 'DESC', 2, 2);
|
||||
|
||||
$this->assertEquals($notifications[0]->fullmessage, 'Message 4');
|
||||
$this->assertEquals($notifications[1]->fullmessage, 'Message 3');
|
||||
|
||||
$notifications = message_get_popup_notifications($recipient->id, '', false, false, '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 that the message_get_popup_notifications function returns embedded user details for the
|
||||
* sender if requested.
|
||||
*/
|
||||
public function test_message_get_popup_notifications_embed_sender() {
|
||||
$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);
|
||||
|
||||
$notifications = message_get_popup_notifications($recipient->id, '', false, true, 'DESC');
|
||||
|
||||
$func = function($type) {
|
||||
return function($notification) use ($type) {
|
||||
$user = new stdClass();
|
||||
$user = username_load_fields_from_object($user, $notification, $type);
|
||||
return $user;
|
||||
};
|
||||
};
|
||||
$senders = array_map($func('userfrom'), $notifications);
|
||||
$recipients = array_map($func('userto'), $notifications);
|
||||
|
||||
$this->assertEquals($senders[0]->firstname, 'Test1');
|
||||
$this->assertEquals($senders[0]->lastname, 'User1');
|
||||
$this->assertEquals($senders[1]->firstname, 'Test1');
|
||||
$this->assertEquals($senders[1]->lastname, 'User1');
|
||||
|
||||
// Make sure we didn't get recipient details when they weren't requested.
|
||||
$this->assertEmpty($recipients[0]->firstname);
|
||||
$this->assertEmpty($recipients[0]->lastname);
|
||||
$this->assertEmpty($recipients[1]->firstname);
|
||||
$this->assertEmpty($recipients[1]->lastname);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the message_get_popup_notifications function returns embedded user details for the
|
||||
* recipient if requested.
|
||||
*/
|
||||
public function test_message_get_popup_notifications_embed_recipient() {
|
||||
$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);
|
||||
|
||||
$notifications = message_get_popup_notifications($recipient->id, '', true, false, 'DESC');
|
||||
|
||||
$func = function($type) {
|
||||
return function($notification) use ($type) {
|
||||
$user = new stdClass();
|
||||
$user = username_load_fields_from_object($user, $notification, $type);
|
||||
return $user;
|
||||
};
|
||||
};
|
||||
$senders = array_map($func('userfrom'), $notifications);
|
||||
$recipients = array_map($func('userto'), $notifications);
|
||||
|
||||
$this->assertEquals($recipients[0]->firstname, 'Test2');
|
||||
$this->assertEquals($recipients[0]->lastname, 'User2');
|
||||
$this->assertEquals($recipients[1]->firstname, 'Test2');
|
||||
$this->assertEquals($recipients[1]->lastname, 'User2');
|
||||
|
||||
// Make sure we didn't get sender details when they weren't requested.
|
||||
$this->assertEmpty($senders[0]->firstname);
|
||||
$this->assertEmpty($senders[0]->lastname);
|
||||
$this->assertEmpty($senders[1]->firstname);
|
||||
$this->assertEmpty($senders[1]->lastname);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the message_get_popup_notifications function returns embedded all user details.
|
||||
*/
|
||||
public function test_message_get_popup_notifications_embed_both() {
|
||||
$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);
|
||||
|
||||
$notifications = message_get_popup_notifications($recipient->id, '', true, true, 'DESC');
|
||||
|
||||
$func = function($type) {
|
||||
return function($notification) use ($type) {
|
||||
$user = new stdClass();
|
||||
$user = username_load_fields_from_object($user, $notification, $type);
|
||||
return $user;
|
||||
};
|
||||
};
|
||||
$senders = array_map($func('userfrom'), $notifications);
|
||||
$recipients = array_map($func('userto'), $notifications);
|
||||
|
||||
$this->assertEquals($recipients[0]->firstname, 'Test2');
|
||||
$this->assertEquals($recipients[0]->lastname, 'User2');
|
||||
$this->assertEquals($recipients[1]->firstname, 'Test2');
|
||||
$this->assertEquals($recipients[1]->lastname, 'User2');
|
||||
|
||||
// Make sure we didn't get sender details when they weren't requested.
|
||||
$this->assertEquals($senders[0]->firstname, 'Test1');
|
||||
$this->assertEquals($senders[0]->lastname, 'User1');
|
||||
$this->assertEquals($senders[1]->firstname, 'Test1');
|
||||
$this->assertEquals($senders[1]->lastname, 'User1');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test message_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_count_unread_popup_notifications($recipient1->id), 3);
|
||||
$this->assertEquals(message_count_unread_popup_notifications($recipient2->id), 5);
|
||||
}
|
||||
|
||||
|
||||
public function test_message_mark_all_read_for_user_touser() {
|
||||
$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);
|
||||
$this->send_fake_message($sender, $recipient);
|
||||
$this->send_fake_message($sender, $recipient);
|
||||
|
||||
message_mark_all_read_for_user($recipient->id);
|
||||
$this->assertEquals(message_count_unread_messages($recipient), 0);
|
||||
}
|
||||
|
||||
public function test_message_mark_all_read_for_user_touser_with_fromuser() {
|
||||
$sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
|
||||
$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);
|
||||
$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);
|
||||
$this->send_fake_message($sender2, $recipient);
|
||||
$this->send_fake_message($sender2, $recipient);
|
||||
|
||||
message_mark_all_read_for_user($recipient->id, $sender1->id);
|
||||
$this->assertEquals(message_count_unread_messages($recipient), 6);
|
||||
}
|
||||
|
||||
public function test_message_mark_all_read_for_user_touser_with_type() {
|
||||
$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);
|
||||
$this->send_fake_message($sender, $recipient);
|
||||
$this->send_fake_message($sender, $recipient);
|
||||
|
||||
message_mark_all_read_for_user($recipient->id, 0, MESSAGE_TYPE_NOTIFICATION);
|
||||
$this->assertEquals(message_count_unread_messages($recipient), 3);
|
||||
|
||||
message_mark_all_read_for_user($recipient->id, 0, MESSAGE_TYPE_MESSAGE);
|
||||
$this->assertEquals(message_count_unread_messages($recipient), 0);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue