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);
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);
42 $dateto = optional_param('dateto', 0, PARAM_INT
); // Ending date
47 if (empty($search)) { // Check the other parameters instead
49 $search .= ' '.$words;
51 if (!empty($userid)) {
52 $search .= ' userid:'.$userid;
54 if (!empty($forumid)) {
55 $search .= ' forumid:'.$forumid;
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;
80 $individualparams = false;
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&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 $crumbs[] = array('name' => $strforums, 'link' => "index.php?id=$course->id", 'type' => 'activity');
102 if (!$search ||
$showform) {
104 $crumns[] = array('name' => $strsearch, 'link' => '', 'type' => 'title');
105 $navigation = build_navigation($crumbs);
107 print_header_simple("$strsearch", "", $navigation, 'search.words',
108 "", "", " ", navmenu($course));
110 forum_print_big_search_form($course);
111 print_footer($course);
115 /// We need to do a search now and print results
117 $searchterms = str_replace('forumid:', 'instance:', $search);
118 $searchterms = explode(' ', $searchterms);
120 $searchform = forum_search_form($course, $search);
122 $crumbs[] = array('name' => $strsearch, 'link' => "search.php?id=$course->id", 'type' => 'activityinstance');
123 $crumbs[] = array('name' => s($search, true), 'link' => '', 'type' => 'link');
124 $navigation = build_navigation($crumbs);
127 if (!$posts = forum_search_posts($searchterms, $course->id
, $page*$perpage, $perpage, $totalcount)) {
128 print_header_simple("$strsearchresults", "", $navigation, 'search.words', "", "", " ", navmenu($course));
129 print_heading(get_string("nopostscontaining", "forum", $search));
131 if (!$individualparams) {
135 forum_print_big_search_form($course);
137 print_footer($course);
142 print_header_simple("$strsearchresults", "", $navigation, '', "", "", $searchform, navmenu($course));
144 echo '<div class="reportlink">';
145 echo '<a href="search.php?id='.$course->id
.
146 '&user='.urlencode($user).
147 '&userid='.$userid.
148 '&forumid='.$forumid.
149 '&subject='.urlencode($subject).
150 '&phrase='.urlencode($phrase).
151 '&words='.urlencode($words).
152 '&fullwords='.urlencode($fullwords).
153 '&notwords='.urlencode($notwords).
154 '&dateto='.$dateto.
155 '&datefrom='.$datefrom.
157 '">'.get_string('advancedsearch','forum').'...</a>';
160 print_heading("$strsearchresults: $totalcount");
162 print_paging_bar($totalcount, $page, $perpage, "search.php?search=".urlencode(stripslashes($search))."&id=$course->id&perpage=$perpage&");
164 //added to implement highlighting of search terms found only in HTML markup
165 //fiedorow - 9/2/2005
166 $strippedsearch = str_replace('user:','',$search);
167 $strippedsearch = str_replace('subject:','',$strippedsearch);
168 $strippedsearch = str_replace('"','',$strippedsearch);
169 $searchterms = explode(' ', $strippedsearch); // Search for words independently
170 foreach ($searchterms as $key => $searchterm) {
171 if (preg_match('/^\-/',$searchterm)) {
172 unset($searchterms[$key]);
174 $searchterms[$key] = preg_replace('/^\+/','',$searchterm);
177 $strippedsearch = implode(' ', $searchterms); // Rebuild the string
179 foreach ($posts as $post) {
181 if (! $discussion = get_record('forum_discussions', 'id', $post->discussion
)) {
182 error('Discussion ID was incorrect');
184 if (! $forum = get_record('forum', 'id', "$discussion->forum")) {
185 error("Could not find forum $discussion->forum");
188 $post->subject
= highlight($strippedsearch, $post->subject
);
189 $discussion->name
= highlight($strippedsearch, $discussion->name
);
191 $fullsubject = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name
,true)."</a>";
192 if ($forum->type
!= 'single') {
193 $fullsubject .= " -> <a href=\"discuss.php?d=$discussion->id\">".format_string($discussion->name
,true)."</a>";
194 if ($post->parent
!= 0) {
195 $fullsubject .= " -> <a href=\"discuss.php?d=$post->discussion&parent=$post->id\">".format_string($post->subject
,true)."</a>";
199 $post->subject
= $fullsubject;
201 //Indicate search terms only found in HTML markup
202 //Use highlight() with nonsense tags to spot search terms in the
203 //actual text content first. fiedorow - 9/2/2005
206 // Hack for posts of format FORMAT_PLAIN. Otherwise html tags added by
207 // the highlight() call bellow get stripped out by forum_print_post().
208 if ($post->format
== FORMAT_PLAIN
) {
209 $post->message
= stripslashes_safe($post->message
);
210 $post->message
= rebuildnolinktag($post->message
);
211 $post->message
= str_replace(' ', ' ', $post->message
);
212 $post->message
= nl2br($post->message
);
213 $post->format
= FORMAT_HTML
;
216 $options = new object();
217 $options->trusttext
= true;
218 // detect TRUSTTEXT marker before first call to format_text
219 if (trusttext_present($post->message
)) {
224 $message = highlight($strippedsearch,
225 format_text($post->message
, $post->format
, $options, $course->id
),
226 0, '<fgw9sdpq4>', '</fgw9sdpq4>');
228 foreach ($searchterms as $searchterm) {
229 if (preg_match("/$searchterm/i",$message) && !preg_match('/<fgw9sdpq4>'.$searchterm.'<\/fgw9sdpq4>/i',$message)) {
230 $missing_terms .= " $searchterm";
233 // now is the right time to strip the TRUSTTEXT marker, we will add it later if needed
234 $post->message
= trusttext_strip($post->message
);
236 $message = str_replace('<fgw9sdpq4>','<span class="highlight">',$message);
237 $message = str_replace('</fgw9sdpq4>','</span>',$message);
239 if ($missing_terms) {
240 $strmissingsearchterms = get_string('missingsearchterms','forum');
241 $post->message
= '<p class="highlight2">'.$strmissingsearchterms.' '.$missing_terms.'</p>'.$message;
244 $post->message
= $message;
247 $fulllink = "<a href=\"discuss.php?d=$post->discussion#$post->id\">".get_string("postincontext", "forum")."</a>";
248 //search terms already highlighted - fiedorow - 9/2/2005
249 $SESSION->forum_search
= true;
251 // reconstruct the TRUSTTEXT properly after processing
253 $post->message
= trusttext_mark($post->message
);
255 $post->message
= trusttext_strip($post->message
); //make 100% sure TRUSTTEXT marker was not created during processing
257 forum_print_post($post, $course->id
, false, false, false, false, $fulllink);
258 unset($SESSION->forum_search
);
263 print_paging_bar($totalcount, $page, $perpage, "search.php?search=".urlencode(stripslashes($search))."&id=$course->id&perpage=$perpage&");
265 print_footer($course);
270 * @todo Document this function
272 function forum_print_big_search_form($course) {
273 global $CFG, $words, $subject, $phrase, $user, $userid, $fullwords, $notwords, $datefrom, $dateto;
275 print_simple_box(get_string('searchforumintro', 'forum'), 'center', '', '', 'searchbox', 'intro');
277 print_simple_box_start("center");
279 echo "<script type=\"text/javascript\">\n";
280 echo "var timefromitems = ['fromday','frommonth','fromyear','fromhour', 'fromminute'];\n";
281 echo "var timetoitems = ['today','tomonth','toyear','tohour','tominute'];\n";
284 echo '<form id="searchform" action="search.php" method="get">';
285 echo '<table cellpadding="10" class="searchbox" id="form">';
288 echo '<td class="c0">'.get_string('searchwords', 'forum').':';
289 echo '<input type="hidden" value="'.$course->id
.'" name="id" alt="" /></td>';
290 echo '<td class="c1"><input type="text" size="35" name="words" value="'.s($words, true).'" alt="" /></td>';
294 echo '<td class="c0">'.get_string('searchphrase', 'forum').':</td>';
295 echo '<td class="c1"><input type="text" size="35" name="phrase" value="'.s($phrase, true).'" alt="" /></td>';
299 echo '<td class="c0">'.get_string('searchnotwords', 'forum').':</td>';
300 echo '<td class="c1"><input type="text" size="35" name="notwords" value="'.s($notwords, true).'" alt="" /></td>';
303 if ($CFG->dbfamily
== 'mysql' ||
$CFG->dbfamily
== 'postgres') {
305 echo '<td class="c0">'.get_string('searchfullwords', 'forum').':</td>';
306 echo '<td class="c1"><input type="text" size="35" name="fullwords" value="'.s($fullwords, true).'" alt="" /></td>';
311 echo '<td class="c0">'.get_string('searchdatefrom', 'forum').':</td>';
312 echo '<td class="c1">';
313 if (empty($datefrom)) {
314 $datefromchecked = '';
315 $datefrom = make_timestamp(2000, 1, 1, 0, 0, 0);
317 $datefromchecked = 'checked="checked"';
320 echo '<input name="timefromrestrict" type="checkbox" value="1" alt="'.get_string('searchdatefrom', 'forum').'" onclick="return lockoptions(\'searchform\', \'timefromrestrict\', timefromitems)" '. $datefromchecked . ' /> ';
321 print_date_selector('fromday', 'frommonth', 'fromyear', $datefrom);
322 print_time_selector('fromhour', 'fromminute', $datefrom);
324 echo '<input type="hidden" name="hfromday" value="0" />';
325 echo '<input type="hidden" name="hfrommonth" value="0" />';
326 echo '<input type="hidden" name="hfromyear" value="0" />';
327 echo '<input type="hidden" name="hfromhour" value="0" />';
328 echo '<input type="hidden" name="hfromminute" value="0" />';
334 echo '<td class="c0">'.get_string('searchdateto', 'forum').':</td>';
335 echo '<td class="c1">';
336 if (empty($dateto)) {
338 $dateto = time()+
3600;
340 $datetochecked = 'checked="checked"';
343 echo '<input name="timetorestrict" type="checkbox" value="1" alt="'.get_string('searchdateto', 'forum').'" onclick="return lockoptions(\'searchform\', \'timetorestrict\', timetoitems)" ' .$datetochecked. ' /> ';
344 print_date_selector('today', 'tomonth', 'toyear', $dateto);
345 print_time_selector('tohour', 'tominute', $dateto);
347 echo '<input type="hidden" name="htoday" value="0" />';
348 echo '<input type="hidden" name="htomonth" value="0" />';
349 echo '<input type="hidden" name="htoyear" value="0" />';
350 echo '<input type="hidden" name="htohour" value="0" />';
351 echo '<input type="hidden" name="htominute" value="0" />';
357 echo '<td class="c0">'.get_string('searchwhichforums', 'forum').':</td>';
358 echo '<td class="c1">';
359 choose_from_menu(forum_menu_list($course), 'forumid', '', get_string('allforums', 'forum'), '');
364 echo '<td class="c0">'.get_string('searchsubject', 'forum').':</td>';
365 echo '<td class="c1"><input type="text" size="35" name="subject" value="'.s($subject, true).'" alt="" /></td>';
369 echo '<td class="c0">'.get_string('searchuser', 'forum').':</td>';
370 echo '<td class="c1"><input type="text" size="35" name="user" value="'.s($user, true).'" alt="" /></td>';
374 echo '<td class="submit" colspan="2" align="center">';
375 echo '<input type="submit" value="'.get_string('searchforums', 'forum').'" alt="" /></td>';
381 echo "<script type=\"text/javascript\">";
382 echo "lockoptions('searchform','timefromrestrict', timefromitems);";
383 echo "lockoptions('searchform','timetorestrict', timetoitems);";
386 print_simple_box_end();
390 * @todo Document this function
392 function forum_clean_search_terms($words, $prefix='') {
393 $searchterms = explode(' ', $words);
394 foreach ($searchterms as $key => $searchterm) {
395 if (strlen($searchterm) < 2) {
396 unset($searchterms[$key]);
397 } else if ($prefix) {
398 $searchterms[$key] = $prefix.$searchterm;
401 return trim(implode(' ', $searchterms));
405 * @todo Document this function
407 function forum_menu_list($course) {
410 $currentgroup = get_current_group($course->id
);
412 if ($forums = get_all_instances_in_course("forum", $course)) {
413 if ($course->format
== 'weeks') {
414 $strsection = get_string('week');
416 $strsection = get_string('topic');
419 foreach ($forums as $forum) {
420 if ($cm = get_coursemodule_from_instance('forum', $forum->id
, $course->id
)) {
421 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
422 if (!isset($forum->visible
)) {
423 if (!instance_is_visible("forum", $forum) &&
424 !has_capability('moodle/course:viewhiddenactivities', $context)) {
428 $groupmode = groupmode($course, $cm); // Groups are being used
429 if ($groupmode == SEPARATEGROUPS
&& ($currentgroup === false) &&
430 !has_capability('moodle/site:accessallgroups', $context)) {
434 $menu[$forum->id
] = format_string($forum->name
,true);