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