1ee32b5aecb2a5cce91168ebb61f96e29b2c5f78
[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     if ($search) {
22         print_header("$course->shortname: Search Results", "$course->fullname",
23                  "<A HREF=\"../../course/view.php?id=$course->id\">$course->shortname</A> -> 
24                   <A HREF=\"index.php?id=$course->id\">Forums</A> -> 
25                   <A HREF=\"search.php?id=$course->id\">Search</A> -> \"$search\"", "search.search");
26     } else {
27         print_header("$course->shortname: Search", "$course->fullname",
28                  "<A HREF=\"../../course/view.php?id=$course->id\">$course->shortname</A> -> 
29                   <A HREF=\"index.php?id=$course->id\">Forums</A> -> Search", "search.search");
30     }
32     echo "<DIV ALIGN=CENTER>";
33     forum_print_search_form($course, $search);
34     echo "</DIV>";
36     if ($search) {
37     
38         if (!isteacher($course->id)) {
39             $notteacherforum = "AND f.type <> 'teacher'";
40         } else {
41             $notteacherforum = "";
42         }
44         $posts = get_records_sql("SELECT p.*,u.firstname,u.lastname,u.email,u.picture,u.id as userid 
45                                   FROM forum_posts p, forum_discussions d, user u, forum f
46                                   WHERE message LIKE '%$search%' AND p.user = u.id 
47                                         AND p.discussion = d.id AND d.course = '$course->id' 
48                                         AND d.forum = f.id $notteacherforum
49                                   ORDER BY p.modified DESC LIMIT 0, 50 ");
51         if (!$posts) {
52             print_heading("<BR>No posts found containing \"$search\"");
54         } else {
55             foreach ($posts as $post) {
56                 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
57                     error("Discussion ID was incorrect");
58                 }
59                 if (! $forum = get_record("forum", "id", "$discussion->forum")) {
60                     error("Could not find forum $discussion->forum");
61                 }
63                 $fullsubject = "<A HREF=\"view.php?f=$forum->id\">$forum->name</A>";
64                 if ($forum->type != "single") {
65                     $fullsubject .= " -> <A HREF=\"discuss.php?d=$discussion->id\">$discussion->name</A>";
66                     if ($post->parent != 0) {
67                         $fullsubject .= " -> <A HREF=\"discuss.php?d=$post->discussion&parent=$post->id\">$post->subject</A>";
68                     }
69                 }
71                 $post->subject = $fullsubject;
72                 $post->message = highlight("$search", $post->message);
74                 $fulllink = "<P ALIGN=right><A HREF=\"discuss.php?d=$post->discussion&parent=$post->id\">See this post in context</A></P>";
75                 forum_print_post($post, $course->id, false, false, false, false, $fulllink);
77                 echo "<BR>";
78             }
79         }
80     }
82     print_footer($course);
84 ?>