Fixed missing tag
[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
ebc3bd2b 6 require_variable($id); // course id
7 optional_variable($search, ""); // search string
b800ac5a 8 optional_variable($page, "0"); // which page to show
9 optional_variable($perpage, "20"); // which page to show
501cdbd8 10
b800ac5a 11 $search = trim(strip_tags($search));
501cdbd8 12
8b9c7aa0 13 if ($search) {
14 $searchterms = explode(" ", $search); // Search for words independently
15 foreach ($searchterms as $key => $searchterm) {
16 if (strlen($searchterm) < 2) {
17 unset($searchterms[$key]);
18 }
19 }
c7563a9c 20 $search = s(trim(implode(" ", $searchterms)));
8b9c7aa0 21 }
22
501cdbd8 23 if (! $course = get_record("course", "id", $id)) {
24 error("Course id is incorrect.");
25 }
26
ec81373f 27 require_course_login($course);
501cdbd8 28
8f0cd6ef 29 add_to_log($course->id, "forum", "search", "search.php?id=$course->id&amp;search=".urlencode($search), $search);
501cdbd8 30
46217d06 31 $strforums = get_string("modulenameplural", "forum");
32 $strsearch = get_string("search", "forum");
33 $strsearchresults = get_string("searchresults", "forum");
8b9c7aa0 34 $strpage = get_string("page");
9044a387 35 $strmissingsearchterms = get_string('missingsearchterms','forum');
46217d06 36
f0b8884d 37 $searchform = forum_print_search_form($course, $search, true, "navbar");
97485d07 38
1930e959 39 if (!$search) {
f950af3c 40 print_header_simple("$strsearch", "",
41905731 41 "<a href=\"index.php?id=$course->id\">$strforums</a> -> $strsearch", "search.search",
b2432b7f 42 "", "", "&nbsp;", navmenu($course));
680afe2e 43
8b9c7aa0 44 print_simple_box_start("center");
45 echo "<center>";
1ede7c03 46 echo "<br />";
8b9c7aa0 47 echo $searchform;
b1267a18 48 echo "<br /><p>";
8b9c7aa0 49 print_string("searchhelp");
b1267a18 50 echo "</p>";
51 echo "</center>";
8b9c7aa0 52 print_simple_box_end();
501cdbd8 53 }
54
501cdbd8 55 if ($search) {
9044a387 56 $strippedsearch = str_replace("user:","",$search);
57 $strippedsearch = str_replace("subject:","",$strippedsearch);
58 $strippedsearch = str_replace("&quot;","",$strippedsearch);
c7a5b3b1 59 if ($group = user_group($id, $USER->id)) {
60 $groupid = $group->id;
61 } else {
62 $groupid = 0;
63 }
64 if (!$posts = forum_search_posts($searchterms, $course->id, $page*$perpage, $perpage, $totalcount, $groupid)) {
cd6f0c1e 65
f950af3c 66 print_header_simple("$strsearchresults", "",
8f0cd6ef 67 "<a href=\"index.php?id=$course->id\">$strforums</a> ->
68 <a href=\"search.php?id=$course->id\">$strsearch</a> -> \"$search\"", "search.search",
b2432b7f 69 "", "", "&nbsp;", navmenu($course));
1930e959 70 print_heading(get_string("nopostscontaining", "forum", $search));
71
72 print_simple_box_start("center");
73 echo "<center>";
74 echo "<br />";
75 echo $searchform;
76 echo "<br /><p>";
77 print_string("searchhelp");
78 echo "</p>";
79 echo "</center>";
80 print_simple_box_end();
ceca2ad2 81 print_footer($course);
82 exit;
83 }
501cdbd8 84
f950af3c 85 print_header_simple("$strsearchresults", "",
8f0cd6ef 86 "<a href=\"index.php?id=$course->id\">$strforums</a> ->
87 <a href=\"search.php?id=$course->id\">$strsearch</a> -> \"$search\"", "search.search",
b2432b7f 88 "", "", $searchform, navmenu($course));
680afe2e 89
8b9c7aa0 90 print_heading("$strsearchresults: $totalcount");
91
92 echo "<center>";
8f0cd6ef 93 print_paging_bar($totalcount, $page, $perpage, "search.php?search=$search&amp;id=$course->id&amp;perpage=$perpage&amp;");
8b9c7aa0 94 echo "</center>";
95
cdea3a53 96 //added to implement highlighting of search terms found only in HTML markup
97 //fiedorow - 9/2/2005
98 $searchterms = explode(" ", $strippedsearch); // Search for words independently
99 foreach ($searchterms as $key => $searchterm) {
100 if (preg_match('/^\-/',$searchterm)) {
101 unset($searchterms[$key]);
102 } else {
9044a387 103 $searchterms[$key] = preg_replace('/^\+/','',$searchterm);
cdea3a53 104 }
105 }
106
ceca2ad2 107 foreach ($posts as $post) {
b800ac5a 108
ceca2ad2 109 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
110 error("Discussion ID was incorrect");
111 }
112 if (! $forum = get_record("forum", "id", "$discussion->forum")) {
113 error("Could not find forum $discussion->forum");
114 }
501cdbd8 115
fe98d35d 116 $post->subject = highlight("$strippedsearch", $post->subject);
117 $discussion->name = highlight("$strippedsearch", $discussion->name);
048ccc47 118
ceca2ad2 119 $fullsubject = "<a href=\"view.php?f=$forum->id\">$forum->name</a>";
120 if ($forum->type != "single") {
121 $fullsubject .= " -> <a href=\"discuss.php?d=$discussion->id\">$discussion->name</a>";
122 if ($post->parent != 0) {
839f2456 123 $fullsubject .= " -> <a href=\"discuss.php?d=$post->discussion&amp;parent=$post->id\">$post->subject</a>";
8223d271 124 }
ceca2ad2 125 }
8223d271 126
ceca2ad2 127 $post->subject = $fullsubject;
501cdbd8 128
f37da850 129 /// Add the forum id to the post object - used by read tracking.
9044a387 130 $post->forum = $forum->id;
f37da850 131
cdea3a53 132 //Indicate search terms only found in HTML markup
133 //Use highlight() with nonsense tags to spot search terms in the
134 //actual text content first.
135 //fiedorow - 9/2/2005
136 $missing_terms = "";
9044a387 137 $message = highlight($strippedsearch,format_text($post->message, $post->format, NULL, $course->id),
138 0,'<fgw9sdpq4>','</fgw9sdpq4>');
cdea3a53 139 foreach ($searchterms as $searchterm) {
140 if (preg_match("/$searchterm/i",$message) && !preg_match('/<fgw9sdpq4>'.$searchterm.'<\/fgw9sdpq4>/i',$message)) {
9044a387 141 $missing_terms .= " $searchterm";
142 }
cdea3a53 143 }
9044a387 144 $message = str_replace('<fgw9sdpq4>','<span class="highlight">',$message);
145 $message = str_replace('</fgw9sdpq4>','</span>',$message);
146
cdea3a53 147 if ($missing_terms) {
9044a387 148 $post->message = '<p class="highlight2">'.$strmissingsearchterms.' '.$missing_terms.'</p>'.$message;
cdea3a53 149 }
cdea3a53 150
839f2456 151 $fulllink = "<a href=\"discuss.php?d=$post->discussion#$post->id\">".get_string("postincontext", "forum")."</a>";
cdea3a53 152 //search terms already highlighted - fiedorow - 9/2/2005
153 forum_print_post($post, $course->id, false, false, false, false, $fulllink);
501cdbd8 154
ceca2ad2 155 echo "<br />";
501cdbd8 156 }
b800ac5a 157
8b9c7aa0 158 echo "<center>";
8f0cd6ef 159 print_paging_bar($totalcount, $page, $perpage, "search.php?search=".urlencode($search)."&amp;id=$course->id&amp;perpage=$perpage&amp;");
8b9c7aa0 160 echo "</center>";
501cdbd8 161 }
162
163 print_footer($course);
164
165?>
166