MDL-10239:
[moodle-linuxchix.git] / mod / forum / search.php
blob228346801be3d3b6ce379431e904c3c743a0b062
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 $navlinks = array();
101 $navlinks[] = array('name' => $strforums, 'link' => "index.php?id=$course->id", 'type' => 'activity');
103 if (!$search || $showform) {
105 $crumns[] = array('name' => $strsearch, 'link' => '', 'type' => 'title');
106 $navigation = build_navigation($navlinks);
108 print_header_simple("$strsearch", "", $navigation, 'search.words',
109 "", "", "&nbsp;", navmenu($course));
111 forum_print_big_search_form($course);
112 print_footer($course);
113 exit;
116 /// We need to do a search now and print results
118 $searchterms = str_replace('forumid:', 'instance:', $search);
119 $searchterms = explode(' ', $searchterms);
121 $searchform = forum_search_form($course, $search);
123 $navlinks = array();
124 $navlinks[] = array('name' => $strsearch, 'link' => "search.php?id=$course->id", 'type' => 'activityinstance');
125 $navlinks[] = array('name' => s($search, true), 'link' => '', 'type' => 'link');
126 $navigation = build_navigation($navlinks);
129 if (!$posts = forum_search_posts($searchterms, $course->id, $page*$perpage, $perpage, $totalcount)) {
130 print_header_simple("$strsearchresults", "", $navigation, 'search.words', "", "", "&nbsp;", navmenu($course));
131 print_heading(get_string("nopostscontaining", "forum", $search));
133 if (!$individualparams) {
134 $words = $search;
137 forum_print_big_search_form($course);
139 print_footer($course);
140 exit;
144 print_header_simple("$strsearchresults", "", $navigation, '', "", "", $searchform, navmenu($course));
146 echo '<div class="reportlink">';
147 echo '<a href="search.php?id='.$course->id.
148 '&amp;user='.urlencode($user).
149 '&amp;userid='.$userid.
150 '&amp;forumid='.$forumid.
151 '&amp;subject='.urlencode($subject).
152 '&amp;phrase='.urlencode($phrase).
153 '&amp;words='.urlencode($words).
154 '&amp;fullwords='.urlencode($fullwords).
155 '&amp;notwords='.urlencode($notwords).
156 '&amp;dateto='.$dateto.
157 '&amp;datefrom='.$datefrom.
158 '&amp;showform=1'.
159 '">'.get_string('advancedsearch','forum').'...</a>';
160 echo '</div>';
162 print_heading("$strsearchresults: $totalcount");
164 print_paging_bar($totalcount, $page, $perpage, "search.php?search=".urlencode(stripslashes($search))."&amp;id=$course->id&amp;perpage=$perpage&amp;");
166 //added to implement highlighting of search terms found only in HTML markup
167 //fiedorow - 9/2/2005
168 $strippedsearch = str_replace('user:','',$search);
169 $strippedsearch = str_replace('subject:','',$strippedsearch);
170 $strippedsearch = str_replace('&quot;','',$strippedsearch);
171 $searchterms = explode(' ', $strippedsearch); // Search for words independently
172 foreach ($searchterms as $key => $searchterm) {
173 if (preg_match('/^\-/',$searchterm)) {
174 unset($searchterms[$key]);
175 } else {
176 $searchterms[$key] = preg_replace('/^\+/','',$searchterm);
179 $strippedsearch = implode(' ', $searchterms); // Rebuild the string
181 foreach ($posts as $post) {
183 // Replace the simple subject with the three items forum name -> thread name -> subject
184 // (if all three are appropriate) each as a link.
185 if (! $discussion = get_record('forum_discussions', 'id', $post->discussion)) {
186 error('Discussion ID was incorrect');
188 if (! $forum = get_record('forum', 'id', "$discussion->forum")) {
189 error("Could not find forum $discussion->forum");
192 $post->subject = highlight($strippedsearch, $post->subject);
193 $discussion->name = highlight($strippedsearch, $discussion->name);
195 $fullsubject = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
196 if ($forum->type != 'single') {
197 $fullsubject .= " -> <a href=\"discuss.php?d=$discussion->id\">".format_string($discussion->name,true)."</a>";
198 if ($post->parent != 0) {
199 $fullsubject .= " -> <a href=\"discuss.php?d=$post->discussion&amp;parent=$post->id\">".format_string($post->subject,true)."</a>";
203 $post->subject = $fullsubject;
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.
208 $missing_terms = "";
210 $options = new object();
211 $options->trusttext = true;
212 $message = highlight($strippedsearch,
213 format_text($post->message, $post->format, $options, $course->id),
214 0, '<fgw9sdpq4>', '</fgw9sdpq4>');
216 foreach ($searchterms as $searchterm) {
217 if (preg_match("/$searchterm/i",$message) && !preg_match('/<fgw9sdpq4>'.$searchterm.'<\/fgw9sdpq4>/i',$message)) {
218 $missing_terms .= " $searchterm";
222 if ($missing_terms) {
223 $strmissingsearchterms = get_string('missingsearchterms','forum');
224 $post->message = '<p class="highlight2">'.$strmissingsearchterms.' '.$missing_terms.'</p>'.$post->message;
227 // Prepare a link to the post in context, to be displayed after the forum post.
228 $fulllink = "<a href=\"discuss.php?d=$post->discussion#p$post->id\">".get_string("postincontext", "forum")."</a>";
230 // Now pring the post.
231 forum_print_post($post, $course->id, false, false, false, false,
232 $fulllink, $strippedsearch, -99, false);
235 print_paging_bar($totalcount, $page, $perpage, "search.php?search=".urlencode(stripslashes($search))."&amp;id=$course->id&amp;perpage=$perpage&amp;");
237 print_footer($course);
242 * @todo Document this function
244 function forum_print_big_search_form($course) {
245 global $CFG, $words, $subject, $phrase, $user, $userid, $fullwords, $notwords, $datefrom, $dateto;
247 print_simple_box(get_string('searchforumintro', 'forum'), 'center', '', '', 'searchbox', 'intro');
249 print_simple_box_start("center");
251 echo "<script type=\"text/javascript\">\n";
252 echo "var timefromitems = ['fromday','frommonth','fromyear','fromhour', 'fromminute'];\n";
253 echo "var timetoitems = ['today','tomonth','toyear','tohour','tominute'];\n";
254 echo "</script>\n";
256 echo '<form id="searchform" action="search.php" method="get">';
257 echo '<table cellpadding="10" class="searchbox" id="form">';
259 echo '<tr>';
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>';
263 echo '</tr>';
265 echo '<tr>';
266 echo '<td class="c0">'.get_string('searchphrase', 'forum').':</td>';
267 echo '<td class="c1"><input type="text" size="35" name="phrase" value="'.s($phrase, true).'" alt="" /></td>';
268 echo '</tr>';
270 echo '<tr>';
271 echo '<td class="c0">'.get_string('searchnotwords', 'forum').':</td>';
272 echo '<td class="c1"><input type="text" size="35" name="notwords" value="'.s($notwords, true).'" alt="" /></td>';
273 echo '</tr>';
275 if ($CFG->dbfamily == 'mysql' || $CFG->dbfamily == 'postgres') {
276 echo '<tr>';
277 echo '<td class="c0">'.get_string('searchfullwords', 'forum').':</td>';
278 echo '<td class="c1"><input type="text" size="35" name="fullwords" value="'.s($fullwords, true).'" alt="" /></td>';
279 echo '</tr>';
282 echo '<tr>';
283 echo '<td class="c0">'.get_string('searchdatefrom', 'forum').':</td>';
284 echo '<td class="c1">';
285 if (empty($datefrom)) {
286 $datefromchecked = '';
287 $datefrom = make_timestamp(2000, 1, 1, 0, 0, 0);
288 }else{
289 $datefromchecked = 'checked="checked"';
292 echo '<input name="timefromrestrict" type="checkbox" value="1" alt="'.get_string('searchdatefrom', 'forum').'" onclick="return lockoptions(\'searchform\', \'timefromrestrict\', timefromitems)" '. $datefromchecked . ' /> ';
293 print_date_selector('fromday', 'frommonth', 'fromyear', $datefrom);
294 print_time_selector('fromhour', 'fromminute', $datefrom);
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" />';
302 echo '</td>';
303 echo '</tr>';
305 echo '<tr>';
306 echo '<td class="c0">'.get_string('searchdateto', 'forum').':</td>';
307 echo '<td class="c1">';
308 if (empty($dateto)) {
309 $datetochecked = '';
310 $dateto = time()+3600;
311 }else{
312 $datetochecked = 'checked="checked"';
315 echo '<input name="timetorestrict" type="checkbox" value="1" alt="'.get_string('searchdateto', 'forum').'" onclick="return lockoptions(\'searchform\', \'timetorestrict\', timetoitems)" ' .$datetochecked. ' /> ';
316 print_date_selector('today', 'tomonth', 'toyear', $dateto);
317 print_time_selector('tohour', 'tominute', $dateto);
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" />';
325 echo '</td>';
326 echo '</tr>';
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>';
335 echo '<tr>';
336 echo '<td class="c0">'.get_string('searchsubject', 'forum').':</td>';
337 echo '<td class="c1"><input type="text" size="35" name="subject" value="'.s($subject, true).'" alt="" /></td>';
338 echo '</tr>';
340 echo '<tr>';
341 echo '<td class="c0">'.get_string('searchuser', 'forum').':</td>';
342 echo '<td class="c1"><input type="text" size="35" name="user" value="'.s($user, true).'" alt="" /></td>';
343 echo '</tr>';
345 echo '<tr>';
346 echo '<td class="submit" colspan="2" align="center">';
347 echo '<input type="submit" value="'.get_string('searchforums', 'forum').'" alt="" /></td>';
348 echo '</tr>';
350 echo '</table>';
351 echo '</form>';
353 echo "<script type=\"text/javascript\">";
354 echo "lockoptions('searchform','timefromrestrict', timefromitems);";
355 echo "lockoptions('searchform','timetorestrict', timetoitems);";
356 echo "</script>\n";
358 print_simple_box_end();
362 * @todo Document this function
364 function 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;
373 return trim(implode(' ', $searchterms));
377 * @todo Document this function
379 function forum_menu_list($course) {
381 $menu = array();
382 $currentgroup = get_and_set_current_group($course, groupmode($course));
384 if ($forums = get_all_instances_in_course("forum", $course)) {
385 if ($course->format == 'weeks') {
386 $strsection = get_string('week');
387 } else {
388 $strsection = get_string('topic');
391 foreach ($forums as $forum) {
392 if ($cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) {
393 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
394 if (!isset($forum->visible)) {
395 if (!instance_is_visible("forum", $forum) &&
396 !has_capability('moodle/course:viewhiddenactivities', $context)) {
397 continue;
400 $groupmode = groupmode($course, $cm); // Groups are being used
401 if ($groupmode == SEPARATEGROUPS && ($currentgroup === false) &&
402 !has_capability('moodle/site:accessallgroups', $context)) {
403 continue;
406 $menu[$forum->id] = format_string($forum->name,true);
410 return $menu;