diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index 89fc141c9c1..72dd359a92c 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -4910,3 +4910,711 @@ class css_optimiser { return $css; } } + +/** + * Load the course contexts for all of the users courses + * + * @deprecated since Moodle 3.2 + * @param array $courses array of course objects. The courses the user is enrolled in. + * @return array of course contexts + */ +function message_get_course_contexts($courses) { + debugging('message_get_course_contexts() is deprecated and is no longer used.', DEBUG_DEVELOPER); + + $coursecontexts = array(); + + foreach($courses as $course) { + $coursecontexts[$course->id] = context_course::instance($course->id); + } + + return $coursecontexts; +} + +/** + * strip off action parameters like 'removecontact' + * + * @deprecated since Moodle 3.2 + * @param moodle_url/string $moodleurl a URL. Typically the current page URL. + * @return string the URL minus parameters that perform actions (like adding/removing/blocking a contact). + */ +function message_remove_url_params($moodleurl) { + debugging('message_remove_url_params() is deprecated and is no longer used.', DEBUG_DEVELOPER); + + $newurl = new moodle_url($moodleurl); + $newurl->remove_params('addcontact','removecontact','blockcontact','unblockcontact'); + return $newurl->out(); +} + +/** + * Count the number of messages with a field having a specified value. + * if $field is empty then return count of the whole array + * if $field is non-existent then return 0 + * + * @deprecated since Moodle 3.2 + * @param array $messagearray array of message objects + * @param string $field the field to inspect on the message objects + * @param string $value the value to test the field against + */ +function message_count_messages($messagearray, $field='', $value='') { + debugging('message_count_messages() is deprecated and is no longer used.', DEBUG_DEVELOPER); + + if (!is_array($messagearray)) return 0; + if ($field == '' or empty($messagearray)) return count($messagearray); + + $count = 0; + foreach ($messagearray as $message) { + $count += ($message->$field == $value) ? 1 : 0; + } + return $count; +} + +/** + * Count the number of users blocked by $user1 + * + * @deprecated since Moodle 3.2 + * @param object $user1 user object + * @return int the number of blocked users + */ +function message_count_blocked_users($user1=null) { + debugging('message_count_blocked_users() is deprecated, please use \core_message\api::count_blocked_users() instead.', + DEBUG_DEVELOPER); + + return \core_message\api::count_blocked_users($user1); +} + +/** + * Print a message contact link + * + * @deprecated since Moodle 3.2 + * @param int $userid the ID of the user to apply to action to + * @param string $linktype can be add, remove, block or unblock + * @param bool $return if true return the link as a string. If false echo the link. + * @param string $script the URL to send the user to when the link is clicked. If null, the current page. + * @param bool $text include text next to the icons? + * @param bool $icon include a graphical icon? + * @return string if $return is true otherwise bool + */ +function message_contact_link($userid, $linktype='add', $return=false, $script=null, $text=false, $icon=true) { + debugging('message_contact_link() is deprecated and is no longer used.', DEBUG_DEVELOPER); + + global $OUTPUT, $PAGE; + + //hold onto the strings as we're probably creating a bunch of links + static $str; + + if (empty($script)) { + //strip off previous action params like 'removecontact' + $script = message_remove_url_params($PAGE->url); + } + + if (empty($str->blockcontact)) { + $str = new stdClass(); + $str->blockcontact = get_string('blockcontact', 'message'); + $str->unblockcontact = get_string('unblockcontact', 'message'); + $str->removecontact = get_string('removecontact', 'message'); + $str->addcontact = get_string('addcontact', 'message'); + } + + $command = $linktype.'contact'; + $string = $str->{$command}; + + $safealttext = s($string); + + $safestring = ''; + if (!empty($text)) { + $safestring = $safealttext; + } + + $img = ''; + if ($icon) { + $iconpath = null; + switch ($linktype) { + case 'block': + $iconpath = 't/block'; + break; + case 'unblock': + $iconpath = 't/unblock'; + break; + case 'remove': + $iconpath = 't/removecontact'; + break; + case 'add': + default: + $iconpath = 't/addcontact'; + } + + $img = ''.$safealttext.''; + } + + $output = ''. + ''. + $img. + $safestring.''; + + if ($return) { + return $output; + } else { + echo $output; + return true; + } +} + +/** + * Get the users recent event notifications + * + * @deprecated since Moodle 3.2 + * @param object $user the current user + * @param int $limitfrom can be used for paging + * @param int $limitto can be used for paging + * @return array + */ +function message_get_recent_notifications($user, $limitfrom=0, $limitto=100) { + debugging('message_get_recent_notifications() is deprecated and is no longer used.', DEBUG_DEVELOPER); + + global $DB; + + $userfields = user_picture::fields('u', array('lastaccess')); + $sql = "SELECT mr.id AS message_read_id, $userfields, mr.notification, mr.smallmessage, mr.fullmessage, mr.fullmessagehtml, mr.fullmessageformat, mr.timecreated as timecreated, mr.contexturl, mr.contexturlname + FROM {message_read} mr + JOIN {user} u ON u.id=mr.useridfrom + WHERE mr.useridto = :userid1 AND u.deleted = '0' AND mr.notification = :notification + ORDER BY mr.timecreated DESC"; + $params = array('userid1' => $user->id, 'notification' => 1); + + $notifications = $DB->get_records_sql($sql, $params, $limitfrom, $limitto); + return $notifications; +} + +/** + * echo or return a link to take the user to the full message history between themselves and another user + * + * @deprecated since Moodle 3.2 + * @param int $userid1 the ID of the user displayed on the left (usually the current user) + * @param int $userid2 the ID of the other user + * @param bool $return true to return the link as a string. False to echo the link. + * @param string $keywords any keywords to highlight in the message history + * @param string $position anchor name to jump to within the message history + * @param string $linktext optionally specify the link text + * @return string|bool. Returns a string if $return is true. Otherwise returns a boolean. + */ +function message_history_link($userid1, $userid2, $return=false, $keywords='', $position='', $linktext='') { + debugging('message_history_link() is deprecated and is no longer used.', DEBUG_DEVELOPER); + + global $OUTPUT, $PAGE; + static $strmessagehistory; + + if (empty($strmessagehistory)) { + $strmessagehistory = get_string('messagehistory', 'message'); + } + + if ($position) { + $position = "#$position"; + } + if ($keywords) { + $keywords = "&search=".urlencode($keywords); + } + + if ($linktext == 'icon') { // Icon only + $fulllink = ''.$strmessagehistory.''; + } else if ($linktext == 'both') { // Icon and standard name + $fulllink = ''; + $fulllink .= ' '.$strmessagehistory; + } else if ($linktext) { // Custom name + $fulllink = $linktext; + } else { // Standard name only + $fulllink = $strmessagehistory; + } + + $popupoptions = array( + 'height' => 500, + 'width' => 500, + 'menubar' => false, + 'location' => false, + 'status' => true, + 'scrollbars' => true, + 'resizable' => true); + + $link = new moodle_url('/message/index.php?history='.MESSAGE_HISTORY_ALL."&user1=$userid1&user2=$userid2$keywords$position"); + if ($PAGE->url && $PAGE->url->get_param('viewing')) { + $link->param('viewing', $PAGE->url->get_param('viewing')); + } + $action = null; + $str = $OUTPUT->action_link($link, $fulllink, $action, array('title' => $strmessagehistory)); + + $str = ''.$str.''; + + if ($return) { + return $str; + } else { + echo $str; + return true; + } +} + +/** + * Search a user's messages + * + * Returns a list of posts found using an array of search terms + * eg word +word -word + * + * @deprecated since Moodle 3.2 + * @param array $searchterms an array of search terms (strings) + * @param bool $fromme include messages from the user? + * @param bool $tome include messages to the user? + * @param mixed $courseid SITEID for admins searching all messages. Other behaviour not yet implemented + * @param int $userid the user ID of the current user + * @return mixed An array of messages or false if no matching messages were found + */ +function message_search($searchterms, $fromme=true, $tome=true, $courseid='none', $userid=0) { + debugging('message_search() is deprecated and is no longer used.', DEBUG_DEVELOPER); + + global $CFG, $USER, $DB; + + // If user is searching all messages check they are allowed to before doing anything else. + if ($courseid == SITEID && !has_capability('moodle/site:readallmessages', context_system::instance())) { + print_error('accessdenied','admin'); + } + + // If no userid sent then assume current user. + if ($userid == 0) $userid = $USER->id; + + // Some differences in SQL syntax. + if ($DB->sql_regex_supported()) { + $REGEXP = $DB->sql_regex(true); + $NOTREGEXP = $DB->sql_regex(false); + } + + $searchcond = array(); + $params = array(); + $i = 0; + + // Preprocess search terms to check whether we have at least 1 eligible search term. + // If we do we can drop words around it like 'a'. + $dropshortwords = false; + foreach ($searchterms as $searchterm) { + if (strlen($searchterm) >= 2) { + $dropshortwords = true; + } + } + + foreach ($searchterms as $searchterm) { + $i++; + + $NOT = false; // Initially we aren't going to perform NOT LIKE searches, only MSSQL and Oracle. + + if ($dropshortwords && strlen($searchterm) < 2) { + continue; + } + // Under Oracle and MSSQL, trim the + and - operators and perform simpler LIKE search. + if (!$DB->sql_regex_supported()) { + if (substr($searchterm, 0, 1) == '-') { + $NOT = true; + } + $searchterm = trim($searchterm, '+-'); + } + + if (substr($searchterm,0,1) == "+") { + $searchterm = substr($searchterm,1); + $searchterm = preg_quote($searchterm, '|'); + $searchcond[] = "m.fullmessage $REGEXP :ss$i"; + $params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)"; + + } else if (substr($searchterm,0,1) == "-") { + $searchterm = substr($searchterm,1); + $searchterm = preg_quote($searchterm, '|'); + $searchcond[] = "m.fullmessage $NOTREGEXP :ss$i"; + $params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)"; + + } else { + $searchcond[] = $DB->sql_like("m.fullmessage", ":ss$i", false, true, $NOT); + $params['ss'.$i] = "%$searchterm%"; + } + } + + if (empty($searchcond)) { + $searchcond = " ".$DB->sql_like('m.fullmessage', ':ss1', false); + $params['ss1'] = "%"; + } else { + $searchcond = implode(" AND ", $searchcond); + } + + // There are several possibilities + // 1. courseid = SITEID : The admin is searching messages by all users + // 2. courseid = ?? : A teacher is searching messages by users in + // one of their courses - currently disabled + // 3. courseid = none : User is searching their own messages; + // a. Messages from user + // b. Messages to user + // c. Messages to and from user + + if ($fromme && $tome) { + $searchcond .= " AND ((useridto = :useridto AND timeusertodeleted = 0) OR + (useridfrom = :useridfrom AND timeuserfromdeleted = 0))"; + $params['useridto'] = $userid; + $params['useridfrom'] = $userid; + } else if ($fromme) { + $searchcond .= " AND (useridfrom = :useridfrom AND timeuserfromdeleted = 0)"; + $params['useridfrom'] = $userid; + } else if ($tome) { + $searchcond .= " AND (useridto = :useridto AND timeusertodeleted = 0)"; + $params['useridto'] = $userid; + } + if ($courseid == SITEID) { // Admin is searching all messages. + $m_read = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.smallmessage, m.fullmessage, m.timecreated + FROM {message_read} m + WHERE $searchcond", $params, 0, MESSAGE_SEARCH_MAX_RESULTS); + $m_unread = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.smallmessage, m.fullmessage, m.timecreated + FROM {message} m + WHERE $searchcond", $params, 0, MESSAGE_SEARCH_MAX_RESULTS); + + } else if ($courseid !== 'none') { + // This has not been implemented due to security concerns. + $m_read = array(); + $m_unread = array(); + + } else { + + if ($fromme and $tome) { + $searchcond .= " AND (m.useridfrom=:userid1 OR m.useridto=:userid2)"; + $params['userid1'] = $userid; + $params['userid2'] = $userid; + + } else if ($fromme) { + $searchcond .= " AND m.useridfrom=:userid"; + $params['userid'] = $userid; + + } else if ($tome) { + $searchcond .= " AND m.useridto=:userid"; + $params['userid'] = $userid; + } + + $m_read = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.smallmessage, m.fullmessage, m.timecreated + FROM {message_read} m + WHERE $searchcond", $params, 0, MESSAGE_SEARCH_MAX_RESULTS); + $m_unread = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.smallmessage, m.fullmessage, m.timecreated + FROM {message} m + WHERE $searchcond", $params, 0, MESSAGE_SEARCH_MAX_RESULTS); + + } + + /// The keys may be duplicated in $m_read and $m_unread so we can't + /// do a simple concatenation + $messages = array(); + foreach ($m_read as $m) { + $messages[] = $m; + } + foreach ($m_unread as $m) { + $messages[] = $m; + } + + return (empty($messages)) ? false : $messages; +} + +/** + * Given a message object that we already know has a long message + * this function truncates the message nicely to the first + * sane place between $CFG->forum_longpost and $CFG->forum_shortpost + * + * @deprecated since Moodle 3.2 + * @param string $message the message + * @param int $minlength the minimum length to trim the message to + * @return string the shortened message + */ +function message_shorten_message($message, $minlength = 0) { + debugging('message_shorten_message() is deprecated and is no longer used.', DEBUG_DEVELOPER); + + $i = 0; + $tag = false; + $length = strlen($message); + $count = 0; + $stopzone = false; + $truncate = 0; + if ($minlength == 0) $minlength = MESSAGE_SHORTLENGTH; + + + for ($i=0; $i<$length; $i++) { + $char = $message[$i]; + + switch ($char) { + case "<": + $tag = true; + break; + case ">": + $tag = false; + break; + default: + if (!$tag) { + if ($stopzone) { + if ($char == '.' or $char == ' ') { + $truncate = $i+1; + break 2; + } + } + $count++; + } + break; + } + if (!$stopzone) { + if ($count > $minlength) { + $stopzone = true; + } + } + } + + if (!$truncate) { + $truncate = $i; + } + + return substr($message, 0, $truncate); +} + +/** + * Given a string and an array of keywords, this function looks + * for the first keyword in the string, and then chops out a + * small section from the text that shows that word in context. + * + * @deprecated since Moodle 3.2 + * @param string $message the text to search + * @param array $keywords array of keywords to find + */ +function message_get_fragment($message, $keywords) { + debugging('message_get_fragment() is deprecated and is no longer used.', DEBUG_DEVELOPER); + + $fullsize = 160; + $halfsize = (int)($fullsize/2); + + $message = strip_tags($message); + + foreach ($keywords as $keyword) { // Just get the first one + if ($keyword !== '') { + break; + } + } + if (empty($keyword)) { // None found, so just return start of message + return message_shorten_message($message, 30); + } + + $leadin = $leadout = ''; + +/// Find the start of the fragment + $start = 0; + $length = strlen($message); + + $pos = strpos($message, $keyword); + if ($pos > $halfsize) { + $start = $pos - $halfsize; + $leadin = '...'; + } +/// Find the end of the fragment + $end = $start + $fullsize; + if ($end > $length) { + $end = $length; + } else { + $leadout = '...'; + } + +/// Pull out the fragment and format it + + $fragment = substr($message, $start, $end - $start); + $fragment = $leadin.highlight(implode(' ',$keywords), $fragment).$leadout; + return $fragment; +} + +/** + * Retrieve the messages between two users + * + * @deprecated since Moodle 3.2 + * @param object $user1 the current user + * @param object $user2 the other user + * @param int $limitnum the maximum number of messages to retrieve + * @param bool $viewingnewmessages are we currently viewing new messages? + */ +function message_get_history($user1, $user2, $limitnum=0, $viewingnewmessages=false) { + debugging('message_get_history() is deprecated and is no longer used.', DEBUG_DEVELOPER); + + global $DB, $CFG; + + $messages = array(); + + //we want messages sorted oldest to newest but if getting a subset of messages we need to sort + //desc to get the last $limitnum messages then flip the order in php + $sort = 'asc'; + if ($limitnum>0) { + $sort = 'desc'; + } + + $notificationswhere = null; + //we have just moved new messages to read. If theyre here to see new messages dont hide notifications + if (!$viewingnewmessages && $CFG->messaginghidereadnotifications) { + $notificationswhere = 'AND notification=0'; + } + + //prevent notifications of your own actions appearing in your own message history + $ownnotificationwhere = ' AND NOT (useridfrom=? AND notification=1)'; + + $sql = "((useridto = ? AND useridfrom = ? AND timeusertodeleted = 0) OR + (useridto = ? AND useridfrom = ? AND timeuserfromdeleted = 0))"; + if ($messages_read = $DB->get_records_select('message_read', $sql . $notificationswhere . $ownnotificationwhere, + array($user1->id, $user2->id, $user2->id, $user1->id, $user1->id), + "timecreated $sort", '*', 0, $limitnum)) { + foreach ($messages_read as $message) { + $messages[] = $message; + } + } + if ($messages_new = $DB->get_records_select('message', $sql . $ownnotificationwhere, + array($user1->id, $user2->id, $user2->id, $user1->id, $user1->id), + "timecreated $sort", '*', 0, $limitnum)) { + foreach ($messages_new as $message) { + $messages[] = $message; + } + } + + $result = core_collator::asort_objects_by_property($messages, 'timecreated', core_collator::SORT_NUMERIC); + + //if we only want the last $limitnum messages + $messagecount = count($messages); + if ($limitnum > 0 && $messagecount > $limitnum) { + $messages = array_slice($messages, $messagecount - $limitnum, $limitnum, true); + } + + return $messages; +} + +/** + * Constructs the add/remove contact link to display next to other users + * + * @deprecated since Moodle 3.2 + * @param bool $incontactlist is the user a contact + * @param bool $isblocked is the user blocked + * @param stdClass $contact contact object + * @param string $script the URL to send the user to when the link is clicked. If null, the current page. + * @param bool $text include text next to the icons? + * @param bool $icon include a graphical icon? + * @return string + */ +function message_get_contact_add_remove_link($incontactlist, $isblocked, $contact, $script=null, $text=false, $icon=true) { + debugging('message_get_contact_add_remove_link() is deprecated and is no longer used.', DEBUG_DEVELOPER); + + $strcontact = ''; + + if($incontactlist){ + $strcontact = message_contact_link($contact->id, 'remove', true, $script, $text, $icon); + } else if ($isblocked) { + $strcontact = message_contact_link($contact->id, 'add', true, $script, $text, $icon); + } else{ + $strcontact = message_contact_link($contact->id, 'add', true, $script, $text, $icon); + } + + return $strcontact; +} + +/** + * Constructs the block contact link to display next to other users + * + * @deprecated since Moodle 3.2 + * @param bool $incontactlist is the user a contact? + * @param bool $isblocked is the user blocked? + * @param stdClass $contact contact object + * @param string $script the URL to send the user to when the link is clicked. If null, the current page. + * @param bool $text include text next to the icons? + * @param bool $icon include a graphical icon? + * @return string + */ +function message_get_contact_block_link($incontactlist, $isblocked, $contact, $script=null, $text=false, $icon=true) { + debugging('message_get_contact_block_link() is deprecated and is no longer used.', DEBUG_DEVELOPER); + + $strblock = ''; + + //commented out to allow the user to block a contact without having to remove them first + /*if ($incontactlist) { + //$strblock = ''; + } else*/ + if ($isblocked) { + $strblock = message_contact_link($contact->id, 'unblock', true, $script, $text, $icon); + } else{ + $strblock = message_contact_link($contact->id, 'block', true, $script, $text, $icon); + } + + return $strblock; +} + +/** + * marks ALL messages being sent from $fromuserid to $touserid as read + * + * @deprecated since Moodle 3.2 + * @param int $touserid the id of the message recipient + * @param int $fromuserid the id of the message sender + * @return void + */ +function message_mark_messages_read($touserid, $fromuserid) { + debugging('message_mark_messages_read() is deprecated and is no longer used, please use + \core_message\api::mark_all_read_for_user() instead.', DEBUG_DEVELOPER); + + \core_message\api::mark_all_read_for_user($touserid, $fromuserid); +} + +/** + * Return a list of page types + * + * @deprecated since Moodle 3.2 + * @param string $pagetype current page type + * @param stdClass $parentcontext Block's parent context + * @param stdClass $currentcontext Current context of block + */ +function message_page_type_list($pagetype, $parentcontext, $currentcontext) { + debugging('message_page_type_list() is deprecated and is no longer used.', DEBUG_DEVELOPER); + + return array('messages-*'=>get_string('page-message-x', 'message')); +} + +/** + * 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. + * + * @deprecated since Moodle 3.2 + * @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) { + debugging('message_can_post_message() is deprecated and is no longer used, please use + \core_message\api::can_post_message() instead.', DEBUG_DEVELOPER); + + return \core_message\api::can_post_message($recipient, $sender); +} + +/** + * 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. + * + * @deprecated since Moodle 3.2 + * @param object $recipient User object. + * @param object $sender User object. + * @return bool true if $sender is blocked, false otherwise. + */ +function message_is_user_non_contact_blocked($recipient, $sender = null) { + debugging('message_is_user_non_contact_blocked() is deprecated and is no longer used, please use + \core_message\api::is_user_non_contact_blocked() instead.', DEBUG_DEVELOPER); + + return \core_message\api::is_user_non_contact_blocked($recipient, $sender); +} + +/** + * 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. + * + * @deprecated since Moodle 3.2 + * @param object $recipient User object. + * @param object $sender User object. + * @return bool true if $sender is blocked, false otherwise. + */ +function message_is_user_blocked($recipient, $sender = null) { + debugging('message_is_user_blocked() is deprecated and is no longer used, please use + \core_message\api::is_user_blocked() instead.', DEBUG_DEVELOPER); + + return \core_message\api::is_user_blocked($recipient, $sender); +} diff --git a/message/bell.mp3 b/message/bell.mp3 deleted file mode 100644 index 1aacdb82eaf..00000000000 Binary files a/message/bell.mp3 and /dev/null differ diff --git a/message/bell.ogg b/message/bell.ogg deleted file mode 100644 index e3f1a5e2d1e..00000000000 Binary files a/message/bell.ogg and /dev/null differ diff --git a/message/bell.wav b/message/bell.wav deleted file mode 100644 index cb19faca25a..00000000000 Binary files a/message/bell.wav and /dev/null differ diff --git a/message/classes/api.php b/message/classes/api.php index 1348e5dcfbe..75cb783bd62 100644 --- a/message/classes/api.php +++ b/message/classes/api.php @@ -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; + } } diff --git a/message/discussion.php b/message/discussion.php deleted file mode 100644 index ce6e892638f..00000000000 --- a/message/discussion.php +++ /dev/null @@ -1,38 +0,0 @@ -. - -/** - * 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); -?> diff --git a/message/lib.php b/message/lib.php index 3ed279c34f3..e8af2fcdedc 100644 --- a/message/lib.php +++ b/message/lib.php @@ -52,7 +52,6 @@ define('MESSAGE_READ', 'read'); define('MESSAGE_TYPE_NOTIFICATION', 'notification'); define('MESSAGE_TYPE_MESSAGE', 'message'); - /** * Define contants for messaging default settings population. For unambiguity of * plugin developer intentions we use 4-bit value (LSB numbering): @@ -218,54 +217,6 @@ function message_get_contacts($user1=null, $user2=null) { return array($onlinecontacts, $offlinecontacts, $strangers); } -/** - * Load the course contexts for all of the users courses - * - * @param array $courses array of course objects. The courses the user is enrolled in. - * @return array of course contexts - */ -function message_get_course_contexts($courses) { - $coursecontexts = array(); - - foreach($courses as $course) { - $coursecontexts[$course->id] = context_course::instance($course->id); - } - - return $coursecontexts; -} - -/** - * strip off action parameters like 'removecontact' - * - * @param moodle_url/string $moodleurl a URL. Typically the current page URL. - * @return string the URL minus parameters that perform actions (like adding/removing/blocking a contact). - */ -function message_remove_url_params($moodleurl) { - $newurl = new moodle_url($moodleurl); - $newurl->remove_params('addcontact','removecontact','blockcontact','unblockcontact'); - return $newurl->out(); -} - -/** - * Count the number of messages with a field having a specified value. - * if $field is empty then return count of the whole array - * if $field is non-existent then return 0 - * - * @param array $messagearray array of message objects - * @param string $field the field to inspect on the message objects - * @param string $value the value to test the field against - */ -function message_count_messages($messagearray, $field='', $value='') { - if (!is_array($messagearray)) return 0; - if ($field == '' or empty($messagearray)) return count($messagearray); - - $count = 0; - foreach ($messagearray as $message) { - $count += ($message->$field == $value) ? 1 : 0; - } - return $count; -} - /** * Returns the count of unread messages for user. Either from a specific user or from all users. * @@ -289,27 +240,6 @@ function message_count_unread_messages($user1=null, $user2=null) { } } -/** - * Count the number of users blocked by $user1 - * - * @param object $user1 user object - * @return int the number of blocked users - */ -function message_count_blocked_users($user1=null) { - global $USER, $DB; - - if (empty($user1)) { - $user1 = $USER; - } - - $sql = "SELECT count(mc.id) - FROM {message_contacts} mc - WHERE mc.userid = :userid AND mc.blocked = 1"; - $params = array('userid' => $user1->id); - - return $DB->count_records_sql($sql, $params); -} - /** * Get the users recent conversations meaning all the people they've recently * sent or received a message from plus the most recent message sent to or received from each other user @@ -447,29 +377,6 @@ function message_get_recent_conversations($user, $limitfrom=0, $limitto=100) { return $conversations; } -/** - * Get the users recent event notifications - * - * @param object $user the current user - * @param int $limitfrom can be used for paging - * @param int $limitto can be used for paging - * @return array - */ -function message_get_recent_notifications($user, $limitfrom=0, $limitto=100) { - global $DB; - - $userfields = user_picture::fields('u', array('lastaccess')); - $sql = "SELECT mr.id AS message_read_id, $userfields, mr.notification, mr.smallmessage, mr.fullmessage, mr.fullmessagehtml, mr.fullmessageformat, mr.timecreated as timecreated, mr.contexturl, mr.contexturlname - FROM {message_read} mr - JOIN {user} u ON u.id=mr.useridfrom - WHERE mr.useridto = :userid1 AND u.deleted = '0' AND mr.notification = :notification - ORDER BY mr.timecreated DESC"; - $params = array('userid1' => $user->id, 'notification' => 1); - - $notifications = $DB->get_records_sql($sql, $params, $limitfrom, $limitto); - return $notifications; -} - /** * Try to guess how to convert the message to html. * @@ -752,145 +659,6 @@ function message_get_contact($contactid) { return $DB->get_record('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid)); } -/** - * Print a message contact link - * - * @param int $userid the ID of the user to apply to action to - * @param string $linktype can be add, remove, block or unblock - * @param bool $return if true return the link as a string. If false echo the link. - * @param string $script the URL to send the user to when the link is clicked. If null, the current page. - * @param bool $text include text next to the icons? - * @param bool $icon include a graphical icon? - * @return string if $return is true otherwise bool - */ -function message_contact_link($userid, $linktype='add', $return=false, $script=null, $text=false, $icon=true) { - global $OUTPUT, $PAGE; - - //hold onto the strings as we're probably creating a bunch of links - static $str; - - if (empty($script)) { - //strip off previous action params like 'removecontact' - $script = message_remove_url_params($PAGE->url); - } - - if (empty($str->blockcontact)) { - $str = new stdClass(); - $str->blockcontact = get_string('blockcontact', 'message'); - $str->unblockcontact = get_string('unblockcontact', 'message'); - $str->removecontact = get_string('removecontact', 'message'); - $str->addcontact = get_string('addcontact', 'message'); - } - - $command = $linktype.'contact'; - $string = $str->{$command}; - - $safealttext = s($string); - - $safestring = ''; - if (!empty($text)) { - $safestring = $safealttext; - } - - $img = ''; - if ($icon) { - $iconpath = null; - switch ($linktype) { - case 'block': - $iconpath = 't/block'; - break; - case 'unblock': - $iconpath = 't/unblock'; - break; - case 'remove': - $iconpath = 't/removecontact'; - break; - case 'add': - default: - $iconpath = 't/addcontact'; - } - - $img = ''.$safealttext.''; - } - - $output = ''. - ''. - $img. - $safestring.''; - - if ($return) { - return $output; - } else { - echo $output; - return true; - } -} - -/** - * echo or return a link to take the user to the full message history between themselves and another user - * - * @param int $userid1 the ID of the user displayed on the left (usually the current user) - * @param int $userid2 the ID of the other user - * @param bool $return true to return the link as a string. False to echo the link. - * @param string $keywords any keywords to highlight in the message history - * @param string $position anchor name to jump to within the message history - * @param string $linktext optionally specify the link text - * @return string|bool. Returns a string if $return is true. Otherwise returns a boolean. - */ -function message_history_link($userid1, $userid2, $return=false, $keywords='', $position='', $linktext='') { - global $OUTPUT, $PAGE; - static $strmessagehistory; - - if (empty($strmessagehistory)) { - $strmessagehistory = get_string('messagehistory', 'message'); - } - - if ($position) { - $position = "#$position"; - } - if ($keywords) { - $keywords = "&search=".urlencode($keywords); - } - - if ($linktext == 'icon') { // Icon only - $fulllink = ''.$strmessagehistory.''; - } else if ($linktext == 'both') { // Icon and standard name - $fulllink = ''; - $fulllink .= ' '.$strmessagehistory; - } else if ($linktext) { // Custom name - $fulllink = $linktext; - } else { // Standard name only - $fulllink = $strmessagehistory; - } - - $popupoptions = array( - 'height' => 500, - 'width' => 500, - 'menubar' => false, - 'location' => false, - 'status' => true, - 'scrollbars' => true, - 'resizable' => true); - - $link = new moodle_url('/message/index.php?history='.MESSAGE_HISTORY_ALL."&user1=$userid1&user2=$userid2$keywords$position"); - if ($PAGE->url && $PAGE->url->get_param('viewing')) { - $link->param('viewing', $PAGE->url->get_param('viewing')); - } - $action = null; - $str = $OUTPUT->action_link($link, $fulllink, $action, array('title' => $strmessagehistory)); - - $str = ''.$str.''; - - if ($return) { - return $str; - } else { - echo $str; - return true; - } -} - - /** * Search through course users. * @@ -984,325 +752,6 @@ function message_search_users($courseids, $searchtext, $sort='', $exceptions='') } } -/** - * Search a user's messages - * - * Returns a list of posts found using an array of search terms - * eg word +word -word - * - * @param array $searchterms an array of search terms (strings) - * @param bool $fromme include messages from the user? - * @param bool $tome include messages to the user? - * @param mixed $courseid SITEID for admins searching all messages. Other behaviour not yet implemented - * @param int $userid the user ID of the current user - * @return mixed An array of messages or false if no matching messages were found - */ -function message_search($searchterms, $fromme=true, $tome=true, $courseid='none', $userid=0) { - global $CFG, $USER, $DB; - - // If user is searching all messages check they are allowed to before doing anything else. - if ($courseid == SITEID && !has_capability('moodle/site:readallmessages', context_system::instance())) { - print_error('accessdenied','admin'); - } - - // If no userid sent then assume current user. - if ($userid == 0) $userid = $USER->id; - - // Some differences in SQL syntax. - if ($DB->sql_regex_supported()) { - $REGEXP = $DB->sql_regex(true); - $NOTREGEXP = $DB->sql_regex(false); - } - - $searchcond = array(); - $params = array(); - $i = 0; - - // Preprocess search terms to check whether we have at least 1 eligible search term. - // If we do we can drop words around it like 'a'. - $dropshortwords = false; - foreach ($searchterms as $searchterm) { - if (strlen($searchterm) >= 2) { - $dropshortwords = true; - } - } - - foreach ($searchterms as $searchterm) { - $i++; - - $NOT = false; // Initially we aren't going to perform NOT LIKE searches, only MSSQL and Oracle. - - if ($dropshortwords && strlen($searchterm) < 2) { - continue; - } - // Under Oracle and MSSQL, trim the + and - operators and perform simpler LIKE search. - if (!$DB->sql_regex_supported()) { - if (substr($searchterm, 0, 1) == '-') { - $NOT = true; - } - $searchterm = trim($searchterm, '+-'); - } - - if (substr($searchterm,0,1) == "+") { - $searchterm = substr($searchterm,1); - $searchterm = preg_quote($searchterm, '|'); - $searchcond[] = "m.fullmessage $REGEXP :ss$i"; - $params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)"; - - } else if (substr($searchterm,0,1) == "-") { - $searchterm = substr($searchterm,1); - $searchterm = preg_quote($searchterm, '|'); - $searchcond[] = "m.fullmessage $NOTREGEXP :ss$i"; - $params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)"; - - } else { - $searchcond[] = $DB->sql_like("m.fullmessage", ":ss$i", false, true, $NOT); - $params['ss'.$i] = "%$searchterm%"; - } - } - - if (empty($searchcond)) { - $searchcond = " ".$DB->sql_like('m.fullmessage', ':ss1', false); - $params['ss1'] = "%"; - } else { - $searchcond = implode(" AND ", $searchcond); - } - - // There are several possibilities - // 1. courseid = SITEID : The admin is searching messages by all users - // 2. courseid = ?? : A teacher is searching messages by users in - // one of their courses - currently disabled - // 3. courseid = none : User is searching their own messages; - // a. Messages from user - // b. Messages to user - // c. Messages to and from user - - if ($fromme && $tome) { - $searchcond .= " AND ((useridto = :useridto AND timeusertodeleted = 0) OR - (useridfrom = :useridfrom AND timeuserfromdeleted = 0))"; - $params['useridto'] = $userid; - $params['useridfrom'] = $userid; - } else if ($fromme) { - $searchcond .= " AND (useridfrom = :useridfrom AND timeuserfromdeleted = 0)"; - $params['useridfrom'] = $userid; - } else if ($tome) { - $searchcond .= " AND (useridto = :useridto AND timeusertodeleted = 0)"; - $params['useridto'] = $userid; - } - if ($courseid == SITEID) { // Admin is searching all messages. - $m_read = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.smallmessage, m.fullmessage, m.timecreated - FROM {message_read} m - WHERE $searchcond", $params, 0, MESSAGE_SEARCH_MAX_RESULTS); - $m_unread = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.smallmessage, m.fullmessage, m.timecreated - FROM {message} m - WHERE $searchcond", $params, 0, MESSAGE_SEARCH_MAX_RESULTS); - - } else if ($courseid !== 'none') { - // This has not been implemented due to security concerns. - $m_read = array(); - $m_unread = array(); - - } else { - - if ($fromme and $tome) { - $searchcond .= " AND (m.useridfrom=:userid1 OR m.useridto=:userid2)"; - $params['userid1'] = $userid; - $params['userid2'] = $userid; - - } else if ($fromme) { - $searchcond .= " AND m.useridfrom=:userid"; - $params['userid'] = $userid; - - } else if ($tome) { - $searchcond .= " AND m.useridto=:userid"; - $params['userid'] = $userid; - } - - $m_read = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.smallmessage, m.fullmessage, m.timecreated - FROM {message_read} m - WHERE $searchcond", $params, 0, MESSAGE_SEARCH_MAX_RESULTS); - $m_unread = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.smallmessage, m.fullmessage, m.timecreated - FROM {message} m - WHERE $searchcond", $params, 0, MESSAGE_SEARCH_MAX_RESULTS); - - } - - /// The keys may be duplicated in $m_read and $m_unread so we can't - /// do a simple concatenation - $messages = array(); - foreach ($m_read as $m) { - $messages[] = $m; - } - foreach ($m_unread as $m) { - $messages[] = $m; - } - - return (empty($messages)) ? false : $messages; -} - -/** - * Given a message object that we already know has a long message - * this function truncates the message nicely to the first - * sane place between $CFG->forum_longpost and $CFG->forum_shortpost - * - * @param string $message the message - * @param int $minlength the minimum length to trim the message to - * @return string the shortened message - */ -function message_shorten_message($message, $minlength = 0) { - $i = 0; - $tag = false; - $length = strlen($message); - $count = 0; - $stopzone = false; - $truncate = 0; - if ($minlength == 0) $minlength = MESSAGE_SHORTLENGTH; - - - for ($i=0; $i<$length; $i++) { - $char = $message[$i]; - - switch ($char) { - case "<": - $tag = true; - break; - case ">": - $tag = false; - break; - default: - if (!$tag) { - if ($stopzone) { - if ($char == '.' or $char == ' ') { - $truncate = $i+1; - break 2; - } - } - $count++; - } - break; - } - if (!$stopzone) { - if ($count > $minlength) { - $stopzone = true; - } - } - } - - if (!$truncate) { - $truncate = $i; - } - - return substr($message, 0, $truncate); -} - - -/** - * Given a string and an array of keywords, this function looks - * for the first keyword in the string, and then chops out a - * small section from the text that shows that word in context. - * - * @param string $message the text to search - * @param array $keywords array of keywords to find - */ -function message_get_fragment($message, $keywords) { - - $fullsize = 160; - $halfsize = (int)($fullsize/2); - - $message = strip_tags($message); - - foreach ($keywords as $keyword) { // Just get the first one - if ($keyword !== '') { - break; - } - } - if (empty($keyword)) { // None found, so just return start of message - return message_shorten_message($message, 30); - } - - $leadin = $leadout = ''; - -/// Find the start of the fragment - $start = 0; - $length = strlen($message); - - $pos = strpos($message, $keyword); - if ($pos > $halfsize) { - $start = $pos - $halfsize; - $leadin = '...'; - } -/// Find the end of the fragment - $end = $start + $fullsize; - if ($end > $length) { - $end = $length; - } else { - $leadout = '...'; - } - -/// Pull out the fragment and format it - - $fragment = substr($message, $start, $end - $start); - $fragment = $leadin.highlight(implode(' ',$keywords), $fragment).$leadout; - return $fragment; -} - -/** - * Retrieve the messages between two users - * - * @param object $user1 the current user - * @param object $user2 the other user - * @param int $limitnum the maximum number of messages to retrieve - * @param bool $viewingnewmessages are we currently viewing new messages? - */ -function message_get_history($user1, $user2, $limitnum=0, $viewingnewmessages=false) { - global $DB, $CFG; - - $messages = array(); - - //we want messages sorted oldest to newest but if getting a subset of messages we need to sort - //desc to get the last $limitnum messages then flip the order in php - $sort = 'asc'; - if ($limitnum>0) { - $sort = 'desc'; - } - - $notificationswhere = null; - //we have just moved new messages to read. If theyre here to see new messages dont hide notifications - if (!$viewingnewmessages && $CFG->messaginghidereadnotifications) { - $notificationswhere = 'AND notification=0'; - } - - //prevent notifications of your own actions appearing in your own message history - $ownnotificationwhere = ' AND NOT (useridfrom=? AND notification=1)'; - - $sql = "((useridto = ? AND useridfrom = ? AND timeusertodeleted = 0) OR - (useridto = ? AND useridfrom = ? AND timeuserfromdeleted = 0))"; - if ($messages_read = $DB->get_records_select('message_read', $sql . $notificationswhere . $ownnotificationwhere, - array($user1->id, $user2->id, $user2->id, $user1->id, $user1->id), - "timecreated $sort", '*', 0, $limitnum)) { - foreach ($messages_read as $message) { - $messages[] = $message; - } - } - if ($messages_new = $DB->get_records_select('message', $sql . $ownnotificationwhere, - array($user1->id, $user2->id, $user2->id, $user1->id, $user1->id), - "timecreated $sort", '*', 0, $limitnum)) { - foreach ($messages_new as $message) { - $messages[] = $message; - } - } - - $result = core_collator::asort_objects_by_property($messages, 'timecreated', core_collator::SORT_NUMERIC); - - //if we only want the last $limitnum messages - $messagecount = count($messages); - if ($limitnum > 0 && $messagecount > $limitnum) { - $messages = array_slice($messages, $messagecount - $limitnum, $limitnum, true); - } - - return $messages; -} - /** * Format a message for display in the message history * @@ -1419,58 +868,6 @@ function message_post_message($userfrom, $userto, $message, $format) { return message_send($eventdata); } -/** - * Constructs the add/remove contact link to display next to other users - * - * @param bool $incontactlist is the user a contact - * @param bool $isblocked is the user blocked - * @param stdClass $contact contact object - * @param string $script the URL to send the user to when the link is clicked. If null, the current page. - * @param bool $text include text next to the icons? - * @param bool $icon include a graphical icon? - * @return string - */ -function message_get_contact_add_remove_link($incontactlist, $isblocked, $contact, $script=null, $text=false, $icon=true) { - $strcontact = ''; - - if($incontactlist){ - $strcontact = message_contact_link($contact->id, 'remove', true, $script, $text, $icon); - } else if ($isblocked) { - $strcontact = message_contact_link($contact->id, 'add', true, $script, $text, $icon); - } else{ - $strcontact = message_contact_link($contact->id, 'add', true, $script, $text, $icon); - } - - return $strcontact; -} - -/** - * Constructs the block contact link to display next to other users - * - * @param bool $incontactlist is the user a contact? - * @param bool $isblocked is the user blocked? - * @param stdClass $contact contact object - * @param string $script the URL to send the user to when the link is clicked. If null, the current page. - * @param bool $text include text next to the icons? - * @param bool $icon include a graphical icon? - * @return string - */ -function message_get_contact_block_link($incontactlist, $isblocked, $contact, $script=null, $text=false, $icon=true) { - $strblock = ''; - - //commented out to allow the user to block a contact without having to remove them first - /*if ($incontactlist) { - //$strblock = ''; - } else*/ - if ($isblocked) { - $strblock = message_contact_link($contact->id, 'unblock', true, $script, $text, $icon); - } else{ - $strblock = message_contact_link($contact->id, 'block', true, $script, $text, $icon); - } - - return $strblock; -} - /** * Moves messages from a particular user from the message table (unread messages) to message_read * This is typically only used when a user is deleted @@ -1490,18 +887,6 @@ function message_move_userfrom_unread2read($userid) { return true; } -/** - * marks ALL messages being sent from $fromuserid to $touserid as read - * - * @param int $touserid the id of the message recipient - * @param int $fromuserid the id of the message sender - * @return void - */ -function message_mark_messages_read($touserid, $fromuserid) { - return \core_message\api::mark_all_read_for_user($touserid, $fromuserid); -} - - /** * Mark a single message as read * @@ -1726,16 +1111,6 @@ function translate_message_default_setting($plugindefault, $processorname) { return array($permitted, $loggedin, $loggedoff); } -/** - * Return a list of page types - * @param string $pagetype current page type - * @param stdClass $parentcontext Block's parent context - * @param stdClass $currentcontext Current context of block - */ -function message_page_type_list($pagetype, $parentcontext, $currentcontext) { - return array('messages-*'=>get_string('page-message-x', 'message')); -} - /** * Get messages sent or/and received by the specified users. * Please note that this function return deleted messages too. @@ -1793,111 +1168,6 @@ function message_get_messages($useridto, $useridfrom = 0, $notifications = -1, $ return $messages; } - - - - - - -/** - * 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; -} - -/** - * 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 object $recipient User object. - * @param object $sender User object. - * @return bool true if $sender is blocked, false otherwise. - */ -function message_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. - */ -function message_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; -} - /** * Handles displaying processor settings in a fragment. * diff --git a/message/settings.html b/message/settings.html deleted file mode 100644 index 1f75ddb3c8d..00000000000 --- a/message/settings.html +++ /dev/null @@ -1,26 +0,0 @@ -
-
- - -
- - - - - - - - - - - - - - - - - - -
/>
/>

- -
diff --git a/message/tests/api_test.php b/message/tests/api_test.php index 32ab2b2bef8..5b7b0d8e192 100644 --- a/message/tests/api_test.php +++ b/message/tests/api_test.php @@ -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 diff --git a/message/tests/events_test.php b/message/tests/events_test.php index 5a6843de43d..5fb32340106 100644 --- a/message/tests/events_test.php +++ b/message/tests/events_test.php @@ -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()); } /** diff --git a/message/tests/messagelib_test.php b/message/tests/messagelib_test.php index cf50693078e..a03f5b78858 100644 --- a/message/tests/messagelib_test.php +++ b/message/tests/messagelib_test.php @@ -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)); - } - - } diff --git a/message/upgrade.txt b/message/upgrade.txt index 8d46bab61db..98915ee5727 100644 --- a/message/upgrade.txt +++ b/message/upgrade.txt @@ -1,6 +1,30 @@ 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 + === 3.1 === * get_message_processors accepts an addition parameter for testing, which will just reset processor and exit. * Deprecated method message_current_user_is_involved() has been removed.