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