432cbfd84cfcd7ceaceb3b50adf93d9773991a02
[moodle.git] / mod / forum / search.php
1 <?PHP // $Id$
3     require("../../config.php");
4     require("lib.php");
6     require_variable($id);       // course id
7     optional_variable($search, "");  // user id
9     $search = strip_tags($search);
11     if (! $course = get_record("course", "id", $id)) {
12         error("Course id is incorrect.");
13     }
15     if ($course->category) {
16         require_login($course->id);
17     }
19     add_to_log($course->id, "forum", "search", "search.php?id=$course->id&search=$search", "$search"); 
21     $strforums = get_string("modulenameplural", "forum");
22     $strsearch = get_string("search", "forum");
23     $strsearchresults = get_string("searchresults", "forum");
25     if ($search) {
26         print_header("$course->shortname: $strsearchresults", "$course->fullname",
27                  "<A HREF=\"../../course/view.php?id=$course->id\">$course->shortname</A> -> 
28                   <A HREF=\"index.php?id=$course->id\">$strforums</A> -> 
29                   <A HREF=\"search.php?id=$course->id\">$strsearch</A> -> \"$search\"", "search.search");
30     } else {
31         print_header("$course->shortname: $strsearch", "$course->fullname",
32                  "<A HREF=\"../../course/view.php?id=$course->id\">$course->shortname</A> -> 
33                   <A HREF=\"index.php?id=$course->id\">$strforums</A> -> $strsearch", "search.search");
34     }
36     echo "<DIV ALIGN=CENTER>";
37     forum_print_search_form($course, $search);
38     echo "</DIV>";
40     if ($search) {
41     
42         if (!isteacher($course->id)) {
43             $notteacherforum = "AND f.type <> 'teacher'";
44         } else {
45             $notteacherforum = "";
46         }
48         $posts = get_records_sql("SELECT p.*,u.firstname,u.lastname,u.email,u.picture,u.id as userid 
49                                   FROM forum_posts p, forum_discussions d, user u, forum f
50                                   WHERE message LIKE '%$search%' AND p.user = u.id 
51                                         AND p.discussion = d.id AND d.course = '$course->id' 
52                                         AND d.forum = f.id $notteacherforum
53                                   ORDER BY p.modified DESC LIMIT 0, 50 ");
55         if (!$posts) {
56             print_heading("<BR>No posts found containing \"$search\"");
58         } else {
59             foreach ($posts as $post) {
60                 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
61                     error("Discussion ID was incorrect");
62                 }
63                 if (! $forum = get_record("forum", "id", "$discussion->forum")) {
64                     error("Could not find forum $discussion->forum");
65                 }
67                 $fullsubject = "<A HREF=\"view.php?f=$forum->id\">$forum->name</A>";
68                 if ($forum->type != "single") {
69                     $fullsubject .= " -> <A HREF=\"discuss.php?d=$discussion->id\">$discussion->name</A>";
70                     if ($post->parent != 0) {
71                         $fullsubject .= " -> <A HREF=\"discuss.php?d=$post->discussion&parent=$post->id\">$post->subject</A>";
72                     }
73                 }
75                 $post->subject = $fullsubject;
76                 $post->message = highlight("$search", $post->message);
78                 $fulllink = "<P ALIGN=right><A HREF=\"discuss.php?d=$post->discussion&parent=$post->id\">See this post in context</A></P>";
79                 forum_print_post($post, $course->id, false, false, false, false, $fulllink);
81                 echo "<BR>";
82             }
83         }
84     }
86     print_footer($course);
88 ?>