mod-folder MDL-19799 Added set_url calls, replaced deprecated functions
[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
579d45b4 87 if (!$course = $DB->get_record('course', array('id'=>$id))) {
dde78cb8 88 print_error('invalidcourseid');
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
15ca5e5e 102 $PAGE->navbar->add($strforums, new moodle_url($CFG->wwwroot.'/mod/forum/index.php', array('id'=>$course->id)));
103 $PAGE->navbar->add($strsearch);
65bcf17b 104
15ca5e5e 105 $PAGE->set_title($strsearch);
106 $PAGE->set_focuscontrol('search.words');
107 echo $OUTPUT->header();
680afe2e 108
e6ae4dc8 109 forum_print_big_search_form($course);
396fb912 110 echo $OUTPUT->footer();
e6ae4dc8 111 exit;
501cdbd8 112 }
113
e6ae4dc8 114/// We need to do a search now and print results
115
77ffdf4b 116 $searchterms = str_replace('forumid:', 'instance:', $search);
117 $searchterms = explode(' ', $searchterms);
e6ae4dc8 118
6f1cc8d6 119 $searchform = forum_search_form($course, $search);
e6ae4dc8 120
15ca5e5e 121 $PAGE->navbar->add($strsearch, new moodle_url($CFG->wwwroot.'/mod/forum/search.php', array('id'=>$course->id)));
122 $PAGE->navbar->add(s($search, true));
e3f58dfb 123 if (!$posts = forum_search_posts($searchterms, $course->id, $page*$perpage, $perpage, $totalcount)) {
15ca5e5e 124 $PAGE->set_title($strsearchresults);
125 $PAGE->set_focuscontrol('search.words');
126 echo $OUTPUT->header();
f43f1834 127 echo $OUTPUT->heading(get_string("nopostscontaining", "forum", $search));
e6ae4dc8 128
129 if (!$individualparams) {
130 $words = $search;
c7a5b3b1 131 }
e6ae4dc8 132
133 forum_print_big_search_form($course);
77ffdf4b 134
396fb912 135 echo $OUTPUT->footer();
e6ae4dc8 136 exit;
137 }
65bcf17b 138
15ca5e5e 139 $PAGE->set_title($strsearchresults);
140 $PAGE->set_button($searchform);
141 echo $OUTPUT->header();
10b9a26a 142 echo '<div class="reportlink">';
143 echo '<a href="search.php?id='.$course->id.
144 '&amp;user='.urlencode($user).
145 '&amp;userid='.$userid.
146 '&amp;forumid='.$forumid.
147 '&amp;subject='.urlencode($subject).
148 '&amp;phrase='.urlencode($phrase).
149 '&amp;words='.urlencode($words).
150 '&amp;fullwords='.urlencode($fullwords).
151 '&amp;notwords='.urlencode($notwords).
152 '&amp;dateto='.$dateto.
153 '&amp;datefrom='.$datefrom.
154 '&amp;showform=1'.
155 '">'.get_string('advancedsearch','forum').'...</a>';
156 echo '</div>';
157
f43f1834 158 echo $OUTPUT->heading("$strsearchresults: $totalcount");
4e412c45 159
160 $url = new moodle_url('search.php', array('search' => urlencode($search), 'id' => $course->id, 'perpage' => $perpage));
161 echo $OUTPUT->paging_bar(moodle_paging_bar::make($totalcount, $page, $perpage, $url));
e6ae4dc8 162
163 //added to implement highlighting of search terms found only in HTML markup
164 //fiedorow - 9/2/2005
165 $strippedsearch = str_replace('user:','',$search);
166 $strippedsearch = str_replace('subject:','',$strippedsearch);
167 $strippedsearch = str_replace('&quot;','',$strippedsearch);
ab00aa12 168 $searchterms = explode(' ', $strippedsearch); // Search for words independently
e6ae4dc8 169 foreach ($searchterms as $key => $searchterm) {
170 if (preg_match('/^\-/',$searchterm)) {
171 unset($searchterms[$key]);
172 } else {
173 $searchterms[$key] = preg_replace('/^\+/','',$searchterm);
ceca2ad2 174 }
e6ae4dc8 175 }
a8d7ea4d 176 $strippedsearch = implode(' ', $searchterms); // Rebuild the string
501cdbd8 177
e6ae4dc8 178 foreach ($posts as $post) {
179
a7a89738 180 // Replace the simple subject with the three items forum name -> thread name -> subject
65bcf17b 181 // (if all three are appropriate) each as a link.
4e445355 182 if (! $discussion = $DB->get_record('forum_discussions', array('id' => $post->discussion))) {
dde78cb8 183 print_error('invaliddiscussionid', 'forum');
e6ae4dc8 184 }
4e445355 185 if (! $forum = $DB->get_record('forum', array('id' => "$discussion->forum"))) {
dde78cb8 186 print_error('invalidforumid', 'forum');
cdea3a53 187 }
188
65bcf17b 189 if (!$cm = get_coursemodule_from_instance('forum', $forum->id)) {
dde78cb8 190 print_error('invalidcoursemodule');
65bcf17b 191 }
192
a8d7ea4d 193 $post->subject = highlight($strippedsearch, $post->subject);
194 $discussion->name = highlight($strippedsearch, $discussion->name);
b800ac5a 195
3849dae8 196 $fullsubject = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
ab00aa12 197 if ($forum->type != 'single') {
c78ac798 198 $fullsubject .= " -> <a href=\"discuss.php?d=$discussion->id\">".format_string($discussion->name,true)."</a>";
e6ae4dc8 199 if ($post->parent != 0) {
17dc3f3c 200 $fullsubject .= " -> <a href=\"discuss.php?d=$post->discussion&amp;parent=$post->id\">".format_string($post->subject,true)."</a>";
ceca2ad2 201 }
e6ae4dc8 202 }
501cdbd8 203
e6ae4dc8 204 $post->subject = $fullsubject;
5b1059bb 205 $post->subjectnoformat = true;
048ccc47 206
a7a89738 207 // Identify search terms only found in HTML markup, and add a warning about them to
208 // the start of the message text. However, do not do the highlighting here. forum_print_post
209 // will do it for us later.
e6ae4dc8 210 $missing_terms = "";
b343df86 211
f2b5d7e3 212 $options = new object();
e2d7687f 213 $options->trusted = $post->messagetrust;
b343df86 214 $message = highlight($strippedsearch,
e2d7687f 215 format_text($post->message, $post->messageformat, $options, $course->id),
b343df86 216 0, '<fgw9sdpq4>', '</fgw9sdpq4>');
9044a387 217
e6ae4dc8 218 foreach ($searchterms as $searchterm) {
219 if (preg_match("/$searchterm/i",$message) && !preg_match('/<fgw9sdpq4>'.$searchterm.'<\/fgw9sdpq4>/i',$message)) {
220 $missing_terms .= " $searchterm";
cdea3a53 221 }
e6ae4dc8 222 }
501cdbd8 223
e6ae4dc8 224 if ($missing_terms) {
225 $strmissingsearchterms = get_string('missingsearchterms','forum');
a7a89738 226 $post->message = '<p class="highlight2">'.$strmissingsearchterms.' '.$missing_terms.'</p>'.$post->message;
501cdbd8 227 }
b800ac5a 228
a7a89738 229 // Prepare a link to the post in context, to be displayed after the forum post.
850e7ac5 230 $fulllink = "<a href=\"discuss.php?d=$post->discussion#p$post->id\">".get_string("postincontext", "forum")."</a>";
f2b5d7e3 231
a7a89738 232 // Now pring the post.
f4c0cb7c 233 forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false, false,
a7a89738 234 $fulllink, $strippedsearch, -99, false);
501cdbd8 235 }
236
4e412c45 237 echo $OUTPUT->paging_bar(moodle_paging_bar::make($totalcount, $page, $perpage, $url));
e6ae4dc8 238
396fb912 239 echo $OUTPUT->footer();
501cdbd8 240
e6ae4dc8 241
242
e9ff8e10 243/**
244 * @todo Document this function
245 */
e6ae4dc8 246function forum_print_big_search_form($course) {
85ed1699 247 global $CFG, $DB, $words, $subject, $phrase, $user, $userid, $fullwords, $notwords, $datefrom, $dateto, $PAGE, $OUTPUT;
e6ae4dc8 248
9146b979 249 echo $OUTPUT->box(get_string('searchforumintro', 'forum'), 'searchbox boxaligncenter', 'intro');
e6ae4dc8 250
9146b979 251 echo $OUTPUT->box_start('generalbox boxaligncenter');
ab00aa12 252
970df3e0 253 echo $PAGE->requires->js('mod/forum/forum.js')->asap();
ab00aa12 254
d2ce367f 255 echo '<form id="searchform" action="search.php" method="get">';
e6ae4dc8 256 echo '<table cellpadding="10" class="searchbox" id="form">';
257
258 echo '<tr>';
debd3d62 259 echo '<td class="c0"><label for="words">'.get_string('searchwords', 'forum').'</label>';
e9ff8e10 260 echo '<input type="hidden" value="'.$course->id.'" name="id" alt="" /></td>';
debd3d62 261 echo '<td class="c1"><input type="text" size="35" name="words" id="words"value="'.s($words, true).'" alt="" /></td>';
e6ae4dc8 262 echo '</tr>';
263
264 echo '<tr>';
debd3d62 265 echo '<td class="c0"><label for="phrase">'.get_string('searchphrase', 'forum').'</label></td>';
266 echo '<td class="c1"><input type="text" size="35" name="phrase" id="phrase" value="'.s($phrase, true).'" alt="" /></td>';
e6ae4dc8 267 echo '</tr>';
268
269 echo '<tr>';
debd3d62 270 echo '<td class="c0"><label for="notwords">'.get_string('searchnotwords', 'forum').'</label></td>';
271 echo '<td class="c1"><input type="text" size="35" name="notwords" id="notwords" value="'.s($notwords, true).'" alt="" /></td>';
e6ae4dc8 272 echo '</tr>';
273
68533e53 274 if ($DB->get_dbfamily() == 'mysql' || $DB->get_dbfamily() == 'postgres') {
428aa337 275 echo '<tr>';
debd3d62 276 echo '<td class="c0"><label for="fullwords">'.get_string('searchfullwords', 'forum').'</label></td>';
277 echo '<td class="c1"><input type="text" size="35" name="fullwords" id="fullwords" value="'.s($fullwords, true).'" alt="" /></td>';
428aa337 278 echo '</tr>';
279 }
4e471fc6 280
281 echo '<tr>';
debd3d62 282 echo '<td class="c0">'.get_string('searchdatefrom', 'forum').'</td>';
4e471fc6 283 echo '<td class="c1">';
2c3f3e77 284 if (empty($datefrom)) {
285 $datefromchecked = '';
4e471fc6 286 $datefrom = make_timestamp(2000, 1, 1, 0, 0, 0);
2c3f3e77 287 }else{
288 $datefromchecked = 'checked="checked"';
4e471fc6 289 }
2c3f3e77 290
291 echo '<input name="timefromrestrict" type="checkbox" value="1" alt="'.get_string('searchdatefrom', 'forum').'" onclick="return lockoptions(\'searchform\', \'timefromrestrict\', timefromitems)" '. $datefromchecked . ' /> ';
7b1f2c82 292 $selectors = html_select::make_time_selectors(array('days' => 'fromday','months' => 'frommonth', 'years' => 'fromyear', 'hours' => 'fromhour', 'minutes' => 'fromminute'), $datefrom);
85ed1699 293 foreach ($selectors as $select) {
294 echo $OUTPUT->select($select);
295 }
ab00aa12 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>';
debd3d62 306 echo '<td class="c0">'.get_string('searchdateto', 'forum').'</td>';
4e471fc6 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. ' /> ';
7b1f2c82 316 $selectors = html_select::make_time_selectors(array('days' => 'today','months' => 'tomonth', 'years' => 'toyear', 'hours' => 'tohour', 'minutes' => 'tominute'), $dateto);
db056cf2 317 foreach ($selectors as $select) {
318 echo $OUTPUT->select($select);
319 }
ab00aa12 320
321 echo '<input type="hidden" name="htoday" value="0" />';
322 echo '<input type="hidden" name="htomonth" value="0" />';
323 echo '<input type="hidden" name="htoyear" value="0" />';
324 echo '<input type="hidden" name="htohour" value="0" />';
325 echo '<input type="hidden" name="htominute" value="0" />';
326
4e471fc6 327 echo '</td>';
e6ae4dc8 328 echo '</tr>';
329
77ffdf4b 330 echo '<tr>';
debd3d62 331 echo '<td class="c0"><label for="menuforumid">'.get_string('searchwhichforums', 'forum').'</label></td>';
77ffdf4b 332 echo '<td class="c1">';
b46eec3b 333 echo $OUTPUT->select(html_select::make(forum_menu_list($course), 'forumid', '', get_string('allforums', 'forum')));
77ffdf4b 334 echo '</td>';
335 echo '</tr>';
336
e6ae4dc8 337 echo '<tr>';
debd3d62 338 echo '<td class="c0"><label for="subject">'.get_string('searchsubject', 'forum').'</label></td>';
339 echo '<td class="c1"><input type="text" size="35" name="subject" id="subject" value="'.s($subject, true).'" alt="" /></td>';
e6ae4dc8 340 echo '</tr>';
341
342 echo '<tr>';
debd3d62 343 echo '<td class="c0"><label for="user">'.get_string('searchuser', 'forum').'</label></td>';
344 echo '<td class="c1"><input type="text" size="35" name="user" id="user" value="'.s($user, true).'" alt="" /></td>';
e6ae4dc8 345 echo '</tr>';
346
347 echo '<tr>';
348 echo '<td class="submit" colspan="2" align="center">';
76b1f3c7 349 echo '<input type="submit" value="'.get_string('searchforums', 'forum').'" alt="" /></td>';
e6ae4dc8 350 echo '</tr>';
351
352 echo '</table>';
353 echo '</form>';
ab00aa12 354
002575ad 355 echo $PAGE->requires->js_function_call('lockoptions_timetoitems')->asap();
356 echo $PAGE->requires->js_function_call('lockoptions_timefromitems')->asap();
ab00aa12 357
9146b979 358 echo $OUTPUT->box_end();
e6ae4dc8 359}
360
e9ff8e10 361/**
65bcf17b 362 * This function takes each word out of the search string, makes sure they are at least
5cce2e75 363 * two characters long and returns an array containing every good word.
65bcf17b 364 *
5cce2e75 365 * @param string $words String containing space-separated strings to search for
366 * @param string $prefix String to prepend to the each token taken out of $words
65bcf17b 367 * @returns array
5cce2e75 368 * @todo Take the hardcoded limit out of this function and put it into a user-specified parameter
e9ff8e10 369 */
e6ae4dc8 370function forum_clean_search_terms($words, $prefix='') {
371 $searchterms = explode(' ', $words);
372 foreach ($searchterms as $key => $searchterm) {
373 if (strlen($searchterm) < 2) {
374 unset($searchterms[$key]);
375 } else if ($prefix) {
376 $searchterms[$key] = $prefix.$searchterm;
377 }
378 }
379 return trim(implode(' ', $searchterms));
380}
381
e9ff8e10 382/**
383 * @todo Document this function
384 */
77ffdf4b 385function forum_menu_list($course) {
386
387 $menu = array();
77ffdf4b 388
debd3d62 389 $modinfo = get_fast_modinfo($course);
77ffdf4b 390
debd3d62 391 if (empty($modinfo->instances['forum'])) {
392 return $menu;
393 }
394
395 foreach ($modinfo->instances['forum'] as $cm) {
396 if (!$cm->uservisible) {
397 continue;
398 }
399 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
400 if (!has_capability('mod/forum:viewdiscussion', $context)) {
401 continue;
77ffdf4b 402 }
debd3d62 403 $menu[$cm->instance] = format_string($cm->name);
77ffdf4b 404 }
405
406 return $menu;
407}
408
0468976c 409?>