mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 00:46:50 +02:00
MDL-63211 core_message: deprecated functions and added new API
This commit is contained in:
parent
bacebf3738
commit
0b3eadcd96
6 changed files with 674 additions and 155 deletions
|
@ -825,7 +825,8 @@ $functions = array(
|
|||
'classname' => 'core_message_external',
|
||||
'methodname' => 'block_contacts',
|
||||
'classpath' => 'message/externallib.php',
|
||||
'description' => 'Block contacts',
|
||||
'description' => '** DEPRECATED ** Please do not call this function any more.
|
||||
Block contacts',
|
||||
'type' => 'write',
|
||||
'ajax' => true,
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
|
||||
|
@ -834,11 +835,11 @@ $functions = array(
|
|||
'classname' => 'core_message_external',
|
||||
'methodname' => 'create_contacts',
|
||||
'classpath' => 'message/externallib.php',
|
||||
'description' => 'Add contacts to the contact list',
|
||||
'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),
|
||||
'ajax' => true,
|
||||
),
|
||||
'core_message_delete_contacts' => array(
|
||||
'classname' => 'core_message_external',
|
||||
|
@ -848,7 +849,6 @@ $functions = array(
|
|||
'type' => 'write',
|
||||
'ajax' => true,
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
|
||||
'ajax' => true,
|
||||
),
|
||||
'core_message_delete_conversation' => array(
|
||||
'classname' => 'core_message_external',
|
||||
|
@ -1047,7 +1047,8 @@ $functions = array(
|
|||
'classname' => 'core_message_external',
|
||||
'methodname' => 'unblock_contacts',
|
||||
'classpath' => 'message/externallib.php',
|
||||
'description' => 'Unblock contacts',
|
||||
'description' => '** DEPRECATED ** Please do not call this function any more.
|
||||
Unblock contacts',
|
||||
'type' => 'write',
|
||||
'ajax' => true,
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
|
||||
|
|
|
@ -3455,3 +3455,159 @@ function get_roles_with_assignment_on_context(context $context) {
|
|||
|
||||
return get_roles_used_in_context($context, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the selected user as a contact for the current user
|
||||
*
|
||||
* @deprecated since Moodle 3.6
|
||||
* @param int $contactid the ID of the user to add as a contact
|
||||
* @param int $blocked 1 if you wish to block the contact
|
||||
* @param int $userid the user ID of the user we want to add the contact for, defaults to current user if not specified.
|
||||
* @return bool/int false if the $contactid isnt a valid user id. True if no changes made.
|
||||
* Otherwise returns the result of update_record() or insert_record()
|
||||
*/
|
||||
function message_add_contact($contactid, $blocked = 0, $userid = 0) {
|
||||
debugging('message_add_contact() is deprecated. Please use \core_message\api::create_contact_request() instead. ' .
|
||||
'If you wish to block or unblock a user please use \core_message\api::is_blocked() and ' .
|
||||
'\core_message\api::block_user() or \core_message\api::unblock_user() respectively.', DEBUG_DEVELOPER);
|
||||
|
||||
global $USER, $DB;
|
||||
|
||||
if (!$DB->record_exists('user', array('id' => $contactid))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (empty($userid)) {
|
||||
$userid = $USER->id;
|
||||
}
|
||||
|
||||
// Check if a record already exists as we may be changing blocking status.
|
||||
if (\core_message\api::is_contact($userid, $contactid)) {
|
||||
$isblocked = \core_message\api::is_blocked($userid, $contactid);
|
||||
// Check if blocking status has been changed.
|
||||
if ($isblocked != $blocked) {
|
||||
if ($blocked == 1) {
|
||||
if (!$isblocked) {
|
||||
\core_message\api::block_user($userid, $contactid);
|
||||
}
|
||||
} else {
|
||||
\core_message\api::unblock_user($userid, $contactid);
|
||||
}
|
||||
|
||||
return true;
|
||||
} else {
|
||||
// No change to blocking status.
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
if ($blocked == 1) {
|
||||
if (!\core_message\api::is_blocked($userid, $contactid)) {
|
||||
\core_message\api::block_user($userid, $contactid);
|
||||
}
|
||||
} else {
|
||||
\core_message\api::unblock_user($userid, $contactid);
|
||||
if (!\core_message\api::does_contact_request_exist($userid, $contactid)) {
|
||||
\core_message\api::create_contact_request($userid, $contactid);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a contact.
|
||||
*
|
||||
* @deprecated since Moodle 3.6
|
||||
* @param int $contactid the user ID of the contact to remove
|
||||
* @param int $userid the user ID of the user we want to remove the contacts for, defaults to current user if not specified.
|
||||
* @return bool returns the result of delete_records()
|
||||
*/
|
||||
function message_remove_contact($contactid, $userid = 0) {
|
||||
debugging('message_remove_contact() is deprecated. Please use \core_message\api::remove_contact() instead.',
|
||||
DEBUG_DEVELOPER);
|
||||
|
||||
global $USER;
|
||||
|
||||
if (empty($userid)) {
|
||||
$userid = $USER->id;
|
||||
}
|
||||
|
||||
\core_message\api::remove_contact($userid, $contactid);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unblock a contact.
|
||||
*
|
||||
* @deprecated since Moodle 3.6
|
||||
* @param int $contactid the user ID of the contact to unblock
|
||||
* @param int $userid the user ID of the user we want to unblock the contact for, defaults to current user
|
||||
* if not specified.
|
||||
* @return bool returns the result of delete_records()
|
||||
*/
|
||||
function message_unblock_contact($contactid, $userid = 0) {
|
||||
debugging('message_unblock_contact() is deprecated. Please use \core_message\api::unblock_user() instead.',
|
||||
DEBUG_DEVELOPER);
|
||||
|
||||
global $DB, $USER;
|
||||
|
||||
if (!$DB->record_exists('user', array('id' => $contactid))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (empty($userid)) {
|
||||
$userid = $USER->id;
|
||||
}
|
||||
|
||||
\core_message\api::unblock_user($userid, $contactid);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Block a user.
|
||||
*
|
||||
* @deprecated since Moodle 3.6
|
||||
* @param int $contactid the user ID of the user to block
|
||||
* @param int $userid the user ID of the user we want to unblock the contact for, defaults to current user
|
||||
* if not specified.
|
||||
* @return bool
|
||||
*/
|
||||
function message_block_contact($contactid, $userid = 0) {
|
||||
debugging('message_block_contact() is deprecated. Please use \core_message\api::is_blocked() and ' .
|
||||
'\core_message\api::block_user() instead.', DEBUG_DEVELOPER);
|
||||
|
||||
global $DB, $USER;
|
||||
|
||||
if (!$DB->record_exists('user', array('id' => $contactid))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (empty($userid)) {
|
||||
$userid = $USER->id;
|
||||
}
|
||||
|
||||
if (!\core_message\api::is_blocked($userid, $contactid)) {
|
||||
\core_message\api::block_user($userid, $contactid);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a user's contact record
|
||||
*
|
||||
* @deprecated since Moodle 3.6
|
||||
* @param int $contactid the user ID of the user whose contact record you want
|
||||
* @return array message contacts
|
||||
*/
|
||||
function message_get_contact($contactid) {
|
||||
debugging('message_get_contact() is deprecated. Please use \core_message\api::get_contact() instead.',
|
||||
DEBUG_DEVELOPER);
|
||||
|
||||
global $USER;
|
||||
|
||||
return \core_message\api::get_contact($USER->id, $contactid);
|
||||
}
|
||||
|
|
|
@ -1287,4 +1287,252 @@ class api {
|
|||
|
||||
return $conversation->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles creating a contact request.
|
||||
*
|
||||
* @param int $userid The id of the user who is creating the contact request
|
||||
* @param int $requesteduserid The id of the user being requested
|
||||
*/
|
||||
public static function create_contact_request(int $userid, int $requesteduserid) {
|
||||
global $DB;
|
||||
|
||||
$request = new \stdClass();
|
||||
$request->userid = $userid;
|
||||
$request->requesteduserid = $requesteduserid;
|
||||
$request->timecreated = time();
|
||||
|
||||
$DB->insert_record('message_contact_requests', $request);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Handles confirming a contact request.
|
||||
*
|
||||
* @param int $userid The id of the user who created the contact request
|
||||
* @param int $requesteduserid The id of the user confirming the request
|
||||
*/
|
||||
public static function confirm_contact_request(int $userid, int $requesteduserid) {
|
||||
global $DB;
|
||||
|
||||
if ($request = $DB->get_record('message_contact_requests', ['userid' => $userid,
|
||||
'requesteduserid' => $requesteduserid])) {
|
||||
self::add_contact($userid, $requesteduserid);
|
||||
|
||||
$DB->delete_records('message_contact_requests', ['id' => $request->id]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles declining a contact request.
|
||||
*
|
||||
* @param int $userid The id of the user who created the contact request
|
||||
* @param int $requesteduserid The id of the user declining the request
|
||||
*/
|
||||
public static function decline_contact_request(int $userid, int $requesteduserid) {
|
||||
global $DB;
|
||||
|
||||
if ($request = $DB->get_record('message_contact_requests', ['userid' => $userid,
|
||||
'requesteduserid' => $requesteduserid])) {
|
||||
$DB->delete_records('message_contact_requests', ['id' => $request->id]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles returning the contact requests for a user.
|
||||
*
|
||||
* This also includes the user data necessary to display information
|
||||
* about the user.
|
||||
*
|
||||
* It will not include blocked users.
|
||||
*
|
||||
* @param int $userid
|
||||
* @return array The list of contact requests
|
||||
*/
|
||||
public static function get_contact_requests(int $userid) : array {
|
||||
global $DB;
|
||||
|
||||
// Used to search for contacts.
|
||||
$ufields = \user_picture::fields('u');
|
||||
|
||||
$sql = "SELECT $ufields, mcr.id as contactrequestid
|
||||
FROM {user} u
|
||||
JOIN {message_contact_requests} mcr
|
||||
ON u.id = mcr.userid
|
||||
LEFT JOIN {message_users_blocked} mub
|
||||
ON (mub.userid = ? AND mub.blockeduserid = u.id)
|
||||
WHERE mcr.requesteduserid = ?
|
||||
AND u.deleted = 0
|
||||
AND mub.id is NULL
|
||||
ORDER BY mcr.timecreated DESC";
|
||||
|
||||
return $DB->get_records_sql($sql, [$userid, $userid]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles adding a contact.
|
||||
*
|
||||
* @param int $userid The id of the user who requested to be a contact
|
||||
* @param int $contactid The id of the contact
|
||||
*/
|
||||
public static function add_contact(int $userid, int $contactid) {
|
||||
global $DB;
|
||||
|
||||
$messagecontact = new \stdClass();
|
||||
$messagecontact->userid = $userid;
|
||||
$messagecontact->contactid = $contactid;
|
||||
$messagecontact->timecreated = time();
|
||||
$messagecontact->id = $DB->insert_record('message_contacts', $messagecontact);
|
||||
|
||||
$eventparams = [
|
||||
'objectid' => $messagecontact->id,
|
||||
'userid' => $userid,
|
||||
'relateduserid' => $contactid,
|
||||
'context' => \context_user::instance($userid)
|
||||
];
|
||||
$event = \core\event\message_contact_added::create($eventparams);
|
||||
$event->add_record_snapshot('message_contacts', $messagecontact);
|
||||
$event->trigger();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles removing a contact.
|
||||
*
|
||||
* @param int $userid The id of the user who is removing a user as a contact
|
||||
* @param int $contactid The id of the user to be removed as a contact
|
||||
*/
|
||||
public static function remove_contact(int $userid, int $contactid) {
|
||||
global $DB;
|
||||
|
||||
if ($contact = self::get_contact($userid, $contactid)) {
|
||||
$DB->delete_records('message_contacts', ['id' => $contact->id]);
|
||||
|
||||
$event = \core\event\message_contact_removed::create(array(
|
||||
'objectid' => $contact->id,
|
||||
'userid' => $userid,
|
||||
'relateduserid' => $contactid,
|
||||
'context' => \context_user::instance($userid)
|
||||
));
|
||||
$event->add_record_snapshot('message_contacts', $contact);
|
||||
$event->trigger();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles blocking a user.
|
||||
*
|
||||
* @param int $userid The id of the user who is blocking
|
||||
* @param int $usertoblockid The id of the user being blocked
|
||||
*/
|
||||
public static function block_user(int $userid, int $usertoblockid) {
|
||||
global $DB;
|
||||
|
||||
$blocked = new \stdClass();
|
||||
$blocked->userid = $userid;
|
||||
$blocked->blockeduserid = $usertoblockid;
|
||||
$blocked->timecreated = time();
|
||||
$blocked->id = $DB->insert_record('message_users_blocked', $blocked);
|
||||
|
||||
// Trigger event for blocking a contact.
|
||||
$event = \core\event\message_user_blocked::create(array(
|
||||
'objectid' => $blocked->id,
|
||||
'userid' => $userid,
|
||||
'relateduserid' => $usertoblockid,
|
||||
'context' => \context_user::instance($userid)
|
||||
));
|
||||
$event->add_record_snapshot('message_users_blocked', $blocked);
|
||||
$event->trigger();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles unblocking a user.
|
||||
*
|
||||
* @param int $userid The id of the user who is unblocking
|
||||
* @param int $usertounblockid The id of the user being unblocked
|
||||
*/
|
||||
public static function unblock_user(int $userid, int $usertounblockid) {
|
||||
global $DB;
|
||||
|
||||
if ($blockeduser = $DB->get_record('message_users_blocked',
|
||||
['userid' => $userid, 'blockeduserid' => $usertounblockid])) {
|
||||
$DB->delete_records('message_users_blocked', ['id' => $blockeduser->id]);
|
||||
|
||||
// Trigger event for unblocking a contact.
|
||||
$event = \core\event\message_user_unblocked::create(array(
|
||||
'objectid' => $blockeduser->id,
|
||||
'userid' => $userid,
|
||||
'relateduserid' => $usertounblockid,
|
||||
'context' => \context_user::instance($userid)
|
||||
));
|
||||
$event->add_record_snapshot('message_users_blocked', $blockeduser);
|
||||
$event->trigger();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if users are already contacts.
|
||||
*
|
||||
* @param int $userid The id of one of the users
|
||||
* @param int $contactid The id of the other user
|
||||
* @return bool Returns true if they are a contact, false otherwise
|
||||
*/
|
||||
public static function is_contact(int $userid, int $contactid) : bool {
|
||||
global $DB;
|
||||
|
||||
$sql = "SELECT id
|
||||
FROM {message_contacts} mc
|
||||
WHERE (mc.userid = ? AND mc.contactid = ?)
|
||||
OR (mc.userid = ? AND mc.contactid = ?)";
|
||||
return $DB->record_exists_sql($sql, [$userid, $contactid, $contactid, $userid]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the row in the database table message_contacts that represents the contact between two people.
|
||||
*
|
||||
* @param int $userid The id of one of the users
|
||||
* @param int $contactid The id of the other user
|
||||
* @return mixed A fieldset object containing the record, false otherwise
|
||||
*/
|
||||
public static function get_contact(int $userid, int $contactid) {
|
||||
global $DB;
|
||||
|
||||
$sql = "SELECT mc.*
|
||||
FROM {message_contacts} mc
|
||||
WHERE (mc.userid = ? AND mc.contactid = ?)
|
||||
OR (mc.userid = ? AND mc.contactid = ?)";
|
||||
return $DB->get_record_sql($sql, [$userid, $contactid, $contactid, $userid]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a user is already blocked.
|
||||
*
|
||||
* This is different than self::is_user_blocked() as it does not check any capabilities.
|
||||
* It simply checks if an entry exists in the DB.
|
||||
*
|
||||
* @param int $userid
|
||||
* @param int $blockeduserid
|
||||
* @return bool Returns true if they are a blocked, false otherwise
|
||||
*/
|
||||
public static function is_blocked(int $userid, int $blockeduserid) : bool {
|
||||
global $DB;
|
||||
|
||||
return $DB->record_exists('message_users_blocked', ['userid' => $userid, 'blockeduserid' => $blockeduserid]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a contact request already exists between users.
|
||||
*
|
||||
* @param int $userid The id of the user who is creating the contact request
|
||||
* @param int $requesteduserid The id of the user being requested
|
||||
* @return bool Returns true if a contact request exists, false otherwise
|
||||
*/
|
||||
public static function does_contact_request_exist(int $userid, int $requesteduserid) : bool {
|
||||
global $DB;
|
||||
|
||||
$sql = "SELECT id
|
||||
FROM {message_contact_requests} mcr
|
||||
WHERE (mcr.userid = ? AND mcr.requesteduserid = ?)
|
||||
OR (mcr.userid = ? AND mcr.requesteduserid = ?)";
|
||||
return $DB->record_exists_sql($sql, [$userid, $requesteduserid, $requesteduserid, $userid]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -191,6 +191,7 @@ class core_message_external extends external_api {
|
|||
/**
|
||||
* Create contacts parameters description.
|
||||
*
|
||||
* @deprecated since Moodle 3.6
|
||||
* @return external_function_parameters
|
||||
* @since Moodle 2.5
|
||||
*/
|
||||
|
@ -210,6 +211,7 @@ class core_message_external extends external_api {
|
|||
/**
|
||||
* Create contacts.
|
||||
*
|
||||
* @deprecated since Moodle 3.6
|
||||
* @param array $userids array of user IDs.
|
||||
* @param int $userid The id of the user we are creating the contacts for
|
||||
* @return external_description
|
||||
|
@ -256,6 +258,7 @@ class core_message_external extends external_api {
|
|||
/**
|
||||
* Create contacts return description.
|
||||
*
|
||||
* @deprecated since Moodle 3.6
|
||||
* @return external_description
|
||||
* @since Moodle 2.5
|
||||
*/
|
||||
|
@ -263,6 +266,15 @@ class core_message_external extends external_api {
|
|||
return new external_warnings();
|
||||
}
|
||||
|
||||
/**
|
||||
* Marking the method as deprecated.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function create_contacts_is_deprecated() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete contacts parameters description.
|
||||
*
|
||||
|
@ -334,6 +346,7 @@ class core_message_external extends external_api {
|
|||
/**
|
||||
* Block contacts parameters description.
|
||||
*
|
||||
* @deprecated since Moodle 3.6
|
||||
* @return external_function_parameters
|
||||
* @since Moodle 2.5
|
||||
*/
|
||||
|
@ -353,6 +366,7 @@ class core_message_external extends external_api {
|
|||
/**
|
||||
* Block contacts.
|
||||
*
|
||||
* @deprecated since Moodle 3.6
|
||||
* @param array $userids array of user IDs.
|
||||
* @param int $userid The id of the user we are blocking the contacts for
|
||||
* @return external_description
|
||||
|
@ -399,6 +413,7 @@ class core_message_external extends external_api {
|
|||
/**
|
||||
* Block contacts return description.
|
||||
*
|
||||
* @deprecated since Moodle 3.6
|
||||
* @return external_description
|
||||
* @since Moodle 2.5
|
||||
*/
|
||||
|
@ -406,9 +421,19 @@ class core_message_external extends external_api {
|
|||
return new external_warnings();
|
||||
}
|
||||
|
||||
/**
|
||||
* Marking the method as deprecated.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function block_contacts_is_deprecated() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unblock contacts parameters description.
|
||||
*
|
||||
* @deprecated since Moodle 3.6
|
||||
* @return external_function_parameters
|
||||
* @since Moodle 2.5
|
||||
*/
|
||||
|
@ -428,6 +453,7 @@ class core_message_external extends external_api {
|
|||
/**
|
||||
* Unblock contacts.
|
||||
*
|
||||
* @deprecated since Moodle 3.6
|
||||
* @param array $userids array of user IDs.
|
||||
* @param int $userid The id of the user we are unblocking the contacts for
|
||||
* @return null
|
||||
|
@ -467,6 +493,7 @@ class core_message_external extends external_api {
|
|||
/**
|
||||
* Unblock contacts return description.
|
||||
*
|
||||
* @deprecated since Moodle 3.6
|
||||
* @return external_description
|
||||
* @since Moodle 2.5
|
||||
*/
|
||||
|
@ -474,6 +501,15 @@ class core_message_external extends external_api {
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Marking the method as deprecated.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function unblock_contacts_is_deprecated() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the structure of a message area contact.
|
||||
*
|
||||
|
|
150
message/lib.php
150
message/lib.php
|
@ -151,156 +151,6 @@ function message_format_message_text($message, $forcetexttohtml = false) {
|
|||
return format_text($messagetext, $format, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the selected user as a contact for the current user
|
||||
*
|
||||
* @param int $contactid the ID of the user to add as a contact
|
||||
* @param int $blocked 1 if you wish to block the contact
|
||||
* @param int $userid the user ID of the user we want to add the contact for, defaults to current user if not specified.
|
||||
* @return bool/int false if the $contactid isnt a valid user id. True if no changes made.
|
||||
* Otherwise returns the result of update_record() or insert_record()
|
||||
*/
|
||||
function message_add_contact($contactid, $blocked = 0, $userid = 0) {
|
||||
global $USER, $DB;
|
||||
|
||||
if (!$DB->record_exists('user', array('id' => $contactid))) { // invalid userid
|
||||
return false;
|
||||
}
|
||||
|
||||
if (empty($userid)) {
|
||||
$userid = $USER->id;
|
||||
}
|
||||
|
||||
// Check if a record already exists as we may be changing blocking status.
|
||||
if (($contact = $DB->get_record('message_contacts', array('userid' => $userid, 'contactid' => $contactid))) !== false) {
|
||||
// Check if blocking status has been changed.
|
||||
if ($contact->blocked != $blocked) {
|
||||
$contact->blocked = $blocked;
|
||||
$DB->update_record('message_contacts', $contact);
|
||||
|
||||
if ($blocked == 1) {
|
||||
// Trigger event for blocking a contact.
|
||||
$event = \core\event\message_contact_blocked::create(array(
|
||||
'objectid' => $contact->id,
|
||||
'userid' => $contact->userid,
|
||||
'relateduserid' => $contact->contactid,
|
||||
'context' => context_user::instance($contact->userid)
|
||||
));
|
||||
$event->add_record_snapshot('message_contacts', $contact);
|
||||
$event->trigger();
|
||||
} else {
|
||||
// Trigger event for unblocking a contact.
|
||||
$event = \core\event\message_contact_unblocked::create(array(
|
||||
'objectid' => $contact->id,
|
||||
'userid' => $contact->userid,
|
||||
'relateduserid' => $contact->contactid,
|
||||
'context' => context_user::instance($contact->userid)
|
||||
));
|
||||
$event->add_record_snapshot('message_contacts', $contact);
|
||||
$event->trigger();
|
||||
}
|
||||
|
||||
return true;
|
||||
} else {
|
||||
// No change to blocking status.
|
||||
return true;
|
||||
}
|
||||
|
||||
} else {
|
||||
// New contact record.
|
||||
$contact = new stdClass();
|
||||
$contact->userid = $userid;
|
||||
$contact->contactid = $contactid;
|
||||
$contact->blocked = $blocked;
|
||||
$contact->id = $DB->insert_record('message_contacts', $contact);
|
||||
|
||||
$eventparams = array(
|
||||
'objectid' => $contact->id,
|
||||
'userid' => $contact->userid,
|
||||
'relateduserid' => $contact->contactid,
|
||||
'context' => context_user::instance($contact->userid)
|
||||
);
|
||||
|
||||
if ($blocked) {
|
||||
$event = \core\event\message_contact_blocked::create($eventparams);
|
||||
} else {
|
||||
$event = \core\event\message_contact_added::create($eventparams);
|
||||
}
|
||||
// Trigger event.
|
||||
$event->trigger();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* remove a contact
|
||||
*
|
||||
* @param int $contactid the user ID of the contact to remove
|
||||
* @param int $userid the user ID of the user we want to remove the contacts for, defaults to current user if not specified.
|
||||
* @return bool returns the result of delete_records()
|
||||
*/
|
||||
function message_remove_contact($contactid, $userid = 0) {
|
||||
global $USER, $DB;
|
||||
|
||||
if (empty($userid)) {
|
||||
$userid = $USER->id;
|
||||
}
|
||||
|
||||
if ($contact = $DB->get_record('message_contacts', array('userid' => $userid, 'contactid' => $contactid))) {
|
||||
$DB->delete_records('message_contacts', array('id' => $contact->id));
|
||||
|
||||
// Trigger event for removing a contact.
|
||||
$event = \core\event\message_contact_removed::create(array(
|
||||
'objectid' => $contact->id,
|
||||
'userid' => $contact->userid,
|
||||
'relateduserid' => $contact->contactid,
|
||||
'context' => context_user::instance($contact->userid)
|
||||
));
|
||||
$event->add_record_snapshot('message_contacts', $contact);
|
||||
$event->trigger();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unblock a contact. Note that this reverts the previously blocked user back to a non-contact.
|
||||
*
|
||||
* @param int $contactid the user ID of the contact to unblock
|
||||
* @param int $userid the user ID of the user we want to unblock the contact for, defaults to current user
|
||||
* if not specified.
|
||||
* @return bool returns the result of delete_records()
|
||||
*/
|
||||
function message_unblock_contact($contactid, $userid = 0) {
|
||||
return message_add_contact($contactid, 0, $userid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Block a user.
|
||||
*
|
||||
* @param int $contactid the user ID of the user to block
|
||||
* @param int $userid the user ID of the user we want to unblock the contact for, defaults to current user
|
||||
* if not specified.
|
||||
* @return bool
|
||||
*/
|
||||
function message_block_contact($contactid, $userid = 0) {
|
||||
return message_add_contact($contactid, 1, $userid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a user's contact record
|
||||
*
|
||||
* @param int $contactid the user ID of the user whose contact record you want
|
||||
* @return array message contacts
|
||||
*/
|
||||
function message_get_contact($contactid) {
|
||||
global $USER, $DB;
|
||||
return $DB->get_record('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid));
|
||||
}
|
||||
|
||||
/**
|
||||
* Search through course users.
|
||||
*
|
||||
|
|
|
@ -1939,4 +1939,232 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
|
|||
$this->assertEquals($conversationid,
|
||||
\core_message\api::get_conversation_between_users([$user1->id, $user2->id]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a contact request.
|
||||
*/
|
||||
public function test_create_contact_request() {
|
||||
global $DB;
|
||||
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
|
||||
\core_message\api::create_contact_request($user1->id, $user2->id);
|
||||
|
||||
$request = $DB->get_records('message_contact_requests');
|
||||
|
||||
$this->assertCount(1, $request);
|
||||
|
||||
$request = reset($request);
|
||||
|
||||
$this->assertEquals($user1->id, $request->userid);
|
||||
$this->assertEquals($user2->id, $request->requesteduserid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test confirming a contact request.
|
||||
*/
|
||||
public function test_confirm_contact_request() {
|
||||
global $DB;
|
||||
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
|
||||
\core_message\api::create_contact_request($user1->id, $user2->id);
|
||||
|
||||
\core_message\api::confirm_contact_request($user1->id, $user2->id);
|
||||
|
||||
$this->assertEquals(0, $DB->count_records('message_contact_requests'));
|
||||
|
||||
$contact = $DB->get_records('message_contacts');
|
||||
|
||||
$this->assertCount(1, $contact);
|
||||
|
||||
$contact = reset($contact);
|
||||
|
||||
$this->assertEquals($user1->id, $contact->userid);
|
||||
$this->assertEquals($user2->id, $contact->contactid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test declining a contact request.
|
||||
*/
|
||||
public function test_decline_contact_request() {
|
||||
global $DB;
|
||||
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
|
||||
\core_message\api::create_contact_request($user1->id, $user2->id);
|
||||
|
||||
\core_message\api::decline_contact_request($user1->id, $user2->id);
|
||||
|
||||
$this->assertEquals(0, $DB->count_records('message_contact_requests'));
|
||||
$this->assertEquals(0, $DB->count_records('message_contacts'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test retrieving contact requests.
|
||||
*/
|
||||
public function test_get_contact_requests() {
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
$user3 = self::getDataGenerator()->create_user();
|
||||
|
||||
// Block one user, their request should not show up.
|
||||
\core_message\api::block_user($user1->id, $user3->id);
|
||||
|
||||
\core_message\api::create_contact_request($user2->id, $user1->id);
|
||||
\core_message\api::create_contact_request($user3->id, $user1->id);
|
||||
|
||||
$requests = \core_message\api::get_contact_requests($user1->id);
|
||||
|
||||
$this->assertCount(1, $requests);
|
||||
|
||||
$request = reset($requests);
|
||||
|
||||
$this->assertEquals($user2->id, $request->id);
|
||||
$this->assertEquals($user2->picture, $request->picture);
|
||||
$this->assertEquals($user2->firstname, $request->firstname);
|
||||
$this->assertEquals($user2->lastname, $request->lastname);
|
||||
$this->assertEquals($user2->firstnamephonetic, $request->firstnamephonetic);
|
||||
$this->assertEquals($user2->lastnamephonetic, $request->lastnamephonetic);
|
||||
$this->assertEquals($user2->middlename, $request->middlename);
|
||||
$this->assertEquals($user2->alternatename, $request->alternatename);
|
||||
$this->assertEquals($user2->email, $request->email);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test adding contacts.
|
||||
*/
|
||||
public function test_add_contact() {
|
||||
global $DB;
|
||||
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
|
||||
\core_message\api::add_contact($user1->id, $user2->id);
|
||||
|
||||
$contact = $DB->get_records('message_contacts');
|
||||
|
||||
$this->assertCount(1, $contact);
|
||||
|
||||
$contact = reset($contact);
|
||||
|
||||
$this->assertEquals($user1->id, $contact->userid);
|
||||
$this->assertEquals($user2->id, $contact->contactid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test removing contacts.
|
||||
*/
|
||||
public function test_remove_contact() {
|
||||
global $DB;
|
||||
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
|
||||
\core_message\api::add_contact($user1->id, $user2->id);
|
||||
\core_message\api::remove_contact($user1->id, $user2->id);
|
||||
|
||||
$this->assertEquals(0, $DB->count_records('message_contacts'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test blocking users.
|
||||
*/
|
||||
public function test_block_user() {
|
||||
global $DB;
|
||||
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
|
||||
\core_message\api::block_user($user1->id, $user2->id);
|
||||
|
||||
$blockedusers = $DB->get_records('message_users_blocked');
|
||||
|
||||
$this->assertCount(1, $blockedusers);
|
||||
|
||||
$blockeduser = reset($blockedusers);
|
||||
|
||||
$this->assertEquals($user1->id, $blockeduser->userid);
|
||||
$this->assertEquals($user2->id, $blockeduser->blockeduserid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test unblocking users.
|
||||
*/
|
||||
public function test_unblock_user() {
|
||||
global $DB;
|
||||
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
|
||||
\core_message\api::block_user($user1->id, $user2->id);
|
||||
\core_message\api::unblock_user($user1->id, $user2->id);
|
||||
|
||||
$this->assertEquals(0, $DB->count_records('message_users_blocked'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test is contact check.
|
||||
*/
|
||||
public function test_is_contact() {
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
$user3 = self::getDataGenerator()->create_user();
|
||||
|
||||
\core_message\api::add_contact($user1->id, $user2->id);
|
||||
|
||||
$this->assertTrue(\core_message\api::is_contact($user1->id, $user2->id));
|
||||
$this->assertTrue(\core_message\api::is_contact($user2->id, $user1->id));
|
||||
$this->assertFalse(\core_message\api::is_contact($user2->id, $user3->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get contact.
|
||||
*/
|
||||
public function test_get_contact() {
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
|
||||
\core_message\api::add_contact($user1->id, $user2->id);
|
||||
|
||||
$contact = \core_message\api::get_contact($user1->id, $user2->id);
|
||||
|
||||
$this->assertEquals($user1->id, $contact->userid);
|
||||
$this->assertEquals($user2->id, $contact->contactid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test is blocked checked.
|
||||
*/
|
||||
public function test_is_blocked() {
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
|
||||
$this->assertFalse(\core_message\api::is_blocked($user1->id, $user2->id));
|
||||
$this->assertFalse(\core_message\api::is_blocked($user2->id, $user1->id));
|
||||
|
||||
\core_message\api::block_user($user1->id, $user2->id);
|
||||
|
||||
$this->assertTrue(\core_message\api::is_blocked($user1->id, $user2->id));
|
||||
$this->assertFalse(\core_message\api::is_blocked($user2->id, $user1->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the contact request exist check.
|
||||
*/
|
||||
public function test_does_contact_request_exist() {
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
|
||||
$this->assertFalse(\core_message\api::does_contact_request_exist($user1->id, $user2->id));
|
||||
$this->assertFalse(\core_message\api::does_contact_request_exist($user2->id, $user1->id));
|
||||
|
||||
\core_message\api::create_contact_request($user1->id, $user2->id);
|
||||
|
||||
$this->assertTrue(\core_message\api::does_contact_request_exist($user1->id, $user2->id));
|
||||
$this->assertTrue(\core_message\api::does_contact_request_exist($user2->id, $user1->id));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue