function message_post_message($userfrom, $userto, $message, $format, $messagetype) { global $CFG, $SITE, $USER; /// Set up current language to suit the receiver of the message $savelang = $USER->lang; if (!empty($userto->lang)) { $USER->lang = $userto->lang; } /// Save the new message in the database $savemessage = NULL; $savemessage->useridfrom = $userfrom->id; $savemessage->useridto = $userto->id; $savemessage->message = $message; $savemessage->format = $format; $savemessage->timecreated = time(); $savemessage->messagetype = 'direct'; if ($CFG->messaging) { if (!($savemessage->id = insert_record('message', $savemessage))) { return false; } $emailforced = false; } else { // $CFG->messaging is not on, we need to force sending of emails $emailforced = true; $savemessage->id = true; } /// Check to see if anything else needs to be done with it $preference = (object) get_user_preferences(NULL, NULL, $userto->id); if ($emailforced || (!isset($preference->message_emailmessages) || $preference->message_emailmessages)) { // Receiver wants mail forwarding if (!isset($preference->message_emailtimenosee)) { $preference->message_emailtimenosee = 10; } if (!isset($preference->message_emailformat)) { $preference->message_emailformat = FORMAT_HTML; } if ($emailforced || time() - $userto->lastaccess > (int) $preference->message_emailtimenosee * 60) { // Long enough $message = stripslashes_safe($message); $tagline = get_string('emailtagline', 'message', $SITE->shortname); $messagesubject = preg_replace('/\\s+/', ' ', strip_tags($message)); // make sure it's all on one line $messagesubject = message_shorten_message($messagesubject, 30) . '...'; $messagetext = format_text_email($message, $format) . "\n\n--\n" . $tagline . "\n" . "{$CFG->wwwroot}/message/index.php?popup=1"; if (isset($preference->message_emailformat) and $preference->message_emailformat == FORMAT_HTML) { $messagehtml = format_text($message, $format); // MDL-10294, do not print link if messaging is disabled if ($CFG->messaging) { $messagehtml .= '<hr /><p><a href="' . $CFG->wwwroot . '/message/index.php?popup=1">' . $tagline . '</a></p>'; } } else { $messagehtml = NULL; } if (!empty($preference->message_emailaddress)) { $userto->email = $preference->message_emailaddress; // Use custom messaging address } if (email_to_user($userto, $userfrom, $messagesubject, $messagetext, $messagehtml)) { $CFG->messagewasjustemailed = true; } sleep(3); } } $USER->lang = $savelang; // restore original language return $savemessage->id; }
/** * 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; }
function message_post_message($userfrom, $userto, $message, $format, $messagetype) { global $CFG, $SITE; /// Save the new message in the database $savemessage = NULL; $savemessage->useridfrom = $userfrom->id; $savemessage->useridto = $userto->id; $savemessage->message = $message; $savemessage->format = $format; $savemessage->timecreated = time(); $savemessage->messagetype = 'direct'; if (!($savemessage->id = insert_record('message', $savemessage))) { return false; } /// Check to see if anything else needs to be done with it $preference = (object) get_user_preferences(NULL, NULL, $userto->id); if (!isset($preference->message_emailmessages) || $preference->message_emailmessages) { // Receiver wants mail forwarding if (!isset($preference->message_emailtimenosee)) { $preference->message_emailtimenosee = 10; } if (!isset($preference->message_emailformat)) { $preference->message_emailformat = FORMAT_HTML; } if (time() - $userto->lastaccess > (int) $preference->message_emailtimenosee * 60) { // Long enough $message = stripslashes_safe($message); $tagline = get_string('emailtagline', 'message', $SITE->shortname); $messagesubject = message_shorten_message(strip_tags($message), 30) . '...'; $messagetext = format_text_email($message, $format) . "\n\n--\n" . $tagline . "\n" . "{$CFG->wwwroot}/message/index.php?popup=1"; if (isset($preference->message_emailformat) and $preference->message_emailformat == FORMAT_HTML) { $messagehtml = format_text($message, $format); $messagehtml .= '<hr /><p><a href="' . $CFG->wwwroot . '/message/index.php?popup=1">' . $tagline . '</a></p>'; } else { $messagehtml = NULL; } if (!empty($preference->message_emailaddress)) { $userto->email = $preference->message_emailaddress; // Use custom messaging address } if (email_to_user($userto, $userfrom, $messagesubject, $messagetext, $messagehtml)) { $CFG->messagewasjustemailed = true; } sleep(3); } } return $savemessage->id; }