MDL-10234
[moodle-linuxchix.git] / mod / forum / search.php
blobbd2e13993c6bc99d4825cf52d04f575921f08128
1 <?php // $Id$
3 require_once('../../config.php');
4 require_once('lib.php');
6 $id = required_param('id', PARAM_INT); // course id
7 $search = trim(optional_param('search', '', PARAM_NOTAGS)); // search string
8 $page = optional_param('page', 0, PARAM_INT); // which page to show
9 $perpage = optional_param('perpage', 10, PARAM_INT); // how many per page
10 $showform = optional_param('showform', 0, PARAM_INT); // Just show the form
12 $user = trim(optional_param('user', '', PARAM_NOTAGS)); // Names to search for
13 $userid = trim(optional_param('userid', 0, PARAM_INT)); // UserID to search for
14 $forumid = trim(optional_param('forumid', 0, PARAM_INT)); // ForumID to search for
15 $subject = trim(optional_param('subject', '', PARAM_NOTAGS)); // Subject
16 $phrase = trim(optional_param('phrase', '', PARAM_NOTAGS)); // Phrase
17 $words = trim(optional_param('words', '', PARAM_NOTAGS)); // Words
18 $fullwords = trim(optional_param('fullwords', '', PARAM_NOTAGS)); // Whole words
19 $notwords = trim(optional_param('notwords', '', PARAM_NOTAGS)); // Words we don't want
21 $timefromrestrict = optional_param('timefromrestrict', 0, PARAM_INT); // Use starting date
22 $fromday = optional_param('fromday', 0, PARAM_INT); // Starting date
23 $frommonth = optional_param('frommonth', 0, PARAM_INT); // Starting date
24 $fromyear = optional_param('fromyear', 0, PARAM_INT); // Starting date
25 $fromhour = optional_param('fromhour', 0, PARAM_INT); // Starting date
26 $fromminute = optional_param('fromminute', 0, PARAM_INT); // Starting date
27 if ($timefromrestrict) {
28 $datefrom = make_timestamp($fromyear, $frommonth, $fromday, $fromhour, $fromminute);
29 } else {
30 $datefrom = optional_param('datefrom', 0, PARAM_INT); // Starting date
33 $timetorestrict = optional_param('timetorestrict', 0, PARAM_INT); // Use ending date
34 $today = optional_param('today', 0, PARAM_INT); // Ending date
35 $tomonth = optional_param('tomonth', 0, PARAM_INT); // Ending date
36 $toyear = optional_param('toyear', 0, PARAM_INT); // Ending date
37 $tohour = optional_param('tohour', 0, PARAM_INT); // Ending date
38 $tominute = optional_param('tominute', 0, PARAM_INT); // Ending date
39 if ($timetorestrict) {
40 $dateto = make_timestamp($toyear, $tomonth, $today, $tohour, $tominute);
41 } else {
42 $dateto = optional_param('dateto', 0, PARAM_INT); // Ending date
47 if (empty($search)) { // Check the other parameters instead
48 if (!empty($words)) {
49 $search .= ' '.$words;
51 if (!empty($userid)) {
52 $search .= ' userid:'.$userid;
54 if (!empty($forumid)) {
55 $search .= ' forumid:'.$forumid;
57 if (!empty($user)) {
58 $search .= ' '.forum_clean_search_terms($user, 'user:');
60 if (!empty($subject)) {
61 $search .= ' '.forum_clean_search_terms($subject, 'subject:');
63 if (!empty($fullwords)) {
64 $search .= ' '.forum_clean_search_terms($fullwords, '+');
66 if (!empty($notwords)) {
67 $search .= ' '.forum_clean_search_terms($notwords, '-');
69 if (!empty($phrase)) {
70 $search .= ' "'.$phrase.'"';
72 if (!empty($datefrom)) {
73 $search .= ' datefrom:'.$datefrom;
75 if (!empty($dateto)) {
76 $search .= ' dateto:'.$dateto;
78 $individualparams = true;
79 } else {
80 $individualparams = false;
83 if ($search) {
84 $search = forum_clean_search_terms($search);
87 if (! $course = get_record("course", "id", $id)) {
88 error("Course id is incorrect.");
91 require_course_login($course);
93 add_to_log($course->id, "forum", "search", "search.php?id=$course->id&amp;search=".urlencode($search), $search);
95 $strforums = get_string("modulenameplural", "forum");
96 $strsearch = get_string("search", "forum");
97 $strsearchresults = get_string("searchresults", "forum");
98 $strpage = get_string("page");
100 $crumbs[] = array('name' => $strforums, 'link' => "index.php?id=$course->id", 'type' => 'activity');
102 if (!$search || $showform) {
104 $crumns[] = array('name' => $strsearch, 'link' => '', 'type' => 'title');
105 $navigation = build_navigation($crumbs);
107 print_header_simple("$strsearch", "", $navigation, 'search.words',
108 "", "", "&nbsp;", navmenu($course));
110 forum_print_big_search_form($course);
111 print_footer($course);
112 exit;
115 /// We need to do a search now and print results
117 $searchterms = str_replace('forumid:', 'instance:', $search);
118 $searchterms = explode(' ', $searchterms);
120 $searchform = forum_search_form($course, $search);
122 $crumbs[] = array('name' => $strsearch, 'link' => "search.php?id=$course->id", 'type' => 'activityinstance');
123 $crumbs[] = array('name' => s($search, true), 'link' => '', 'type' => 'link');
124 $navigation = build_navigation($crumbs);
127 if (!$posts = forum_search_posts($searchterms, $course->id, $page*$perpage, $perpage, $totalcount)) {
128 print_header_simple("$strsearchresults", "", $navigation, 'search.words', "", "", "&nbsp;", navmenu($course));
129 print_heading(get_string("nopostscontaining", "forum", $search));
131 if (!$individualparams) {
132 $words = $search;
135 forum_print_big_search_form($course);
137 print_footer($course);
138 exit;
142 print_header_simple("$strsearchresults", "", $navigation, '', "", "", $searchform, navmenu($course));
144 echo '<div class="reportlink">';
145 echo '<a href="search.php?id='.$course->id.
146 '&amp;user='.urlencode($user).
147 '&amp;userid='.$userid.
148 '&amp;forumid='.$forumid.
149 '&amp;subject='.urlencode($subject).
150 '&amp;phrase='.urlencode($phrase).
151 '&amp;words='.urlencode($words).
152 '&amp;fullwords='.urlencode($fullwords).
153 '&amp;notwords='.urlencode($notwords).
154 '&amp;dateto='.$dateto.
155 '&amp;datefrom='.$datefrom.
156 '&amp;showform=1'.
157 '">'.get_string('advancedsearch','forum').'...</a>';
158 echo '</div>';
160 print_heading("$strsearchresults: $totalcount");
162 print_paging_bar($totalcount, $page, $perpage, "search.php?search=".urlencode(stripslashes($search))."&amp;id=$course->id&amp;perpage=$perpage&amp;");
164 //added to implement highlighting of search terms found only in HTML markup
165 //fiedorow - 9/2/2005
166 $strippedsearch = str_replace('user:','',$search);
167 $strippedsearch = str_replace('subject:','',$strippedsearch);
168 $strippedsearch = str_replace('&quot;','',$strippedsearch);
169 $searchterms = explode(' ', $strippedsearch); // Search for words independently
170 foreach ($searchterms as $key => $searchterm) {
171 if (preg_match('/^\-/',$searchterm)) {
172 unset($searchterms[$key]);
173 } else {
174 $searchterms[$key] = preg_replace('/^\+/','',$searchterm);
177 $strippedsearch = implode(' ', $searchterms); // Rebuild the string
179 foreach ($posts as $post) {
181 if (! $discussion = get_record('forum_discussions', 'id', $post->discussion)) {
182 error('Discussion ID was incorrect');
184 if (! $forum = get_record('forum', 'id', "$discussion->forum")) {
185 error("Could not find forum $discussion->forum");
188 $post->subject = highlight($strippedsearch, $post->subject);
189 $discussion->name = highlight($strippedsearch, $discussion->name);
191 $fullsubject = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
192 if ($forum->type != 'single') {
193 $fullsubject .= " -> <a href=\"discuss.php?d=$discussion->id\">".format_string($discussion->name,true)."</a>";
194 if ($post->parent != 0) {
195 $fullsubject .= " -> <a href=\"discuss.php?d=$post->discussion&amp;parent=$post->id\">".format_string($post->subject,true)."</a>";
199 $post->subject = $fullsubject;
201 //Indicate search terms only found in HTML markup
202 //Use highlight() with nonsense tags to spot search terms in the
203 //actual text content first. fiedorow - 9/2/2005
204 $missing_terms = "";
206 // Hack for posts of format FORMAT_PLAIN. Otherwise html tags added by
207 // the highlight() call bellow get stripped out by forum_print_post().
208 if ($post->format == FORMAT_PLAIN) {
209 $post->message = stripslashes_safe($post->message);
210 $post->message = rebuildnolinktag($post->message);
211 $post->message = str_replace(' ', '&nbsp; ', $post->message);
212 $post->message = nl2br($post->message);
213 $post->format = FORMAT_HTML;
216 $options = new object();
217 $options->trusttext = true;
218 // detect TRUSTTEXT marker before first call to format_text
219 if (trusttext_present($post->message)) {
220 $ttpresent = true;
221 } else {
222 $ttpresent = false;
224 $message = highlight($strippedsearch,
225 format_text($post->message, $post->format, $options, $course->id),
226 0, '<fgw9sdpq4>', '</fgw9sdpq4>');
228 foreach ($searchterms as $searchterm) {
229 if (preg_match("/$searchterm/i",$message) && !preg_match('/<fgw9sdpq4>'.$searchterm.'<\/fgw9sdpq4>/i',$message)) {
230 $missing_terms .= " $searchterm";
233 // now is the right time to strip the TRUSTTEXT marker, we will add it later if needed
234 $post->message = trusttext_strip($post->message);
236 $message = str_replace('<fgw9sdpq4>','<span class="highlight">',$message);
237 $message = str_replace('</fgw9sdpq4>','</span>',$message);
239 if ($missing_terms) {
240 $strmissingsearchterms = get_string('missingsearchterms','forum');
241 $post->message = '<p class="highlight2">'.$strmissingsearchterms.' '.$missing_terms.'</p>'.$message;
242 $ttpresent = false;
243 } else {
244 $post->message = $message;
247 $fulllink = "<a href=\"discuss.php?d=$post->discussion#$post->id\">".get_string("postincontext", "forum")."</a>";
248 //search terms already highlighted - fiedorow - 9/2/2005
249 $SESSION->forum_search = true;
251 // reconstruct the TRUSTTEXT properly after processing
252 if ($ttpresent) {
253 $post->message = trusttext_mark($post->message);
254 } else {
255 $post->message = trusttext_strip($post->message); //make 100% sure TRUSTTEXT marker was not created during processing
257 forum_print_post($post, $course->id, false, false, false, false, $fulllink);
258 unset($SESSION->forum_search);
260 echo "<br />";
263 print_paging_bar($totalcount, $page, $perpage, "search.php?search=".urlencode(stripslashes($search))."&amp;id=$course->id&amp;perpage=$perpage&amp;");
265 print_footer($course);
270 * @todo Document this function
272 function forum_print_big_search_form($course) {
273 global $CFG, $words, $subject, $phrase, $user, $userid, $fullwords, $notwords, $datefrom, $dateto;
275 print_simple_box(get_string('searchforumintro', 'forum'), 'center', '', '', 'searchbox', 'intro');
277 print_simple_box_start("center");
279 echo "<script type=\"text/javascript\">\n";
280 echo "var timefromitems = ['fromday','frommonth','fromyear','fromhour', 'fromminute'];\n";
281 echo "var timetoitems = ['today','tomonth','toyear','tohour','tominute'];\n";
282 echo "</script>\n";
284 echo '<form id="searchform" action="search.php" method="get">';
285 echo '<table cellpadding="10" class="searchbox" id="form">';
287 echo '<tr>';
288 echo '<td class="c0">'.get_string('searchwords', 'forum').':';
289 echo '<input type="hidden" value="'.$course->id.'" name="id" alt="" /></td>';
290 echo '<td class="c1"><input type="text" size="35" name="words" value="'.s($words, true).'" alt="" /></td>';
291 echo '</tr>';
293 echo '<tr>';
294 echo '<td class="c0">'.get_string('searchphrase', 'forum').':</td>';
295 echo '<td class="c1"><input type="text" size="35" name="phrase" value="'.s($phrase, true).'" alt="" /></td>';
296 echo '</tr>';
298 echo '<tr>';
299 echo '<td class="c0">'.get_string('searchnotwords', 'forum').':</td>';
300 echo '<td class="c1"><input type="text" size="35" name="notwords" value="'.s($notwords, true).'" alt="" /></td>';
301 echo '</tr>';
303 if ($CFG->dbfamily == 'mysql' || $CFG->dbfamily == 'postgres') {
304 echo '<tr>';
305 echo '<td class="c0">'.get_string('searchfullwords', 'forum').':</td>';
306 echo '<td class="c1"><input type="text" size="35" name="fullwords" value="'.s($fullwords, true).'" alt="" /></td>';
307 echo '</tr>';
310 echo '<tr>';
311 echo '<td class="c0">'.get_string('searchdatefrom', 'forum').':</td>';
312 echo '<td class="c1">';
313 if (empty($datefrom)) {
314 $datefromchecked = '';
315 $datefrom = make_timestamp(2000, 1, 1, 0, 0, 0);
316 }else{
317 $datefromchecked = 'checked="checked"';
320 echo '<input name="timefromrestrict" type="checkbox" value="1" alt="'.get_string('searchdatefrom', 'forum').'" onclick="return lockoptions(\'searchform\', \'timefromrestrict\', timefromitems)" '. $datefromchecked . ' /> ';
321 print_date_selector('fromday', 'frommonth', 'fromyear', $datefrom);
322 print_time_selector('fromhour', 'fromminute', $datefrom);
324 echo '<input type="hidden" name="hfromday" value="0" />';
325 echo '<input type="hidden" name="hfrommonth" value="0" />';
326 echo '<input type="hidden" name="hfromyear" value="0" />';
327 echo '<input type="hidden" name="hfromhour" value="0" />';
328 echo '<input type="hidden" name="hfromminute" value="0" />';
330 echo '</td>';
331 echo '</tr>';
333 echo '<tr>';
334 echo '<td class="c0">'.get_string('searchdateto', 'forum').':</td>';
335 echo '<td class="c1">';
336 if (empty($dateto)) {
337 $datetochecked = '';
338 $dateto = time()+3600;
339 }else{
340 $datetochecked = 'checked="checked"';
343 echo '<input name="timetorestrict" type="checkbox" value="1" alt="'.get_string('searchdateto', 'forum').'" onclick="return lockoptions(\'searchform\', \'timetorestrict\', timetoitems)" ' .$datetochecked. ' /> ';
344 print_date_selector('today', 'tomonth', 'toyear', $dateto);
345 print_time_selector('tohour', 'tominute', $dateto);
347 echo '<input type="hidden" name="htoday" value="0" />';
348 echo '<input type="hidden" name="htomonth" value="0" />';
349 echo '<input type="hidden" name="htoyear" value="0" />';
350 echo '<input type="hidden" name="htohour" value="0" />';
351 echo '<input type="hidden" name="htominute" value="0" />';
353 echo '</td>';
354 echo '</tr>';
356 echo '<tr>';
357 echo '<td class="c0">'.get_string('searchwhichforums', 'forum').':</td>';
358 echo '<td class="c1">';
359 choose_from_menu(forum_menu_list($course), 'forumid', '', get_string('allforums', 'forum'), '');
360 echo '</td>';
361 echo '</tr>';
363 echo '<tr>';
364 echo '<td class="c0">'.get_string('searchsubject', 'forum').':</td>';
365 echo '<td class="c1"><input type="text" size="35" name="subject" value="'.s($subject, true).'" alt="" /></td>';
366 echo '</tr>';
368 echo '<tr>';
369 echo '<td class="c0">'.get_string('searchuser', 'forum').':</td>';
370 echo '<td class="c1"><input type="text" size="35" name="user" value="'.s($user, true).'" alt="" /></td>';
371 echo '</tr>';
373 echo '<tr>';
374 echo '<td class="submit" colspan="2" align="center">';
375 echo '<input type="submit" value="'.get_string('searchforums', 'forum').'" alt="" /></td>';
376 echo '</tr>';
378 echo '</table>';
379 echo '</form>';
381 echo "<script type=\"text/javascript\">";
382 echo "lockoptions('searchform','timefromrestrict', timefromitems);";
383 echo "lockoptions('searchform','timetorestrict', timetoitems);";
384 echo "</script>\n";
386 print_simple_box_end();
390 * @todo Document this function
392 function forum_clean_search_terms($words, $prefix='') {
393 $searchterms = explode(' ', $words);
394 foreach ($searchterms as $key => $searchterm) {
395 if (strlen($searchterm) < 2) {
396 unset($searchterms[$key]);
397 } else if ($prefix) {
398 $searchterms[$key] = $prefix.$searchterm;
401 return trim(implode(' ', $searchterms));
405 * @todo Document this function
407 function forum_menu_list($course) {
409 $menu = array();
410 $currentgroup = get_current_group($course->id);
412 if ($forums = get_all_instances_in_course("forum", $course)) {
413 if ($course->format == 'weeks') {
414 $strsection = get_string('week');
415 } else {
416 $strsection = get_string('topic');
419 foreach ($forums as $forum) {
420 if ($cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) {
421 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
422 if (!isset($forum->visible)) {
423 if (!instance_is_visible("forum", $forum) &&
424 !has_capability('moodle/course:viewhiddenactivities', $context)) {
425 continue;
428 $groupmode = groupmode($course, $cm); // Groups are being used
429 if ($groupmode == SEPARATEGROUPS && ($currentgroup === false) &&
430 !has_capability('moodle/site:accessallgroups', $context)) {
431 continue;
434 $menu[$forum->id] = format_string($forum->name,true);
438 return $menu;