Merge branch 'MDL-63261-310' of https://github.com/Chocolate-lightning/moodle into MOODLE_310_STABLE

This commit is contained in:
Sara Arjona
2020-09-07 07:30:17 +02:00
24 changed files with 138 additions and 5249 deletions
-126
View File
@@ -1104,26 +1104,6 @@ $functions = array(
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_message_block_contacts' => array(
'classname' => 'core_message_external',
'methodname' => 'block_contacts',
'classpath' => 'message/externallib.php',
'description' => '** DEPRECATED ** Please do not call this function any more.
Block contacts',
'type' => 'write',
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_message_create_contacts' => array(
'classname' => 'core_message_external',
'methodname' => 'create_contacts',
'classpath' => 'message/externallib.php',
'description' => '** DEPRECATED ** Please do not call this function any more.
Add contacts to the contact list',
'type' => 'write',
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_message_get_contact_requests' => array(
'classname' => 'core_message_external',
'methodname' => 'get_contact_requests',
@@ -1178,17 +1158,6 @@ $functions = array(
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_message_delete_conversation' => array(
'classname' => 'core_message_external',
'methodname' => 'delete_conversation',
'classpath' => 'message/externallib.php',
'description' => '** DEPRECATED ** Please do not call this function any more.
Deletes a conversation.',
'type' => 'write',
'capabilities' => 'moodle/site:deleteownmessage',
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_message_delete_conversations_by_id' => array(
'classname' => 'core_message_external',
'methodname' => 'delete_conversations_by_id',
@@ -1226,24 +1195,6 @@ $functions = array(
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_message_data_for_messagearea_search_users' => array(
'classname' => 'core_message_external',
'methodname' => 'data_for_messagearea_search_users',
'classpath' => 'message/externallib.php',
'description' => '** DEPRECATED ** Please do not call this function any more.
Retrieve the template data for searching for people',
'type' => 'read',
'ajax' => true,
),
'core_message_data_for_messagearea_search_users_in_course' => array(
'classname' => 'core_message_external',
'methodname' => 'data_for_messagearea_search_users_in_course',
'classpath' => 'message/externallib.php',
'description' => '** DEPRECATED ** Please do not call this function any more.
Retrieve the template data for searching for people in a course',
'type' => 'read',
'ajax' => true,
),
'core_message_message_search_users' => array(
'classname' => 'core_message_external',
'methodname' => 'message_search_users',
@@ -1253,63 +1204,6 @@ $functions = array(
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_message_data_for_messagearea_conversations' => array(
'classname' => 'core_message_external',
'methodname' => 'data_for_messagearea_conversations',
'classpath' => 'message/externallib.php',
'description' => '** DEPRECATED ** Please do not call this function any more.
Retrieve the template data for the conversation list',
'type' => 'read',
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_message_data_for_messagearea_contacts' => array(
'classname' => 'core_message_external',
'methodname' => 'data_for_messagearea_contacts',
'classpath' => 'message/externallib.php',
'description' => '** DEPRECATED ** Please do not call this function any more.
Retrieve the template data for the contact list',
'type' => 'read',
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_message_data_for_messagearea_messages' => array(
'classname' => 'core_message_external',
'methodname' => 'data_for_messagearea_messages',
'classpath' => 'message/externallib.php',
'description' => '** DEPRECATED ** Please do not call this function any more.
Retrieve the template data for the messages',
'type' => 'read',
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_message_data_for_messagearea_get_most_recent_message' => array(
'classname' => 'core_message_external',
'methodname' => 'data_for_messagearea_get_most_recent_message',
'classpath' => 'message/externallib.php',
'description' => '** DEPRECATED ** Please do not call this function any more.
Retrieve the template data for the most recent message',
'type' => 'read',
'ajax' => true,
),
'core_message_data_for_messagearea_get_profile' => array(
'classname' => 'core_message_external',
'methodname' => 'data_for_messagearea_get_profile',
'classpath' => 'message/externallib.php',
'description' => '** DEPRECATED ** Please do not call this function any more.
Retrieve the template data for the users\'s profile',
'type' => 'read',
'ajax' => true,
),
'core_message_get_contacts' => array(
'classname' => 'core_message_external',
'methodname' => 'get_contacts',
'classpath' => 'message/externallib.php',
'description' => '** DEPRECATED ** Please do not call this function any more.
Retrieve the contact list',
'type' => 'read',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_message_get_user_contacts' => array(
'classname' => 'core_message_external',
'methodname' => 'get_user_contacts',
@@ -1419,16 +1313,6 @@ $functions = array(
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_message_mark_all_messages_as_read' => array(
'classname' => 'core_message_external',
'methodname' => 'mark_all_messages_as_read',
'classpath' => 'message/externallib.php',
'description' => '** DEPRECATED ** Please do not call this function any more.
Mark all messages as read for a given user',
'type' => 'write',
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_message_mark_all_conversation_messages_as_read' => array(
'classname' => 'core_message_external',
'methodname' => 'mark_all_conversation_messages_as_read',
@@ -1519,16 +1403,6 @@ $functions = array(
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_message_unblock_contacts' => array(
'classname' => 'core_message_external',
'methodname' => 'unblock_contacts',
'classpath' => 'message/externallib.php',
'description' => '** DEPRECATED ** Please do not call this function any more.
Unblock contacts',
'type' => 'write',
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_message_get_user_notification_preferences' => array(
'classname' => 'core_message_external',
'methodname' => 'get_user_notification_preferences',
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -1,2 +1,2 @@
define ("core_message/toggle_contact_button",["jquery","core/ajax","core/templates","core/notification","core/custom_interaction_events"],function(a,b,c,d,e){var f=function(a){return"1"==a.attr("data-is-contact")},g=function(a){a.attr("data-is-contact","1")},h=function(a){a.attr("data-is-contact","0")},i=function(a){return a.attr("data-userid")},j=function(a){return a.hasClass("loading")||a.attr("disabled")},k=function(c,e){if(j(c)){return a.Deferred()}c.addClass("loading");c.attr("disabled","disabled");return b.call([e])[0].fail(d.exception).always(function(){c.removeClass("loading");c.removeAttr("disabled")})},l=function(a){if(j(a)){return}var b={methodname:"core_message_create_contacts",args:{userids:[i(a)]}};k(a,b).done(function(){g(a);c.render("message/remove_contact_button",{}).done(function(b,d){c.replaceNodeContents(a,b,d)})})},m=function(a){if(j(a)){return}var b={methodname:"core_message_delete_contacts",args:{userids:[i(a)]}};k(a,b).done(function(){h(a);c.render("message/add_contact_button",{}).done(function(b,d){c.replaceNodeContents(a,b,d)})})};return{enhance:function enhance(b){b=a(b);if(!b.children(".loading-icon").length){c.render("core/loading",{}).done(function(a,c){b.append(a,c)})}e.define(b,[e.events.activate]);b.on(e.events.activate,function(a,c){if(f(b)){m(b)}else{l(b)}a.preventDefault();c.originalEvent.preventDefault()})}}});
define ("core_message/toggle_contact_button",["jquery","core/ajax","core/templates","core/notification","core/custom_interaction_events"],function(a,b,c,d,e){var f=function(a){return"1"==a.attr("data-is-contact")},g=function(a){a.attr("data-is-contact","1")},h=function(a){a.attr("data-is-contact","0")},i=function(a){return a.attr("data-userid")},j=function(a){return a.attr("data-currentuserid")},k=function(a){return a.hasClass("loading")||a.attr("disabled")},l=function(c,e){if(k(c)){return a.Deferred()}c.addClass("loading");c.attr("disabled","disabled");return b.call([e])[0].fail(d.exception).always(function(){c.removeClass("loading");c.removeAttr("disabled")})},m=function(a){if(k(a)){return}var b={methodname:"core_message_create_contact_request",args:{userid:j(a),requesteduserid:i(a)}};l(a,b).done(function(){g(a);c.render("message/remove_contact_button",{}).done(function(b,d){c.replaceNodeContents(a,b,d)})})},n=function(a){if(k(a)){return}var b={methodname:"core_message_delete_contacts",args:{userids:[i(a)]}};l(a,b).done(function(){h(a);c.render("message/add_contact_button",{}).done(function(b,d){c.replaceNodeContents(a,b,d)})})};return{enhance:function enhance(b){b=a(b);if(!b.children(".loading-icon").length){c.render("core/loading",{}).done(function(a,c){b.append(a,c)})}e.define(b,[e.events.activate]);b.on(e.events.activate,function(a,c){if(f(b)){n(b)}else{m(b)}a.preventDefault();c.originalEvent.preventDefault()})}}});
//# sourceMappingURL=toggle_contact_button.min.js.map
File diff suppressed because one or more lines are too long
-83
View File
@@ -36,48 +36,6 @@ define(
var CONVERSATION_TYPES = Constants.CONVERSATION_TYPES;
/**
* Retrieve a list of messages from the server.
*
* @param {object} args The request arguments:
* @return {object} jQuery promise
*/
var query = function(args) {
// Normalise the arguments to use limit/offset rather than limitnum/limitfrom.
if (typeof args.limit === 'undefined') {
args.limit = 0;
}
if (typeof args.offset === 'undefined') {
args.offset = 0;
}
if (typeof args.type === 'undefined') {
args.type = null;
}
if (typeof args.favouritesonly === 'undefined') {
args.favouritesonly = false;
}
args.limitfrom = args.offset;
args.limitnum = args.limit;
delete args.limit;
delete args.offset;
var request = {
methodname: 'core_message_data_for_messagearea_conversations',
args: args
};
var promise = Ajax.call([request])[0];
promise.fail(Notification.exception);
return promise;
};
/**
* Count the number of unread conversations (one or more messages from a user)
* for a given user.
@@ -98,25 +56,6 @@ define(
return promise;
};
/**
* Mark all of unread messages for a user as read.
*
* @param {object} args The request arguments:
* @return {object} jQuery promise
*/
var markAllAsRead = function(args) {
var request = {
methodname: 'core_message_mark_all_messages_as_read',
args: args
};
var promise = Ajax.call([request])[0];
promise.fail(Notification.exception);
return promise;
};
/**
* Get contacts for given user.
*
@@ -146,25 +85,6 @@ define(
return Ajax.call([request])[0];
};
/**
* Request profile information as a user for a given user.
*
* @param {int} userId The requesting user
* @param {int} profileUserId The id of the user who's profile is being requested
* @return {object} jQuery promise
*/
var getProfile = function(userId, profileUserId) {
var request = {
methodname: 'core_message_data_for_messagearea_get_profile',
args: {
currentuserid: userId,
otheruserid: profileUserId
}
};
return Ajax.call([request])[0];
};
/**
* Block a user.
*
@@ -1139,11 +1059,8 @@ define(
};
return {
query: query,
countUnreadConversations: countUnreadConversations,
markAllAsRead: markAllAsRead,
getContacts: getContacts,
getProfile: getProfile,
blockUser: blockUser,
unblockUser: unblockUser,
createContactRequest: createContactRequest,
+14 -2
View File
@@ -67,6 +67,17 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust
return element.attr('data-userid');
};
/**
* Get the id for the logged in user.
*
* @method getUserId
* @param {object} element jQuery object for the button
* @return {int}
*/
var getCurrentUserId = function(element) {
return element.attr('data-currentuserid');
};
/**
* Check if this element is currently loading.
*
@@ -117,9 +128,10 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust
}
var request = {
methodname: 'core_message_create_contacts',
methodname: 'core_message_create_contact_request',
args: {
userids: [getUserId(element)],
userid: getCurrentUserId(element),
requesteduserid: getUserId(element),
}
};
sendRequest(element, request).done(function() {
+38 -391
View File
@@ -167,138 +167,17 @@ class api {
}
/**
* Handles searching for user in a particular course in the message area.
*
* TODO: This function should be removed once the related web service goes through final deprecation.
* The related web service is data_for_messagearea_search_users_in_course.
* Followup: MDL-63261
*
* @param int $userid The user id doing the searching
* @param int $courseid The id of the course we are searching in
* @param string $search The string the user is searching
* @param int $limitfrom
* @param int $limitnum
* @return array
* @deprecated since 3.6
*/
public static function search_users_in_course($userid, $courseid, $search, $limitfrom = 0, $limitnum = 0) {
global $DB;
// Get all the users in the course.
list($esql, $params) = get_enrolled_sql(\context_course::instance($courseid), '', 0, true);
$sql = "SELECT u.*, mub.id as isblocked
FROM {user} u
JOIN ($esql) je
ON je.id = u.id
LEFT JOIN {message_users_blocked} mub
ON (mub.blockeduserid = u.id AND mub.userid = :userid)
WHERE u.deleted = 0";
// Add more conditions.
$fullname = $DB->sql_fullname();
$sql .= " AND u.id != :userid2
AND " . $DB->sql_like($fullname, ':search', false) . "
ORDER BY " . $DB->sql_fullname();
$params = array_merge(array('userid' => $userid, 'userid2' => $userid, 'search' => '%' . $search . '%'), $params);
// Convert all the user records into contacts.
$contacts = array();
if ($users = $DB->get_records_sql($sql, $params, $limitfrom, $limitnum)) {
foreach ($users as $user) {
$user->blocked = $user->isblocked ? 1 : 0;
$contacts[] = helper::create_contact($user);
}
}
return $contacts;
public static function search_users_in_course() {
throw new \coding_exception('\core_message\api::search_users_in_course has been removed.');
}
/**
* Handles searching for user in the message area.
*
* TODO: This function should be removed once the related web service goes through final deprecation.
* The related web service is data_for_messagearea_search_users.
* Followup: MDL-63261
*
* @param int $userid The user id doing the searching
* @param string $search The string the user is searching
* @param int $limitnum
* @return array
* @deprecated since 3.6
*/
public static function search_users($userid, $search, $limitnum = 0) {
global $CFG, $DB;
// Used to search for contacts.
$fullname = $DB->sql_fullname();
$ufields = \user_picture::fields('u', array('lastaccess'));
// Users not to include.
$excludeusers = array($userid, $CFG->siteguest);
list($exclude, $excludeparams) = $DB->get_in_or_equal($excludeusers, SQL_PARAMS_NAMED, 'param', false);
// Ok, let's search for contacts first.
$contacts = array();
$sql = "SELECT $ufields, mub.id as isuserblocked
FROM {user} u
JOIN {message_contacts} mc
ON u.id = mc.contactid
LEFT JOIN {message_users_blocked} mub
ON (mub.userid = :userid2 AND mub.blockeduserid = u.id)
WHERE mc.userid = :userid
AND u.deleted = 0
AND u.confirmed = 1
AND " . $DB->sql_like($fullname, ':search', false) . "
AND u.id $exclude
ORDER BY " . $DB->sql_fullname();
if ($users = $DB->get_records_sql($sql, array('userid' => $userid, 'userid2' => $userid,
'search' => '%' . $search . '%') + $excludeparams, 0, $limitnum)) {
foreach ($users as $user) {
$user->blocked = $user->isuserblocked ? 1 : 0;
$contacts[] = helper::create_contact($user);
}
}
// Now, let's get the courses.
// Make sure to limit searches to enrolled courses.
$enrolledcourses = enrol_get_my_courses(array('id', 'cacherev'));
$courses = array();
// Really we want the user to be able to view the participants if they have the capability
// 'moodle/course:viewparticipants' or 'moodle/course:enrolreview', but since the search_courses function
// only takes required parameters we can't. However, the chance of a user having 'moodle/course:enrolreview' but
// *not* 'moodle/course:viewparticipants' are pretty much zero, so it is not worth addressing.
if ($arrcourses = \core_course_category::search_courses(array('search' => $search), array('limit' => $limitnum),
array('moodle/course:viewparticipants'))) {
foreach ($arrcourses as $course) {
if (isset($enrolledcourses[$course->id])) {
$data = new \stdClass();
$data->id = $course->id;
$data->shortname = $course->shortname;
$data->fullname = $course->fullname;
$courses[] = $data;
}
}
}
// Let's get those non-contacts. Toast them gears boi.
// Note - you can only block contacts, so these users will not be blocked, so no need to get that
// extra detail from the database.
$noncontacts = array();
$sql = "SELECT $ufields
FROM {user} u
WHERE u.deleted = 0
AND u.confirmed = 1
AND " . $DB->sql_like($fullname, ':search', false) . "
AND u.id $exclude
AND u.id NOT IN (SELECT contactid
FROM {message_contacts}
WHERE userid = :userid)
ORDER BY " . $DB->sql_fullname();
if ($users = $DB->get_records_sql($sql, array('userid' => $userid, 'search' => '%' . $search . '%') + $excludeparams,
0, $limitnum)) {
foreach ($users as $user) {
$noncontacts[] = helper::create_contact($user);
}
}
return array($contacts, $courses, $noncontacts);
public static function search_users() {
throw new \coding_exception('\core_message\api::search_users has been removed.');
}
/**
@@ -1070,55 +949,10 @@ class api {
}
/**
* Returns the contacts to display in the contacts area.
*
* TODO: This function should be removed once the related web service goes through final deprecation.
* The related web service is data_for_messagearea_contacts.
* Followup: MDL-63261
*
* @param int $userid The user id
* @param int $limitfrom
* @param int $limitnum
* @return array
* @deprecated since 3.6
*/
public static function get_contacts($userid, $limitfrom = 0, $limitnum = 0) {
global $DB;
$contactids = [];
$sql = "SELECT mc.*
FROM {message_contacts} mc
WHERE mc.userid = ? OR mc.contactid = ?
ORDER BY timecreated DESC";
if ($contacts = $DB->get_records_sql($sql, [$userid, $userid], $limitfrom, $limitnum)) {
foreach ($contacts as $contact) {
if ($userid == $contact->userid) {
$contactids[] = $contact->contactid;
} else {
$contactids[] = $contact->userid;
}
}
}
if (!empty($contactids)) {
list($insql, $inparams) = $DB->get_in_or_equal($contactids);
$sql = "SELECT u.*, mub.id as isblocked
FROM {user} u
LEFT JOIN {message_users_blocked} mub
ON u.id = mub.blockeduserid
WHERE u.id $insql";
if ($contacts = $DB->get_records_sql($sql, $inparams)) {
$arrcontacts = [];
foreach ($contacts as $contact) {
$contact->blocked = $contact->isblocked ? 1 : 0;
$arrcontacts[] = helper::create_contact($contact);
}
return $arrcontacts;
}
}
return [];
public static function get_contacts() {
throw new \coding_exception('\core_message\api::get_contacts has been removed.');
}
/**
@@ -1170,6 +1004,8 @@ class api {
* Returns the an array of the users the given user is in a conversation
* with who are a contact and the number of unread messages.
*
* @deprecated since 3.10
* TODO: MDL-69643
* @param int $userid The user id
* @param int $limitfrom
* @param int $limitnum
@@ -1178,6 +1014,9 @@ class api {
public static function get_contacts_with_unread_message_count($userid, $limitfrom = 0, $limitnum = 0) {
global $DB;
debugging('\core_message\api::get_contacts_with_unread_message_count is deprecated and no longer used',
DEBUG_DEVELOPER);
$userfields = \user_picture::fields('u', array('lastaccess'));
$unreadcountssql = "SELECT $userfields, count(m.id) as messagecount
FROM {message_contacts} mc
@@ -1206,6 +1045,8 @@ class api {
* Returns the an array of the users the given user is in a conversation
* with who are not a contact and the number of unread messages.
*
* @deprecated since 3.10
* TODO: MDL-69643
* @param int $userid The user id
* @param int $limitfrom
* @param int $limitnum
@@ -1214,6 +1055,9 @@ class api {
public static function get_non_contacts_with_unread_message_count($userid, $limitfrom = 0, $limitnum = 0) {
global $DB;
debugging('\core_message\api::get_non_contacts_with_unread_message_count is deprecated and no longer used',
DEBUG_DEVELOPER);
$userfields = \user_picture::fields('u', array('lastaccess'));
$unreadcountssql = "SELECT $userfields, count(m.id) as messagecount
FROM {user} u
@@ -1240,52 +1084,10 @@ class api {
}
/**
* Returns the messages to display in the message area.
*
* TODO: This function should be removed once the related web service goes through final deprecation.
* The related web service is data_for_messagearea_messages.
* Followup: MDL-63261
*
* @param int $userid the current user
* @param int $otheruserid the other user
* @param int $limitfrom
* @param int $limitnum
* @param string $sort
* @param int $timefrom the time from the message being sent
* @param int $timeto the time up until the message being sent
* @return array
* @deprecated since 3.6
*/
public static function get_messages($userid, $otheruserid, $limitfrom = 0, $limitnum = 0,
$sort = 'timecreated ASC', $timefrom = 0, $timeto = 0) {
if (!empty($timefrom)) {
// Get the conversation between userid and otheruserid.
$userids = [$userid, $otheruserid];
if (!$conversationid = self::get_conversation_between_users($userids)) {
// This method was always used for individual conversations.
$conversation = self::create_conversation(self::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, $userids);
$conversationid = $conversation->id;
}
// Check the cache to see if we even need to do a DB query.
$cache = \cache::make('core', 'message_time_last_message_between_users');
$key = helper::get_last_message_time_created_cache_key($conversationid);
$lastcreated = $cache->get($key);
// The last known message time is earlier than the one being requested so we can
// just return an empty result set rather than having to query the DB.
if ($lastcreated && $lastcreated < $timefrom) {
return [];
}
}
$arrmessages = array();
if ($messages = helper::get_messages($userid, $otheruserid, 0, $limitfrom, $limitnum,
$sort, $timefrom, $timeto)) {
$arrmessages = helper::create_messages($userid, $messages);
}
return $arrmessages;
public static function get_messages() {
throw new \coding_exception('\core_message\api::get_messages has been removed.');
}
/**
@@ -1321,26 +1123,10 @@ class api {
}
/**
* Returns the most recent message between two users.
*
* TODO: This function should be removed once the related web service goes through final deprecation.
* The related web service is data_for_messagearea_get_most_recent_message.
* Followup: MDL-63261
*
* @param int $userid the current user
* @param int $otheruserid the other user
* @return \stdClass|null
* @deprecated since 3.6
*/
public static function get_most_recent_message($userid, $otheruserid) {
// We want two messages here so we get an accurate 'blocktime' value.
if ($messages = helper::get_messages($userid, $otheruserid, 0, 0, 2, 'timecreated DESC')) {
// Swap the order so we now have them in historical order.
$messages = array_reverse($messages);
$arrmessages = helper::create_messages($userid, $messages);
return array_pop($arrmessages);
}
return null;
public static function get_most_recent_message() {
throw new \coding_exception('\core_message\api::get_most_recent_message has been removed.');
}
/**
@@ -1366,58 +1152,10 @@ class api {
}
/**
* Returns the profile information for a contact for a user.
*
* TODO: This function should be removed once the related web service goes through final deprecation.
* The related web service is data_for_messagearea_get_profile.
* Followup: MDL-63261
*
* @param int $userid The user id
* @param int $otheruserid The id of the user whose profile we want to view.
* @return \stdClass
* @deprecated since 3.6
*/
public static function get_profile($userid, $otheruserid) {
global $CFG, $PAGE;
require_once($CFG->dirroot . '/user/lib.php');
$user = \core_user::get_user($otheruserid, '*', MUST_EXIST);
// Create the data we are going to pass to the renderable.
$data = new \stdClass();
$data->userid = $otheruserid;
$data->fullname = fullname($user);
$data->city = '';
$data->country = '';
$data->email = '';
$data->isonline = null;
// Get the user picture data - messaging has always shown these to the user.
$userpicture = new \user_picture($user);
$userpicture->size = 1; // Size f1.
$data->profileimageurl = $userpicture->get_url($PAGE)->out(false);
$userpicture->size = 0; // Size f2.
$data->profileimageurlsmall = $userpicture->get_url($PAGE)->out(false);
$userfields = user_get_user_details($user, null, array('city', 'country', 'email', 'lastaccess'));
if ($userfields) {
if (isset($userfields['city'])) {
$data->city = $userfields['city'];
}
if (isset($userfields['country'])) {
$data->country = $userfields['country'];
}
if (isset($userfields['email'])) {
$data->email = $userfields['email'];
}
if (isset($userfields['lastaccess'])) {
$data->isonline = helper::is_online($userfields['lastaccess']);
}
}
$data->isblocked = self::is_blocked($userid, $otheruserid);
$data->iscontact = self::is_contact($userid, $otheruserid);
return $data;
public static function get_profile() {
throw new \coding_exception('\core_message\api::get_profile has been removed.');
}
/**
@@ -1456,30 +1194,11 @@ class api {
}
/**
* Deletes a conversation.
*
* This function does not verify any permissions.
*
* @deprecated since 3.6
* @param int $userid The user id of who we want to delete the messages for (this may be done by the admin
* but will still seem as if it was by the user)
* @param int $otheruserid The id of the other user in the conversation
* @return bool
*/
public static function delete_conversation($userid, $otheruserid) {
debugging('\core_message\api::delete_conversation() is deprecated, please use ' .
'\core_message\api::delete_conversation_by_id() instead.', DEBUG_DEVELOPER);
$conversationid = self::get_conversation_between_users([$userid, $otheruserid]);
// If there is no conversation, there is nothing to do.
if (!$conversationid) {
return true;
}
self::delete_conversation_by_id($userid, $conversationid);
return true;
public static function delete_conversation() {
throw new \coding_exception('\core_message\api::delete_conversation() is deprecated, please use ' .
'\core_message\api::delete_conversation_by_id() instead.');
}
/**
@@ -2028,80 +1747,17 @@ class api {
}
/**
* Checks if the recipient is allowing messages from users that aren't a
* contact. If not then it checks to make sure the sender is in the
* recipient's contacts.
*
* @deprecated since 3.6
* @param \stdClass $recipient The user object.
* @param \stdClass|null $sender The user object.
* @return bool true if $sender is blocked, false otherwise.
*/
public static function is_user_non_contact_blocked($recipient, $sender = null) {
debugging('\core_message\api::is_user_non_contact_blocked() is deprecated', DEBUG_DEVELOPER);
global $USER, $CFG;
if (is_null($sender)) {
// The message is from the logged in user, unless otherwise specified.
$sender = $USER;
}
$privacypreference = self::get_user_privacy_messaging_preference($recipient->id);
switch ($privacypreference) {
case self::MESSAGE_PRIVACY_SITE:
if (!empty($CFG->messagingallusers)) {
// Users can be messaged without being contacts or members of the same course.
break;
}
// When the $CFG->messagingallusers privacy setting is disabled, continue with the next
// case, because MESSAGE_PRIVACY_SITE is replaced to MESSAGE_PRIVACY_COURSEMEMBER.
case self::MESSAGE_PRIVACY_COURSEMEMBER:
// Confirm the sender and the recipient are both members of the same course.
if (enrol_sharing_course($recipient, $sender)) {
// All good, the recipient and the sender are members of the same course.
return false;
}
case self::MESSAGE_PRIVACY_ONLYCONTACTS:
// True if they aren't contacts (they can't send a message because of the privacy settings), false otherwise.
return !self::is_contact($sender->id, $recipient->id);
}
return false;
public static function is_user_non_contact_blocked() {
throw new \coding_exception('\core_message\api::is_user_non_contact_blocked() is deprecated');
}
/**
* Checks if the recipient has specifically blocked the sending user.
*
* Note: This function will always return false if the sender has the
* readallmessages capability at the system context level.
*
* @deprecated since 3.6
* @param int $recipientid User ID of the recipient.
* @param int $senderid User ID of the sender.
* @return bool true if $sender is blocked, false otherwise.
*/
public static function is_user_blocked($recipientid, $senderid = null) {
debugging('\core_message\api::is_user_blocked is deprecated and should not be used.',
DEBUG_DEVELOPER);
global $USER;
if (is_null($senderid)) {
// The message is from the logged in user, unless otherwise specified.
$senderid = $USER->id;
}
$systemcontext = \context_system::instance();
if (has_capability('moodle/site:readallmessages', $systemcontext, $senderid)) {
return false;
}
if (self::is_blocked($recipientid, $senderid)) {
return true;
}
return false;
public static function is_user_blocked() {
throw new \coding_exception('\core_message\api::is_user_blocked is deprecated and should not be used.');
}
/**
@@ -2465,20 +2121,11 @@ class api {
}
/**
* Creates a conversation between two users.
*
* @deprecated since 3.6
* @param array $userids
* @return int The id of the conversation
*/
public static function create_conversation_between_users(array $userids) {
debugging('\core_message\api::create_conversation_between_users is deprecated, please use ' .
'\core_message\api::create_conversation instead.', DEBUG_DEVELOPER);
// This method was always used for individual conversations.
$conversation = self::create_conversation(self::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, $userids);
return $conversation->id;
public static function create_conversation_between_users() {
throw new \coding_exception('\core_message\api::create_conversation_between_users is deprecated, please use ' .
'\core_message\api::create_conversation instead.');
}
/**
+11 -162
View File
@@ -37,82 +37,10 @@ require_once($CFG->dirroot . '/message/lib.php');
class helper {
/**
* Helper function to retrieve the messages between two users
*
* TODO: This function should be removed once the related web services go through final deprecation.
* The related web services are data_for_messagearea_messages AND data_for_messagearea_get_most_recent_message.
* Followup: MDL-63261
*
* @param int $userid the current user
* @param int $otheruserid the other user
* @param int $timedeleted the time the message was deleted
* @param int $limitfrom
* @param int $limitnum
* @param string $sort
* @param int $timefrom the time from the message being sent
* @param int $timeto the time up until the message being sent
* @return array of messages
* @deprecated since 3.6
*/
public static function get_messages($userid, $otheruserid, $timedeleted = 0, $limitfrom = 0, $limitnum = 0,
$sort = 'timecreated ASC', $timefrom = 0, $timeto = 0) {
global $DB;
$hash = self::get_conversation_hash([$userid, $otheruserid]);
$sql = "SELECT m.id, m.useridfrom, m.subject, m.fullmessage, m.fullmessagehtml,
m.fullmessageformat, m.fullmessagetrust, m.smallmessage, m.timecreated,
mc.contextid, muaread.timecreated AS timeread
FROM {message_conversations} mc
INNER JOIN {messages} m
ON m.conversationid = mc.id
LEFT JOIN {message_user_actions} muaread
ON (muaread.messageid = m.id
AND muaread.userid = :userid1
AND muaread.action = :readaction)";
$params = ['userid1' => $userid, 'readaction' => api::MESSAGE_ACTION_READ, 'convhash' => $hash];
if (empty($timedeleted)) {
$sql .= " LEFT JOIN {message_user_actions} mua
ON (mua.messageid = m.id
AND mua.userid = :userid2
AND mua.action = :deleteaction
AND mua.timecreated is NOT NULL)";
} else {
$sql .= " INNER JOIN {message_user_actions} mua
ON (mua.messageid = m.id
AND mua.userid = :userid2
AND mua.action = :deleteaction
AND mua.timecreated = :timedeleted)";
$params['timedeleted'] = $timedeleted;
}
$params['userid2'] = $userid;
$params['deleteaction'] = api::MESSAGE_ACTION_DELETED;
$sql .= " WHERE mc.convhash = :convhash";
if (!empty($timefrom)) {
$sql .= " AND m.timecreated >= :timefrom";
$params['timefrom'] = $timefrom;
}
if (!empty($timeto)) {
$sql .= " AND m.timecreated <= :timeto";
$params['timeto'] = $timeto;
}
if (empty($timedeleted)) {
$sql .= " AND mua.id is NULL";
}
$sql .= " ORDER BY m.$sort";
$messages = $DB->get_records_sql($sql, $params, $limitfrom, $limitnum);
foreach ($messages as &$message) {
$message->useridto = ($message->useridfrom == $userid) ? $otheruserid : $userid;
}
return $messages;
public static function get_messages() {
throw new \coding_exception('\core_message\helper::get_messages has been removed.');
}
/**
@@ -234,55 +162,10 @@ class helper {
}
/**
* Helper function to return an array of messages.
*
* TODO: This function should be removed once the related web services go through final deprecation.
* The related web services are data_for_messagearea_messages AND data_for_messagearea_get_most_recent_message.
* Followup: MDL-63261
*
* @param int $userid
* @param array $messages
* @return array
* @deprecated since 3.6
*/
public static function create_messages($userid, $messages) {
// Store the messages.
$arrmessages = array();
// We always view messages from oldest to newest, ensure we have it in that order.
$lastmessage = end($messages);
$firstmessage = reset($messages);
if ($lastmessage->timecreated < $firstmessage->timecreated) {
$messages = array_reverse($messages);
}
// Keeps track of the last day, month and year combo we were viewing.
$day = '';
$month = '';
$year = '';
foreach ($messages as $message) {
// Check if we are now viewing a different block period.
$displayblocktime = false;
$date = usergetdate($message->timecreated);
if ($day != $date['mday'] || $month != $date['month'] || $year != $date['year']) {
$day = $date['mday'];
$month = $date['month'];
$year = $date['year'];
$displayblocktime = true;
}
// Store the message to pass to the renderable.
$msg = new \stdClass();
$msg->id = $message->id;
$msg->text = message_format_message_text($message);
$msg->currentuserid = $userid;
$msg->useridfrom = $message->useridfrom;
$msg->useridto = $message->useridto;
$msg->displayblocktime = $displayblocktime;
$msg->timecreated = $message->timecreated;
$msg->timeread = $message->timeread;
$arrmessages[] = $msg;
}
return $arrmessages;
public static function create_messages() {
throw new \coding_exception('\core_message\helper::create_messages has been removed.');
}
/**
@@ -428,7 +311,9 @@ class helper {
* @return array
*/
public static function togglecontact_link_params($user, $iscontact = false) {
global $USER;
$params = array(
'data-currentuserid' => $USER->id,
'data-userid' => $user->id,
'data-is-contact' => $iscontact,
'id' => 'toggle-contact-button',
@@ -639,47 +524,11 @@ class helper {
return $members;
}
/**
* Backwards compatibility formatter, transforming the new output of get_conversations() into the old format.
*
* TODO: This function should be removed once the related web services go through final deprecation.
* The related web services are data_for_messagearea_conversations.
* Followup: MDL-63261
*
* @param array $conversations the array of conversations, which must come from get_conversations().
* @return array the array of conversations, formatted in the legacy style.
* @deprecated since 3.6
*/
public static function get_conversations_legacy_formatter(array $conversations) : array {
// Transform new data format back into the old format, just for BC during the deprecation life cycle.
$tmp = [];
foreach ($conversations as $id => $conv) {
// Only individual conversations were supported in legacy messaging.
if ($conv->type != \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL) {
continue;
}
$data = new \stdClass();
// The logic for the 'other user' is as follows:
// If a conversation is of type 'individual', the other user is always the member who is not the current user.
// If the conversation is of type 'group', the other user is always the sender of the most recent message.
// The get_conversations method already follows this logic, so we just need the first member.
$otheruser = reset($conv->members);
$data->userid = $otheruser->id;
$data->useridfrom = $conv->messages[0]->useridfrom ?? null;
$data->fullname = $conv->members[$otheruser->id]->fullname;
$data->profileimageurl = $conv->members[$otheruser->id]->profileimageurl;
$data->profileimageurlsmall = $conv->members[$otheruser->id]->profileimageurlsmall;
$data->ismessaging = isset($conv->messages[0]->text) ? true : false;
$data->lastmessage = $conv->messages[0]->text ? clean_param($conv->messages[0]->text, PARAM_NOTAGS) : null;
$data->lastmessagedate = $conv->messages[0]->timecreated ?? null;
$data->messageid = $conv->messages[0]->id ?? null;
$data->isonline = $conv->members[$otheruser->id]->isonline ?? null;
$data->isblocked = $conv->members[$otheruser->id]->isblocked ?? null;
$data->isread = $conv->isread;
$data->unreadcount = $conv->unreadcount;
$tmp[$data->userid] = $data;
}
return $tmp;
public static function get_conversations_legacy_formatter() {
throw new \coding_exception('\core_message\helper::get_conversations_legacy_formatter has been removed.');
}
/**
@@ -1,168 +0,0 @@
<?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 prepare a contact for display.
*
* TODO: This file should be removed once the related web services go through final deprecation.
* Followup: MDL-63261
*
* @package core_message
* @copyright 2016 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_message\output\messagearea;
defined('MOODLE_INTERNAL') || die();
use renderable;
use templatable;
/**
* Class to prepare a contact for display.
*
* @package core_message
* @copyright 2016 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class contact implements templatable, renderable {
/**
* @var int Maximum length of message to show in panel.
*/
const MAX_MSG_LENGTH = 60;
/**
* @var int The userid.
*/
public $userid;
/**
* @var int The id of the user who sent the last message.
*/
public $useridfrom;
/**
* @var string The fullname.
*/
public $fullname;
/**
* @var string The profile image url.
*/
public $profileimageurl;
/**
* @var string The small profile image url.
*/
public $profileimageurlsmall;
/**
* @var int The message id.
*/
public $messageid;
/**
* @var bool Are we messaging the user?
*/
public $ismessaging;
/**
* @var string The last message sent.
*/
public $lastmessage;
/**
* @var int The last message sent timestamp.
*/
public $lastmessagedate;
/**
* @var bool Is the user online?
*/
public $isonline;
/**
* @var bool Is the user blocked?
*/
public $isblocked;
/**
* @var bool Is the message read?
*/
public $isread;
/**
* @var int The number of unread messages.
*/
public $unreadcount;
/**
* @var int The id of the conversation to which to message belongs.
*/
public $conversationid;
/**
* Constructor.
*
* @param \stdClass $contact
*/
public function __construct($contact) {
$this->userid = $contact->userid;
$this->useridfrom = $contact->useridfrom;
$this->fullname = $contact->fullname;
$this->profileimageurl = $contact->profileimageurl;
$this->profileimageurlsmall = $contact->profileimageurlsmall;
$this->messageid = $contact->messageid;
$this->ismessaging = $contact->ismessaging;
$this->lastmessage = $contact->lastmessage;
$this->lastmessagedate = $contact->lastmessagedate;
$this->isonline = $contact->isonline;
$this->isblocked = $contact->isblocked;
$this->isread = $contact->isread;
$this->unreadcount = $contact->unreadcount;
$this->conversationid = $contact->conversationid ?? null;
}
public function export_for_template(\renderer_base $output) {
$contact = new \stdClass();
$contact->userid = $this->userid;
$contact->fullname = $this->fullname;
$contact->profileimageurl = $this->profileimageurl;
$contact->profileimageurlsmall = $this->profileimageurlsmall;
$contact->messageid = $this->messageid;
$contact->ismessaging = $this->ismessaging;
$contact->sentfromcurrentuser = false;
if ($this->lastmessage) {
if ($this->userid !== $this->useridfrom) {
$contact->sentfromcurrentuser = true;
}
$contact->lastmessage = shorten_text($this->lastmessage, self::MAX_MSG_LENGTH);
} else {
$contact->lastmessage = null;
}
$contact->lastmessagedate = $this->lastmessagedate;
$contact->showonlinestatus = is_null($this->isonline) ? false : true;
$contact->isonline = $this->isonline;
$contact->isblocked = $this->isblocked;
$contact->isread = $this->isread;
$contact->unreadcount = $this->unreadcount;
$contact->conversationid = $this->conversationid;
return $contact;
}
}
@@ -1,96 +0,0 @@
<?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 prepare the contacts for display.
*
* TODO: This file should be removed once the related web services go through final deprecation.
* Followup: MDL-63261
*
* @package core_message
* @copyright 2016 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_message\output\messagearea;
defined('MOODLE_INTERNAL') || die();
use renderable;
use templatable;
/**
* Class to prepare the contacts for display.
*
* @package core_message
* @copyright 2016 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class contacts implements templatable, renderable {
/**
* @var int The id of the user that has been selected.
*/
public $contactuserid;
/**
* @var array The contacts.
*/
public $contacts;
/**
* Constructor.
*
* @param int|null $contactuserid The id of the user that has been selected
* @param array $contacts
*/
public function __construct($contactuserid, $contacts) {
$this->contactuserid = $contactuserid;
$this->contacts = $contacts;
}
public function export_for_template(\renderer_base $output) {
$data = new \stdClass();
$data->contacts = array();
$userids = array();
foreach ($this->contacts as $contact) {
$contact = new contact($contact);
$contactdata = $contact->export_for_template($output);
$userids[$contactdata->userid] = $contactdata->userid;
// Check if the contact was selected.
if ($this->contactuserid == $contactdata->userid) {
$contactdata->selected = true;
}
$data->contacts[] = $contactdata;
}
// Check if the other user is not part of the contacts. We may be sending a message to someone
// we have not had a conversation with, so we want to add a new item to the contacts array.
if ($this->contactuserid && !isset($userids[$this->contactuserid])) {
$user = \core_user::get_user($this->contactuserid);
// Set an empty message so that we know we are messaging the user, and not viewing their profile.
$user->smallmessage = '';
$user->useridfrom = $user->id;
$contact = \core_message\helper::create_contact($user);
$contact = new contact($contact);
$contactdata = $contact->export_for_template($output);
$contactdata->selected = true;
// Put the contact at the front.
array_unshift($data->contacts, $contactdata);
}
return $data;
}
}
@@ -1,118 +0,0 @@
<?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 prepare a message for display.
*
* TODO: This file should be removed once the related web services go through final deprecation.
* Followup: MDL-63261
*
* @package core_message
* @copyright 2016 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_message\output\messagearea;
defined('MOODLE_INTERNAL') || die();
use renderable;
use templatable;
/**
* Class to prepare a message for display.
*
* @package core_message
* @copyright 2016 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class message implements templatable, renderable {
/**
* @var int The message id.
*/
public $id;
/**
* @var int The current userid.
*/
public $currentuserid;
/**
* @var int The userid to.
*/
public $useridto;
/**
* @var int The userid from.
*/
public $useridfrom;
/**
* @var string The message text.
*/
public $text;
/**
* @var bool Are we displaying the time?
*/
public $displayblocktime;
/**
* @var int The time created of the message.
*/
public $timecreated;
/**
* @var int The time the message was read.
*/
public $timeread;
/**
* Constructor.
*
* @param \stdClass $message
*/
public function __construct($message) {
$this->id = $message->id;
$this->currentuserid = $message->currentuserid;
$this->useridto = $message->useridto;
$this->useridfrom = $message->useridfrom;
$this->text = $message->text;
$this->displayblocktime = $message->displayblocktime;
$this->timecreated = $message->timecreated;
$this->timeread = $message->timeread;
}
public function export_for_template(\renderer_base $output) {
$message = new \stdClass();
$message->id = $this->id;
$message->useridto = $this->useridto;
$message->useridfrom = $this->useridfrom;
$message->text = $this->text;
$message->displayblocktime = $this->displayblocktime;
$message->blocktime = userdate($this->timecreated, get_string('strftimedaydate'));
$message->position = 'left';
if ($this->currentuserid == $this->useridfrom) {
$message->position = 'right';
}
$message->timesent = userdate($this->timecreated, get_string('strftimetime'));
$message->timecreated = $this->timecreated;
$message->isread = !empty($this->timeread) ? 1 : 0;
return $message;
}
}
@@ -1,137 +0,0 @@
<?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 prepare the message area for display.
*
* TODO: This file should be removed once the related web services go through final deprecation.
* Followup: MDL-63261
*
* @package core_message
* @copyright 2016 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_message\output\messagearea;
defined('MOODLE_INTERNAL') || die();
use renderable;
use templatable;
/**
* Class to prepare the message area for display.
*
* @package core_message
* @copyright 2016 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class message_area implements templatable, renderable {
/**
* @var int The user id.
*/
public $userid;
/**
* @var int The other user id.
*/
public $otheruserid;
/**
* @var array The contacts for the users.
*/
public $contacts;
/**
* @var array The messages for the user.
*/
public $messages;
/**
* @var bool Was a specific conversation requested.
*/
public $requestedconversation;
/**
* @var int The minimum time to poll for messages.
*/
public $pollmin;
/**
* @var int The maximum time to poll for messages.
*/
public $pollmax;
/**
* @var int The time used once we have reached the maximum polling time.
*/
public $polltimeout;
/**
* @var bool Are we creating a new message and show the contacts section first?
*/
public $contactsfirst;
/**
* Constructor.
*
* @param int $userid The ID of the user whose contacts and messages we are viewing
* @param int|null $otheruserid The id of the user we are viewing, null if none
* @param array $contacts
* @param array|null $messages
* @param bool $requestedconversation
* @param bool $contactsfirst Whether we are viewing the contacts first.
* @param int $pollmin
* @param int $pollmax
* @param int $polltimeout
*/
public function __construct($userid, $otheruserid, $contacts, $messages, $requestedconversation, $contactsfirst, $pollmin,
$pollmax, $polltimeout) {
$this->userid = $userid;
// Setting the other user to null when showing contacts will remove any contact from being selected.
$this->otheruserid = (!$contactsfirst) ? $otheruserid : null;
$this->contacts = $contacts;
$this->messages = $messages;
$this->requestedconversation = $requestedconversation;
$this->pollmin = $pollmin;
$this->pollmax = $pollmax;
$this->polltimeout = $polltimeout;
$this->contactsfirst = $contactsfirst;
}
public function export_for_template(\renderer_base $output) {
$data = new \stdClass();
$data->userid = $this->userid;
$contacts = new contacts($this->otheruserid, $this->contacts);
$data->contacts = $contacts->export_for_template($output);
if ($this->contactsfirst) {
// Don't show any messages if we are creating a new message.
$messages = new messages($this->userid, null, array());
} else {
$messages = new messages($this->userid, $this->otheruserid, $this->messages);
}
$data->messages = $messages->export_for_template($output);
$data->isconversation = ($this->contactsfirst) ? false : true;
$data->requestedconversation = $this->requestedconversation;
$data->pollmin = $this->pollmin;
$data->pollmax = $this->pollmax;
$data->polltimeout = $this->polltimeout;
$data->contactsfirst = $this->contactsfirst;
return $data;
}
}
@@ -1,112 +0,0 @@
<?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 prepare the messages for display.
*
* TODO: This file should be removed once the related web services go through final deprecation.
* Followup: MDL-63261
*
* @package core_message
* @copyright 2016 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_message\output\messagearea;
defined('MOODLE_INTERNAL') || die();
use core_message\api;
use renderable;
use templatable;
/**
* Class to prepare the messages for display.
*
* @package core_message
* @copyright 2016 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class messages implements templatable, renderable {
/**
* @var array The messages.
*/
public $messages;
/**
* @var int The current user id.
*/
public $currentuserid;
/**
* @var int The other user id.
*/
public $otheruserid;
/**
* @var \stdClass The other user.
*/
public $otheruser;
/**
* Constructor.
*
* @param int $currentuserid The current user we are wanting to view messages for
* @param int $otheruserid The other user we are wanting to view messages for
* @param array $messages
*/
public function __construct($currentuserid, $otheruserid, $messages) {
$ufields = 'id, ' . get_all_user_name_fields(true) . ', lastaccess';
$this->currentuserid = $currentuserid;
if ($otheruserid) {
$this->otheruserid = $otheruserid;
$this->otheruser = \core_user::get_user($otheruserid, $ufields, MUST_EXIST);
}
$this->messages = $messages;
}
public function export_for_template(\renderer_base $output) {
global $USER;
$data = new \stdClass();
$data->iscurrentuser = $USER->id == $this->currentuserid;
$data->currentuserid = $this->currentuserid;
if ($this->otheruserid) {
$data->otheruserid = $this->otheruserid;
$data->otheruserfullname = fullname($this->otheruser);
}
$data->isonline = null;
if ($this->otheruserid) {
if (\core_message\helper::show_online_status($this->otheruser)) {
$data->isonline = \core_message\helper::is_online($this->otheruser->lastaccess);
}
}
$data->showonlinestatus = is_null($data->isonline) ? false : true;
$data->messages = array();
foreach ($this->messages as $message) {
$message = new message($message);
$data->messages[] = $message->export_for_template($output);
}
$blockeduserid = $this->otheruserid ?? $USER->id;
$data->isblocked = api::is_blocked($this->currentuserid, $blockeduserid);
return $data;
}
}
@@ -1,132 +0,0 @@
<?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 prepare a profile for display.
*
* TODO: This file should be removed once the related web services go through final deprecation.
* Followup: MDL-63261
*
* @package core_message
* @copyright 2016 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_message\output\messagearea;
defined('MOODLE_INTERNAL') || die();
use renderable;
use templatable;
/**
* Class to prepare a profile for display.
*
* @package core_message
* @copyright 2016 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class profile implements templatable, renderable {
/**
* @var int The id of the user we are going to view.
*/
public $userid;
/**
* @var string The fullname.
*/
public $fullname;
/**
* @var string The city.
*/
public $city;
/**
* @var string The country.
*/
public $country;
/**
* @var string The email.
*/
public $email;
/**
* @var string The profile image url.
*/
public $profileimageurl;
/**
* @var string The small profile image url.
*/
public $profileimageurlsmall;
/**
* @var bool Is the user online?
*/
public $isonline;
/**
* @var bool Is the user blocked?
*/
public $isblocked;
/**
* @var bool Is the user a contact?
*/
public $iscontact;
/**
* Constructor.
*
* @param \stdClass $profile
*/
public function __construct($profile) {
$this->userid = $profile->userid;
$this->fullname = $profile->fullname;
$this->isonline = $profile->isonline;
$this->email = $profile->email;
$this->country = $profile->country;
$this->city = $profile->city;
$this->profileimageurl = $profile->profileimageurl;
$this->profileimageurlsmall = $profile->profileimageurlsmall;
$this->isblocked = $profile->isblocked;
$this->iscontact = $profile->iscontact;
}
public function export_for_template(\renderer_base $output) {
$data = new \stdClass();
$data->userid = $this->userid;
$data->fullname = $this->fullname;
$data->showonlinestatus = is_null($this->isonline) ? false : true;
$data->isonline = $this->isonline;
$data->email = $this->email;
if (!empty($this->country)) {
$data->country = get_string($this->country, 'countries');
} else {
$data->country = '';
}
$data->city = $this->city;
$data->profileimageurl = $this->profileimageurl;
$data->profileimageurlsmall = $this->profileimageurlsmall;
$data->isblocked = $this->isblocked;
$data->iscontact = $this->iscontact;
return $data;
}
}
@@ -1,114 +0,0 @@
<?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 display user search results.
*
* TODO: This file should be removed once the related web services go through final deprecation.
* Followup: MDL-63261
*
* @package core_message
* @copyright 2016 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_message\output\messagearea;
defined('MOODLE_INTERNAL') || die();
use renderable;
use templatable;
/**
* Class used to display user search results.
*
* @package core_message
* @copyright 2016 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_search_results implements templatable, renderable {
/**
* @var array The list of contacts.
*/
public $contacts;
/**
* @var array The list of courses.
*/
public $courses;
/**
* @var array The list of non-contacts.
*/
public $noncontacts;
/**
* Constructor.
*
* @param array $contacts list of contacts.
* @param array $courses list of courses.
* @param array $noncontacts list of nonconcat users.
*/
public function __construct($contacts, $courses = array(), $noncontacts = array()) {
$this->contacts = $contacts;
$this->courses = $courses;
$this->noncontacts = $noncontacts;
}
public function export_for_template(\renderer_base $output) {
// Set the defaults for the data we are going to export.
$data = new \stdClass();
$data->hascontacts = false;
$data->contacts = array();
$data->hascourses = false;
$data->courses = array();
$data->hasnoncontacts = false;
$data->noncontacts = array();
// Check if there are any contacts.
if (!empty($this->contacts)) {
$data->hascontacts = true;
foreach ($this->contacts as $contact) {
$contact = new contact($contact);
$data->contacts[] = $contact->export_for_template($output);
}
}
// Check if there are any courses.
if (!empty($this->courses)) {
$data->hascourses = true;
$data->courses = [];
foreach ($this->courses as $course) {
$coursecontext = \context_course::instance($course->id);
$course->shortname = external_format_string($course->shortname, $coursecontext->id, true);
$course->fullname = external_format_string($course->fullname, $coursecontext->id, true);
$data->courses[] = $course;
}
}
// Check if there are any non-contacts.
if (!empty($this->noncontacts)) {
$data->hasnoncontacts = true;
foreach ($this->noncontacts as $noncontact) {
$noncontact = new contact($noncontact);
$data->noncontacts[] = $noncontact->export_for_template($output);
}
}
return $data;
}
}
File diff suppressed because it is too large Load Diff
+5
View File
@@ -65,6 +65,8 @@ define('MESSAGE_DEFAULT_TIMEOUT_POLL_IN_SECONDS', 5 * MINSECS);
/**
* Returns the count of unread messages for user. Either from a specific user or from all users.
*
* @deprecated since 3.10
* TODO: MDL-69643
* @param object $user1 the first user. Defaults to $USER
* @param object $user2 the second user. If null this function will count all of user 1's unread messages.
* @return int the count of $user1's unread messages
@@ -72,6 +74,9 @@ define('MESSAGE_DEFAULT_TIMEOUT_POLL_IN_SECONDS', 5 * MINSECS);
function message_count_unread_messages($user1=null, $user2=null) {
global $USER, $DB;
debugging('message_count_unread_messages is deprecated and no longer used',
DEBUG_DEVELOPER);
if (empty($user1)) {
$user1 = $USER;
}
+17 -677
View File
@@ -54,7 +54,9 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
\core_message\api::mark_all_notifications_as_read($recipient->id);
\core_message\api::mark_all_messages_as_read($recipient->id);
$this->assertEquals(message_count_unread_messages($recipient), 0);
$this->assertDebuggingCalled();
}
public function test_mark_all_read_for_user_touser_with_fromuser() {
@@ -80,6 +82,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
\core_message\api::mark_all_messages_as_read($recipient->id, $conversationid);
$this->assertEquals(message_count_unread_messages($recipient), 3);
$this->assertDebuggingCalled();
}
public function test_mark_all_read_for_user_touser_with_type() {
@@ -95,9 +98,11 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
\core_message\api::mark_all_notifications_as_read($recipient->id);
$this->assertEquals(message_count_unread_messages($recipient), 3);
$this->assertDebuggingCalled();
\core_message\api::mark_all_messages_as_read($recipient->id);
$this->assertEquals(message_count_unread_messages($recipient), 0);
$this->assertDebuggingCalled();
}
/**
@@ -122,190 +127,6 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
$this->assertEquals(1, \core_message\api::count_blocked_users());
}
/**
* Tests searching users in a course.
*/
public function test_search_users_in_course() {
// Create some users.
$user1 = new stdClass();
$user1->firstname = 'User';
$user1->lastname = 'One';
$user1 = self::getDataGenerator()->create_user($user1);
// The person doing the search.
$this->setUser($user1);
// Second user is going to have their last access set to now, so they are online.
$user2 = new stdClass();
$user2->firstname = 'User';
$user2->lastname = 'Two';
$user2->lastaccess = time();
$user2 = self::getDataGenerator()->create_user($user2);
// Block the second user.
\core_message\api::block_user($user1->id, $user2->id);
$user3 = new stdClass();
$user3->firstname = 'User';
$user3->lastname = 'Three';
$user3 = self::getDataGenerator()->create_user($user3);
// Create a course.
$course1 = new stdClass();
$course1->fullname = 'Course';
$course1->shortname = 'One';
$course1 = $this->getDataGenerator()->create_course($course1);
// Enrol the searcher and one user in the course.
$this->getDataGenerator()->enrol_user($user1->id, $course1->id);
$this->getDataGenerator()->enrol_user($user2->id, $course1->id);
// Perform a search.
$results = \core_message\api::search_users_in_course($user1->id, $course1->id, 'User');
$this->assertEquals(1, count($results));
$user = $results[0];
$this->assertEquals($user2->id, $user->userid);
$this->assertEquals(fullname($user2), $user->fullname);
$this->assertFalse($user->ismessaging);
$this->assertNull($user->lastmessage);
$this->assertNull($user->messageid);
$this->assertNull($user->isonline);
$this->assertFalse($user->isread);
$this->assertTrue($user->isblocked);
$this->assertNull($user->unreadcount);
}
/**
* Tests searching users.
*/
public function test_search_users() {
global $DB;
// Create some users.
$user1 = new stdClass();
$user1->firstname = 'User';
$user1->lastname = 'One';
$user1 = self::getDataGenerator()->create_user($user1);
// Set as the user performing the search.
$this->setUser($user1);
$user2 = new stdClass();
$user2->firstname = 'User search';
$user2->lastname = 'Two';
$user2 = self::getDataGenerator()->create_user($user2);
$user3 = new stdClass();
$user3->firstname = 'User search';
$user3->lastname = 'Three';
$user3 = self::getDataGenerator()->create_user($user3);
$user4 = new stdClass();
$user4->firstname = 'User';
$user4->lastname = 'Four';
$user4 = self::getDataGenerator()->create_user($user4);
$user5 = new stdClass();
$user5->firstname = 'User search';
$user5->lastname = 'Five';
$user5 = self::getDataGenerator()->create_user($user5);
$user6 = new stdClass();
$user6->firstname = 'User';
$user6->lastname = 'Six';
$user6 = self::getDataGenerator()->create_user($user6);
// Create some courses.
$course1 = new stdClass();
$course1->fullname = 'Course search';
$course1->shortname = 'One';
$course1 = $this->getDataGenerator()->create_course($course1);
$course2 = new stdClass();
$course2->fullname = 'Course';
$course2->shortname = 'Two';
$course2 = $this->getDataGenerator()->create_course($course2);
$course3 = new stdClass();
$course3->fullname = 'Course';
$course3->shortname = 'Three search';
$course3 = $this->getDataGenerator()->create_course($course3);
$course4 = new stdClass();
$course4->fullname = 'Course Four';
$course4->shortname = 'CF100';
$course4 = $this->getDataGenerator()->create_course($course4);
$course5 = new stdClass();
$course5->fullname = 'Course';
$course5->shortname = 'Five search';
$course5 = $this->getDataGenerator()->create_course($course5);
$role = $DB->get_record('role', ['shortname' => 'student']);
$this->getDataGenerator()->enrol_user($user1->id, $course1->id, $role->id);
$this->getDataGenerator()->enrol_user($user1->id, $course2->id, $role->id);
$this->getDataGenerator()->enrol_user($user1->id, $course3->id, $role->id);
$this->getDataGenerator()->enrol_user($user1->id, $course5->id, $role->id);
// Add some users as contacts.
\core_message\api::add_contact($user1->id, $user2->id);
\core_message\api::add_contact($user1->id, $user3->id);
\core_message\api::add_contact($user1->id, $user4->id);
// Remove the viewparticipants capability from one of the courses.
$course5context = context_course::instance($course5->id);
assign_capability('moodle/course:viewparticipants', CAP_PROHIBIT, $role->id, $course5context->id);
// Perform a search $CFG->messagingallusers setting enabled.
set_config('messagingallusers', 1);
list($contacts, $courses, $noncontacts) = \core_message\api::search_users($user1->id, 'search');
// Check that we retrieved the correct contacts.
$this->assertEquals(2, count($contacts));
$this->assertEquals($user3->id, $contacts[0]->userid);
$this->assertEquals($user2->id, $contacts[1]->userid);
// Check that we retrieved the correct courses.
$this->assertEquals(2, count($courses));
$this->assertEquals($course3->id, $courses[0]->id);
$this->assertEquals($course1->id, $courses[1]->id);
// Check that we retrieved the correct non-contacts.
$this->assertEquals(1, count($noncontacts));
$this->assertEquals($user5->id, $noncontacts[0]->userid);
}
/**
* Tests searching users with empty result.
*/
public function test_search_users_with_empty_result() {
// Create some users.
$user1 = new stdClass();
$user1->firstname = 'User';
$user1->lastname = 'One';
$user1 = self::getDataGenerator()->create_user($user1);
// Set as the user performing the search.
$this->setUser($user1);
$user2 = new stdClass();
$user2->firstname = 'User';
$user2->lastname = 'Two';
$user2 = self::getDataGenerator()->create_user($user2);
// Perform a search $CFG->messagingallusers setting enabled.
set_config('messagingallusers', 1);
list($contacts, $courses, $noncontacts) = \core_message\api::search_users($user1->id, 'search');
// Check results are empty.
$this->assertEquals(0, count($contacts));
$this->assertEquals(0, count($courses));
$this->assertEquals(0, count($noncontacts));
}
/**
* Tests searching for users when site-wide messaging is disabled.
*
@@ -2350,83 +2171,6 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
}
}
/**
* Tests retrieving contacts.
*/
public function test_get_contacts() {
// Create some users.
$user1 = self::getDataGenerator()->create_user();
// Set as the user.
$this->setUser($user1);
$user2 = new stdClass();
$user2->firstname = 'User';
$user2->lastname = 'A';
$user2 = self::getDataGenerator()->create_user($user2);
$user3 = new stdClass();
$user3->firstname = 'User';
$user3->lastname = 'B';
$user3 = self::getDataGenerator()->create_user($user3);
$user4 = new stdClass();
$user4->firstname = 'User';
$user4->lastname = 'C';
$user4 = self::getDataGenerator()->create_user($user4);
$user5 = new stdClass();
$user5->firstname = 'User';
$user5->lastname = 'D';
$user5 = self::getDataGenerator()->create_user($user5);
// Add some users as contacts.
\core_message\api::add_contact($user1->id, $user2->id);
\core_message\api::add_contact($user1->id, $user3->id);
\core_message\api::add_contact($user1->id, $user4->id);
// Retrieve the contacts.
$contacts = \core_message\api::get_contacts($user1->id);
// Confirm the data is correct.
$this->assertEquals(3, count($contacts));
usort($contacts, ['static', 'sort_contacts']);
$contact1 = $contacts[0];
$contact2 = $contacts[1];
$contact3 = $contacts[2];
$this->assertEquals($user2->id, $contact1->userid);
$this->assertEmpty($contact1->useridfrom);
$this->assertFalse($contact1->ismessaging);
$this->assertNull($contact1->lastmessage);
$this->assertNull($contact1->messageid);
$this->assertNull($contact1->isonline);
$this->assertFalse($contact1->isread);
$this->assertFalse($contact1->isblocked);
$this->assertNull($contact1->unreadcount);
$this->assertEquals($user3->id, $contact2->userid);
$this->assertEmpty($contact2->useridfrom);
$this->assertFalse($contact2->ismessaging);
$this->assertNull($contact2->lastmessage);
$this->assertNull($contact2->messageid);
$this->assertNull($contact2->isonline);
$this->assertFalse($contact2->isread);
$this->assertFalse($contact2->isblocked);
$this->assertNull($contact2->unreadcount);
$this->assertEquals($user4->id, $contact3->userid);
$this->assertEmpty($contact3->useridfrom);
$this->assertFalse($contact3->ismessaging);
$this->assertNull($contact3->lastmessage);
$this->assertNull($contact3->messageid);
$this->assertNull($contact3->isonline);
$this->assertFalse($contact3->isread);
$this->assertFalse($contact3->isblocked);
$this->assertNull($contact3->unreadcount);
}
/**
* Tests retrieving user contacts.
*/
@@ -2487,56 +2231,6 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
$this->assertTrue($contact3->iscontact);
}
/**
* Tests retrieving messages.
*/
public function test_get_messages() {
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
// The person doing the search.
$this->setUser($user1);
// Send some messages back and forth.
$time = 1;
$this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
$this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
$this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
$this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
// Retrieve the messages.
$messages = \core_message\api::get_messages($user1->id, $user2->id);
// Confirm the message data is correct.
$this->assertEquals(4, count($messages));
$message1 = $messages[0];
$message2 = $messages[1];
$message3 = $messages[2];
$message4 = $messages[3];
$this->assertEquals($user1->id, $message1->useridfrom);
$this->assertEquals($user2->id, $message1->useridto);
$this->assertTrue($message1->displayblocktime);
$this->assertContains('Yo!', $message1->text);
$this->assertEquals($user2->id, $message2->useridfrom);
$this->assertEquals($user1->id, $message2->useridto);
$this->assertFalse($message2->displayblocktime);
$this->assertContains('Sup mang?', $message2->text);
$this->assertEquals($user1->id, $message3->useridfrom);
$this->assertEquals($user2->id, $message3->useridto);
$this->assertFalse($message3->displayblocktime);
$this->assertContains('Writing PHPUnit tests!', $message3->text);
$this->assertEquals($user2->id, $message4->useridfrom);
$this->assertEquals($user1->id, $message4->useridto);
$this->assertFalse($message4->displayblocktime);
$this->assertContains('Word.', $message4->text);
}
/**
* Tests retrieving conversation messages.
*/
@@ -3009,33 +2703,6 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
$this->assertEquals(1, count($members));
}
/**
* Tests retrieving most recent message.
*/
public function test_get_most_recent_message() {
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
// The person doing the search.
$this->setUser($user1);
// Send some messages back and forth.
$time = 1;
$this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
$this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
$this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
$this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
// Retrieve the most recent messages.
$message = \core_message\api::get_most_recent_message($user1->id, $user2->id);
// Check the results are correct.
$this->assertEquals($user2->id, $message->useridfrom);
$this->assertEquals($user1->id, $message->useridto);
$this->assertContains('Word.', $message->text);
}
/**
* Tests retrieving most recent conversation message.
*/
@@ -3069,62 +2736,6 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
$this->assertContains('Word.', $message->text);
}
/**
* Tests retrieving a user's profile.
*/
public function test_get_profile() {
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = new stdClass();
$user2->country = 'AU';
$user2->city = 'Perth';
$user2 = self::getDataGenerator()->create_user($user2);
// The person doing the search.
$this->setUser($user1);
// Get the profile.
$profile = \core_message\api::get_profile($user1->id, $user2->id);
$this->assertEquals($user2->id, $profile->userid);
$this->assertEmpty($profile->email);
$this->assertEmpty($profile->country);
$this->assertEmpty($profile->city);
$this->assertEquals(fullname($user2), $profile->fullname);
$this->assertNull($profile->isonline);
$this->assertFalse($profile->isblocked);
$this->assertFalse($profile->iscontact);
}
/**
* Tests retrieving a user's profile.
*/
public function test_get_profile_as_admin() {
// The person doing the search.
$this->setAdminUser();
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = new stdClass();
$user2->country = 'AU';
$user2->city = 'Perth';
$user2 = self::getDataGenerator()->create_user($user2);
// Get the profile.
$profile = \core_message\api::get_profile($user1->id, $user2->id);
$this->assertEquals($user2->id, $profile->userid);
$this->assertEquals($user2->email, $profile->email);
$this->assertEquals($user2->country, $profile->country);
$this->assertEquals($user2->city, $profile->city);
$this->assertEquals(fullname($user2), $profile->fullname);
$this->assertFalse($profile->isonline);
$this->assertFalse($profile->isblocked);
$this->assertFalse($profile->iscontact);
}
/**
* Tests checking if a user can mark all messages as read.
*/
@@ -3195,57 +2806,6 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
$this->assertFalse(\core_message\api::can_delete_conversation($user2->id, $conversationid));
}
/**
* Tests deleting a conversation.
*/
public function test_delete_conversation() {
global $DB;
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
// The person doing the search.
$this->setUser($user1);
// Send some messages back and forth.
$time = 1;
$m1id = $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
$m2id = $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
$m3id = $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
$m4id = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
// Delete the conversation as user 1.
\core_message\api::delete_conversation($user1->id, $user2->id);
$this->assertDebuggingCalled();
$muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC');
$this->assertCount(4, $muas);
// Sort by id.
ksort($muas);
$mua1 = array_shift($muas);
$mua2 = array_shift($muas);
$mua3 = array_shift($muas);
$mua4 = array_shift($muas);
$this->assertEquals($user1->id, $mua1->userid);
$this->assertEquals($m1id, $mua1->messageid);
$this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua1->action);
$this->assertEquals($user1->id, $mua2->userid);
$this->assertEquals($m2id, $mua2->messageid);
$this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua2->action);
$this->assertEquals($user1->id, $mua3->userid);
$this->assertEquals($m3id, $mua3->messageid);
$this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua3->action);
$this->assertEquals($user1->id, $mua4->userid);
$this->assertEquals($m4id, $mua4->messageid);
$this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua4->action);
}
/**
* Tests deleting a conversation by conversation id.
*/
@@ -4157,99 +3717,6 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
);
}
/**
* Tests that when blocking messages from non-contacts is enabled that
* non-contacts trying to send a message return false.
*/
public function test_is_user_non_contact_blocked() {
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
// Set as the first user.
$this->setUser($user1);
// By default, user only can be messaged by contacts and members of any of his/her courses.
$this->assertTrue(\core_message\api::is_user_non_contact_blocked($user2));
$this->assertDebuggingCalled();
// Enable all users privacy messaging and check now the default user's preference has been set to allow receiving
// messages from everybody.
set_config('messagingallusers', true);
// Check that the return result is now false because any site user can contact him/her.
$this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
$this->assertDebuggingCalled();
// Set the second user's preference to not receive messages from non-contacts.
set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2->id);
// Check that the return result is still true (because is even more restricted).
$this->assertTrue(\core_message\api::is_user_non_contact_blocked($user2));
$this->assertDebuggingCalled();
// Add the first user as a contact for the second user.
\core_message\api::add_contact($user2->id, $user1->id);
// Check that the return result is now false.
$this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
$this->assertDebuggingCalled();
// Set the second user's preference to receive messages from course members.
set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER, $user2->id);
// Check that the return result is still false (because $user1 is still his/her contact).
$this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
$this->assertDebuggingCalled();
}
/**
* Tests that we return true when a user is blocked, or false
* if they are not blocked.
*/
public function test_is_user_blocked() {
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
// Set the user.
$this->setUser($user1);
// User shouldn't be blocked.
$this->assertFalse(\core_message\api::is_user_blocked($user1->id, $user2->id));
$this->assertDebuggingCalled();
// Block the user.
\core_message\api::block_user($user1->id, $user2->id);
// User should be blocked.
$this->assertTrue(\core_message\api::is_user_blocked($user1->id, $user2->id));
$this->assertDebuggingCalled();
// Unblock the user.
\core_message\api::unblock_user($user1->id, $user2->id);
$this->assertFalse(\core_message\api::is_user_blocked($user1->id, $user2->id));
$this->assertDebuggingCalled();
}
/**
* Tests that the admin is not blocked even if someone has chosen to block them.
*/
public function test_is_user_blocked_as_admin() {
// Create a user.
$user1 = self::getDataGenerator()->create_user();
// Set the user.
$this->setUser($user1);
// Block the admin user.
\core_message\api::block_user($user1->id, 2);
// Now change to the admin user.
$this->setAdminUser();
// As the admin you should still be able to send messages to the user.
$this->assertFalse(\core_message\api::is_user_blocked($user1->id));
$this->assertDebuggingCalled();
}
/*
* Tes get_message_processor api.
*/
@@ -4339,131 +3806,6 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
$this->assertEquals(1, $status);
}
/**
* Test retrieving messages by providing a minimum timecreated value.
*/
public function test_get_messages_time_from_only() {
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
// The person doing the search.
$this->setUser($user1);
// Send some messages back and forth.
$time = 1;
$this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
$this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
$this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
$this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
// Retrieve the messages from $time, which should be all of them.
$messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time);
// Confirm the message data is correct.
$this->assertEquals(4, count($messages));
$message1 = $messages[0];
$message2 = $messages[1];
$message3 = $messages[2];
$message4 = $messages[3];
$this->assertContains('Message 1', $message1->text);
$this->assertContains('Message 2', $message2->text);
$this->assertContains('Message 3', $message3->text);
$this->assertContains('Message 4', $message4->text);
// Retrieve the messages from $time + 3, which should only be the 2 last messages.
$messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time + 3);
// Confirm the message data is correct.
$this->assertEquals(2, count($messages));
$message1 = $messages[0];
$message2 = $messages[1];
$this->assertContains('Message 3', $message1->text);
$this->assertContains('Message 4', $message2->text);
}
/**
* Test retrieving messages by providing a maximum timecreated value.
*/
public function test_get_messages_time_to_only() {
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
// The person doing the search.
$this->setUser($user1);
// Send some messages back and forth.
$time = 1;
$this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
$this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
$this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
$this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
// Retrieve the messages up until $time + 4, which should be all of them.
$messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', 0, $time + 4);
// Confirm the message data is correct.
$this->assertEquals(4, count($messages));
$message1 = $messages[0];
$message2 = $messages[1];
$message3 = $messages[2];
$message4 = $messages[3];
$this->assertContains('Message 1', $message1->text);
$this->assertContains('Message 2', $message2->text);
$this->assertContains('Message 3', $message3->text);
$this->assertContains('Message 4', $message4->text);
// Retrieve the messages up until $time + 2, which should be the first two.
$messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', 0, $time + 2);
// Confirm the message data is correct.
$this->assertEquals(2, count($messages));
$message1 = $messages[0];
$message2 = $messages[1];
$this->assertContains('Message 1', $message1->text);
$this->assertContains('Message 2', $message2->text);
}
/**
* Test retrieving messages by providing a minimum and maximum timecreated value.
*/
public function test_get_messages_time_from_and_to() {
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
// The person doing the search.
$this->setUser($user1);
// Send some messages back and forth.
$time = 1;
$this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1);
$this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2);
$this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3);
$this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4);
// Retrieve the messages from $time + 2 up until $time + 3, which should be 2nd and 3rd message.
$messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time + 2, $time + 3);
// Confirm the message data is correct.
$this->assertEquals(2, count($messages));
$message1 = $messages[0];
$message2 = $messages[1];
$this->assertContains('Message 2', $message1->text);
$this->assertContains('Message 3', $message2->text);
}
/**
* Test returning blocked users.
*/
@@ -4493,6 +3835,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
/**
* Test returning contacts with unread message count.
* MDL-69643
*/
public function test_get_contacts_with_unread_message_count() {
global $DB;
@@ -4522,6 +3865,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
// Get the contacts and the unread message count.
$messages = \core_message\api::get_contacts_with_unread_message_count($user2->id);
$this->assertDebuggingCalled();
// Confirm the size is correct.
$this->assertCount(2, $messages);
@@ -4543,6 +3887,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
// Get the contacts and the unread message count.
$messages = \core_message\api::get_contacts_with_unread_message_count($user2->id);
$this->assertDebuggingCalled();
// Confirm the size is correct.
$this->assertCount(2, $messages);
@@ -4566,6 +3911,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
// Get the contacts and the unread message count.
$messages = \core_message\api::get_contacts_with_unread_message_count($user1->id);
$this->assertDebuggingCalled();
// Confirm the size is correct.
$this->assertCount(1, $messages);
@@ -4578,6 +3924,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
\core_message\api::mark_message_as_read($user1->id, $m);
$messages = \core_message\api::get_contacts_with_unread_message_count($user1->id);
$this->assertDebuggingCalled();
// Confirm the size is correct.
$this->assertCount(1, $messages);
@@ -4600,6 +3947,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
// Check we get the correct message count.
$messages = \core_message\api::get_contacts_with_unread_message_count($user2->id);
$this->assertDebuggingCalled();
// Confirm the size is correct.
$this->assertCount(1, $messages);
@@ -4612,6 +3960,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
/**
* Test returning non-contacts with unread message count.
* MDL-69643
*/
public function test_get_non_contacts_with_unread_message_count() {
global $DB;
@@ -4639,8 +3988,10 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
// Send a message that should never be included as the user is a contact.
$this->send_fake_message($user4, $user2);
// Get the non-contacts and the unread message count.
$messages = \core_message\api::get_non_contacts_with_unread_message_count($user2->id);
$this->assertDebuggingCalled();
// Check we get the correct message count.
ksort($messages);
@@ -4660,6 +4011,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
// Get the non-contacts and the unread message count.
$messages = \core_message\api::get_non_contacts_with_unread_message_count($user2->id);
$this->assertDebuggingCalled();
// Check the marked message is not returned in the message count.
ksort($messages);
@@ -4681,6 +4033,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
// Get the non-contacts and the unread message count.
$messages = \core_message\api::get_non_contacts_with_unread_message_count($user1->id);
$this->assertDebuggingCalled();
// Confirm the size is correct.
$this->assertCount(1, $messages);
@@ -4694,6 +4047,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
// Get the non-contacts and the unread message count.
$messages = \core_message\api::get_non_contacts_with_unread_message_count($user1->id);
$this->assertDebuggingCalled();
// Check the marked message is not returned in the message count.
$this->assertCount(1, $messages);
@@ -4781,20 +4135,6 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
$this->assertFalse(\core_message\api::get_conversation_between_users([$user1->id, $user2->id]));
}
/**
* Test we can return a conversation that exists between users.
*/
public function test_get_conversation_between_users_with_existing_conversation() {
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$conversationid = \core_message\api::create_conversation_between_users([$user1->id, $user2->id]);
$this->assertDebuggingCalled();
$this->assertEquals($conversationid,
\core_message\api::get_conversation_between_users([$user1->id, $user2->id]));
}
/**
* Test count_conversation_members for non existing conversation.
*/
+2 -2
View File
@@ -458,8 +458,8 @@ class core_message_events_testcase extends core_message_messagelib_testcase {
// Trigger and capture the event.
$sink = $this->redirectEvents();
\core_message\api::delete_conversation($user1->id, $user2->id);
$this->assertDebuggingCalled();
$conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
\core_message\api::delete_conversation_by_id($user1->id, $conversationid);
$events = $sink->get_events();
// Get the user actions for the messages deleted by that user.
File diff suppressed because it is too large Load Diff
+13
View File
@@ -143,6 +143,7 @@ class core_message_messagelib_testcase extends advanced_testcase {
/**
* Test message_count_unread_messages.
* TODO: MDL-69643
*/
public function test_message_count_unread_messages() {
// Create users to send and receive message.
@@ -151,13 +152,17 @@ class core_message_messagelib_testcase extends advanced_testcase {
$userto = $this->getDataGenerator()->create_user();
$this->assertEquals(0, message_count_unread_messages($userto));
$this->assertDebuggingCalled();
// Send fake messages.
$this->send_fake_message($userfrom1, $userto);
$this->send_fake_message($userfrom2, $userto);
$this->assertEquals(2, message_count_unread_messages($userto));
$this->assertDebuggingCalled();
$this->assertEquals(1, message_count_unread_messages($userto, $userfrom1));
$this->assertDebuggingCalled();
}
/**
@@ -183,7 +188,10 @@ class core_message_messagelib_testcase extends advanced_testcase {
// Should only count the messages that weren't read by the current user.
$this->assertEquals(1, message_count_unread_messages($userto));
$this->assertDebuggingCalledCount(2);
$this->assertEquals(0, message_count_unread_messages($userto, $userfrom1));
$this->assertDebuggingCalled();
}
/**
@@ -198,6 +206,7 @@ class core_message_messagelib_testcase extends advanced_testcase {
$userto = $this->getDataGenerator()->create_user();
$this->assertEquals(0, message_count_unread_messages($userto));
$this->assertDebuggingCalled();
// Send fake messages.
$messageid = $this->send_fake_message($userfrom1, $userto);
@@ -208,7 +217,9 @@ class core_message_messagelib_testcase extends advanced_testcase {
// Should only count the messages that weren't deleted by the current user.
$this->assertEquals(1, message_count_unread_messages($userto));
$this->assertDebuggingCalled();
$this->assertEquals(0, message_count_unread_messages($userto, $userfrom1));
$this->assertDebuggingCalled();
}
/**
@@ -220,7 +231,9 @@ class core_message_messagelib_testcase extends advanced_testcase {
$this->send_fake_message($userfrom, $userto);
// Ensure an exception is thrown.
$this->assertEquals(0, message_count_unread_messages($userfrom));
$this->assertDebuggingCalled();
}
/**
+32
View File
@@ -1,6 +1,38 @@
This files describes API changes in /message/ messaging system,
information provided here is intended especially for developers.
=== 3.10 ===
* The following methods have been deprecated and should not be used any more:
* message_count_unread_messages()
* get_non_contacts_with_unread_message_count()
* get_contacts_with_unread_message_count()
* The following functions have been finally deprecated and can not be used anymore::
* search_users_in_course()
* search_users()
* get_contacts()
* get_messages()
* get_most_recent_message()
* get_profile()
* get_messages()
* create_messages()
* get_conversations_legacy_formatter()
* create_contacts()
* block_contacts()
* unblock_contacts()
* data_for_messagearea_search_users_in_course()
* data_for_messagearea_search_users()
* message_search_users()
* data_for_messagearea_conversations()
* data_for_messagearea_contacts()
* data_for_messagearea_messages()
* get_conversation_messages()
* data_for_messagearea_get_most_recent_message()
* data_for_messagearea_get_profile()
* mark_all_messages_as_read()
* delete_conversation()
=== 3.9 ===
* Removed the following deprecated functions:
- message_move_userfrom_unread2read
+1 -1
View File
@@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2020090300.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2020090300.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '3.10dev (Build: 20200903)';// Human-friendly version name