Commit 5b0769db authored by Mark Nelson's avatar Mark Nelson
Browse files

MDL-54744 core_message: deprecated functions in message/lib.php

Also -

1) Made note of the deprecation as well as the functions and
   files that were removed earlier in upgrade.txt.
2) Added new functions to api.php to replace the deprecated ones
   that were being used in unit tests.
3) Removed files that are no longer being used.
parent 7b477f59
This diff is collapsed.
......@@ -673,4 +673,122 @@ class api {
return $preferences;
}
/**
* Count the number of users blocked by a user.
*
* @param \stdClass $user The user object
* @return int the number of blocked users
*/
public static function count_blocked_users($user = null) {
global $USER, $DB;
if (empty($user)) {
$user = $USER;
}
$sql = "SELECT count(mc.id)
FROM {message_contacts} mc
WHERE mc.userid = :userid AND mc.blocked = 1";
return $DB->count_records_sql($sql, array('userid' => $user->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.
*/
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;
}
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 (self::is_user_non_contact_blocked($recipient, $sender)) {
return false;
}
// The recipient has specifically blocked this sender.
if (self::is_user_blocked($recipient, $sender)) {
return false;
}
return true;
}
/**
* Checks if the recipient is allowing messages from users that aren't a
* contact. If not then it checks to make sure the sender is in the
* recipient's contacts.
*
* @param \stdClass $recipient The user object.
* @param \stdClass|null $sender The user object.
* @return bool true if $sender is blocked, false otherwise.
*/
public static function is_user_non_contact_blocked($recipient, $sender = null) {
global $USER, $DB;
if (is_null($sender)) {
// The message is from the logged in user, unless otherwise specified.
$sender = $USER;
}
$blockednoncontacts = get_user_preferences('message_blocknoncontacts', '', $recipient->id);
if (!empty($blockednoncontacts)) {
// Confirm the sender is a contact of the recipient.
$exists = $DB->record_exists('message_contacts', array('userid' => $recipient->id, 'contactid' => $sender->id));
if ($exists) {
// All good, the recipient is a contact of the sender.
return false;
} else {
// Oh no, the recipient is not a contact. Looks like we can't send the message.
return true;
}
}
return false;
}
/**
* Checks if the recipient has specifically blocked the sending user.
*
* Note: This function will always return false if the sender has the
* readallmessages capability at the system context level.
*
* @param object $recipient User object.
* @param object $sender User object.
* @return bool true if $sender is blocked, false otherwise.
*/
public static function is_user_blocked($recipient, $sender = null) {
global $USER, $DB;
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:readallmessages', $systemcontext, $sender)) {
return false;
}
if ($contact = $DB->get_record('message_contacts', array('userid' => $recipient->id, 'contactid' => $sender->id))) {
if ($contact->blocked) {
return true;
}
}
return false;
}
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file was replaced by index.php in Moodle 2.0 and now simply redirects to index.php
*
* @package core_message
* @copyright 2005 Luis Rodrigues and Martin Dougiamas
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require(__DIR__ . '/../config.php');
require_once($CFG->dirroot . '/message/lib.php');
//the same URL params as in 1.9
$userid = required_param('id', PARAM_INT);
$noframesjs = optional_param('noframesjs', 0, PARAM_BOOL);
$params = array('user2'=>$userid);
if (!empty($noframesjs)) {
$params['noframesjs'] = $noframesjs;
}
$url = new moodle_url('/message/index.php', $params);
redirect($url);
?>
This diff is collapsed.
<form id="message_settings" action="index.php" method="post">
<div>
<input type="hidden" name="tab" value="settings" />
<input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
</div>
<table cellpadding="5" class="message_form boxaligncenter">
<tr valign="top">
<td align="right"><input type="checkbox" name="blocknoncontacts" id="blocknoncontacts" <?php echo $cbblocknoncontacts ?> /></td>
<td colspan="2"><label for="blocknoncontacts"><?php print_string('blocknoncontacts', 'message') ?></label></td>
</tr>
<tr valign="top">
<td align="right"><input type="checkbox" name="beepnewmessage" id="beepnewmessage" <?php echo $cbbeepnewmessage ?> /></td>
<td colspan="2"><label for="beepnewmessage"><?php print_string('beepnewmessage', 'message') ?></label></td>
</tr>
<tr valign="top">
<td colspan="3" align="center"><br /><input type="submit" value="<?php print_string('savemysettings', 'message') ?>" /></td>
</tr>
</table>
</form>
......@@ -331,4 +331,26 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
$this->assertEquals(\core_message\api::count_unread_popup_notifications($recipient1->id), 3);
$this->assertEquals(\core_message\api::count_unread_popup_notifications($recipient2->id), 5);
}
/**
* Test count_blocked_users.
*
*/
public function test_message_count_blocked_users() {
// Set this user as the admin.
$this->setAdminUser();
// Create users to add to the admin's contact list.
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$this->assertEquals(0, \core_message\api::count_blocked_users());
// Add 1 blocked and 1 normal contact to admin's contact list.
message_add_contact($user1->id);
message_add_contact($user2->id, 1);
$this->assertEquals(0, \core_message\api::count_blocked_users($user2));
$this->assertEquals(1, \core_message\api::count_blocked_users());
}
}
\ No newline at end of file
......@@ -126,7 +126,7 @@ class core_message_events_testcase extends advanced_testcase {
$event = reset($events);
$this->assertEmpty($event);
// Make sure that we still have 1 blocked user.
$this->assertEquals(1, message_count_blocked_users());
$this->assertEquals(1, \core_message\api::count_blocked_users());
// Now blocking a user that is not a contact.
$sink->clear();
......@@ -159,7 +159,7 @@ class core_message_events_testcase extends advanced_testcase {
// Block the user.
message_block_contact($user->id);
// Make sure that we have 1 blocked user.
$this->assertEquals(1, message_count_blocked_users());
$this->assertEquals(1, \core_message\api::count_blocked_users());
// Trigger and capture the event when unblocking a contact.
$sink = $this->redirectEvents();
......@@ -176,7 +176,7 @@ class core_message_events_testcase extends advanced_testcase {
$this->assertEquals($url, $event->get_url());
// Make sure that we have no blocked users.
$this->assertEmpty(message_count_blocked_users());
$this->assertEmpty(\core_message\api::count_blocked_users());
// Make sure that the contact unblocked event is not triggered again.
$sink->clear();
......@@ -186,7 +186,7 @@ class core_message_events_testcase extends advanced_testcase {
$this->assertEmpty($event);
// Make sure that we still have no blocked users.
$this->assertEmpty(message_count_blocked_users());
$this->assertEmpty(\core_message\api::count_blocked_users());
}
/**
......
......@@ -244,30 +244,6 @@ class core_message_messagelib_testcase extends advanced_testcase {
$this->assertCount(1, $strangers);
}
/**
* Test message_count_messages.
*/
public function test_message_count_messages() {
global $DB;
// Create users to send and receive message.
$userfrom = $this->getDataGenerator()->create_user();
$userto = $this->getDataGenerator()->create_user();
message_post_message($userfrom, $userto, 'Message 1', FORMAT_PLAIN);
message_post_message($userfrom, $userto, 'Message 2', FORMAT_PLAIN);
message_post_message($userto, $userfrom, 'Message 3', FORMAT_PLAIN);
// Return 0 when no message.
$messages = array();
$this->assertEquals(0, message_count_messages($messages, 'Test', 'Test'));
// Check number of messages from userfrom and userto.
$messages = $this->messagesink->get_messages();
$this->assertEquals(2, message_count_messages($messages, 'useridfrom', $userfrom->id));
$this->assertEquals(1, message_count_messages($messages, 'useridfrom', $userto->id));
}
/**
* Test message_count_unread_messages.
*/
......@@ -287,27 +263,6 @@ class core_message_messagelib_testcase extends advanced_testcase {
$this->assertEquals(1, message_count_unread_messages($userto, $userfrom1));
}
/**
* Test message_count_blocked_users.
*
*/
public function test_message_count_blocked_users() {
// Set this user as the admin.
$this->setAdminUser();
// Create users to add to the admin's contact list.
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$this->assertEquals(0, message_count_blocked_users());
// Add 1 blocked and 1 normal contact to admin's contact list.
message_add_contact($user1->id);
message_add_contact($user2->id, 1);
$this->assertEquals(0, message_count_blocked_users($user2));
$this->assertEquals(1, message_count_blocked_users());
}
/**
* Test message_add_contact.
......@@ -329,7 +284,7 @@ class core_message_messagelib_testcase extends advanced_testcase {
$this->assertNotEmpty(message_get_contact($user1->id));
$this->assertNotEmpty(message_get_contact($user2->id));
$this->assertEquals(false, message_get_contact($user3->id));
$this->assertEquals(1, message_count_blocked_users());
$this->assertEquals(1, \core_message\api::count_blocked_users());
}
/**
......@@ -366,11 +321,11 @@ class core_message_messagelib_testcase extends advanced_testcase {
message_add_contact($user1->id);
message_add_contact($user2->id);
$this->assertEquals(0, message_count_blocked_users());
$this->assertEquals(0, \core_message\api::count_blocked_users());
// Block 1 user.
message_block_contact($user2->id);
$this->assertEquals(1, message_count_blocked_users());
$this->assertEquals(1, \core_message\api::count_blocked_users());
}
......@@ -389,11 +344,11 @@ class core_message_messagelib_testcase extends advanced_testcase {
message_add_contact($user1->id);
message_add_contact($user2->id, 1); // Add blocked contact.
$this->assertEquals(1, message_count_blocked_users());
$this->assertEquals(1, \core_message\api::count_blocked_users());
// Unblock user.
message_unblock_contact($user2->id);
$this->assertEquals(0, message_count_blocked_users());
$this->assertEquals(0, \core_message\api::count_blocked_users());
}
/**
......@@ -417,39 +372,6 @@ class core_message_messagelib_testcase extends advanced_testcase {
$this->assertCount(2, message_search_users(0, 'user'));
}
/**
* Test message_search.
*/
public function test_message_search() {
global $USER;
// Set this user as the admin.
$this->setAdminUser();
// Create a user to add to the admin's contact list.
$user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
$user2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'user2'));
// Send few messages, real (read).
message_post_message($user1, $USER, 'Message 1', FORMAT_PLAIN);
message_post_message($USER, $user1, 'Message 2', FORMAT_PLAIN);
message_post_message($USER, $user2, 'Message 3', FORMAT_PLAIN);
$this->assertCount(2, message_search(array('Message'), true, false));
$this->assertCount(3, message_search(array('Message'), true, true));
// Send fake message (not-read).
$this->send_fake_message($USER, $user1, 'Message 4');
$this->send_fake_message($user1, $USER, 'Message 5');
$this->assertCount(3, message_search(array('Message'), true, false));
$this->assertCount(5, message_search(array('Message'), true, true));
// If courseid given then should be 0.
$this->assertEquals(false, message_search(array('Message'), true, true, ''));
$this->assertEquals(false, message_search(array('Message'), true, true, 2));
$this->assertCount(5, message_search(array('Message'), true, true, SITEID));
}
/**
* The data provider for message_get_recent_conversations.
*
......@@ -760,191 +682,4 @@ class core_message_messagelib_testcase extends advanced_testcase {
}
}
}
/**
* Test message_get_recent_notifications.
*/
public function test_message_get_recent_notifications() {
global $DB, $USER;
// Set this user as the admin.
$this->setAdminUser();
// Create a user to send messages from.
$user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
// Add two messages - will mark them as notifications later.
$m1 = message_post_message($user1, $USER, 'Message 1', FORMAT_PLAIN);
$m2 = message_post_message($user1, $USER, 'Message 2', FORMAT_PLAIN);
// Mark the second message as a notification.
$updatemessage = new stdClass();
$updatemessage->id = $m2;
$updatemessage->notification = 1;
$DB->update_record('message_read', $updatemessage);
// Mark the first message as a notification and change the timecreated to 0.
$updatemessage->id = $m1;
$updatemessage->notification = 1;
$updatemessage->timecreated = 0;
$DB->update_record('message_read', $updatemessage);
$notifications = message_get_recent_notifications($USER);
// Get the messages.
$firstmessage = array_shift($notifications);
$secondmessage = array_shift($notifications);
// Confirm that we have received the notifications with the maximum timecreated, rather than the max id.
$this->assertEquals('Message 2', $firstmessage->smallmessage);
$this->assertEquals('Message 1', $secondmessage->smallmessage);
}
/**
* Test that message_can_post_message returns false if the sender does not have the
* moode/site:sendmessage capability.
*/
public function test_message_can_post_message_returns_false_without_capability() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$context = context_system::instance();
$roleid = $this->getDataGenerator()->create_role();
$this->getDataGenerator()->role_assign($roleid, $sender->id, $context->id);
assign_capability('moodle/site:sendmessage', CAP_PROHIBIT, $roleid, $context);
$this->assertFalse(message_can_post_message($recipient, $sender));
}
/**
* Test that message_can_post_message returns false if the receiver only accepts
* messages from contacts and the sender isn't a contact.
*/
public function test_message_can_post_message_returns_false_non_contact_blocked() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
set_user_preference('message_blocknoncontacts', true, $recipient);
$this->assertFalse(message_can_post_message($recipient, $sender));
}
/**
* Test that message_can_post_message returns false if the receiver has blocked the
* sender from messaging them.
*/
public function test_message_can_post_message_returns_false_if_blocked() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$this->setUser($recipient);
message_block_contact($sender->id);
$this->assertFalse(message_can_post_message($recipient, $sender));
}
/**
* Test that message_can_post_message returns false if the receiver has blocked the
* sender from messaging them.
*/
public function test_message_can_post_message_returns_true() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$this->assertTrue(message_can_post_message($recipient, $sender));
}
/**
* Test that message_is_user_non_contact_blocked returns false if the recipient allows
* messages from non-contacts.
*/
public function test_message_is_user_non_contact_blocked_false_without_preference() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
set_user_preference('message_blocknoncontacts', false, $recipient);
$this->assertFalse(message_is_user_non_contact_blocked($recipient, $sender));
}
/**
* Test that message_is_user_non_contact_blocked returns true if the recipient doesn't
* allow messages from non-contacts and the sender isn't a contact.
*/
public function test_message_is_user_non_contact_blocked_true_with_preference() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
set_user_preference('message_blocknoncontacts', true, $recipient);
$this->assertTrue(message_is_user_non_contact_blocked($recipient, $sender));
}
/**
* 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));
}
/**
* Test that message_is_user_blocked returns false if the sender is not a contact of
* the recipient.
*/
public function test_message_is_user_blocked_false_no_contact() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$this->assertFalse(message_is_user_blocked($recipient, $sender));
}
/**
* Test that message_is_user_blocked returns false if the sender is a contact that is
* blocked by the recipient but has the moodle/site:readallmessages capability.
*/
public function test_message_is_user_blocked_false_if_readallmessages() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$this->setUser($recipient);
message_block_contact($sender->id);
$context = context_system::instance();
$roleid = $this->getDataGenerator()->create_role();
$this->getDataGenerator()->role_assign($roleid, $sender->id, $context->id);
assign_capability('moodle/site:readallmessages', CAP_ALLOW, $roleid, $context);
$this->assertFalse(message_is_user_blocked($recipient, $sender));
}
/**
* Test that message_is_user_blocked returns true if the sender is a contact that is
* blocked by the recipient and does not have the moodle/site:readallmessages capability.
*/
public function test_message_is_user_blocked_true_if_blocked() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$this->setUser($recipient);
message_block_contact($sender->id);
$context = context_system::instance();
$roleid = $this->getDataGenerator()->create_role();
$this->getDataGenerator()->role_assign($roleid, $sender->id, $context->id);
assign_capability('moodle/site:readallmessages', CAP_PROHIBIT, $roleid, $context);
$this->assertTrue(message_is_user_blocked($recipient, $sender));
}
}
This files describes API changes in /message/ messaging system,
information provided here is intended especially for developers.
=== 3.2 ===
* Removed all message_print_* functions as well as the files search.html, search_advanced.html and
send_form.php due to messaging UI rehaul.
* Deprecated the following functions as they are no longer used.
- message_get_course_contexts
- message_remove_url_params
- message_count_messages
- message_count_blocked_users
- message_contact_link
- message_get_recent_notifications
- message_history_link
- message_search
- message_shorten_message
- message_get_fragment
- message_get_history
- message_get_contact_add_remove_link
- message_get_contact_block_link
- message_mark_messages_read
- message_page_type_list
- message_can_post_message
- message_is_user_non_contact_blocked
- message_is_user_blocked