MDL-63724 core_message: fix method signature for can_contact_user()

This method required objects, but only used their ids, so has been
changed to require only ids.
This commit is contained in:
Jake Dallimore 2018-11-05 16:08:48 +08:00
parent 8e3bf150d9
commit 9f82758c01

View file

@ -1428,7 +1428,7 @@ class api {
}
// Check if the recipient can be messaged by the sender.
return (self::can_contact_user($recipient, $sender));
return (self::can_contact_user($recipient->id, $sender->id));
}
/**
@ -1465,11 +1465,7 @@ class api {
});
$otheruser = reset($otheruser);
// Spoof the stdClass for now - can_contact_user() only uses the ids.
$user = (object) ['id' => $userid];
$otheruser = (object) ['id' => $otheruser->id];
return self::can_contact_user($otheruser, $user);
return self::can_contact_user($otheruser->id, $userid);
} else {
throw new \moodle_exception("Invalid conversation type '$conversation->type'.");
}
@ -2274,12 +2270,12 @@ class api {
/**
* Checks if the sender can message the recipient.
*
* @param \stdClass $recipient The user object.
* @param \stdClass $sender The user object.
* @param int $recipientid
* @param int $senderid
* @return bool true if recipient hasn't blocked sender and sender can contact to recipient, false otherwise.
*/
protected static function can_contact_user(\stdClass $recipient, \stdClass $sender) : bool {
if (has_capability('moodle/site:messageanyuser', \context_system::instance(), $sender->id)) {
protected static function can_contact_user(int $recipientid, int $senderid) : bool {
if (has_capability('moodle/site:messageanyuser', \context_system::instance(), $senderid)) {
// The sender has the ability to contact any user across the entire site.
return true;
}
@ -2287,7 +2283,7 @@ class api {
// The initial value of $cancontact is null to indicate that a value has not been determined.
$cancontact = null;
if (self::is_blocked($recipient->id, $sender->id)) {
if (self::is_blocked($recipientid, $senderid)) {
// The recipient has specifically blocked this sender.
$cancontact = false;
}
@ -2301,7 +2297,7 @@ class api {
//
// The Site option is only possible when the messagingallusers site setting is also enabled.
$privacypreference = self::get_user_privacy_messaging_preference($recipient->id);
$privacypreference = self::get_user_privacy_messaging_preference($recipientid);
if (self::MESSAGE_PRIVACY_SITE === $privacypreference) {
// The user preference is to allow any user to contact them.
// No need to check anything else.
@ -2309,12 +2305,12 @@ class api {
} else {
// This user only allows their own contacts, and possibly course peers, to contact them.
// If the users are contacts then we can avoid the more expensive shared courses check.
$cancontact = self::is_contact($sender->id, $recipient->id);
$cancontact = self::is_contact($senderid, $recipientid);
if (!$cancontact && self::MESSAGE_PRIVACY_COURSEMEMBER === $privacypreference) {
// The users are not contacts and the user allows course member messaging.
// Check whether these two users share any course together.
$sharedcourses = enrol_get_shared_courses($recipient->id, $sender->id, true);
$sharedcourses = enrol_get_shared_courses($recipientid, $senderid, true);
$cancontact = (!empty($sharedcourses));
}
}
@ -2327,12 +2323,12 @@ class api {
// Note: You cannot use empty($sharedcourses) here because this may be an empty array.
if (null === $sharedcourses) {
$sharedcourses = enrol_get_shared_courses($recipient->id, $sender->id, true);
$sharedcourses = enrol_get_shared_courses($recipientid, $senderid, true);
}
foreach ($sharedcourses as $course) {
// Note: enrol_get_shared_courses will preload any shared context.
if (has_capability('moodle/site:messageanyuser', \context_course::instance($course->id), $sender->id)) {
if (has_capability('moodle/site:messageanyuser', \context_course::instance($course->id), $senderid)) {
$cancontact = true;
break;
}