|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 require_once($CFG->dirroot.'/lib/rsslib.php'); 00004 require_once($CFG->dirroot .'/blog/lib.php'); 00005 00006 function blog_rss_get_url($contextid, $userid, $filtertype, $filterselect=0, $tagid=0) { 00007 $componentname = 'blog'; 00008 00009 $additionalargs = null; 00010 switch ($filtertype) { 00011 case 'site': 00012 $additionalargs = 'site/'.SITEID; 00013 break; 00014 case 'course': 00015 $additionalargs = 'course/'.$filterselect; 00016 break; 00017 case 'group': 00018 $additionalargs = 'group/'.$filterselect; 00019 break; 00020 case 'user': 00021 $additionalargs = 'user/'.$filterselect; 00022 break; 00023 } 00024 00025 if ($tagid) { 00026 $additionalargs .= '/'.$tagid; 00027 } 00028 00029 return rss_get_url($contextid, $userid, $componentname, $additionalargs); 00030 } 00031 00032 // This function returns the icon (from theme) with the link to rss/file.php 00033 // needs some hacking to rss/file.php 00034 function blog_rss_print_link($context, $filtertype, $filterselect=0, $tagid=0, $tooltiptext='') { 00035 global $CFG, $USER, $OUTPUT; 00036 00037 if (!isloggedin()) { 00038 $userid = $CFG->siteguest; 00039 } else { 00040 $userid = $USER->id; 00041 } 00042 00043 $url = blog_rss_get_url($context->id, $userid, $filtertype, $filterselect, $tagid); 00044 $rsspix = $OUTPUT->pix_url('i/rss'); 00045 print '<div class="mdl-right"><a href="'. $url .'"><img src="'. $rsspix .'" title="'. strip_tags($tooltiptext) .'" alt="'.get_string('rss').'" /></a></div>'; 00046 } 00047 00048 function blog_rss_add_http_header($context, $title, $filtertype, $filterselect=0, $tagid=0) { 00049 global $PAGE, $USER, $CFG; 00050 00051 //$componentname = 'blog'; 00052 //rss_add_http_header($context, $componentname, $filterselect, $title); 00053 00054 if (!isloggedin()) { 00055 $userid = $CFG->siteguest; 00056 } else { 00057 $userid = $USER->id; 00058 } 00059 00060 $rsspath = blog_rss_get_url($context->id, $userid, $filtertype, $filterselect, $tagid); 00061 $PAGE->add_alternate_version($title, $rsspath, 'application/rss+xml'); 00062 } 00063 00069 function blog_rss_get_params($filters) { 00070 $thingid = $rsscontext = $filtertype = null; 00071 00072 $sitecontext = get_context_instance(CONTEXT_SYSTEM); 00073 00074 if (!$filters) { 00075 $thingid = SITEID; 00076 $rsscontext = $sitecontext; 00077 $filtertype = 'site'; 00078 } else if (array_key_exists('course', $filters)) { 00079 $thingid = $filters['course']; 00080 00081 $coursecontext = get_context_instance(CONTEXT_COURSE, $thingid); 00082 $rsscontext = $coursecontext; 00083 00084 $filtertype = 'course'; 00085 } else if (array_key_exists('user', $filters)) { 00086 $thingid = $filters['user']; 00087 00088 $usercontext = get_context_instance(CONTEXT_USER, $thingid); 00089 $rsscontext = $usercontext; 00090 00091 $filtertype = 'user'; 00092 } else if (array_key_exists('group', $filters)) { 00093 $thingid = $filters['group']; 00094 00095 $rsscontext = $sitecontext; //is this the context we should be using for group blogs? 00096 $filtertype = 'group'; 00097 } 00098 00099 return array($thingid, $rsscontext, $filtertype); 00100 } 00101 00102 00103 // Generate any blog RSS feed via one function (called by ../rss/file.php) 00104 function blog_rss_get_feed($context, $args) { 00105 global $CFG, $SITE, $DB; 00106 00107 if (empty($CFG->enablerssfeeds)) { 00108 debugging('Sorry, RSS feeds are disabled on this site'); 00109 return ''; 00110 } 00111 00112 $sitecontext = get_context_instance(CONTEXT_SYSTEM); 00113 if (!has_capability('moodle/blog:view', $sitecontext)) { 00114 return null; 00115 } 00116 00117 $type = clean_param($args[3], PARAM_ALPHA); 00118 $id = clean_param($args[4], PARAM_INT); // could be groupid / courseid / userid depending on $type 00119 00120 $tagid=0; 00121 if ($args[5] != 'rss.xml') { 00122 $tagid = clean_param($args[5], PARAM_INT); 00123 } else { 00124 $tagid = 0; 00125 } 00126 00127 $filename = blog_rss_file_name($type, $id, $tagid); 00128 00129 if (file_exists($filename)) { 00130 if (filemtime($filename) + 3600 > time()) { 00131 return $filename; // It's already done so we return cached version 00132 } 00133 } 00134 00135 $courseid = $groupid = $userid = null; 00136 switch ($type) { 00137 case 'site': 00138 //$siteid = $id; 00139 break; 00140 case 'course': 00141 $courseid = $id; 00142 break; 00143 case 'group': 00144 $groupid = $id; 00145 break; 00146 case 'user': 00147 $userid = $id; 00148 break; 00149 } 00150 00151 // Get all the entries from the database 00152 require_once($CFG->dirroot .'/blog/locallib.php'); 00153 $blogheaders = blog_get_headers($courseid, $groupid, $userid, $tagid); 00154 00155 $bloglisting = new blog_listing($blogheaders['filters']); 00156 $blogentries = $bloglisting->get_entries(); 00157 00158 // Now generate an array of RSS items 00159 if ($blogentries) { 00160 $items = array(); 00161 foreach ($blogentries as $blog_entry) { 00162 $item = NULL; 00163 $item->author = fullname($DB->get_record('user', array('id'=>$blog_entry->userid))); // TODO: this is slow 00164 $item->title = $blog_entry->subject; 00165 $item->pubdate = $blog_entry->lastmodified; 00166 $item->link = $CFG->wwwroot.'/blog/index.php?entryid='.$blog_entry->id; 00167 $item->description = format_text($blog_entry->summary, $blog_entry->format); 00168 if ( !empty($CFG->usetags) && ($blogtags = tag_get_tags_array('post', $blog_entry->id)) ) { 00169 if ($blogtags) { 00170 $item->tags = $blogtags; 00171 } 00172 $item->tagscheme = $CFG->wwwroot . '/tag'; 00173 } 00174 $items[] = $item; 00175 } 00176 $articles = rss_add_items($items); 00177 } else { 00178 $articles = ''; 00179 } 00180 00182 00183 switch ($type) { 00184 case 'user': 00185 $info = fullname($DB->get_record('user', array('id'=>$id), 'firstname,lastname')); 00186 break; 00187 case 'course': 00188 $info = $DB->get_field('course', 'fullname', array('id'=>$id)); 00189 $info = format_string($info, true, array('context' => get_context_instance(CONTEXT_COURSE, $id))); 00190 break; 00191 case 'site': 00192 $info = format_string($SITE->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))); 00193 break; 00194 case 'group': 00195 $group = groups_get_group($id); 00196 $info = $group->name; //TODO: $DB->get_field('groups', 'name', array('id'=>$id)) 00197 break; 00198 default: 00199 $info = ''; 00200 break; 00201 } 00202 00203 if ($tagid) { 00204 $info .= ': '.$DB->get_field('tags', 'text', array('id'=>$tagid)); 00205 } 00206 00207 $header = rss_standard_header(get_string($type.'blog','blog', $info), 00208 $CFG->wwwroot.'/blog/index.php', 00209 get_string('intro','blog')); 00210 00211 $footer = rss_standard_footer(); 00212 00213 // Save the XML contents to file. 00214 $rssdata = $header.$articles.$footer; 00215 if (blog_rss_save_file($type,$id,$tagid,$rssdata)) { 00216 return $filename; 00217 } else { 00218 return false; // Couldn't find it or make it 00219 } 00220 } 00221 00222 00223 function blog_rss_file_name($type, $id, $tagid=0) { 00224 global $CFG; 00225 00226 if ($tagid) { 00227 return "$CFG->cachedir/rss/blog/$type/$id/$tagid.xml"; 00228 } else { 00229 return "$CFG->cachedir/rss/blog/$type/$id.xml"; 00230 } 00231 } 00232 00233 //This function saves to file the rss feed specified in the parameters 00234 function blog_rss_save_file($type, $id, $tagid=0, $contents='') { 00235 global $CFG; 00236 00237 $status = true; 00238 00239 //blog creates some additional dirs within the rss cache so make sure they all exist 00240 make_cache_directory('rss/blog'); 00241 make_cache_directory('rss/blog/'.$type); 00242 00243 $filename = blog_rss_file_name($type, $id, $tagid); 00244 $expandfilename = false; //we're supplying a full file path 00245 $status = rss_save_file('blog', $filename, $contents, $expandfilename); 00246 00247 return $status; 00248 } 00249