7b948d8eede3a8b93472be0b89cc06af84902596
[moodle.git] / search / documents / forum_document.php
1 <?php
3   /* see wiki_document.php for descriptions */
5   require_once("$CFG->dirroot/search/documents/document.php");
6   require_once("$CFG->dirroot/mod/forum/lib.php");
7   
8   class ForumSearchDocument extends SearchDocument {  
9     public function __construct(&$post, $forum_id, $course_id, $group_id) {
10       // generic information     
11       $doc->id        = $post['id'];
12       $doc->title     = $post['subject'];
13       $doc->author    = $post['firstname']." ".$post['lastname'];
14       $doc->contents  = $post['message'];
15       
16       $doc->url       = forum_make_link($post['discussion'], $post['id']);      
17       
18       // module specific information
19       $data->forum      = $forum_id;
20       $data->discussion = $post['discussion'];
21       
22       parent::__construct($doc, $data, SEARCH_TYPE_FORUM, $course_id, $group_id);
23     } //constructor    
24   } //ForumSearchDocument
25   
26   function forum_make_link($discussion_id, $post_id) {
27     global $CFG;
28     return $CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion_id.'#'.$post_id;
29   } //forum_make_link
30   
31   function forum_iterator() {
32       //no @ = Undefined index:  82 in moodle/lib/datalib.php on line 2671    
33       return @get_all_instances_in_courses("forum", get_courses());
34   } //forum_iterator
35   
36   function forum_get_content_for_index(&$forum) {
37       $documents = array();  
38       if (!$forum) return $documents;
39     
40       $posts = forum_get_discussions_fast($forum->id);     
41       if (!$posts) return $documents;
42       
43       while (!$posts->EOF) {
44         $post = $posts->fields;
45         
46         if (is_array($post)) {
47           if (strlen($post['message']) > 0 && ($post['deleted'] != 1)) {
48             $documents[] = new ForumSearchDocument($post, $forum->id, $forum->course, $post['groupid']);
49           } //if
50             
51           if ($children = forum_get_child_posts_fast($post['id'], $forum->id)) {
52             while (!$children->EOF) {
53               $child = $children->fields;
55               if (strlen($child['message']) > 0 && ($child['deleted'] != 1)) {
56                 $documents[] = new ForumSearchDocument($child, $forum->id, $forum->course, $post['groupid']);
57               } //if
59               $children->MoveNext();
60             } //foreach
61           } //if
62         } //if
63         
64         $posts->MoveNext();
65       } //foreach
66             
67       return $documents;
68   } //forum_get_content_for_index
69   
70   //reworked faster version from /mod/forum/lib.php
71   function forum_get_discussions_fast($forum) {
72     global $CFG, $USER;
73     
74     $timelimit='';
75   
76     if (!empty($CFG->forum_enabletimedposts)) {
77       if (!((isadmin() and !empty($CFG->admineditalways)) || isteacher(get_field('forum', 'course', 'id', $forum)))) {
78         $now = time();
79         $timelimit = " AND ((d.timestart = 0 OR d.timestart <= '$now') AND (d.timeend = 0 OR d.timeend > '$now')";
80         if (!empty($USER->id)) {
81           $timelimit .= " OR d.userid = '$USER->id'";
82         }
83         $timelimit .= ')';
84       }
85     }
86   
87     if ($CFG->dbtype == 'postgres7') {
88         return get_recordset_sql("SELECT p.id, p.subject, p.discussion, p.message,
89                                   p.deleted, d.groupid, u.firstname, u.lastname 
90                               FROM {$CFG->prefix}forum_discussions d
91                               JOIN {$CFG->prefix}forum_posts p ON p.discussion = d.id
92                               JOIN {$CFG->prefix}user u ON p.userid = u.id
93                              WHERE d.forum = '$forum'
94                                AND p.parent = 0
95                                    $timelimit
96                           ORDER BY d.timemodified DESC");
97     } else {
98         return get_recordset_sql("SELECT p.id, p.subject, p.discussion, p.message, p.deleted,
99                                   d.groupid, u.firstname, u.lastname
100                               FROM ({$CFG->prefix}forum_posts p,
101                                    {$CFG->prefix}user u,
102                                    {$CFG->prefix}forum_discussions d)
103                              WHERE d.forum = '$forum'
104                                AND p.discussion = d.id
105                                AND p.parent = 0
106                                AND p.userid = u.id $timelimit
107                           ORDER BY d.timemodified DESC");
108     } //else
109   } //forum_get_discussions_fast
110   
111   //reworked faster version from /mod/forum/lib.php
112   function forum_get_child_posts_fast($parent, $forumid) {
113     global $CFG;
114   
115     return get_recordset_sql("SELECT p.id, p.subject, p.discussion, p.message, p.deleted,
116                               $forumid AS forum, u.firstname, u.lastname
117                               FROM {$CFG->prefix}forum_posts p
118                          LEFT JOIN {$CFG->prefix}user u ON p.userid = u.id
119                              WHERE p.parent = '$parent'
120                           ORDER BY p.created ASC");
121   } //forum_get_child_posts_fast
122   
123 ?>