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