MDL-11433 Correcting non-null and default value (Default will be NULL)
[moodle-pu.git] / mod / forum / search.php
blob4c5218736907e40b1bb28a9978258ef2bc3130aa
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;
204 $post->subjectnoformat = true;
206 // Identify search terms only found in HTML markup, and add a warning about them to
207 // the start of the message text. However, do not do the highlighting here. forum_print_post
208 // will do it for us later.
209 $missing_terms = "";
211 $options = new object();
212 $options->trusttext = true;
213 $message = highlight($strippedsearch,
214 format_text($post->message, $post->format, $options, $course->id),
215 0, '<fgw9sdpq4>', '</fgw9sdpq4>');
217 foreach ($searchterms as $searchterm) {
218 if (preg_match("/$searchterm/i",$message) && !preg_match('/<fgw9sdpq4>'.$searchterm.'<\/fgw9sdpq4>/i',$message)) {
219 $missing_terms .= " $searchterm";
223 if ($missing_terms) {
224 $strmissingsearchterms = get_string('missingsearchterms','forum');
225 $post->message = '<p class="highlight2">'.$strmissingsearchterms.' '.$missing_terms.'</p>'.$post->message;
228 // Prepare a link to the post in context, to be displayed after the forum post.
229 $fulllink = "<a href=\"discuss.php?d=$post->discussion#p$post->id\">".get_string("postincontext", "forum")."</a>";
231 // Now pring the post.
232 forum_print_post($post, $course->id, false, false, false, false,
233 $fulllink, $strippedsearch, -99, false);
236 print_paging_bar($totalcount, $page, $perpage, "search.php?search=".urlencode(stripslashes($search))."&amp;id=$course->id&amp;perpage=$perpage&amp;");
238 print_footer($course);
243 * @todo Document this function
245 function forum_print_big_search_form($course) {
246 global $CFG, $words, $subject, $phrase, $user, $userid, $fullwords, $notwords, $datefrom, $dateto;
248 print_simple_box(get_string('searchforumintro', 'forum'), 'center', '', '', 'searchbox', 'intro');
250 print_simple_box_start("center");
252 echo "<script type=\"text/javascript\">\n";
253 echo "var timefromitems = ['fromday','frommonth','fromyear','fromhour', 'fromminute'];\n";
254 echo "var timetoitems = ['today','tomonth','toyear','tohour','tominute'];\n";
255 echo "</script>\n";
257 echo '<form id="searchform" action="search.php" method="get">';
258 echo '<table cellpadding="10" class="searchbox" id="form">';
260 echo '<tr>';
261 echo '<td class="c0">'.get_string('searchwords', 'forum').':';
262 echo '<input type="hidden" value="'.$course->id.'" name="id" alt="" /></td>';
263 echo '<td class="c1"><input type="text" size="35" name="words" value="'.s($words, true).'" alt="" /></td>';
264 echo '</tr>';
266 echo '<tr>';
267 echo '<td class="c0">'.get_string('searchphrase', 'forum').':</td>';
268 echo '<td class="c1"><input type="text" size="35" name="phrase" value="'.s($phrase, true).'" alt="" /></td>';
269 echo '</tr>';
271 echo '<tr>';
272 echo '<td class="c0">'.get_string('searchnotwords', 'forum').':</td>';
273 echo '<td class="c1"><input type="text" size="35" name="notwords" value="'.s($notwords, true).'" alt="" /></td>';
274 echo '</tr>';
276 if ($CFG->dbfamily == 'mysql' || $CFG->dbfamily == 'postgres') {
277 echo '<tr>';
278 echo '<td class="c0">'.get_string('searchfullwords', 'forum').':</td>';
279 echo '<td class="c1"><input type="text" size="35" name="fullwords" value="'.s($fullwords, true).'" alt="" /></td>';
280 echo '</tr>';
283 echo '<tr>';
284 echo '<td class="c0">'.get_string('searchdatefrom', 'forum').':</td>';
285 echo '<td class="c1">';
286 if (empty($datefrom)) {
287 $datefromchecked = '';
288 $datefrom = make_timestamp(2000, 1, 1, 0, 0, 0);
289 }else{
290 $datefromchecked = 'checked="checked"';
293 echo '<input name="timefromrestrict" type="checkbox" value="1" alt="'.get_string('searchdatefrom', 'forum').'" onclick="return lockoptions(\'searchform\', \'timefromrestrict\', timefromitems)" '. $datefromchecked . ' /> ';
294 print_date_selector('fromday', 'frommonth', 'fromyear', $datefrom);
295 print_time_selector('fromhour', 'fromminute', $datefrom);
297 echo '<input type="hidden" name="hfromday" value="0" />';
298 echo '<input type="hidden" name="hfrommonth" value="0" />';
299 echo '<input type="hidden" name="hfromyear" value="0" />';
300 echo '<input type="hidden" name="hfromhour" value="0" />';
301 echo '<input type="hidden" name="hfromminute" value="0" />';
303 echo '</td>';
304 echo '</tr>';
306 echo '<tr>';
307 echo '<td class="c0">'.get_string('searchdateto', 'forum').':</td>';
308 echo '<td class="c1">';
309 if (empty($dateto)) {
310 $datetochecked = '';
311 $dateto = time()+3600;
312 }else{
313 $datetochecked = 'checked="checked"';
316 echo '<input name="timetorestrict" type="checkbox" value="1" alt="'.get_string('searchdateto', 'forum').'" onclick="return lockoptions(\'searchform\', \'timetorestrict\', timetoitems)" ' .$datetochecked. ' /> ';
317 print_date_selector('today', 'tomonth', 'toyear', $dateto);
318 print_time_selector('tohour', 'tominute', $dateto);
320 echo '<input type="hidden" name="htoday" value="0" />';
321 echo '<input type="hidden" name="htomonth" value="0" />';
322 echo '<input type="hidden" name="htoyear" value="0" />';
323 echo '<input type="hidden" name="htohour" value="0" />';
324 echo '<input type="hidden" name="htominute" value="0" />';
326 echo '</td>';
327 echo '</tr>';
329 echo '<tr>';
330 echo '<td class="c0">'.get_string('searchwhichforums', 'forum').':</td>';
331 echo '<td class="c1">';
332 choose_from_menu(forum_menu_list($course), 'forumid', '', get_string('allforums', 'forum'), '');
333 echo '</td>';
334 echo '</tr>';
336 echo '<tr>';
337 echo '<td class="c0">'.get_string('searchsubject', 'forum').':</td>';
338 echo '<td class="c1"><input type="text" size="35" name="subject" value="'.s($subject, true).'" alt="" /></td>';
339 echo '</tr>';
341 echo '<tr>';
342 echo '<td class="c0">'.get_string('searchuser', 'forum').':</td>';
343 echo '<td class="c1"><input type="text" size="35" name="user" value="'.s($user, true).'" alt="" /></td>';
344 echo '</tr>';
346 echo '<tr>';
347 echo '<td class="submit" colspan="2" align="center">';
348 echo '<input type="submit" value="'.get_string('searchforums', 'forum').'" alt="" /></td>';
349 echo '</tr>';
351 echo '</table>';
352 echo '</form>';
354 echo "<script type=\"text/javascript\">";
355 echo "lockoptions('searchform','timefromrestrict', timefromitems);";
356 echo "lockoptions('searchform','timetorestrict', timetoitems);";
357 echo "</script>\n";
359 print_simple_box_end();
363 * @todo Document this function
365 function forum_clean_search_terms($words, $prefix='') {
366 $searchterms = explode(' ', $words);
367 foreach ($searchterms as $key => $searchterm) {
368 if (strlen($searchterm) < 2) {
369 unset($searchterms[$key]);
370 } else if ($prefix) {
371 $searchterms[$key] = $prefix.$searchterm;
374 return trim(implode(' ', $searchterms));
378 * @todo Document this function
380 function forum_menu_list($course) {
382 $menu = array();
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 $currentgroup = groups_get_activity_group($cm);
395 if (!isset($forum->visible)) {
396 if (!instance_is_visible("forum", $forum) &&
397 !has_capability('moodle/course:viewhiddenactivities', $context)) {
398 continue;
401 $groupmode = groups_get_activity_groupmode($cm); // Groups are being used
402 if ($groupmode == SEPARATEGROUPS && ($currentgroup === false) &&
403 !has_capability('moodle/site:accessallgroups', $context)) {
404 continue;
407 $menu[$forum->id] = format_string($forum->name,true);
411 return $menu;