output MDLSITE-1070 Added set methods to custom_menu class as properties are protected
[moodle.git] / mod / forum / search.php
CommitLineData
cd4e6b17 1<?php
2
8f685009
SH
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * @package mod-forum
20 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
cd4e6b17 24require_once('../../config.php');
25require_once('lib.php');
26
27$id = required_param('id', PARAM_INT); // course id
28$search = trim(optional_param('search', '', PARAM_NOTAGS)); // search string
29$page = optional_param('page', 0, PARAM_INT); // which page to show
30$perpage = optional_param('perpage', 10, PARAM_INT); // how many per page
31$showform = optional_param('showform', 0, PARAM_INT); // Just show the form
32
33$user = trim(optional_param('user', '', PARAM_NOTAGS)); // Names to search for
34$userid = trim(optional_param('userid', 0, PARAM_INT)); // UserID to search for
35$forumid = trim(optional_param('forumid', 0, PARAM_INT)); // ForumID to search for
36$subject = trim(optional_param('subject', '', PARAM_NOTAGS)); // Subject
37$phrase = trim(optional_param('phrase', '', PARAM_NOTAGS)); // Phrase
38$words = trim(optional_param('words', '', PARAM_NOTAGS)); // Words
39$fullwords = trim(optional_param('fullwords', '', PARAM_NOTAGS)); // Whole words
40$notwords = trim(optional_param('notwords', '', PARAM_NOTAGS)); // Words we don't want
41
42$timefromrestrict = optional_param('timefromrestrict', 0, PARAM_INT); // Use starting date
43$fromday = optional_param('fromday', 0, PARAM_INT); // Starting date
44$frommonth = optional_param('frommonth', 0, PARAM_INT); // Starting date
45$fromyear = optional_param('fromyear', 0, PARAM_INT); // Starting date
46$fromhour = optional_param('fromhour', 0, PARAM_INT); // Starting date
47$fromminute = optional_param('fromminute', 0, PARAM_INT); // Starting date
48if ($timefromrestrict) {
49 $datefrom = make_timestamp($fromyear, $frommonth, $fromday, $fromhour, $fromminute);
50} else {
51 $datefrom = optional_param('datefrom', 0, PARAM_INT); // Starting date
52}
4e471fc6 53
cd4e6b17 54$timetorestrict = optional_param('timetorestrict', 0, PARAM_INT); // Use ending date
55$today = optional_param('today', 0, PARAM_INT); // Ending date
56$tomonth = optional_param('tomonth', 0, PARAM_INT); // Ending date
57$toyear = optional_param('toyear', 0, PARAM_INT); // Ending date
58$tohour = optional_param('tohour', 0, PARAM_INT); // Ending date
59$tominute = optional_param('tominute', 0, PARAM_INT); // Ending date
60if ($timetorestrict) {
61 $dateto = make_timestamp($toyear, $tomonth, $today, $tohour, $tominute);
62} else {
63 $dateto = optional_param('dateto', 0, PARAM_INT); // Ending date
64}
4e471fc6 65
267aff7f 66$PAGE->set_pagelayout('standard');
cd4e6b17 67$PAGE->set_url($FULLME);
e6ae4dc8 68
cd4e6b17 69if (empty($search)) { // Check the other parameters instead
70 if (!empty($words)) {
71 $search .= ' '.$words;
e6ae4dc8 72 }
cd4e6b17 73 if (!empty($userid)) {
74 $search .= ' userid:'.$userid;
8b9c7aa0 75 }
cd4e6b17 76 if (!empty($forumid)) {
77 $search .= ' forumid:'.$forumid;
78 }
79 if (!empty($user)) {
80 $search .= ' '.forum_clean_search_terms($user, 'user:');
81 }
82 if (!empty($subject)) {
83 $search .= ' '.forum_clean_search_terms($subject, 'subject:');
84 }
85 if (!empty($fullwords)) {
86 $search .= ' '.forum_clean_search_terms($fullwords, '+');
87 }
88 if (!empty($notwords)) {
89 $search .= ' '.forum_clean_search_terms($notwords, '-');
501cdbd8 90 }
cd4e6b17 91 if (!empty($phrase)) {
92 $search .= ' "'.$phrase.'"';
93 }
94 if (!empty($datefrom)) {
95 $search .= ' datefrom:'.$datefrom;
96 }
97 if (!empty($dateto)) {
98 $search .= ' dateto:'.$dateto;
99 }
100 $individualparams = true;
101} else {
102 $individualparams = false;
103}
501cdbd8 104
cd4e6b17 105if ($search) {
106 $search = forum_clean_search_terms($search);
107}
501cdbd8 108
cd4e6b17 109if (!$course = $DB->get_record('course', array('id'=>$id))) {
110 print_error('invalidcourseid');
111}
501cdbd8 112
cd4e6b17 113require_course_login($course);
97485d07 114
cd4e6b17 115add_to_log($course->id, "forum", "search", "search.php?id=$course->id&amp;search=".urlencode($search), $search);
65bcf17b 116
cd4e6b17 117$strforums = get_string("modulenameplural", "forum");
118$strsearch = get_string("search", "forum");
119$strsearchresults = get_string("searchresults", "forum");
120$strpage = get_string("page");
65bcf17b 121
cd4e6b17 122if (!$search || $showform) {
680afe2e 123
a6855934 124 $PAGE->navbar->add($strforums, new moodle_url('/mod/forum/index.php', array('id'=>$course->id)));
b4c07395 125 $PAGE->navbar->add(get_string('advancedsearch', 'forum'));
501cdbd8 126
cd4e6b17 127 $PAGE->set_title($strsearch);
39790bd8 128 $PAGE->set_heading($course->fullname);
cd4e6b17 129 echo $OUTPUT->header();
e6ae4dc8 130
cd4e6b17 131 forum_print_big_search_form($course);
132 echo $OUTPUT->footer();
133 exit;
134}
e6ae4dc8 135
cd4e6b17 136/// We need to do a search now and print results
e6ae4dc8 137
cd4e6b17 138$searchterms = str_replace('forumid:', 'instance:', $search);
139$searchterms = explode(' ', $searchterms);
e6ae4dc8 140
cd4e6b17 141$searchform = forum_search_form($course, $search);
e6ae4dc8 142
a6855934 143$PAGE->navbar->add($strsearch, new moodle_url('/mod/forum/search.php', array('id'=>$course->id)));
cd4e6b17 144$PAGE->navbar->add(s($search, true));
145if (!$posts = forum_search_posts($searchterms, $course->id, $page*$perpage, $perpage, $totalcount)) {
146 $PAGE->set_title($strsearchresults);
39790bd8 147 $PAGE->set_heading($course->fullname);
cd4e6b17 148 echo $OUTPUT->header();
149 echo $OUTPUT->heading(get_string("nopostscontaining", "forum", $search));
77ffdf4b 150
cd4e6b17 151 if (!$individualparams) {
152 $words = $search;
e6ae4dc8 153 }
65bcf17b 154
cd4e6b17 155 forum_print_big_search_form($course);
156
157 echo $OUTPUT->footer();
158 exit;
159}
160
161$PAGE->set_title($strsearchresults);
b4c07395 162$PAGE->set_heading($course->fullname);
cd4e6b17 163$PAGE->set_button($searchform);
164echo $OUTPUT->header();
165echo '<div class="reportlink">';
166echo '<a href="search.php?id='.$course->id.
167 '&amp;user='.urlencode($user).
168 '&amp;userid='.$userid.
169 '&amp;forumid='.$forumid.
170 '&amp;subject='.urlencode($subject).
171 '&amp;phrase='.urlencode($phrase).
172 '&amp;words='.urlencode($words).
173 '&amp;fullwords='.urlencode($fullwords).
174 '&amp;notwords='.urlencode($notwords).
175 '&amp;dateto='.$dateto.
176 '&amp;datefrom='.$datefrom.
177 '&amp;showform=1'.
178 '">'.get_string('advancedsearch','forum').'...</a>';
179echo '</div>';
180
181echo $OUTPUT->heading("$strsearchresults: $totalcount");
182
183$url = new moodle_url('search.php', array('search' => urlencode($search), 'id' => $course->id, 'perpage' => $perpage));
929d7a83 184echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $url);
cd4e6b17 185
186//added to implement highlighting of search terms found only in HTML markup
187//fiedorow - 9/2/2005
188$strippedsearch = str_replace('user:','',$search);
189$strippedsearch = str_replace('subject:','',$strippedsearch);
190$strippedsearch = str_replace('&quot;','',$strippedsearch);
191$searchterms = explode(' ', $strippedsearch); // Search for words independently
192foreach ($searchterms as $key => $searchterm) {
193 if (preg_match('/^\-/',$searchterm)) {
194 unset($searchterms[$key]);
195 } else {
196 $searchterms[$key] = preg_replace('/^\+/','',$searchterm);
e6ae4dc8 197 }
cd4e6b17 198}
199$strippedsearch = implode(' ', $searchterms); // Rebuild the string
501cdbd8 200
cd4e6b17 201foreach ($posts as $post) {
e6ae4dc8 202
cd4e6b17 203 // Replace the simple subject with the three items forum name -> thread name -> subject
204 // (if all three are appropriate) each as a link.
205 if (! $discussion = $DB->get_record('forum_discussions', array('id' => $post->discussion))) {
206 print_error('invaliddiscussionid', 'forum');
207 }
208 if (! $forum = $DB->get_record('forum', array('id' => "$discussion->forum"))) {
209 print_error('invalidforumid', 'forum');
210 }
cdea3a53 211
cd4e6b17 212 if (!$cm = get_coursemodule_from_instance('forum', $forum->id)) {
213 print_error('invalidcoursemodule');
214 }
65bcf17b 215
cd4e6b17 216 $post->subject = highlight($strippedsearch, $post->subject);
217 $discussion->name = highlight($strippedsearch, $discussion->name);
b800ac5a 218
cd4e6b17 219 $fullsubject = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
220 if ($forum->type != 'single') {
221 $fullsubject .= " -> <a href=\"discuss.php?d=$discussion->id\">".format_string($discussion->name,true)."</a>";
222 if ($post->parent != 0) {
223 $fullsubject .= " -> <a href=\"discuss.php?d=$post->discussion&amp;parent=$post->id\">".format_string($post->subject,true)."</a>";
e6ae4dc8 224 }
cd4e6b17 225 }
501cdbd8 226
cd4e6b17 227 $post->subject = $fullsubject;
228 $post->subjectnoformat = true;
048ccc47 229
cd4e6b17 230 // Identify search terms only found in HTML markup, and add a warning about them to
231 // the start of the message text. However, do not do the highlighting here. forum_print_post
232 // will do it for us later.
233 $missing_terms = "";
b343df86 234
39790bd8 235 $options = new stdClass();
cd4e6b17 236 $options->trusted = $post->messagetrust;
237 $message = highlight($strippedsearch,
238 format_text($post->message, $post->messageformat, $options, $course->id),
239 0, '<fgw9sdpq4>', '</fgw9sdpq4>');
9044a387 240
cd4e6b17 241 foreach ($searchterms as $searchterm) {
242 if (preg_match("/$searchterm/i",$message) && !preg_match('/<fgw9sdpq4>'.$searchterm.'<\/fgw9sdpq4>/i',$message)) {
243 $missing_terms .= " $searchterm";
e6ae4dc8 244 }
cd4e6b17 245 }
501cdbd8 246
cd4e6b17 247 if ($missing_terms) {
248 $strmissingsearchterms = get_string('missingsearchterms','forum');
249 $post->message = '<p class="highlight2">'.$strmissingsearchterms.' '.$missing_terms.'</p>'.$post->message;
250 }
b800ac5a 251
cd4e6b17 252 // Prepare a link to the post in context, to be displayed after the forum post.
253 $fulllink = "<a href=\"discuss.php?d=$post->discussion#p$post->id\">".get_string("postincontext", "forum")."</a>";
f2b5d7e3 254
cd4e6b17 255 // Now pring the post.
256 forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false, false,
257 $fulllink, $strippedsearch, -99, false);
258}
501cdbd8 259
929d7a83 260echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $url);
e6ae4dc8 261
cd4e6b17 262echo $OUTPUT->footer();
501cdbd8 263
e6ae4dc8 264
265
e9ff8e10 266/**
267 * @todo Document this function
268 */
e6ae4dc8 269function forum_print_big_search_form($course) {
85ed1699 270 global $CFG, $DB, $words, $subject, $phrase, $user, $userid, $fullwords, $notwords, $datefrom, $dateto, $PAGE, $OUTPUT;
e6ae4dc8 271
9146b979 272 echo $OUTPUT->box(get_string('searchforumintro', 'forum'), 'searchbox boxaligncenter', 'intro');
e6ae4dc8 273
9146b979 274 echo $OUTPUT->box_start('generalbox boxaligncenter');
ab00aa12 275
c17f05fb 276 echo html_writer::script('', $CFG->wwwroot.'/mod/forum/forum.js');
ab00aa12 277
d2ce367f 278 echo '<form id="searchform" action="search.php" method="get">';
e6ae4dc8 279 echo '<table cellpadding="10" class="searchbox" id="form">';
280
281 echo '<tr>';
debd3d62 282 echo '<td class="c0"><label for="words">'.get_string('searchwords', 'forum').'</label>';
e9ff8e10 283 echo '<input type="hidden" value="'.$course->id.'" name="id" alt="" /></td>';
debd3d62 284 echo '<td class="c1"><input type="text" size="35" name="words" id="words"value="'.s($words, true).'" alt="" /></td>';
e6ae4dc8 285 echo '</tr>';
286
287 echo '<tr>';
debd3d62 288 echo '<td class="c0"><label for="phrase">'.get_string('searchphrase', 'forum').'</label></td>';
289 echo '<td class="c1"><input type="text" size="35" name="phrase" id="phrase" value="'.s($phrase, true).'" alt="" /></td>';
e6ae4dc8 290 echo '</tr>';
291
292 echo '<tr>';
debd3d62 293 echo '<td class="c0"><label for="notwords">'.get_string('searchnotwords', 'forum').'</label></td>';
294 echo '<td class="c1"><input type="text" size="35" name="notwords" id="notwords" value="'.s($notwords, true).'" alt="" /></td>';
e6ae4dc8 295 echo '</tr>';
296
68533e53 297 if ($DB->get_dbfamily() == 'mysql' || $DB->get_dbfamily() == 'postgres') {
428aa337 298 echo '<tr>';
debd3d62 299 echo '<td class="c0"><label for="fullwords">'.get_string('searchfullwords', 'forum').'</label></td>';
300 echo '<td class="c1"><input type="text" size="35" name="fullwords" id="fullwords" value="'.s($fullwords, true).'" alt="" /></td>';
428aa337 301 echo '</tr>';
302 }
4e471fc6 303
304 echo '<tr>';
debd3d62 305 echo '<td class="c0">'.get_string('searchdatefrom', 'forum').'</td>';
4e471fc6 306 echo '<td class="c1">';
2c3f3e77 307 if (empty($datefrom)) {
308 $datefromchecked = '';
4e471fc6 309 $datefrom = make_timestamp(2000, 1, 1, 0, 0, 0);
2c3f3e77 310 }else{
311 $datefromchecked = 'checked="checked"';
4e471fc6 312 }
2c3f3e77 313
314 echo '<input name="timefromrestrict" type="checkbox" value="1" alt="'.get_string('searchdatefrom', 'forum').'" onclick="return lockoptions(\'searchform\', \'timefromrestrict\', timefromitems)" '. $datefromchecked . ' /> ';
f83b9b63
PS
315 $selectors = html_writer::select_time('days', 'fromday', $datefrom)
316 . html_writer::select_time('months', 'frommonth', $datefrom)
317 . html_writer::select_time('years', 'fromyear', $datefrom)
318 . html_writer::select_time('hours', 'fromhour', $datefrom)
319 . html_writer::select_time('minutes', 'fromminute', $datefrom);
320 echo $selectors;
ab00aa12 321 echo '<input type="hidden" name="hfromday" value="0" />';
322 echo '<input type="hidden" name="hfrommonth" value="0" />';
323 echo '<input type="hidden" name="hfromyear" value="0" />';
324 echo '<input type="hidden" name="hfromhour" value="0" />';
325 echo '<input type="hidden" name="hfromminute" value="0" />';
326
4e471fc6 327 echo '</td>';
328 echo '</tr>';
329
330 echo '<tr>';
debd3d62 331 echo '<td class="c0">'.get_string('searchdateto', 'forum').'</td>';
4e471fc6 332 echo '<td class="c1">';
333 if (empty($dateto)) {
2c3f3e77 334 $datetochecked = '';
4e471fc6 335 $dateto = time()+3600;
2c3f3e77 336 }else{
337 $datetochecked = 'checked="checked"';
4e471fc6 338 }
2c3f3e77 339
340 echo '<input name="timetorestrict" type="checkbox" value="1" alt="'.get_string('searchdateto', 'forum').'" onclick="return lockoptions(\'searchform\', \'timetorestrict\', timetoitems)" ' .$datetochecked. ' /> ';
f83b9b63
PS
341 $selectors = html_writer::select_time('days', 'fromday', $dateto)
342 . html_writer::select_time('months', 'frommonth', $dateto)
343 . html_writer::select_time('years', 'fromyear', $dateto)
344 . html_writer::select_time('hours', 'fromhour', $dateto)
345 . html_writer::select_time('minutes', 'fromminute', $dateto);
346 echo $selectors;
ab00aa12 347
348 echo '<input type="hidden" name="htoday" value="0" />';
349 echo '<input type="hidden" name="htomonth" value="0" />';
350 echo '<input type="hidden" name="htoyear" value="0" />';
351 echo '<input type="hidden" name="htohour" value="0" />';
352 echo '<input type="hidden" name="htominute" value="0" />';
353
4e471fc6 354 echo '</td>';
e6ae4dc8 355 echo '</tr>';
356
77ffdf4b 357 echo '<tr>';
debd3d62 358 echo '<td class="c0"><label for="menuforumid">'.get_string('searchwhichforums', 'forum').'</label></td>';
77ffdf4b 359 echo '<td class="c1">';
d776d59e 360 echo html_writer::select(forum_menu_list($course), 'forumid', '', array(''=>get_string('allforums', 'forum')));
77ffdf4b 361 echo '</td>';
362 echo '</tr>';
363
e6ae4dc8 364 echo '<tr>';
debd3d62 365 echo '<td class="c0"><label for="subject">'.get_string('searchsubject', 'forum').'</label></td>';
366 echo '<td class="c1"><input type="text" size="35" name="subject" id="subject" value="'.s($subject, true).'" alt="" /></td>';
e6ae4dc8 367 echo '</tr>';
368
369 echo '<tr>';
debd3d62 370 echo '<td class="c0"><label for="user">'.get_string('searchuser', 'forum').'</label></td>';
371 echo '<td class="c1"><input type="text" size="35" name="user" id="user" value="'.s($user, true).'" alt="" /></td>';
e6ae4dc8 372 echo '</tr>';
373
374 echo '<tr>';
375 echo '<td class="submit" colspan="2" align="center">';
76b1f3c7 376 echo '<input type="submit" value="'.get_string('searchforums', 'forum').'" alt="" /></td>';
e6ae4dc8 377 echo '</tr>';
378
379 echo '</table>';
380 echo '</form>';
ab00aa12 381
c17f05fb
PS
382 echo html_writer::script(js_writer::function_call('lockoptions_timetoitems'));
383 echo html_writer::script(js_writer::function_call('lockoptions_timefromitems'));
ab00aa12 384
9146b979 385 echo $OUTPUT->box_end();
e6ae4dc8 386}
387
e9ff8e10 388/**
65bcf17b 389 * This function takes each word out of the search string, makes sure they are at least
5cce2e75 390 * two characters long and returns an array containing every good word.
65bcf17b 391 *
5cce2e75 392 * @param string $words String containing space-separated strings to search for
393 * @param string $prefix String to prepend to the each token taken out of $words
65bcf17b 394 * @returns array
5cce2e75 395 * @todo Take the hardcoded limit out of this function and put it into a user-specified parameter
e9ff8e10 396 */
e6ae4dc8 397function forum_clean_search_terms($words, $prefix='') {
398 $searchterms = explode(' ', $words);
399 foreach ($searchterms as $key => $searchterm) {
400 if (strlen($searchterm) < 2) {
401 unset($searchterms[$key]);
402 } else if ($prefix) {
403 $searchterms[$key] = $prefix.$searchterm;
404 }
405 }
406 return trim(implode(' ', $searchterms));
407}
408
e9ff8e10 409/**
410 * @todo Document this function
411 */
77ffdf4b 412function forum_menu_list($course) {
413
414 $menu = array();
77ffdf4b 415
debd3d62 416 $modinfo = get_fast_modinfo($course);
77ffdf4b 417
debd3d62 418 if (empty($modinfo->instances['forum'])) {
419 return $menu;
420 }
421
422 foreach ($modinfo->instances['forum'] as $cm) {
423 if (!$cm->uservisible) {
424 continue;
425 }
426 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
427 if (!has_capability('mod/forum:viewdiscussion', $context)) {
428 continue;
77ffdf4b 429 }
debd3d62 430 $menu[$cm->instance] = format_string($cm->name);
77ffdf4b 431 }
432
433 return $menu;
434}
435