public static function can_post_message($recipient, $sender = null) {
        global $USER;

        if (is_null($sender)) {
            // The message is from the logged in user, unless otherwise specified.
            $sender = $USER;
        }

        $systemcontext = \context_system::instance();
        if (!has_capability('moodle/site:sendmessage', $systemcontext, $sender)) {
            return false;
        }

        if (has_capability('moodle/site:readallmessages', $systemcontext, $sender->id)) {
            return true;
        }

        // Check if the recipient can be messaged by the sender.
        return (self::can_contact_user($recipient->id, $sender->id));
    }
public static function can_post_message($recipient, $sender = null) {
        global $USER;

        if (is_null($sender)) {
            // The message is from the logged in user, unless otherwise specified.
            $sender = $USER;
        }

        $systemcontext = \context_system::instance();
        if (!has_capability('moodle/site:sendmessage', $systemcontext, $sender)) {
            return false;
        }

        if (has_capability('moodle/site:readallmessages', $systemcontext, $sender->id)) {
            return true;
        }

        // Check if the recipient can be messaged by the sender.
        return (self::can_contact_user($recipient->id, $sender->id));
    }
public static function can_post_message ( $ recipient , $ sender = null ) { global $ USER ; if ( is_null ( $ sender ) ) { // The message is from the logged in user, unless otherwise specified. $ sender = $ USER ; } $ systemcontext = \ context_system :: instance ( ) ; if ( ! has_capability ( 'moodle/site:sendmessage' , $ systemcontext , $ sender ) ) { return false ; } if ( has_capability ( 'moodle/site:readallmessages' , $ systemcontext , $ sender -> id ) ) { return true ; } // Check if the recipient can be messaged by the sender. return ( self :: can_contact_user ( $ recipient -> id , $ sender -> id ) ) ; }
Determines if a user is permitted to send another user a private message.
If no sender is provided then it defaults to the logged in user.

@param \stdClass $recipient The user object.
@param \stdClass|null $sender The user object.
@return bool true if user is permitted, false otherwise.
Determines if a user is permitted to send another user a private message . If no sender is provided then it defaults to the logged in user .