|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00035 function forum_rss_get_feed($context, $args) { 00036 global $CFG, $DB; 00037 00038 $status = true; 00039 00040 //are RSS feeds enabled? 00041 if (empty($CFG->forum_enablerssfeeds)) { 00042 debugging('DISABLED (module configuration)'); 00043 return null; 00044 } 00045 00046 $forumid = clean_param($args[3], PARAM_INT); 00047 $cm = get_coursemodule_from_instance('forum', $forumid, 0, false, MUST_EXIST); 00048 if ($cm) { 00049 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); 00050 00051 //context id from db should match the submitted one 00052 if ($context->id != $modcontext->id || !has_capability('mod/forum:viewdiscussion', $modcontext)) { 00053 return null; 00054 } 00055 } 00056 00057 $forum = $DB->get_record('forum', array('id' => $forumid), '*', MUST_EXIST); 00058 if (!rss_enabled_for_mod('forum', $forum)) { 00059 return null; 00060 } 00061 00062 //the sql that will retreive the data for the feed and be hashed to get the cache filename 00063 $sql = forum_rss_get_sql($forum, $cm); 00064 00065 //hash the sql to get the cache file name 00066 $filename = rss_get_file_name($forum, $sql); 00067 $cachedfilepath = rss_get_file_full_name('mod_forum', $filename); 00068 00069 //Is the cache out of date? 00070 $cachedfilelastmodified = 0; 00071 if (file_exists($cachedfilepath)) { 00072 $cachedfilelastmodified = filemtime($cachedfilepath); 00073 } 00074 //if the cache is more than 60 seconds old and there's new stuff 00075 $dontrecheckcutoff = time()-60; 00076 if ( $dontrecheckcutoff > $cachedfilelastmodified && forum_rss_newstuff($forum, $cm, $cachedfilelastmodified)) { 00077 //need to regenerate the cached version 00078 $result = forum_rss_feed_contents($forum, $sql); 00079 if (!empty($result)) { 00080 $status = rss_save_file('mod_forum',$filename,$result); 00081 } 00082 } 00083 00084 //return the path to the cached version 00085 return $cachedfilepath; 00086 } 00087 00094 function forum_rss_delete_file($forum) { 00095 rss_delete_file('mod_forum', $forum); 00096 } 00097 00099 //Utility functions 00100 00110 function forum_rss_newstuff($forum, $cm, $time) { 00111 global $DB; 00112 00113 $sql = forum_rss_get_sql($forum, $cm, $time); 00114 00115 $recs = $DB->get_records_sql($sql, null, 0, 1);//limit of 1. If we get even 1 back we have new stuff 00116 return ($recs && !empty($recs)); 00117 } 00118 00119 function forum_rss_get_sql($forum, $cm, $time=0) { 00120 $sql = null; 00121 00122 if (!empty($forum->rsstype)) { 00123 if ($forum->rsstype == 1) { //Discussion RSS 00124 $sql = forum_rss_feed_discussions_sql($forum, $cm, $time); 00125 } else { //Post RSS 00126 $sql = forum_rss_feed_posts_sql($forum, $cm, $time); 00127 } 00128 } 00129 00130 return $sql; 00131 } 00132 00133 function forum_rss_feed_discussions_sql($forum, $cm, $newsince=0) { 00134 global $CFG, $DB, $USER; 00135 00136 $timelimit = ''; 00137 00138 $modcontext = null; 00139 00140 $now = round(time(), -2); 00141 $params = array($cm->instance); 00142 00143 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); 00144 00145 if (!empty($CFG->forum_enabletimedposts)) { 00146 if (!has_capability('mod/forum:viewhiddentimedposts', $modcontext)) { 00147 $timelimit = " AND ((d.timestart <= :now1 AND (d.timeend = 0 OR d.timeend > :now2))"; 00148 $params['now1'] = $now; 00149 $params['now2'] = $now; 00150 if (isloggedin()) { 00151 $timelimit .= " OR d.userid = :userid"; 00152 $params['userid'] = $USER->id; 00153 } 00154 $timelimit .= ")"; 00155 } 00156 } 00157 00158 //do we only want new posts? 00159 if ($newsince) { 00160 $newsince = " AND p.modified > '$newsince'"; 00161 } else { 00162 $newsince = ''; 00163 } 00164 00165 //get group enforcing SQL 00166 $groupmode = groups_get_activity_groupmode($cm); 00167 $currentgroup = groups_get_activity_group($cm); 00168 $groupselect = forum_rss_get_group_sql($cm, $groupmode, $currentgroup, $modcontext); 00169 00170 if ($groupmode && $currentgroup) { 00171 $params['groupid'] = $currentgroup; 00172 } 00173 00174 $forumsort = "d.timemodified DESC"; 00175 $postdata = "p.id, p.subject, p.created as postcreated, p.modified, p.discussion, p.userid, p.message as postmessage, p.messageformat AS postformat, p.messagetrust AS posttrust"; 00176 00177 $sql = "SELECT $postdata, d.id as discussionid, d.name as discussionname, d.timemodified, d.usermodified, d.groupid, d.timestart, d.timeend, 00178 u.firstname as userfirstname, u.lastname as userlastname, u.email, u.picture, u.imagealt 00179 FROM {forum_discussions} d 00180 JOIN {forum_posts} p ON p.discussion = d.id 00181 JOIN {user} u ON p.userid = u.id 00182 WHERE d.forum = {$forum->id} AND p.parent = 0 00183 $timelimit $groupselect $newsince 00184 ORDER BY $forumsort"; 00185 return $sql; 00186 } 00187 00188 function forum_rss_feed_posts_sql($forum, $cm, $newsince=0) { 00189 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); 00190 00191 //get group enforcement SQL 00192 $groupmode = groups_get_activity_groupmode($cm); 00193 $currentgroup = groups_get_activity_group($cm); 00194 00195 $groupselect = forum_rss_get_group_sql($cm, $groupmode, $currentgroup, $modcontext); 00196 00197 if ($groupmode && $currentgroup) { 00198 $params['groupid'] = $currentgroup; 00199 } 00200 00201 //do we only want new posts? 00202 if ($newsince) { 00203 $newsince = " AND p.modified > '$newsince'"; 00204 } else { 00205 $newsince = ''; 00206 } 00207 00208 $sql = "SELECT p.id AS postid, 00209 d.id AS discussionid, 00210 d.name AS discussionname, 00211 u.id AS userid, 00212 u.firstname AS userfirstname, 00213 u.lastname AS userlastname, 00214 p.subject AS postsubject, 00215 p.message AS postmessage, 00216 p.created AS postcreated, 00217 p.messageformat AS postformat, 00218 p.messagetrust AS posttrust 00219 FROM {forum_discussions} d, 00220 {forum_posts} p, 00221 {user} u 00222 WHERE d.forum = {$forum->id} AND 00223 p.discussion = d.id AND 00224 u.id = p.userid $newsince 00225 $groupselect 00226 ORDER BY p.created desc"; 00227 00228 return $sql; 00229 } 00230 00231 function forum_rss_get_group_sql($cm, $groupmode, $currentgroup, $modcontext=null) { 00232 $groupselect = ''; 00233 00234 if ($groupmode) { 00235 if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $modcontext)) { 00236 if ($currentgroup) { 00237 $groupselect = "AND (d.groupid = :groupid OR d.groupid = -1)"; 00238 $params['groupid'] = $currentgroup; 00239 } 00240 } else { 00241 //seprate groups without access all 00242 if ($currentgroup) { 00243 $groupselect = "AND (d.groupid = :groupid OR d.groupid = -1)"; 00244 $params['groupid'] = $currentgroup; 00245 } else { 00246 $groupselect = "AND d.groupid = -1"; 00247 } 00248 } 00249 } 00250 00251 return $groupselect; 00252 } 00253 00254 00255 00256 00264 function forum_rss_feed_contents($forum, $sql) { 00265 global $CFG, $DB; 00266 00267 $status = true; 00268 00269 $params = array(); 00270 //$params['forumid'] = $forum->id; 00271 $recs = $DB->get_recordset_sql($sql, $params, 0, $forum->rssarticles); 00272 00273 //set a flag. Are we displaying discussions or posts? 00274 $isdiscussion = true; 00275 if (!empty($forum->rsstype) && $forum->rsstype!=1) { 00276 $isdiscussion = false; 00277 } 00278 00279 $formatoptions = new stdClass(); 00280 $items = array(); 00281 foreach ($recs as $rec) { 00282 $item = new stdClass(); 00283 $user = new stdClass(); 00284 if ($isdiscussion && !empty($rec->discussionname)) { 00285 $item->title = format_string($rec->discussionname); 00286 } else if (!empty($rec->postsubject)) { 00287 $item->title = format_string($rec->postsubject); 00288 } else { 00289 //we should have an item title by now but if we dont somehow then substitute something somewhat meaningful 00290 $item->title = format_string($forum->name.' '.userdate($rec->postcreated,get_string('strftimedatetimeshort', 'langconfig'))); 00291 } 00292 $user->firstname = $rec->userfirstname; 00293 $user->lastname = $rec->userlastname; 00294 $item->author = fullname($user); 00295 $item->pubdate = $rec->postcreated; 00296 if ($isdiscussion) { 00297 $item->link = $CFG->wwwroot."/mod/forum/discuss.php?d=".$rec->discussionid; 00298 } else { 00299 $item->link = $CFG->wwwroot."/mod/forum/discuss.php?d=".$rec->discussionid."&parent=".$rec->postid; 00300 } 00301 00302 $formatoptions->trusted = $rec->posttrust; 00303 $item->description = format_text($rec->postmessage,$rec->postformat,$formatoptions,$forum->course); 00304 00305 //TODO: implement post attachment handling 00306 /*if (!$isdiscussion) { 00307 $post_file_area_name = str_replace('//', '/', "$forum->course/$CFG->moddata/forum/$forum->id/$rec->postid"); 00308 $post_files = get_directory_list("$CFG->dataroot/$post_file_area_name"); 00309 00310 if (!empty($post_files)) { 00311 $item->attachments = array(); 00312 } 00313 }*/ 00314 00315 $items[] = $item; 00316 } 00317 $recs->close(); 00318 00319 00320 if (!empty($items)) { 00321 //First the RSS header 00322 $header = rss_standard_header(strip_tags(format_string($forum->name,true)), 00323 $CFG->wwwroot."/mod/forum/view.php?f=".$forum->id, 00324 format_string($forum->intro,true)); // TODO: fix format 00325 //Now all the rss items 00326 if (!empty($header)) { 00327 $articles = rss_add_items($items); 00328 } 00329 //Now the RSS footer 00330 if (!empty($header) && !empty($articles)) { 00331 $footer = rss_standard_footer(); 00332 } 00333 //Now, if everything is ok, concatenate it 00334 if (!empty($header) && !empty($articles) && !empty($footer)) { 00335 $status = $header.$articles.$footer; 00336 } else { 00337 $status = false; 00338 } 00339 } else { 00340 $status = false; 00341 } 00342 00343 return $status; 00344 }