/** * 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 object $recipient User object. * @param object $sender User object. * @return bool true if user is permitted, false otherwise. */ function message_can_post_message($recipient, $sender = null) { global $USER, $DB; if (is_null($sender)) { // The message is from the logged in user, unless otherwise specified. $sender = $USER; } if (!has_capability('moodle/site:sendmessage', context_system::instance(), $sender)) { return false; } // The recipient blocks messages from non-contacts and the // sender isn't a contact. if (message_is_user_non_contact_blocked($recipient, $sender)) { return false; } // The recipient has specifically blocked this sender. if (message_is_user_blocked($recipient, $sender)) { return false; } return true; }
//, array('class' => $historyclass) $messagehistorylink .= get_string('unreadnewmessages', 'message', $displaycount); $messagehistorylink .= html_writer::end_tag('span'); } $messagehistorylink .= html_writer::end_tag('div'); message_print_message_history($user1, $user2, $search, $displaycount, $messagehistorylink, $viewingnewmessages, $showactionlinks); echo html_writer::end_tag('div'); //send message form if ($currentuser && has_capability('moodle/site:sendmessage', $systemcontext) && $user2realuser) { echo html_writer::start_tag('div', array('class' => 'mdl-align messagesend')); if (!empty($messageerror)) { echo html_writer::tag('span', $messageerror, array('id' => 'messagewarning')); } else { // Display a warning if the current user is blocking non-contacts and is about to message to a non-contact // Otherwise they may wonder why they never get a reply if (message_is_user_non_contact_blocked($user1, $user2)) { $msg = get_string('messagingblockednoncontact', 'message', fullname($user2)); echo html_writer::tag('span', $msg, array('id' => 'messagewarning')); } $mform = new send_form(); $defaultmessage = new stdClass(); $defaultmessage->id = $user2->id; $defaultmessage->viewing = $viewing; $defaultmessage->message = ''; //$defaultmessage->messageformat = FORMAT_MOODLE; $mform->set_data($defaultmessage); $mform->display(); } echo html_writer::end_tag('div'); } } else {
/** * Test that message_is_user_non_contact_blocked returns false if the recipient doesn't * allow messages from non-contacts but the sender is a contact. */ public function test_message_is_user_non_contact_blocked_false_with_if_contact() { $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); $this->setUser($recipient); set_user_preference('message_blocknoncontacts', true, $recipient); message_add_contact($sender->id); $this->assertFalse(message_is_user_non_contact_blocked($recipient, $sender)); }