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