First version of RSS System.
[moodle.git] / mod / forum / rsslib.php
CommitLineData
8adcb49f 1<?PHP // $Id$
2 //This file adds support to rss feeds generation
3
4 //This function is the main entry point to forum
5 //rss feeds generation. Foreach site forum with rss enabled
6 //build one XML rss structure.
7 function forum_rss_feeds() {
8
9 global $CFG;
10
11 $status = true;
12
13 //Check CFG->enablerssfeeds
14 if (empty($CFG->enablerssfeeds)) {
15 //Some debug...
16 if ($CFG->debug > 7) {
17 echo "DISABLED (admin variables)";
18 }
19 //Check CFG->forum_enablerssfeeds
20 } else if (empty($CFG->forum_enablerssfeeds)) {
21 //Some debug...
22 if ($CFG->debug > 7) {
23 echo "DISABLED (module configuration)";
24 }
25 //It's working so we start...
26 } else {
27 //Iterate over all forums
28 if ($forums = get_records("forum")) {
29 foreach ($forums as $forum) {
30 if (!empty($forum->rsstype) && !empty($forum->rssarticles) && $status) {
31 //Some debug...
32 if ($CFG->debug > 7) {
33 echo "ID: $forum->id->";
34 }
35 //Get the XML contents
36 $result = forum_rss_feed($forum);
37 //Save the XML contents to file
38 if (!empty($result)) {
39 $status = rss_save_file("forum",$forum,$result);
40 }
41 //Some debug...
42 if ($CFG->debug > 7) {
43 if (empty($result)) {
44 echo "(empty) ";
45 } else {
46 if (!empty($status)) {
47 echo "OK ";
48 } else {
49 echo "FAIL ";
50 }
51 }
52 }
53 }
54 }
55 }
56 }
57 return $status;
58 }
59
60 //This function return the XML rss contents about the forum record passed as parameter
61 //It returns false if something is wrong
62 function forum_rss_feed($forum) {
63
64 global $CFG;
65
66 $status = true;
67
68 //Check CFG->enablerssfeeds
69 if (empty($CFG->enablerssfeeds)) {
70 //Some debug...
71 if ($CFG->debug > 7) {
72 echo "DISABLED (admin variables)";
73 }
74 //Check CFG->forum_enablerssfeeds
75 } else if (empty($CFG->forum_enablerssfeeds)) {
76 //Some debug...
77 if ($CFG->debug > 7) {
78 echo "DISABLED (module configuration)";
79 }
80 //It's working so we start...
81 } else {
82 //Check the forum has rss activated
83 if (!empty($forum->rsstype) && !empty($forum->rssarticles)) {
84 //Depending of the forum->rsstype, we are going to execute, different sqls
85 if ($forum->rsstype == 1) { //Discussion RSS
86 $items = forum_rss_feed_discussions($forum);
87 } else { //Post RSS
88 $items = forum_rss_feed_posts($forum);
89
90 }
91 //Now, if items, we begin building the structure
92 if (!empty($items)) {
93 //First all rss feeds common headers
94 $header = rss_standard_header($forum->name,
95 $CFG->wwwroot."/mod/forum/view.php?f=".$forum->id,
96 $forum->intro);
97 //Now all the rss items
98 if (!empty($header)) {
99 $articles = rss_add_items($items);
100 }
101 //Now all rss feeds common footers
102 if (!empty($header) && !empty($articles)) {
103 $footer = rss_standard_footer();
104 }
105 //Now, if everything is ok, concatenate it
106 if (!empty($header) && !empty($articles) && !empty($footer)) {
107 $status = $header.$articles.$footer;
108 } else {
109 $status = false;
110 }
111 } else {
112 $status = false;
113 }
114 }
115 }
116 return $status;
117 }
118
119 //This function returns "items" record array to be used to build the rss feed
120 //for a Type=discussions forum
121 function forum_rss_feed_discussions($forum) {
122
123 global $CFG;
124
125 $items = array();
126
127 if ($recs = get_records_sql ("SELECT d.id discussionid,
128 d.name discussionname,
129 u.id userid,
130 u.firstname userfirstname,
131 u.lastname userlastname,
132 p.message postmessage,
133 p.created postcreated
134 FROM {$CFG->prefix}forum_discussions d,
135 {$CFG->prefix}forum_posts p,
136 {$CFG->prefix}user u
137 WHERE d.forum = '$forum->id' AND
138 p.discussion = d.id AND
139 p.parent = 0 AND
140 u.id = p.userid
141 ORDER BY p.created desc")) {
142 //Iterate over each discussion to get forum->rssarticles records
143 $articlesleft = $forum->rssarticles;
144 $item = NULL;
145 $user = NULL;
146 foreach ($recs as $rec) {
147 unset($item);
148 unset($user);
149 $item->title = $rec->discussionname;
150 $user->firstname = $rec->userfirstname;
151 $user->lastname = $rec->userlastname;
152 $item->author = fullname($user);
153 $item->pubdate = $rec->postcreated;
154 $item->link = $CFG->wwwroot."/mod/forum/discuss.php?d=".$rec->discussionid;
155 $item->description = $rec->postmessage;
156 $items[] = $item;
157 $articlesleft--;
158 if ($articlesleft < 1) {
159 break;
160 }
161 }
162 }
163 return $items;
164 }
165
166 //This function returns "items" record array to be used to build the rss feed
167 //for a Type=posts forum
168 function forum_rss_feed_posts($forum) {
169
170 global $CFG;
171
172 $items = array();
173
174 if ($recs = get_records_sql ("SELECT p.id postid,
175 d.id discussionid,
176 d.name discussionname,
177 u.id userid,
178 u.firstname userfirstname,
179 u.lastname userlastname,
180 p.message postmessage,
181 p.created postcreated
182 FROM {$CFG->prefix}forum_discussions d,
183 {$CFG->prefix}forum_posts p,
184 {$CFG->prefix}user u
185 WHERE d.forum = '$forum->id' AND
186 p.discussion = d.id AND
187 u.id = p.userid
188 ORDER BY p.created desc")) {
189 //Iterate over each discussion to get forum->rssarticles records
190 $articlesleft = $forum->rssarticles;
191 $item = NULL;
192 $user = NULL;
193 foreach ($recs as $rec) {
194 unset($item);
195 unset($user);
196 $item->title = $rec->discussionname;
197 $user->firstname = $rec->userfirstname;
198 $user->lastname = $rec->userlastname;
199 $item->author = fullname($user);
200 $item->pubdate = $rec->postcreated;
201 $item->link = $CFG->wwwroot."/mod/forum/discuss.php?d=".$rec->discussionid."&parent=".$rec->postid;
202 $item->description = $rec->postmessage;
203 $items[] = $item;
204 $articlesleft--;
205 if ($articlesleft < 1) {
206 break;
207 }
208 }
209 }
210 return $items;
211 }
212?>