'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)
)
);
}
* 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.
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,
'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)
)
);
}
* 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.
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;
'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)
)
);
}
* 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.
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,
'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)
)
);
}
* 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.
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;
*
* @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;
} 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);
* 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.
* 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);
}
/**