. /** * Library functions for messaging * * @package core_message * @copyright 2008 Luis Rodrigues * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ 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); /** * 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; } $sql = "SELECT COUNT(m.id) FROM {messages} m INNER JOIN {message_conversations} mc ON mc.id = m.conversationid INNER JOIN {message_conversation_members} mcm ON mcm.conversationid = mc.id LEFT JOIN {message_user_actions} mua ON (mua.messageid = m.id AND mua.userid = ? AND (mua.action = ? OR mua.action = ?)) WHERE mua.id is NULL AND mcm.userid = ?"; $params = [$user1->id, \core_message\api::MESSAGE_ACTION_DELETED, \core_message\api::MESSAGE_ACTION_READ, $user1->id]; if (!empty($user2)) { $sql .= " AND m.useridfrom = ?"; $params[] = $user2->id; } else { $sql .= " AND m.useridfrom <> ?"; $params[] = $user1->id; } return $DB->count_records_sql($sql, $params); } /** * 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; $options->trusted = isset($message->fullmessagetrust) ? $message->fullmessagetrust : false; $format = $message->fullmessageformat; if (strval($message->smallmessage) !== '') { if (!empty($message->notification)) { 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); } /** * 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, 'userid2' => $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, mub.id as userblockedid FROM {user} u LEFT JOIN {message_contacts} mc ON mc.contactid = u.id AND mc.userid = :userid LEFT JOIN {message_users_blocked} mub ON mub.userid = :userid2 AND mub.blockeduserid = u.id 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, mub.id as userblockedid 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 LEFT JOIN {message_users_blocked} mub ON mub.userid = :userid2 AND mub.blockeduserid = u.id 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 <<