From: moodler Date: Mon, 3 Jan 2005 12:16:06 +0000 (+0000) Subject: Big improvements on message searching. It now highlights the results, and X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=38c6a9280938729cfa58123d7d36754669c9381b;p=moodle.git Big improvements on message searching. It now highlights the results, and shows you the fragment containing the results (not just a shortened version) --- diff --git a/lang/en/message.php b/lang/en/message.php index 3e612bed02..c8bb495d85 100644 --- a/lang/en/message.php +++ b/lang/en/message.php @@ -8,6 +8,7 @@ $string['allusers'] = 'All messages from all users'; $string['beepnewmessage'] = 'Beep when a new message comes in'; $string['blockcontact'] = 'Block contact'; $string['blockedmessages'] = '$a message(s) to/from blocked users'; +$string['context'] = 'context'; $string['deletemessagesdays'] = 'Number of days before old messages are automatically deleted'; $string['discussion'] = 'Discussion'; $string['emailmessages'] = 'Email messages when I am offline'; diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 939f2adfca..d2397e6b22 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -680,6 +680,7 @@ $string['maximumupload'] = 'Maximum upload size'; $string['maxsize'] = 'Max size: $a'; $string['min'] = 'min'; $string['mins'] = 'mins'; +$string['minutes'] = 'minutes'; $string['miscellaneous'] = 'Miscellaneous'; $string['missingcategory'] = 'You need to choose a category'; $string['missingcity'] = 'Missing city/town'; diff --git a/message/history.php b/message/history.php index 3586a967b9..f49e6b7e3d 100644 --- a/message/history.php +++ b/message/history.php @@ -21,6 +21,7 @@ $userid2 = $USER->id; // Can only see messages involving yourself $user2 = $USER; } + $search = optional_param('search', '', PARAM_CLEAN); /// Our two users are defined - let's set up the page @@ -66,9 +67,9 @@ print_heading(userdate($message->timecreated, $blockdate), 'center', 4); } if ($message->useridfrom == $user1->id) { - echo message_format_message($message, $user1, $messagedate); + echo message_format_message($message, $user1, $messagedate, $search); } else { - echo message_format_message($message, $user2, $messagedate); + echo message_format_message($message, $user2, $messagedate, $search); } } } else { diff --git a/message/lib.php b/message/lib.php index d22aab65c7..31d5b4bce6 100644 --- a/message/lib.php +++ b/message/lib.php @@ -370,7 +370,8 @@ function message_print_search_results($frm) { /// search messages for keywords } else if (!empty($frm->keywordssubmit) and !empty($frm->keywords)) { - $keywords = explode(' ', $frm->keywords); + $keywordstring = clean_text(trim($frm->keywords)); + $keywords = explode(' ', $keywordstring); $tome = false; $fromme = false; $courseid = 'none'; @@ -405,7 +406,7 @@ function message_print_search_results($frm) { } /// print heading with number of results - echo ''.get_string('keywordssearchresults', 'message', count($messages)).''; + echo '

'.get_string('keywordssearchresults', 'message', count($messages)).' ("'.s($keywordstring).'")

'; /// print table headings echo ''; @@ -418,7 +419,7 @@ function message_print_search_results($frm) { $blockedcount = 0; $dateformat = get_string('strftimedatetime'); - $strmore = get_string('more'); + $strcontext = get_string('context', 'message'); foreach ($messages as $message) { /// ignore messages to and from blocked users unless $frm->includeblocked is set @@ -469,9 +470,8 @@ function message_print_search_results($frm) { echo ''; - echo ''; echo ''; echo "\n"; @@ -574,7 +574,7 @@ function message_contact_link($userid, $linktype='add', $return=false) { } } -function message_history_link ($userid1, $userid2=0, $returnstr=false, $position='', $linktext='') { +function message_history_link($userid1, $userid2=0, $returnstr=false, $keywords='', $position='', $linktext='') { global $USER, $CFG; if (!$userid2) { @@ -583,6 +583,9 @@ function message_history_link ($userid1, $userid2=0, $returnstr=false, $position if ($position) { $position = "#$position"; } + if ($keywords) { + $keywords = "&search=".urlencode($keywords); + } if ($linktext == 'icon') { $linktext = ''; @@ -590,7 +593,7 @@ function message_history_link ($userid1, $userid2=0, $returnstr=false, $position $linktext = get_string('messagehistory', 'message'); } - $str = link_to_popup_window("/message/history.php?user1=$userid1&user2=$userid2$position", + $str = link_to_popup_window("/message/history.php?user1=$userid1&user2=$userid2$keywords$position", "message_history_$user->id", $linktext, 500, 500, '', 'menubar=0,location=0,status,scrollbars,resizable,width=500,height=500', true); @@ -834,6 +837,55 @@ function message_shorten_message($message, $minlength=0) { 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. + */ +function message_get_fragment($message, $keywords) { + + $fullsize = 120; + $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_get_history($user1, $user2) { $messages = get_records_select('message_read', "(useridto = '$user1->id' AND useridfrom = '$user2->id') OR (useridto = '$user2->id' AND useridfrom = '$user1->id')", @@ -848,13 +900,17 @@ function message_get_history($user1, $user2) { return $messages; } -function message_format_message(&$message, &$user, $format='') { +function message_format_message(&$message, &$user, $format='', $keywords='') { if (empty($format)) { $format = get_string('strftimedaytime'); } $time = userdate($message->timecreated, $format); + $messagetext = format_text($message->message, $message->format); + if ($keywords) { + $messagetext = highlight($keywords, $messagetext); + } return '

'.s($user->firstname).' ['.$time.']: '. - format_text($message->message, $message->format).'

'; + $messagetext.'

'; } /* diff --git a/message/search.html b/message/search.html index bb159e0691..afbea697d1 100644 --- a/message/search.html +++ b/message/search.html @@ -41,7 +41,7 @@ - + diff --git a/theme/standardxhtml/styles.php b/theme/standardxhtml/styles.php index 5a8378e018..f64a8cc54f 100644 --- a/theme/standardxhtml/styles.php +++ b/theme/standardxhtml/styles.php @@ -817,6 +817,10 @@ TABLE.formtable TD { .message_summary { font-size: small; } +.message_summary_link { + font-size: small; + text-align: right; +} .message_date { font-size: small; }
'; message_print_user($userto, $tocontact, $toblocked); echo ''.message_shorten_message($message->message, 20); - echo ' ...('.message_history_link($message->useridto, $message->useridfrom, true, $datestring, - $strmore.' ').')'; + echo ''.message_get_fragment($message->message, $keywords); + echo '
'; echo '
'.userdate($message->timecreated, $dateformat).'