. /** * Library functions for messaging * * @package core_message * @copyright 2008 Luis Rodrigues * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require_once($CFG->libdir.'/eventslib.php'); define('MESSAGE_SHORTLENGTH', 300); define('MESSAGE_HISTORY_ALL', 1); define('MESSAGE_SEARCH_MAX_RESULTS', 200); 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): * bit 0 - whether to send message when user is loggedin (MESSAGE_DEFAULT_LOGGEDIN) * bit 1 - whether to send message when user is loggedoff (MESSAGE_DEFAULT_LOGGEDOFF) * bit 2..3 - messaging permission (MESSAGE_DISALLOWED|MESSAGE_PERMITTED|MESSAGE_FORCED) * * MESSAGE_PERMITTED_MASK contains the mask we use to distinguish permission setting */ define('MESSAGE_DEFAULT_LOGGEDIN', 0x01); // 0001 define('MESSAGE_DEFAULT_LOGGEDOFF', 0x02); // 0010 define('MESSAGE_DISALLOWED', 0x04); // 0100 define('MESSAGE_PERMITTED', 0x08); // 1000 define('MESSAGE_FORCED', 0x0c); // 1100 define('MESSAGE_PERMITTED_MASK', 0x0c); // 1100 /** * Set default value for default outputs permitted setting */ define('MESSAGE_DEFAULT_PERMITTED', 'permitted'); /** * Set default values for polling. */ define('MESSAGE_DEFAULT_MIN_POLL_IN_SECONDS', 10); define('MESSAGE_DEFAULT_MAX_POLL_IN_SECONDS', 2 * MINSECS); define('MESSAGE_DEFAULT_TIMEOUT_POLL_IN_SECONDS', 5 * MINSECS); /** * Retrieve users blocked by $user1 * * @param object $user1 the user whose messages are being viewed * @param object $user2 the user $user1 is talking to. If they are being blocked * they will have a variable called 'isblocked' added to their user object * @return array the users blocked by $user1 */ function message_get_blocked_users($user1=null, $user2=null) { global $DB, $USER; if (empty($user1)) { $user1 = $USER; } if (!empty($user2)) { $user2->isblocked = false; } $blockedusers = array(); $userfields = user_picture::fields('u', array('lastaccess')); $blockeduserssql = "SELECT $userfields, COUNT(m.id) AS messagecount FROM {message_contacts} mc JOIN {user} u ON u.id = mc.contactid LEFT OUTER JOIN {message} m ON m.useridfrom = mc.contactid AND m.useridto = :user1id1 WHERE u.deleted = 0 AND mc.userid = :user1id2 AND mc.blocked = 1 GROUP BY $userfields ORDER BY u.firstname ASC"; $rs = $DB->get_recordset_sql($blockeduserssql, array('user1id1' => $user1->id, 'user1id2' => $user1->id)); foreach($rs as $rd) { $blockedusers[] = $rd; if (!empty($user2) && $user2->id == $rd->id) { $user2->isblocked = true; } } $rs->close(); return $blockedusers; } /** * Retrieve $user1's contacts (online, offline and strangers) * * @param object $user1 the user whose messages are being viewed * @param object $user2 the user $user1 is talking to. If they are a contact * they will have a variable called 'iscontact' added to their user object * @return array containing 3 arrays. array($onlinecontacts, $offlinecontacts, $strangers) */ function message_get_contacts($user1=null, $user2=null) { global $DB, $CFG, $USER; if (empty($user1)) { $user1 = $USER; } if (!empty($user2)) { $user2->iscontact = false; } $timetoshowusers = 300; //Seconds default if (isset($CFG->block_online_users_timetosee)) { $timetoshowusers = $CFG->block_online_users_timetosee * 60; } // time which a user is counting as being active since $timefrom = time()-$timetoshowusers; // people in our contactlist who are online $onlinecontacts = array(); // people in our contactlist who are offline $offlinecontacts = array(); // people who are not in our contactlist but have sent us a message $strangers = array(); $userfields = user_picture::fields('u', array('lastaccess')); // get all in our contactlist who are not blocked in our contact list // and count messages we have waiting from each of them $contactsql = "SELECT $userfields, COUNT(m.id) AS messagecount FROM {message_contacts} mc JOIN {user} u ON u.id = mc.contactid LEFT OUTER JOIN {message} m ON m.useridfrom = mc.contactid AND m.useridto = ? WHERE u.deleted = 0 AND mc.userid = ? AND mc.blocked = 0 GROUP BY $userfields ORDER BY u.firstname ASC"; $rs = $DB->get_recordset_sql($contactsql, array($user1->id, $user1->id)); foreach ($rs as $rd) { if ($rd->lastaccess >= $timefrom) { // they have been active recently, so are counted online $onlinecontacts[] = $rd; } else { $offlinecontacts[] = $rd; } if (!empty($user2) && $user2->id == $rd->id) { $user2->iscontact = true; } } $rs->close(); // get messages from anyone who isn't in our contact list and count the number // of messages we have from each of them $strangersql = "SELECT $userfields, count(m.id) as messagecount FROM {message} m JOIN {user} u ON u.id = m.useridfrom LEFT OUTER JOIN {message_contacts} mc ON mc.contactid = m.useridfrom AND mc.userid = m.useridto WHERE u.deleted = 0 AND mc.id IS NULL AND m.useridto = ? GROUP BY $userfields ORDER BY u.firstname ASC"; $rs = $DB->get_recordset_sql($strangersql, array($USER->id)); // Add user id as array index, so supportuser and noreply user don't get duplicated (if they are real users). foreach ($rs as $rd) { $strangers[$rd->id] = $rd; } $rs->close(); // Add noreply user and support user to the list, if they don't exist. $supportuser = core_user::get_support_user(); if (!isset($strangers[$supportuser->id])) { $supportuser->messagecount = message_count_unread_messages($USER, $supportuser); if ($supportuser->messagecount > 0) { $strangers[$supportuser->id] = $supportuser; } } $noreplyuser = core_user::get_noreply_user(); if (!isset($strangers[$noreplyuser->id])) { $noreplyuser->messagecount = message_count_unread_messages($USER, $noreplyuser); if ($noreplyuser->messagecount > 0) { $strangers[$noreplyuser->id] = $noreplyuser; } } return array($onlinecontacts, $offlinecontacts, $strangers); } /** * Returns the count of unread messages for user. Either from a specific user or from all users. * * @param object $user1 the first user. Defaults to $USER * @param object $user2 the second user. If null this function will count all of user 1's unread messages. * @return int the count of $user1's unread messages */ function message_count_unread_messages($user1=null, $user2=null) { global $USER, $DB; if (empty($user1)) { $user1 = $USER; } if (!empty($user2)) { return $DB->count_records_select('message', "useridto = ? AND useridfrom = ?", array($user1->id, $user2->id), "COUNT('id')"); } else { return $DB->count_records_select('message', "useridto = ?", array($user1->id), "COUNT('id')"); } } /** * 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 * * @param object|int $userorid the current user or user id * @param int $limitfrom can be used for paging * @param int $limitto can be used for paging * @return array */ function message_get_recent_conversations($userorid, $limitfrom = 0, $limitto = 100) { global $DB; if (is_object($userorid)) { $user = $userorid; } else { $userid = $userorid; $user = new stdClass(); $user->id = $userid; } $userfields = user_picture::fields('otheruser', array('lastaccess')); // This query retrieves the most recent message received from or sent to // seach other user. // // If two messages have the same timecreated, we take the one with the // larger id. // // There is a separate query for read and unread messages as they are stored // in different tables. They were originally retrieved in one query but it // was so large that it was difficult to be confident in its correctness. $uniquefield = $DB->sql_concat('message.useridfrom', "'-'", 'message.useridto'); $sql = "SELECT $uniquefield, $userfields, message.id as mid, message.notification, message.useridfrom, message.useridto, message.smallmessage, message.fullmessage, message.fullmessagehtml, message.fullmessageformat, message.timecreated, contact.id as contactlistid, contact.blocked FROM {message_read} message JOIN ( SELECT MAX(id) AS messageid, matchedmessage.useridto, matchedmessage.useridfrom FROM {message_read} matchedmessage INNER JOIN ( SELECT MAX(recentmessages.timecreated) timecreated, recentmessages.useridfrom, recentmessages.useridto FROM {message_read} recentmessages WHERE ( (recentmessages.useridfrom = :userid1 AND recentmessages.timeuserfromdeleted = 0) OR (recentmessages.useridto = :userid2 AND recentmessages.timeusertodeleted = 0) ) GROUP BY recentmessages.useridfrom, recentmessages.useridto ) recent ON matchedmessage.useridto = recent.useridto AND matchedmessage.useridfrom = recent.useridfrom AND matchedmessage.timecreated = recent.timecreated WHERE ( (matchedmessage.useridfrom = :userid6 AND matchedmessage.timeuserfromdeleted = 0) OR (matchedmessage.useridto = :userid7 AND matchedmessage.timeusertodeleted = 0) ) GROUP BY matchedmessage.useridto, matchedmessage.useridfrom ) messagesubset ON messagesubset.messageid = message.id JOIN {user} otheruser ON (message.useridfrom = :userid4 AND message.useridto = otheruser.id) OR (message.useridto = :userid5 AND message.useridfrom = otheruser.id) LEFT JOIN {message_contacts} contact ON contact.userid = :userid3 AND contact.contactid = otheruser.id WHERE otheruser.deleted = 0 AND message.notification = 0 ORDER BY message.timecreated DESC"; $params = array( 'userid1' => $user->id, 'userid2' => $user->id, 'userid3' => $user->id, 'userid4' => $user->id, 'userid5' => $user->id, 'userid6' => $user->id, 'userid7' => $user->id ); $read = $DB->get_records_sql($sql, $params, $limitfrom, $limitto); // We want to get the messages that have not been read. These are stored in the 'message' table. It is the // exact same query as the one above, except for the table we are querying. So, simply replace references to // the 'message_read' table with the 'message' table. $sql = str_replace('{message_read}', '{message}', $sql); $unread = $DB->get_records_sql($sql, $params, $limitfrom, $limitto); $unreadcountssql = 'SELECT useridfrom, count(*) as count FROM {message} WHERE useridto = :userid AND timeusertodeleted = 0 AND notification = 0 GROUP BY useridfrom'; $unreadcounts = $DB->get_records_sql($unreadcountssql, array('userid' => $user->id)); // Union the 2 result sets together looking for the message with the most // recent timecreated for each other user. // $conversation->id (the array key) is the other user's ID. $conversations = array(); $conversation_arrays = array($unread, $read); foreach ($conversation_arrays as $conversation_array) { foreach ($conversation_array as $conversation) { // Only consider it unread if $user has unread messages. if (isset($unreadcounts[$conversation->useridfrom])) { $conversation->isread = 0; $conversation->unreadcount = $unreadcounts[$conversation->useridfrom]->count; } else { $conversation->isread = 1; } if (!isset($conversations[$conversation->id])) { $conversations[$conversation->id] = $conversation; } else { $current = $conversations[$conversation->id]; // We need to maintain the isread and unreadcount values from existing // parts of the conversation if we're replacing it. $conversation->isread = ($conversation->isread && $current->isread); if (isset($current->unreadcount) && !isset($conversation->unreadcount)) { $conversation->unreadcount = $current->unreadcount; } if ($current->timecreated < $conversation->timecreated) { $conversations[$conversation->id] = $conversation; } else if ($current->timecreated == $conversation->timecreated) { if ($current->mid < $conversation->mid) { $conversations[$conversation->id] = $conversation; } } } } } // Sort the conversations by $conversation->timecreated, newest to oldest // There may be multiple conversations with the same timecreated // The conversations array contains both read and unread messages (different tables) so sorting by ID won't work $result = core_collator::asort_objects_by_property($conversations, 'timecreated', core_collator::SORT_NUMERIC); $conversations = array_reverse($conversations); return $conversations; } /** * Try to guess how to convert the message to html. * * @access private * * @param stdClass $message * @param bool $forcetexttohtml * @return string html fragment */ function message_format_message_text($message, $forcetexttohtml = false) { // Note: this is a very nasty hack that tries to work around the weird messaging rules and design. $options = new stdClass(); $options->para = false; $options->blanktarget = true; $format = $message->fullmessageformat; if (strval($message->smallmessage) !== '') { if ($message->notification == 1) { if (strval($message->fullmessagehtml) !== '' or strval($message->fullmessage) !== '') { $format = FORMAT_PLAIN; } } $messagetext = $message->smallmessage; } else if ($message->fullmessageformat == FORMAT_HTML) { if (strval($message->fullmessagehtml) !== '') { $messagetext = $message->fullmessagehtml; } else { $messagetext = $message->fullmessage; $format = FORMAT_MOODLE; } } else { if (strval($message->fullmessage) !== '') { $messagetext = $message->fullmessage; } else { $messagetext = $message->fullmessagehtml; $format = FORMAT_HTML; } } if ($forcetexttohtml) { // This is a crazy hack, why not set proper format when creating the notifications? if ($format === FORMAT_PLAIN) { $format = FORMAT_MOODLE; } } return format_text($messagetext, $format, $options); } /** * Add the selected user as a contact for the current user * * @param int $contactid the ID of the user to add as a contact * @param int $blocked 1 if you wish to block the contact * @param int $userid the user ID of the user we want to add the contact for, defaults to current user if not specified. * @return bool/int false if the $contactid isnt a valid user id. True if no changes made. * Otherwise returns the result of update_record() or insert_record() */ function message_add_contact($contactid, $blocked = 0, $userid = 0) { global $USER, $DB; if (!$DB->record_exists('user', array('id' => $contactid))) { // invalid userid return false; } if (empty($userid)) { $userid = $USER->id; } // Check if a record already exists as we may be changing blocking status. if (($contact = $DB->get_record('message_contacts', array('userid' => $userid, 'contactid' => $contactid))) !== false) { // Check if blocking status has been changed. if ($contact->blocked != $blocked) { $contact->blocked = $blocked; $DB->update_record('message_contacts', $contact); if ($blocked == 1) { // Trigger event for blocking a contact. $event = \core\event\message_contact_blocked::create(array( 'objectid' => $contact->id, 'userid' => $contact->userid, 'relateduserid' => $contact->contactid, 'context' => context_user::instance($contact->userid) )); $event->add_record_snapshot('message_contacts', $contact); $event->trigger(); } else { // Trigger event for unblocking a contact. $event = \core\event\message_contact_unblocked::create(array( 'objectid' => $contact->id, 'userid' => $contact->userid, 'relateduserid' => $contact->contactid, 'context' => context_user::instance($contact->userid) )); $event->add_record_snapshot('message_contacts', $contact); $event->trigger(); } return true; } else { // No change to blocking status. return true; } } else { // New contact record. $contact = new stdClass(); $contact->userid = $userid; $contact->contactid = $contactid; $contact->blocked = $blocked; $contact->id = $DB->insert_record('message_contacts', $contact); $eventparams = array( 'objectid' => $contact->id, 'userid' => $contact->userid, 'relateduserid' => $contact->contactid, 'context' => context_user::instance($contact->userid) ); if ($blocked) { $event = \core\event\message_contact_blocked::create($eventparams); } else { $event = \core\event\message_contact_added::create($eventparams); } // Trigger event. $event->trigger(); return true; } } /** * remove a contact * * @param int $contactid the user ID of the contact to remove * @param int $userid the user ID of the user we want to remove the contacts for, defaults to current user if not specified. * @return bool returns the result of delete_records() */ function message_remove_contact($contactid, $userid = 0) { global $USER, $DB; if (empty($userid)) { $userid = $USER->id; } if ($contact = $DB->get_record('message_contacts', array('userid' => $userid, 'contactid' => $contactid))) { $DB->delete_records('message_contacts', array('id' => $contact->id)); // Trigger event for removing a contact. $event = \core\event\message_contact_removed::create(array( 'objectid' => $contact->id, 'userid' => $contact->userid, 'relateduserid' => $contact->contactid, 'context' => context_user::instance($contact->userid) )); $event->add_record_snapshot('message_contacts', $contact); $event->trigger(); return true; } return false; } /** * Unblock a contact. Note that this reverts the previously blocked user back to a non-contact. * * @param int $contactid the user ID of the contact to unblock * @param int $userid the user ID of the user we want to unblock the contact for, defaults to current user * if not specified. * @return bool returns the result of delete_records() */ function message_unblock_contact($contactid, $userid = 0) { return message_add_contact($contactid, 0, $userid); } /** * Block a user. * * @param int $contactid the user ID of the user to block * @param int $userid the user ID of the user we want to unblock the contact for, defaults to current user * if not specified. * @return bool */ function message_block_contact($contactid, $userid = 0) { return message_add_contact($contactid, 1, $userid); } /** * Checks if a user can delete a message. * * @param stdClass $message the message to delete * @param string $userid the user id of who we want to delete the message for (this may be done by the admin * but will still seem as if it was by the user) * @return bool Returns true if a user can delete the message, false otherwise. */ function message_can_delete_message($message, $userid) { global $USER; if ($message->useridfrom == $userid) { $userdeleting = 'useridfrom'; } else if ($message->useridto == $userid) { $userdeleting = 'useridto'; } else { return false; } $systemcontext = context_system::instance(); // Let's check if the user is allowed to delete this message. if (has_capability('moodle/site:deleteanymessage', $systemcontext) || ((has_capability('moodle/site:deleteownmessage', $systemcontext) && $USER->id == $message->$userdeleting))) { return true; } return false; } /** * Deletes a message. * * This function does not verify any permissions. * * @param stdClass $message the message to delete * @param string $userid the user id of who we want to delete the message for (this may be done by the admin * but will still seem as if it was by the user) * @return bool */ function message_delete_message($message, $userid) { global $DB; // The column we want to alter. if ($message->useridfrom == $userid) { $coltimedeleted = 'timeuserfromdeleted'; } else if ($message->useridto == $userid) { $coltimedeleted = 'timeusertodeleted'; } else { return false; } // Don't update it if it's already been deleted. if ($message->$coltimedeleted > 0) { return false; } // Get the table we want to update. if (isset($message->timeread)) { $messagetable = 'message_read'; } else { $messagetable = 'message'; } // Mark the message as deleted. $updatemessage = new stdClass(); $updatemessage->id = $message->id; $updatemessage->$coltimedeleted = time(); $success = $DB->update_record($messagetable, $updatemessage); if ($success) { // Trigger event for deleting a message. \core\event\message_deleted::create_from_ids($message->useridfrom, $message->useridto, $userid, $messagetable, $message->id)->trigger(); } return $success; } /** * Load a user's contact record * * @param int $contactid the user ID of the user whose contact record you want * @return array message contacts */ function message_get_contact($contactid) { global $USER, $DB; return $DB->get_record('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid)); } /** * Search through course users. * * If $courseids contains the site course then this function searches * through all undeleted and confirmed users. * * @param int|array $courseids Course ID or array of course IDs. * @param string $searchtext the text to search for. * @param string $sort the column name to order by. * @param string|array $exceptions comma separated list or array of user IDs to exclude. * @return array An array of {@link $USER} records. */ function message_search_users($courseids, $searchtext, $sort='', $exceptions='') { global $CFG, $USER, $DB; // Basic validation to ensure that the parameter $courseids is not an empty array or an empty value. if (!$courseids) { $courseids = array(SITEID); } // Allow an integer to be passed. if (!is_array($courseids)) { $courseids = array($courseids); } $fullname = $DB->sql_fullname(); $ufields = user_picture::fields('u'); if (!empty($sort)) { $order = ' ORDER BY '. $sort; } else { $order = ''; } $params = array( 'userid' => $USER->id, 'query' => "%$searchtext%" ); if (empty($exceptions)) { $exceptions = array(); } else if (!empty($exceptions) && is_string($exceptions)) { $exceptions = explode(',', $exceptions); } // Ignore self and guest account. $exceptions[] = $USER->id; $exceptions[] = $CFG->siteguest; // Exclude exceptions from the search result. list($except, $params_except) = $DB->get_in_or_equal($exceptions, SQL_PARAMS_NAMED, 'param', false); $except = ' AND u.id ' . $except; $params = array_merge($params_except, $params); if (in_array(SITEID, $courseids)) { // Search on site level. return $DB->get_records_sql("SELECT $ufields, mc.id as contactlistid, mc.blocked FROM {user} u LEFT JOIN {message_contacts} mc ON mc.contactid = u.id AND mc.userid = :userid WHERE u.deleted = '0' AND u.confirmed = '1' AND (".$DB->sql_like($fullname, ':query', false).") $except $order", $params); } else { // Search in courses. // Getting the context IDs or each course. $contextids = array(); foreach ($courseids as $courseid) { $context = context_course::instance($courseid); $contextids = array_merge($contextids, $context->get_parent_context_ids(true)); } list($contextwhere, $contextparams) = $DB->get_in_or_equal(array_unique($contextids), SQL_PARAMS_NAMED, 'context'); $params = array_merge($params, $contextparams); // Everyone who has a role assignment in this course or higher. // TODO: add enabled enrolment join here (skodak) $users = $DB->get_records_sql("SELECT DISTINCT $ufields, mc.id as contactlistid, mc.blocked FROM {user} u JOIN {role_assignments} ra ON ra.userid = u.id LEFT JOIN {message_contacts} mc ON mc.contactid = u.id AND mc.userid = :userid WHERE u.deleted = '0' AND u.confirmed = '1' AND (".$DB->sql_like($fullname, ':query', false).") AND ra.contextid $contextwhere $except $order", $params); return $users; } } /** * Format a message for display in the message history * * @param object $message the message object * @param string $format optional date format * @param string $keywords keywords to highlight * @param string $class CSS class to apply to the div around the message * @return string the formatted message */ function message_format_message($message, $format='', $keywords='', $class='other') { static $dateformat; //if we haven't previously set the date format or they've supplied a new one if ( empty($dateformat) || (!empty($format) && $dateformat != $format) ) { if ($format) { $dateformat = $format; } else { $dateformat = get_string('strftimedatetimeshort'); } } $time = userdate($message->timecreated, $dateformat); $messagetext = message_format_message_text($message, false); if ($keywords) { $messagetext = highlight($keywords, $messagetext); } $messagetext .= message_format_contexturl($message); $messagetext = clean_text($messagetext, FORMAT_HTML); return <<