MDL-12613 - this should fix (part of) the problem with the search block (take 2)
[moodle.git] / mod / forum / search.php
CommitLineData
41905731 1<?php // $Id$
501cdbd8 2
ab00aa12 3 require_once('../../config.php');
4 require_once('lib.php');
501cdbd8 5
e6ae4dc8 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
b1dc6929 9 $perpage = optional_param('perpage', 10, PARAM_INT); // how many per page
10b9a26a 10 $showform = optional_param('showform', 0, PARAM_INT); // Just show the form
e6ae4dc8 11
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
77ffdf4b 14 $forumid = trim(optional_param('forumid', 0, PARAM_INT)); // ForumID to search for
e6ae4dc8 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
20
ab00aa12 21 $timefromrestrict = optional_param('timefromrestrict', 0, PARAM_INT); // Use starting date
4e471fc6 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
ab00aa12 27 if ($timefromrestrict) {
4e471fc6 28 $datefrom = make_timestamp($fromyear, $frommonth, $fromday, $fromhour, $fromminute);
29 } else {
30 $datefrom = optional_param('datefrom', 0, PARAM_INT); // Starting date
31 }
32
ab00aa12 33 $timetorestrict = optional_param('timetorestrict', 0, PARAM_INT); // Use ending date
4e471fc6 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
ab00aa12 39 if ($timetorestrict) {
4e471fc6 40 $dateto = make_timestamp($toyear, $tomonth, $today, $tohour, $tominute);
41 } else {
3ad24cea 42 $dateto = optional_param('dateto', 0, PARAM_INT); // Ending date
4e471fc6 43 }
44
45
e6ae4dc8 46
47 if (empty($search)) { // Check the other parameters instead
48 if (!empty($words)) {
49 $search .= ' '.$words;
50 }
51 if (!empty($userid)) {
52 $search .= ' userid:'.$userid;
53 }
77ffdf4b 54 if (!empty($forumid)) {
55 $search .= ' forumid:'.$forumid;
56 }
e6ae4dc8 57 if (!empty($user)) {
58 $search .= ' '.forum_clean_search_terms($user, 'user:');
59 }
60 if (!empty($subject)) {
61 $search .= ' '.forum_clean_search_terms($subject, 'subject:');
62 }
63 if (!empty($fullwords)) {
64 $search .= ' '.forum_clean_search_terms($fullwords, '+');
65 }
66 if (!empty($notwords)) {
67 $search .= ' '.forum_clean_search_terms($notwords, '-');
68 }
69 if (!empty($phrase)) {
70 $search .= ' "'.$phrase.'"';
71 }
4e471fc6 72 if (!empty($datefrom)) {
73 $search .= ' datefrom:'.$datefrom;
74 }
75 if (!empty($dateto)) {
76 $search .= ' dateto:'.$dateto;
77 }
e6ae4dc8 78 $individualparams = true;
79 } else {
80 $individualparams = false;
81 }
501cdbd8 82
8b9c7aa0 83 if ($search) {
e6ae4dc8 84 $search = forum_clean_search_terms($search);
8b9c7aa0 85 }
86
501cdbd8 87 if (! $course = get_record("course", "id", $id)) {
88 error("Course id is incorrect.");
89 }
90
ec81373f 91 require_course_login($course);
501cdbd8 92
8f0cd6ef 93 add_to_log($course->id, "forum", "search", "search.php?id=$course->id&amp;search=".urlencode($search), $search);
501cdbd8 94
46217d06 95 $strforums = get_string("modulenameplural", "forum");
96 $strsearch = get_string("search", "forum");
97 $strsearchresults = get_string("searchresults", "forum");
8b9c7aa0 98 $strpage = get_string("page");
97485d07 99
10b9a26a 100 if (!$search || $showform) {
e3f58dfb 101
38e179a4 102 $navlinks = array();
103 $navlinks[] = array('name' => $strforums, 'link' => "index.php?id=$course->id", 'type' => 'activity');
104 $navlinks[] = array('name' => $strsearch, 'link' => '', 'type' => 'title');
3b27b0fe 105 $navigation = build_navigation($navlinks);
e3f58dfb 106
107 print_header_simple("$strsearch", "", $navigation, 'search.words',
b2432b7f 108 "", "", "&nbsp;", navmenu($course));
680afe2e 109
e6ae4dc8 110 forum_print_big_search_form($course);
111 print_footer($course);
112 exit;
501cdbd8 113 }
114
e6ae4dc8 115/// We need to do a search now and print results
116
77ffdf4b 117 $searchterms = str_replace('forumid:', 'instance:', $search);
118 $searchterms = explode(' ', $searchterms);
e6ae4dc8 119
6f1cc8d6 120 $searchform = forum_search_form($course, $search);
e6ae4dc8 121
01e38388 122 $navlinks = array();
3b27b0fe 123 $navlinks[] = array('name' => $strsearch, 'link' => "search.php?id=$course->id", 'type' => 'activityinstance');
124 $navlinks[] = array('name' => s($search, true), 'link' => '', 'type' => 'link');
125 $navigation = build_navigation($navlinks);
4e471fc6 126
e6ae4dc8 127
e3f58dfb 128 if (!$posts = forum_search_posts($searchterms, $course->id, $page*$perpage, $perpage, $totalcount)) {
129 print_header_simple("$strsearchresults", "", $navigation, 'search.words', "", "", "&nbsp;", navmenu($course));
e6ae4dc8 130 print_heading(get_string("nopostscontaining", "forum", $search));
131
132 if (!$individualparams) {
133 $words = $search;
c7a5b3b1 134 }
e6ae4dc8 135
136 forum_print_big_search_form($course);
77ffdf4b 137
138 print_footer($course);
e6ae4dc8 139 exit;
140 }
e3f58dfb 141
142
143 print_header_simple("$strsearchresults", "", $navigation, '', "", "", $searchform, navmenu($course));
e6ae4dc8 144
10b9a26a 145 echo '<div class="reportlink">';
146 echo '<a href="search.php?id='.$course->id.
147 '&amp;user='.urlencode($user).
148 '&amp;userid='.$userid.
149 '&amp;forumid='.$forumid.
150 '&amp;subject='.urlencode($subject).
151 '&amp;phrase='.urlencode($phrase).
152 '&amp;words='.urlencode($words).
153 '&amp;fullwords='.urlencode($fullwords).
154 '&amp;notwords='.urlencode($notwords).
155 '&amp;dateto='.$dateto.
156 '&amp;datefrom='.$datefrom.
157 '&amp;showform=1'.
158 '">'.get_string('advancedsearch','forum').'...</a>';
159 echo '</div>';
160
e6ae4dc8 161 print_heading("$strsearchresults: $totalcount");
162
7f9dd6b4 163 print_paging_bar($totalcount, $page, $perpage, "search.php?search=".urlencode(stripslashes($search))."&amp;id=$course->id&amp;perpage=$perpage&amp;");
e6ae4dc8 164
165 //added to implement highlighting of search terms found only in HTML markup
166 //fiedorow - 9/2/2005
167 $strippedsearch = str_replace('user:','',$search);
168 $strippedsearch = str_replace('subject:','',$strippedsearch);
169 $strippedsearch = str_replace('&quot;','',$strippedsearch);
ab00aa12 170 $searchterms = explode(' ', $strippedsearch); // Search for words independently
e6ae4dc8 171 foreach ($searchterms as $key => $searchterm) {
172 if (preg_match('/^\-/',$searchterm)) {
173 unset($searchterms[$key]);
174 } else {
175 $searchterms[$key] = preg_replace('/^\+/','',$searchterm);
ceca2ad2 176 }
e6ae4dc8 177 }
a8d7ea4d 178 $strippedsearch = implode(' ', $searchterms); // Rebuild the string
501cdbd8 179
e6ae4dc8 180 foreach ($posts as $post) {
181
a7a89738 182 // Replace the simple subject with the three items forum name -> thread name -> subject
183 // (if all three are appropriate) each as a link.
ab00aa12 184 if (! $discussion = get_record('forum_discussions', 'id', $post->discussion)) {
185 error('Discussion ID was incorrect');
e6ae4dc8 186 }
ab00aa12 187 if (! $forum = get_record('forum', 'id', "$discussion->forum")) {
e6ae4dc8 188 error("Could not find forum $discussion->forum");
cdea3a53 189 }
190
a8d7ea4d 191 $post->subject = highlight($strippedsearch, $post->subject);
192 $discussion->name = highlight($strippedsearch, $discussion->name);
b800ac5a 193
3849dae8 194 $fullsubject = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
ab00aa12 195 if ($forum->type != 'single') {
c78ac798 196 $fullsubject .= " -> <a href=\"discuss.php?d=$discussion->id\">".format_string($discussion->name,true)."</a>";
e6ae4dc8 197 if ($post->parent != 0) {
17dc3f3c 198 $fullsubject .= " -> <a href=\"discuss.php?d=$post->discussion&amp;parent=$post->id\">".format_string($post->subject,true)."</a>";
ceca2ad2 199 }
e6ae4dc8 200 }
501cdbd8 201
e6ae4dc8 202 $post->subject = $fullsubject;
5b1059bb 203 $post->subjectnoformat = true;
048ccc47 204
a7a89738 205 // Identify search terms only found in HTML markup, and add a warning about them to
206 // the start of the message text. However, do not do the highlighting here. forum_print_post
207 // will do it for us later.
e6ae4dc8 208 $missing_terms = "";
b343df86 209
f2b5d7e3 210 $options = new object();
211 $options->trusttext = true;
b343df86 212 $message = highlight($strippedsearch,
213 format_text($post->message, $post->format, $options, $course->id),
214 0, '<fgw9sdpq4>', '</fgw9sdpq4>');
9044a387 215
e6ae4dc8 216 foreach ($searchterms as $searchterm) {
217 if (preg_match("/$searchterm/i",$message) && !preg_match('/<fgw9sdpq4>'.$searchterm.'<\/fgw9sdpq4>/i',$message)) {
218 $missing_terms .= " $searchterm";
cdea3a53 219 }
e6ae4dc8 220 }
501cdbd8 221
e6ae4dc8 222 if ($missing_terms) {
223 $strmissingsearchterms = get_string('missingsearchterms','forum');
a7a89738 224 $post->message = '<p class="highlight2">'.$strmissingsearchterms.' '.$missing_terms.'</p>'.$post->message;
501cdbd8 225 }
b800ac5a 226
a7a89738 227 // Prepare a link to the post in context, to be displayed after the forum post.
850e7ac5 228 $fulllink = "<a href=\"discuss.php?d=$post->discussion#p$post->id\">".get_string("postincontext", "forum")."</a>";
f2b5d7e3 229
a7a89738 230 // Now pring the post.
231 forum_print_post($post, $course->id, false, false, false, false,
232 $fulllink, $strippedsearch, -99, false);
501cdbd8 233 }
234
7f9dd6b4 235 print_paging_bar($totalcount, $page, $perpage, "search.php?search=".urlencode(stripslashes($search))."&amp;id=$course->id&amp;perpage=$perpage&amp;");
e6ae4dc8 236
501cdbd8 237 print_footer($course);
238
e6ae4dc8 239
240
e9ff8e10 241/**
242 * @todo Document this function
243 */
e6ae4dc8 244function forum_print_big_search_form($course) {
428aa337 245 global $CFG, $words, $subject, $phrase, $user, $userid, $fullwords, $notwords, $datefrom, $dateto;
e6ae4dc8 246
247 print_simple_box(get_string('searchforumintro', 'forum'), 'center', '', '', 'searchbox', 'intro');
248
249 print_simple_box_start("center");
ab00aa12 250
32f0b38a 251 echo "<script type=\"text/javascript\">\n";
ab00aa12 252 echo "var timefromitems = ['fromday','frommonth','fromyear','fromhour', 'fromminute'];\n";
253 echo "var timetoitems = ['today','tomonth','toyear','tohour','tominute'];\n";
254 echo "</script>\n";
255
d2ce367f 256 echo '<form id="searchform" action="search.php" method="get">';
e6ae4dc8 257 echo '<table cellpadding="10" class="searchbox" id="form">';
258
259 echo '<tr>';
e9ff8e10 260 echo '<td class="c0">'.get_string('searchwords', 'forum').':';
261 echo '<input type="hidden" value="'.$course->id.'" name="id" alt="" /></td>';
262 echo '<td class="c1"><input type="text" size="35" name="words" value="'.s($words, true).'" alt="" /></td>';
e6ae4dc8 263 echo '</tr>';
264
265 echo '<tr>';
4e471fc6 266 echo '<td class="c0">'.get_string('searchphrase', 'forum').':</td>';
e9ff8e10 267 echo '<td class="c1"><input type="text" size="35" name="phrase" value="'.s($phrase, true).'" alt="" /></td>';
e6ae4dc8 268 echo '</tr>';
269
270 echo '<tr>';
271 echo '<td class="c0">'.get_string('searchnotwords', 'forum').':</td>';
e9ff8e10 272 echo '<td class="c1"><input type="text" size="35" name="notwords" value="'.s($notwords, true).'" alt="" /></td>';
e6ae4dc8 273 echo '</tr>';
274
a4bad45c 275 if ($CFG->dbfamily == 'mysql' || $CFG->dbfamily == 'postgres') {
428aa337 276 echo '<tr>';
277 echo '<td class="c0">'.get_string('searchfullwords', 'forum').':</td>';
e9ff8e10 278 echo '<td class="c1"><input type="text" size="35" name="fullwords" value="'.s($fullwords, true).'" alt="" /></td>';
428aa337 279 echo '</tr>';
280 }
4e471fc6 281
282 echo '<tr>';
283 echo '<td class="c0">'.get_string('searchdatefrom', 'forum').':</td>';
284 echo '<td class="c1">';
2c3f3e77 285 if (empty($datefrom)) {
286 $datefromchecked = '';
4e471fc6 287 $datefrom = make_timestamp(2000, 1, 1, 0, 0, 0);
2c3f3e77 288 }else{
289 $datefromchecked = 'checked="checked"';
4e471fc6 290 }
2c3f3e77 291
292 echo '<input name="timefromrestrict" type="checkbox" value="1" alt="'.get_string('searchdatefrom', 'forum').'" onclick="return lockoptions(\'searchform\', \'timefromrestrict\', timefromitems)" '. $datefromchecked . ' /> ';
4e471fc6 293 print_date_selector('fromday', 'frommonth', 'fromyear', $datefrom);
294 print_time_selector('fromhour', 'fromminute', $datefrom);
ab00aa12 295
296 echo '<input type="hidden" name="hfromday" value="0" />';
297 echo '<input type="hidden" name="hfrommonth" value="0" />';
298 echo '<input type="hidden" name="hfromyear" value="0" />';
299 echo '<input type="hidden" name="hfromhour" value="0" />';
300 echo '<input type="hidden" name="hfromminute" value="0" />';
301
4e471fc6 302 echo '</td>';
303 echo '</tr>';
304
305 echo '<tr>';
306 echo '<td class="c0">'.get_string('searchdateto', 'forum').':</td>';
307 echo '<td class="c1">';
308 if (empty($dateto)) {
2c3f3e77 309 $datetochecked = '';
4e471fc6 310 $dateto = time()+3600;
2c3f3e77 311 }else{
312 $datetochecked = 'checked="checked"';
4e471fc6 313 }
2c3f3e77 314
315 echo '<input name="timetorestrict" type="checkbox" value="1" alt="'.get_string('searchdateto', 'forum').'" onclick="return lockoptions(\'searchform\', \'timetorestrict\', timetoitems)" ' .$datetochecked. ' /> ';
4e471fc6 316 print_date_selector('today', 'tomonth', 'toyear', $dateto);
317 print_time_selector('tohour', 'tominute', $dateto);
ab00aa12 318
319 echo '<input type="hidden" name="htoday" value="0" />';
320 echo '<input type="hidden" name="htomonth" value="0" />';
321 echo '<input type="hidden" name="htoyear" value="0" />';
322 echo '<input type="hidden" name="htohour" value="0" />';
323 echo '<input type="hidden" name="htominute" value="0" />';
324
4e471fc6 325 echo '</td>';
e6ae4dc8 326 echo '</tr>';
327
77ffdf4b 328 echo '<tr>';
329 echo '<td class="c0">'.get_string('searchwhichforums', 'forum').':</td>';
330 echo '<td class="c1">';
331 choose_from_menu(forum_menu_list($course), 'forumid', '', get_string('allforums', 'forum'), '');
332 echo '</td>';
333 echo '</tr>';
334
e6ae4dc8 335 echo '<tr>';
336 echo '<td class="c0">'.get_string('searchsubject', 'forum').':</td>';
e9ff8e10 337 echo '<td class="c1"><input type="text" size="35" name="subject" value="'.s($subject, true).'" alt="" /></td>';
e6ae4dc8 338 echo '</tr>';
339
340 echo '<tr>';
341 echo '<td class="c0">'.get_string('searchuser', 'forum').':</td>';
e9ff8e10 342 echo '<td class="c1"><input type="text" size="35" name="user" value="'.s($user, true).'" alt="" /></td>';
e6ae4dc8 343 echo '</tr>';
344
345 echo '<tr>';
346 echo '<td class="submit" colspan="2" align="center">';
76b1f3c7 347 echo '<input type="submit" value="'.get_string('searchforums', 'forum').'" alt="" /></td>';
e6ae4dc8 348 echo '</tr>';
349
350 echo '</table>';
351 echo '</form>';
ab00aa12 352
353 echo "<script type=\"text/javascript\">";
d2ce367f 354 echo "lockoptions('searchform','timefromrestrict', timefromitems);";
355 echo "lockoptions('searchform','timetorestrict', timetoitems);";
ab00aa12 356 echo "</script>\n";
357
e6ae4dc8 358 print_simple_box_end();
359}
360
e9ff8e10 361/**
362 * @todo Document this function
363 */
e6ae4dc8 364function forum_clean_search_terms($words, $prefix='') {
365 $searchterms = explode(' ', $words);
366 foreach ($searchterms as $key => $searchterm) {
367 if (strlen($searchterm) < 2) {
368 unset($searchterms[$key]);
369 } else if ($prefix) {
370 $searchterms[$key] = $prefix.$searchterm;
371 }
372 }
373 return trim(implode(' ', $searchterms));
374}
375
e9ff8e10 376/**
377 * @todo Document this function
378 */
77ffdf4b 379function forum_menu_list($course) {
380
381 $menu = array();
77ffdf4b 382
383 if ($forums = get_all_instances_in_course("forum", $course)) {
384 if ($course->format == 'weeks') {
385 $strsection = get_string('week');
386 } else {
387 $strsection = get_string('topic');
388 }
389
390 foreach ($forums as $forum) {
bbbf2d40 391 if ($cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) {
511ec76b 392 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
13534ef7 393 $currentgroup = groups_get_activity_group($cm);
77ffdf4b 394 if (!isset($forum->visible)) {
bbbf2d40 395 if (!instance_is_visible("forum", $forum) &&
0468976c 396 !has_capability('moodle/course:viewhiddenactivities', $context)) {
77ffdf4b 397 continue;
398 }
399 }
13534ef7 400 $groupmode = groups_get_activity_groupmode($cm); // Groups are being used
511ec76b 401 if ($groupmode == SEPARATEGROUPS && ($currentgroup === false) &&
402 !has_capability('moodle/site:accessallgroups', $context)) {
bbbf2d40 403 continue;
77ffdf4b 404 }
405 }
3849dae8 406 $menu[$forum->id] = format_string($forum->name,true);
77ffdf4b 407 }
408 }
409
410 return $menu;
411}
412
0468976c 413?>