Fixed Bug 3021. Forum ID was getting changed in read tables.
[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
9 $perpage = optional_param('perpage', 20, PARAM_INT); // which page to show
10
11 $user = trim(optional_param('user', '', PARAM_NOTAGS)); // Names to search for
12 $userid = trim(optional_param('userid', 0, PARAM_INT)); // UserID to search for
77ffdf4b 13 $forumid = trim(optional_param('forumid', 0, PARAM_INT)); // ForumID to search for
e6ae4dc8 14 $subject = trim(optional_param('subject', '', PARAM_NOTAGS)); // Subject
15 $phrase = trim(optional_param('phrase', '', PARAM_NOTAGS)); // Phrase
16 $words = trim(optional_param('words', '', PARAM_NOTAGS)); // Words
17 $fullwords = trim(optional_param('fullwords', '', PARAM_NOTAGS)); // Whole words
18 $notwords = trim(optional_param('notwords', '', PARAM_NOTAGS)); // Words we don't want
19
ab00aa12 20 $timefromrestrict = optional_param('timefromrestrict', 0, PARAM_INT); // Use starting date
4e471fc6 21 $fromday = optional_param('fromday', 0, PARAM_INT); // Starting date
22 $frommonth = optional_param('frommonth', 0, PARAM_INT); // Starting date
23 $fromyear = optional_param('fromyear', 0, PARAM_INT); // Starting date
24 $fromhour = optional_param('fromhour', 0, PARAM_INT); // Starting date
25 $fromminute = optional_param('fromminute', 0, PARAM_INT); // Starting date
ab00aa12 26 if ($timefromrestrict) {
4e471fc6 27 $datefrom = make_timestamp($fromyear, $frommonth, $fromday, $fromhour, $fromminute);
28 } else {
29 $datefrom = optional_param('datefrom', 0, PARAM_INT); // Starting date
30 }
31
ab00aa12 32 $timetorestrict = optional_param('timetorestrict', 0, PARAM_INT); // Use ending date
4e471fc6 33 $today = optional_param('today', 0, PARAM_INT); // Ending date
34 $tomonth = optional_param('tomonth', 0, PARAM_INT); // Ending date
35 $toyear = optional_param('toyear', 0, PARAM_INT); // Ending date
36 $tohour = optional_param('tohour', 0, PARAM_INT); // Ending date
37 $tominute = optional_param('tominute', 0, PARAM_INT); // Ending date
ab00aa12 38 if ($timetorestrict) {
4e471fc6 39 $dateto = make_timestamp($toyear, $tomonth, $today, $tohour, $tominute);
40 } else {
41 $dateto = optional_param('datefrom', 0, PARAM_INT); // Ending date
42 }
43
44
e6ae4dc8 45
46 if (empty($search)) { // Check the other parameters instead
47 if (!empty($words)) {
48 $search .= ' '.$words;
49 }
50 if (!empty($userid)) {
51 $search .= ' userid:'.$userid;
52 }
77ffdf4b 53 if (!empty($forumid)) {
54 $search .= ' forumid:'.$forumid;
55 }
e6ae4dc8 56 if (!empty($user)) {
57 $search .= ' '.forum_clean_search_terms($user, 'user:');
58 }
59 if (!empty($subject)) {
60 $search .= ' '.forum_clean_search_terms($subject, 'subject:');
61 }
62 if (!empty($fullwords)) {
63 $search .= ' '.forum_clean_search_terms($fullwords, '+');
64 }
65 if (!empty($notwords)) {
66 $search .= ' '.forum_clean_search_terms($notwords, '-');
67 }
68 if (!empty($phrase)) {
69 $search .= ' "'.$phrase.'"';
70 }
4e471fc6 71 if (!empty($datefrom)) {
72 $search .= ' datefrom:'.$datefrom;
73 }
74 if (!empty($dateto)) {
75 $search .= ' dateto:'.$dateto;
76 }
e6ae4dc8 77 $individualparams = true;
78 } else {
79 $individualparams = false;
80 }
501cdbd8 81
8b9c7aa0 82 if ($search) {
e6ae4dc8 83 $search = forum_clean_search_terms($search);
8b9c7aa0 84 }
85
501cdbd8 86 if (! $course = get_record("course", "id", $id)) {
87 error("Course id is incorrect.");
88 }
89
ec81373f 90 require_course_login($course);
501cdbd8 91
8f0cd6ef 92 add_to_log($course->id, "forum", "search", "search.php?id=$course->id&amp;search=".urlencode($search), $search);
501cdbd8 93
46217d06 94 $strforums = get_string("modulenameplural", "forum");
95 $strsearch = get_string("search", "forum");
96 $strsearchresults = get_string("searchresults", "forum");
8b9c7aa0 97 $strpage = get_string("page");
97485d07 98
1930e959 99 if (!$search) {
f950af3c 100 print_header_simple("$strsearch", "",
e6ae4dc8 101 "<a href=\"index.php?id=$course->id\">$strforums</a> -> $strsearch", 'search.words',
b2432b7f 102 "", "", "&nbsp;", navmenu($course));
680afe2e 103
e6ae4dc8 104 forum_print_big_search_form($course);
105 print_footer($course);
106 exit;
501cdbd8 107 }
108
e6ae4dc8 109/// We need to do a search now and print results
110
77ffdf4b 111 $searchterms = str_replace('forumid:', 'instance:', $search);
112 $searchterms = explode(' ', $searchterms);
e6ae4dc8 113
6f1cc8d6 114 $searchform = forum_search_form($course, $search);
e6ae4dc8 115
116 if ($group = user_group($course->id, $USER->id)) {
117 $groupid = $group->id;
118 } else {
119 $groupid = 0;
120 }
4e471fc6 121
e6ae4dc8 122 if (!$posts = forum_search_posts($searchterms, $course->id, $page*$perpage, $perpage, $totalcount, $groupid)) {
123
124 print_header_simple("$strsearchresults", "",
125 "<a href=\"index.php?id=$course->id\">$strforums</a> ->
7369657c 126 <a href=\"search.php?id=$course->id\">$strsearch</a> -> ".s($search), 'search.words',
e6ae4dc8 127 "", "", "&nbsp;", navmenu($course));
128 print_heading(get_string("nopostscontaining", "forum", $search));
129
130 if (!$individualparams) {
131 $words = $search;
c7a5b3b1 132 }
e6ae4dc8 133
134 forum_print_big_search_form($course);
77ffdf4b 135
136 print_footer($course);
e6ae4dc8 137 exit;
138 }
139
140 print_header_simple("$strsearchresults", "",
141 "<a href=\"index.php?id=$course->id\">$strforums</a> ->
7369657c 142 <a href=\"search.php?id=$course->id\">$strsearch</a> -> ".s($search), '',
e6ae4dc8 143 "", "", $searchform, navmenu($course));
144
145 print_heading("$strsearchresults: $totalcount");
146
147 print_paging_bar($totalcount, $page, $perpage, "search.php?search=$search&amp;id=$course->id&amp;perpage=$perpage&amp;");
148
149 //added to implement highlighting of search terms found only in HTML markup
150 //fiedorow - 9/2/2005
151 $strippedsearch = str_replace('user:','',$search);
152 $strippedsearch = str_replace('subject:','',$strippedsearch);
153 $strippedsearch = str_replace('&quot;','',$strippedsearch);
ab00aa12 154 $searchterms = explode(' ', $strippedsearch); // Search for words independently
e6ae4dc8 155 foreach ($searchterms as $key => $searchterm) {
156 if (preg_match('/^\-/',$searchterm)) {
157 unset($searchterms[$key]);
158 } else {
159 $searchterms[$key] = preg_replace('/^\+/','',$searchterm);
ceca2ad2 160 }
e6ae4dc8 161 }
501cdbd8 162
e6ae4dc8 163 foreach ($posts as $post) {
164
ab00aa12 165 if (! $discussion = get_record('forum_discussions', 'id', $post->discussion)) {
166 error('Discussion ID was incorrect');
e6ae4dc8 167 }
ab00aa12 168 if (! $forum = get_record('forum', 'id', "$discussion->forum")) {
e6ae4dc8 169 error("Could not find forum $discussion->forum");
cdea3a53 170 }
171
e6ae4dc8 172 $post->subject = highlight("$strippedsearch", $post->subject);
173 $discussion->name = highlight("$strippedsearch", $discussion->name);
b800ac5a 174
3849dae8 175 $fullsubject = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
ab00aa12 176 if ($forum->type != 'single') {
c78ac798 177 $fullsubject .= " -> <a href=\"discuss.php?d=$discussion->id\">".format_string($discussion->name,true)."</a>";
e6ae4dc8 178 if ($post->parent != 0) {
17dc3f3c 179 $fullsubject .= " -> <a href=\"discuss.php?d=$post->discussion&amp;parent=$post->id\">".format_string($post->subject,true)."</a>";
ceca2ad2 180 }
e6ae4dc8 181 }
501cdbd8 182
e6ae4dc8 183 $post->subject = $fullsubject;
048ccc47 184
e6ae4dc8 185 /// Add the forum id to the post object - used by read tracking.
186 $post->forum = $forum->id;
8223d271 187
e6ae4dc8 188 //Indicate search terms only found in HTML markup
189 //Use highlight() with nonsense tags to spot search terms in the
190 //actual text content first. fiedorow - 9/2/2005
191 $missing_terms = "";
192 $message = highlight($strippedsearch,format_text($post->message, $post->format, NULL, $course->id),
193 0,'<fgw9sdpq4>','</fgw9sdpq4>');
9044a387 194
e6ae4dc8 195 foreach ($searchterms as $searchterm) {
196 if (preg_match("/$searchterm/i",$message) && !preg_match('/<fgw9sdpq4>'.$searchterm.'<\/fgw9sdpq4>/i',$message)) {
197 $missing_terms .= " $searchterm";
cdea3a53 198 }
e6ae4dc8 199 }
cdea3a53 200
e6ae4dc8 201 $message = str_replace('<fgw9sdpq4>','<span class="highlight">',$message);
202 $message = str_replace('</fgw9sdpq4>','</span>',$message);
501cdbd8 203
e6ae4dc8 204 if ($missing_terms) {
205 $strmissingsearchterms = get_string('missingsearchterms','forum');
206 $post->message = '<p class="highlight2">'.$strmissingsearchterms.' '.$missing_terms.'</p>'.$message;
ab00aa12 207 } else {
208 $post->message = $message;
501cdbd8 209 }
b800ac5a 210
e6ae4dc8 211 $fulllink = "<a href=\"discuss.php?d=$post->discussion#$post->id\">".get_string("postincontext", "forum")."</a>";
212 //search terms already highlighted - fiedorow - 9/2/2005
213 forum_print_post($post, $course->id, false, false, false, false, $fulllink);
214
215 echo "<br />";
501cdbd8 216 }
217
e6ae4dc8 218 print_paging_bar($totalcount, $page, $perpage, "search.php?search=".urlencode($search)."&amp;id=$course->id&amp;perpage=$perpage&amp;");
219
501cdbd8 220 print_footer($course);
221
e6ae4dc8 222
223
224function forum_print_big_search_form($course) {
4e471fc6 225 global $words, $subject, $phrase, $user, $userid, $fullwords, $notwords, $datefrom, $dateto;
e6ae4dc8 226
227 print_simple_box(get_string('searchforumintro', 'forum'), 'center', '', '', 'searchbox', 'intro');
228
229 print_simple_box_start("center");
ab00aa12 230
231 echo "<script type=\"text/javascript\" language=\"javascript\">\n";
232 echo "var timefromitems = ['fromday','frommonth','fromyear','fromhour', 'fromminute'];\n";
233 echo "var timetoitems = ['today','tomonth','toyear','tohour','tominute'];\n";
234 echo "</script>\n";
235
e6ae4dc8 236 echo '<form name="search" action="search.php" method="get">';
237 echo '<input type="hidden" value="'.$course->id.'" name="id" alt="">';
238 echo '<table cellpadding="10" class="searchbox" id="form">';
239
240 echo '<tr>';
241 echo '<td class="c0">'.get_string('searchwords', 'forum').':</td>';
242 echo '<td class="c1"><input type="text" size="35" name="words" value="'.s($words).'" alt=""></td>';
243 echo '</tr>';
244
245 echo '<tr>';
4e471fc6 246 echo '<td class="c0">'.get_string('searchphrase', 'forum').':</td>';
247 echo '<td class="c1"><input type="text" size="35" name="phrase" value="'.s($phrase).'" alt=""></td>';
e6ae4dc8 248 echo '</tr>';
249
250 echo '<tr>';
251 echo '<td class="c0">'.get_string('searchnotwords', 'forum').':</td>';
252 echo '<td class="c1"><input type="text" size="35" name="notwords" value="'.s($notwords).'" alt=""></td>';
253 echo '</tr>';
254
255 echo '<tr>';
4e471fc6 256 echo '<td class="c0">'.get_string('searchfullwords', 'forum').':</td>';
257 echo '<td class="c1"><input type="text" size="35" name="fullwords" value="'.s($fullwords).'" alt=""></td>';
258 echo '</tr>';
259
260 echo '<tr>';
261 echo '<td class="c0">'.get_string('searchdatefrom', 'forum').':</td>';
262 echo '<td class="c1">';
ab00aa12 263 echo '<input name="timefromrestrict" type="checkbox" value="1" alt="'.get_string('searchdatefrom', 'forum').'" onclick="return lockoptions(\'search\', \'timefromrestrict\', timefromitems)" /> ';
4e471fc6 264 if (empty($dateto)) {
265 $datefrom = make_timestamp(2000, 1, 1, 0, 0, 0);
266 }
267 print_date_selector('fromday', 'frommonth', 'fromyear', $datefrom);
268 print_time_selector('fromhour', 'fromminute', $datefrom);
ab00aa12 269
270 echo '<input type="hidden" name="hfromday" value="0" />';
271 echo '<input type="hidden" name="hfrommonth" value="0" />';
272 echo '<input type="hidden" name="hfromyear" value="0" />';
273 echo '<input type="hidden" name="hfromhour" value="0" />';
274 echo '<input type="hidden" name="hfromminute" value="0" />';
275
4e471fc6 276 echo '</td>';
277 echo '</tr>';
278
279 echo '<tr>';
280 echo '<td class="c0">'.get_string('searchdateto', 'forum').':</td>';
281 echo '<td class="c1">';
ab00aa12 282 echo '<input name="timetorestrict" type="checkbox" value="1" alt="'.get_string('searchdateto', 'forum').'" onclick="return lockoptions(\'search\', \'timetorestrict\', timetoitems)" /> ';
4e471fc6 283 if (empty($dateto)) {
284 $dateto = time()+3600;
285 }
286 print_date_selector('today', 'tomonth', 'toyear', $dateto);
287 print_time_selector('tohour', 'tominute', $dateto);
ab00aa12 288
289 echo '<input type="hidden" name="htoday" value="0" />';
290 echo '<input type="hidden" name="htomonth" value="0" />';
291 echo '<input type="hidden" name="htoyear" value="0" />';
292 echo '<input type="hidden" name="htohour" value="0" />';
293 echo '<input type="hidden" name="htominute" value="0" />';
294
4e471fc6 295 echo '</td>';
e6ae4dc8 296 echo '</tr>';
297
77ffdf4b 298 echo '<tr>';
299 echo '<td class="c0">'.get_string('searchwhichforums', 'forum').':</td>';
300 echo '<td class="c1">';
301 choose_from_menu(forum_menu_list($course), 'forumid', '', get_string('allforums', 'forum'), '');
302 echo '</td>';
303 echo '</tr>';
304
e6ae4dc8 305 echo '<tr>';
306 echo '<td class="c0">'.get_string('searchsubject', 'forum').':</td>';
307 echo '<td class="c1"><input type="text" size="35" name="subject" value="'.s($subject).'" alt=""></td>';
308 echo '</tr>';
309
310 echo '<tr>';
311 echo '<td class="c0">'.get_string('searchuser', 'forum').':</td>';
312 echo '<td class="c1"><input type="text" size="35" name="user" value="'.s($user).'" alt=""></td>';
313 echo '</tr>';
314
315 echo '<tr>';
316 echo '<td class="submit" colspan="2" align="center">';
4e471fc6 317 echo '<input type="submit" value="'.get_string('searchforums', 'forum').'" alt=""></td>';
e6ae4dc8 318 echo '</tr>';
319
320 echo '</table>';
321 echo '</form>';
ab00aa12 322
323 echo "<script type=\"text/javascript\">";
324 echo "lockoptions('search','timefromrestrict', timefromitems);";
325 echo "lockoptions('search','timetorestrict', timetoitems);";
326 echo "</script>\n";
327
e6ae4dc8 328 print_simple_box_end();
329}
330
331
332function forum_clean_search_terms($words, $prefix='') {
333 $searchterms = explode(' ', $words);
334 foreach ($searchterms as $key => $searchterm) {
335 if (strlen($searchterm) < 2) {
336 unset($searchterms[$key]);
337 } else if ($prefix) {
338 $searchterms[$key] = $prefix.$searchterm;
339 }
340 }
341 return trim(implode(' ', $searchterms));
342}
343
77ffdf4b 344function forum_menu_list($course) {
345
346 $menu = array();
347
348 $currentgroup = get_current_group($course->id);
349 $isteacher = isteacher($course->id);
350
351 if ($isteacher) { // Add teacher forum
352 if ($forum = forum_get_course_forum($course->id, 'teacher')) {
3849dae8 353 $menu[$forum->id] = format_string($forum->name,true);
77ffdf4b 354 }
355 }
356
357 if ($forums = get_all_instances_in_course("forum", $course)) {
358 if ($course->format == 'weeks') {
359 $strsection = get_string('week');
360 } else {
361 $strsection = get_string('topic');
362 }
363
364 foreach ($forums as $forum) {
365 if (!$isteacher) { // Non-teachers
366 if ($forum->type == "teacher") {
367 continue;
368 }
369 if (!isset($forum->visible)) {
370 if (! instance_is_visible("forum", $forum)) {
371 continue;
372 }
373 }
374 if ($cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) {
375 $groupmode = groupmode($course, $cm); // Groups are being used
376 if (($groupmode == SEPARATEGROUPS) and ($currentgroup === false)) {
377 continue;
378 }
379 }
380 }
381
3849dae8 382 $menu[$forum->id] = format_string($forum->name,true);
77ffdf4b 383 }
384 }
385
386 return $menu;
387}
388
501cdbd8 389?>
390