RSS API: Difference between revisions
Jason Fowler (talk | contribs) |
Jason Fowler (talk | contribs) |
||
Line 37: | Line 37: | ||
foreach ($blogentries as $blog_entry) { | foreach ($blogentries as $blog_entry) { | ||
$item = NULL; | $item = NULL; | ||
$item->author = fullname($DB->get_record('user', array('id'=>$blog_entry->userid))); | $item->author = fullname($DB->get_record('user', array('id'=>$blog_entry->userid))); | ||
$item->title = $blog_entry->subject; | $item->title = $blog_entry->subject; | ||
$item->pubdate = $blog_entry->lastmodified; | $item->pubdate = $blog_entry->lastmodified; |
Revision as of 02:21, 25 January 2012
Overview
The RSS API is a core system in Moodle to allow you to create secure RSS feeds of data in your module.
These RSS feeds can then be syndicated by other websites or aggregated by a feed reader.
Any Moodle module can generate RSS feeds to allow secure access to it's data.
File Locations
The primary functions for the RSS API is located in lib/rsslib.php
Additional functions and usage examples can be found in the following files:
- blog/rsslib.php
- mod/data/rsslib.php
- mod/forum/rsslib.php
- mod/glossary/rsslib.php
Examples
Below you will find examples on how to use the RSS functions within your own modules.
Creating an RSS feed
This example shows how you can create the content of an RSS Feed. (Example taken from blog/rsslib.php)
$tagid = 0;
// Get all the entries from the database
require_once($CFG->dirroot .'/blog/locallib.php');
$blogheaders = blog_get_headers($courseid, $groupid, $userid, $tagid);
$bloglisting = new blog_listing($blogheaders['filters']);
$blogentries = $bloglisting->get_entries();
foreach ($blogentries as $blog_entry) {
$item = NULL;
$item->author = fullname($DB->get_record('user', array('id'=>$blog_entry->userid)));
$item->title = $blog_entry->subject;
$item->pubdate = $blog_entry->lastmodified;
$item->link = $CFG->wwwroot.'/blog/index.php?entryid='.$blog_entry->id;
$item->description = format_text($blog_entry->summary, $blog_entry->format);
if ( !empty($CFG->usetags) && ($blogtags = tag_get_tags_array('post', $blog_entry->id)) ) {
if ($blogtags) {
$item->tags = $blogtags;
}
$item->tagscheme = $CFG->wwwroot . '/tag';
}
$items[] = $item;
}
$content = rss_add_items($items); /// Change structure to XML
$info .= ': '.$DB->get_field('tags', 'text', array('id'=>$tagid));
$header = rss_standard_header(get_string($type.'blog','blog', $info), $CFG->wwwroot.'/blog/index.php', get_string('intro','blog'));
$footer = rss_standard_footer();
$rssdata = $header.$content.$footer; // This is the complete RSS FEED
Caching an RSS feed
Following on from the previous example:
$filename = "$CFG->cachedir/rss/blog/user/0/$tagid.xml";
rss_save_file('blog', $filename, $contents, true);
Linking to a RSS feed
This example shows how you can output a link to an RSS feed. (Example taken from blocks/news_items/block_news_items.php)
$cm = $modinfo->instances['forum'][$forum->id];
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$forum = forum_get_course_forum($this->page->course->id, 'news');
$userid = (!isloggedin()) ? 0 : $USER->id;
$tooltiptext = get_string('rsssubscriberssposts','forum');
echo rss_get_link($context->id, $userid, 'mod_forum', $forum->id, $tooltiptext);