MDL-54687 core_message: added userid parameter for altering contacts

This commit is contained in:
Mark Nelson 2016-07-01 11:50:59 +08:00
parent c060cd4904
commit 34c2f34762
2 changed files with 51 additions and 25 deletions

View File

@ -198,7 +198,9 @@ class core_message_external extends external_api {
'userids' => new external_multiple_structure(
new external_value(PARAM_INT, 'User ID'),
'List of user IDs'
)
),
'userid' => new external_value(PARAM_INT, 'The id of the user we are creating the contacts for, 0 for the
current user', VALUE_DEFAULT, 0)
)
);
}
@ -207,10 +209,11 @@ class core_message_external extends external_api {
* Create contacts.
*
* @param array $userids array of user IDs.
* @param int $userid The id of the user we are creating the contacts for
* @return external_description
* @since Moodle 2.5
*/
public static function create_contacts($userids) {
public static function create_contacts($userids, $userid = 0) {
global $CFG;
// Check if messaging is enabled.
@ -218,12 +221,12 @@ class core_message_external extends external_api {
throw new moodle_exception('disabled', 'message');
}
$params = array('userids' => $userids);
$params = array('userids' => $userids, 'userid' => $userid);
$params = self::validate_parameters(self::create_contacts_parameters(), $params);
$warnings = array();
foreach ($params['userids'] as $id) {
if (!message_add_contact($id)) {
if (!message_add_contact($id, 0, $userid)) {
$warnings[] = array(
'item' => 'user',
'itemid' => $id,
@ -257,7 +260,9 @@ class core_message_external extends external_api {
'userids' => new external_multiple_structure(
new external_value(PARAM_INT, 'User ID'),
'List of user IDs'
)
),
'userid' => new external_value(PARAM_INT, 'The id of the user we are deleting the contacts for, 0 for the
current user', VALUE_DEFAULT, 0)
)
);
}
@ -266,10 +271,11 @@ class core_message_external extends external_api {
* Delete contacts.
*
* @param array $userids array of user IDs.
* @param int $userid The id of the user we are deleting the contacts for
* @return null
* @since Moodle 2.5
*/
public static function delete_contacts($userids) {
public static function delete_contacts($userids, $userid = 0) {
global $CFG;
// Check if messaging is enabled.
@ -277,11 +283,11 @@ class core_message_external extends external_api {
throw new moodle_exception('disabled', 'message');
}
$params = array('userids' => $userids);
$params = array('userids' => $userids, 'userid' => $userid);
$params = self::validate_parameters(self::delete_contacts_parameters(), $params);
foreach ($params['userids'] as $id) {
message_remove_contact($id);
message_remove_contact($id, $userid);
}
return null;
@ -309,7 +315,9 @@ class core_message_external extends external_api {
'userids' => new external_multiple_structure(
new external_value(PARAM_INT, 'User ID'),
'List of user IDs'
)
),
'userid' => new external_value(PARAM_INT, 'The id of the user we are blocking the contacts for, 0 for the
current user', VALUE_DEFAULT, 0)
)
);
}
@ -318,10 +326,11 @@ class core_message_external extends external_api {
* Block contacts.
*
* @param array $userids array of user IDs.
* @param int $userid The id of the user we are blocking the contacts for
* @return external_description
* @since Moodle 2.5
*/
public static function block_contacts($userids) {
public static function block_contacts($userids, $userid = 0) {
global $CFG;
// Check if messaging is enabled.
@ -329,12 +338,12 @@ class core_message_external extends external_api {
throw new moodle_exception('disabled', 'message');
}
$params = array('userids' => $userids);
$params = array('userids' => $userids, 'userid' => $userid);
$params = self::validate_parameters(self::block_contacts_parameters(), $params);
$warnings = array();
foreach ($params['userids'] as $id) {
if (!message_block_contact($id)) {
if (!message_block_contact($id, $userid)) {
$warnings[] = array(
'item' => 'user',
'itemid' => $id,
@ -368,7 +377,9 @@ class core_message_external extends external_api {
'userids' => new external_multiple_structure(
new external_value(PARAM_INT, 'User ID'),
'List of user IDs'
)
),
'userid' => new external_value(PARAM_INT, 'The id of the user we are unblocking the contacts for, 0 for the
current user', VALUE_DEFAULT, 0)
)
);
}
@ -377,10 +388,11 @@ class core_message_external extends external_api {
* Unblock contacts.
*
* @param array $userids array of user IDs.
* @param int $userid The id of the user we are unblocking the contacts for
* @return null
* @since Moodle 2.5
*/
public static function unblock_contacts($userids) {
public static function unblock_contacts($userids, $userid = 0) {
global $CFG;
// Check if messaging is enabled.
@ -388,11 +400,11 @@ class core_message_external extends external_api {
throw new moodle_exception('disabled', 'message');
}
$params = array('userids' => $userids);
$params = array('userids' => $userids, 'userid' => $userid);
$params = self::validate_parameters(self::unblock_contacts_parameters(), $params);
foreach ($params['userids'] as $id) {
message_unblock_contact($id);
message_unblock_contact($id, $userid);
}
return null;

View File

@ -496,18 +496,23 @@ function message_format_message_text($message, $forcetexttohtml = false) {
*
* @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) {
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' => $USER->id, 'contactid' => $contactid))) !== false) {
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;
@ -544,7 +549,7 @@ function message_add_contact($contactid, $blocked=0) {
} else {
// New contact record.
$contact = new stdClass();
$contact->userid = $USER->id;
$contact->userid = $userid;
$contact->contactid = $contactid;
$contact->blocked = $blocked;
$contact->id = $DB->insert_record('message_contacts', $contact);
@ -572,12 +577,17 @@ function message_add_contact($contactid, $blocked=0) {
* 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) {
function message_remove_contact($contactid, $userid = 0) {
global $USER, $DB;
if ($contact = $DB->get_record('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid))) {
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.
@ -600,20 +610,24 @@ function message_remove_contact($contactid) {
* 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) {
return message_add_contact($contactid, 0);
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) {
return message_add_contact($contactid, 1);
function message_block_contact($contactid, $userid = 0) {
return message_add_contact($contactid, 1, $userid);
}
/**