MDL-12505 general advanced search cleanup + added capability test to recent activity...
[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)) {
5a2a5331 88 print_error("Course id is incorrect.");
501cdbd8 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) {
65bcf17b 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);
65bcf17b 106
e3f58dfb 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 }
65bcf17b 141
142
e3f58dfb 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
65bcf17b 183 // (if all three are appropriate) each as a link.
ab00aa12 184 if (! $discussion = get_record('forum_discussions', 'id', $post->discussion)) {
5a2a5331 185 print_error('Discussion ID was incorrect');
e6ae4dc8 186 }
ab00aa12 187 if (! $forum = get_record('forum', 'id', "$discussion->forum")) {
5a2a5331 188 print_error("Could not find forum $discussion->forum");
cdea3a53 189 }
190
65bcf17b 191 if (!$cm = get_coursemodule_from_instance('forum', $forum->id)) {
5a2a5331 192 print_error('Course Module ID was incorrect');
65bcf17b 193 }
194
a8d7ea4d 195 $post->subject = highlight($strippedsearch, $post->subject);
196 $discussion->name = highlight($strippedsearch, $discussion->name);
b800ac5a 197
3849dae8 198 $fullsubject = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
ab00aa12 199 if ($forum->type != 'single') {
c78ac798 200 $fullsubject .= " -> <a href=\"discuss.php?d=$discussion->id\">".format_string($discussion->name,true)."</a>";
e6ae4dc8 201 if ($post->parent != 0) {
17dc3f3c 202 $fullsubject .= " -> <a href=\"discuss.php?d=$post->discussion&amp;parent=$post->id\">".format_string($post->subject,true)."</a>";
ceca2ad2 203 }
e6ae4dc8 204 }
501cdbd8 205
e6ae4dc8 206 $post->subject = $fullsubject;
5b1059bb 207 $post->subjectnoformat = true;
048ccc47 208
a7a89738 209 // Identify search terms only found in HTML markup, and add a warning about them to
210 // the start of the message text. However, do not do the highlighting here. forum_print_post
211 // will do it for us later.
e6ae4dc8 212 $missing_terms = "";
b343df86 213
f2b5d7e3 214 $options = new object();
215 $options->trusttext = true;
b343df86 216 $message = highlight($strippedsearch,
217 format_text($post->message, $post->format, $options, $course->id),
218 0, '<fgw9sdpq4>', '</fgw9sdpq4>');
9044a387 219
e6ae4dc8 220 foreach ($searchterms as $searchterm) {
221 if (preg_match("/$searchterm/i",$message) && !preg_match('/<fgw9sdpq4>'.$searchterm.'<\/fgw9sdpq4>/i',$message)) {
222 $missing_terms .= " $searchterm";
cdea3a53 223 }
e6ae4dc8 224 }
501cdbd8 225
e6ae4dc8 226 if ($missing_terms) {
227 $strmissingsearchterms = get_string('missingsearchterms','forum');
a7a89738 228 $post->message = '<p class="highlight2">'.$strmissingsearchterms.' '.$missing_terms.'</p>'.$post->message;
501cdbd8 229 }
b800ac5a 230
a7a89738 231 // Prepare a link to the post in context, to be displayed after the forum post.
850e7ac5 232 $fulllink = "<a href=\"discuss.php?d=$post->discussion#p$post->id\">".get_string("postincontext", "forum")."</a>";
f2b5d7e3 233
a7a89738 234 // Now pring the post.
f4c0cb7c 235 forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false, false,
a7a89738 236 $fulllink, $strippedsearch, -99, false);
501cdbd8 237 }
238
7f9dd6b4 239 print_paging_bar($totalcount, $page, $perpage, "search.php?search=".urlencode(stripslashes($search))."&amp;id=$course->id&amp;perpage=$perpage&amp;");
e6ae4dc8 240
501cdbd8 241 print_footer($course);
242
e6ae4dc8 243
244
e9ff8e10 245/**
246 * @todo Document this function
247 */
e6ae4dc8 248function forum_print_big_search_form($course) {
428aa337 249 global $CFG, $words, $subject, $phrase, $user, $userid, $fullwords, $notwords, $datefrom, $dateto;
e6ae4dc8 250
251 print_simple_box(get_string('searchforumintro', 'forum'), 'center', '', '', 'searchbox', 'intro');
252
253 print_simple_box_start("center");
ab00aa12 254
32f0b38a 255 echo "<script type=\"text/javascript\">\n";
ab00aa12 256 echo "var timefromitems = ['fromday','frommonth','fromyear','fromhour', 'fromminute'];\n";
257 echo "var timetoitems = ['today','tomonth','toyear','tohour','tominute'];\n";
258 echo "</script>\n";
259
d2ce367f 260 echo '<form id="searchform" action="search.php" method="get">';
e6ae4dc8 261 echo '<table cellpadding="10" class="searchbox" id="form">';
262
263 echo '<tr>';
e9ff8e10 264 echo '<td class="c0">'.get_string('searchwords', 'forum').':';
265 echo '<input type="hidden" value="'.$course->id.'" name="id" alt="" /></td>';
266 echo '<td class="c1"><input type="text" size="35" name="words" value="'.s($words, true).'" alt="" /></td>';
e6ae4dc8 267 echo '</tr>';
268
269 echo '<tr>';
4e471fc6 270 echo '<td class="c0">'.get_string('searchphrase', 'forum').':</td>';
e9ff8e10 271 echo '<td class="c1"><input type="text" size="35" name="phrase" value="'.s($phrase, true).'" alt="" /></td>';
e6ae4dc8 272 echo '</tr>';
273
274 echo '<tr>';
275 echo '<td class="c0">'.get_string('searchnotwords', 'forum').':</td>';
e9ff8e10 276 echo '<td class="c1"><input type="text" size="35" name="notwords" value="'.s($notwords, true).'" alt="" /></td>';
e6ae4dc8 277 echo '</tr>';
278
a4bad45c 279 if ($CFG->dbfamily == 'mysql' || $CFG->dbfamily == 'postgres') {
428aa337 280 echo '<tr>';
281 echo '<td class="c0">'.get_string('searchfullwords', 'forum').':</td>';
e9ff8e10 282 echo '<td class="c1"><input type="text" size="35" name="fullwords" value="'.s($fullwords, true).'" alt="" /></td>';
428aa337 283 echo '</tr>';
284 }
4e471fc6 285
286 echo '<tr>';
287 echo '<td class="c0">'.get_string('searchdatefrom', 'forum').':</td>';
288 echo '<td class="c1">';
2c3f3e77 289 if (empty($datefrom)) {
290 $datefromchecked = '';
4e471fc6 291 $datefrom = make_timestamp(2000, 1, 1, 0, 0, 0);
2c3f3e77 292 }else{
293 $datefromchecked = 'checked="checked"';
4e471fc6 294 }
2c3f3e77 295
296 echo '<input name="timefromrestrict" type="checkbox" value="1" alt="'.get_string('searchdatefrom', 'forum').'" onclick="return lockoptions(\'searchform\', \'timefromrestrict\', timefromitems)" '. $datefromchecked . ' /> ';
4e471fc6 297 print_date_selector('fromday', 'frommonth', 'fromyear', $datefrom);
298 print_time_selector('fromhour', 'fromminute', $datefrom);
ab00aa12 299
300 echo '<input type="hidden" name="hfromday" value="0" />';
301 echo '<input type="hidden" name="hfrommonth" value="0" />';
302 echo '<input type="hidden" name="hfromyear" value="0" />';
303 echo '<input type="hidden" name="hfromhour" value="0" />';
304 echo '<input type="hidden" name="hfromminute" value="0" />';
305
4e471fc6 306 echo '</td>';
307 echo '</tr>';
308
309 echo '<tr>';
310 echo '<td class="c0">'.get_string('searchdateto', 'forum').':</td>';
311 echo '<td class="c1">';
312 if (empty($dateto)) {
2c3f3e77 313 $datetochecked = '';
4e471fc6 314 $dateto = time()+3600;
2c3f3e77 315 }else{
316 $datetochecked = 'checked="checked"';
4e471fc6 317 }
2c3f3e77 318
319 echo '<input name="timetorestrict" type="checkbox" value="1" alt="'.get_string('searchdateto', 'forum').'" onclick="return lockoptions(\'searchform\', \'timetorestrict\', timetoitems)" ' .$datetochecked. ' /> ';
4e471fc6 320 print_date_selector('today', 'tomonth', 'toyear', $dateto);
321 print_time_selector('tohour', 'tominute', $dateto);
ab00aa12 322
323 echo '<input type="hidden" name="htoday" value="0" />';
324 echo '<input type="hidden" name="htomonth" value="0" />';
325 echo '<input type="hidden" name="htoyear" value="0" />';
326 echo '<input type="hidden" name="htohour" value="0" />';
327 echo '<input type="hidden" name="htominute" value="0" />';
328
4e471fc6 329 echo '</td>';
e6ae4dc8 330 echo '</tr>';
331
77ffdf4b 332 echo '<tr>';
333 echo '<td class="c0">'.get_string('searchwhichforums', 'forum').':</td>';
334 echo '<td class="c1">';
335 choose_from_menu(forum_menu_list($course), 'forumid', '', get_string('allforums', 'forum'), '');
336 echo '</td>';
337 echo '</tr>';
338
e6ae4dc8 339 echo '<tr>';
340 echo '<td class="c0">'.get_string('searchsubject', 'forum').':</td>';
e9ff8e10 341 echo '<td class="c1"><input type="text" size="35" name="subject" value="'.s($subject, true).'" alt="" /></td>';
e6ae4dc8 342 echo '</tr>';
343
344 echo '<tr>';
345 echo '<td class="c0">'.get_string('searchuser', 'forum').':</td>';
e9ff8e10 346 echo '<td class="c1"><input type="text" size="35" name="user" value="'.s($user, true).'" alt="" /></td>';
e6ae4dc8 347 echo '</tr>';
348
349 echo '<tr>';
350 echo '<td class="submit" colspan="2" align="center">';
76b1f3c7 351 echo '<input type="submit" value="'.get_string('searchforums', 'forum').'" alt="" /></td>';
e6ae4dc8 352 echo '</tr>';
353
354 echo '</table>';
355 echo '</form>';
ab00aa12 356
357 echo "<script type=\"text/javascript\">";
d2ce367f 358 echo "lockoptions('searchform','timefromrestrict', timefromitems);";
359 echo "lockoptions('searchform','timetorestrict', timetoitems);";
ab00aa12 360 echo "</script>\n";
361
e6ae4dc8 362 print_simple_box_end();
363}
364
e9ff8e10 365/**
65bcf17b 366 * This function takes each word out of the search string, makes sure they are at least
5cce2e75 367 * two characters long and returns an array containing every good word.
65bcf17b 368 *
5cce2e75 369 * @param string $words String containing space-separated strings to search for
370 * @param string $prefix String to prepend to the each token taken out of $words
65bcf17b 371 * @returns array
5cce2e75 372 * @todo Take the hardcoded limit out of this function and put it into a user-specified parameter
e9ff8e10 373 */
e6ae4dc8 374function forum_clean_search_terms($words, $prefix='') {
375 $searchterms = explode(' ', $words);
376 foreach ($searchterms as $key => $searchterm) {
377 if (strlen($searchterm) < 2) {
378 unset($searchterms[$key]);
379 } else if ($prefix) {
380 $searchterms[$key] = $prefix.$searchterm;
381 }
382 }
383 return trim(implode(' ', $searchterms));
384}
385
e9ff8e10 386/**
387 * @todo Document this function
388 */
77ffdf4b 389function forum_menu_list($course) {
390
391 $menu = array();
77ffdf4b 392
dd97c328 393 if ($cms = get_coursemodules_in_course('forum', $course->id)) {
77ffdf4b 394 if ($course->format == 'weeks') {
395 $strsection = get_string('week');
396 } else {
397 $strsection = get_string('topic');
398 }
399
dd97c328 400 foreach ($cms as $cm) {
401 if (!coursemodule_visible_for_user($cm)) {
402 continue;
77ffdf4b 403 }
dd97c328 404 $menu[$cm->instance] = format_string($cm->name,true);
77ffdf4b 405 }
406 }
407
408 return $menu;
409}
410
0468976c 411?>