Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

RSS API: Difference between revisions

From MoodleDocs
No edit summary
m (Text replacement - "</code>" to "</syntaxhighlight>")
 
(18 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==Overview==
{{Moodle 2.3}}==Overview==


The Events API is a core system in Moodle to allow communication between modules.
The RSS API is a core system in Moodle to allow you to create secure RSS feeds of data in your module.  


An '''event''' is when something "interesting" happens in Moodle that is worth alerting the system about.
These RSS feeds can then be syndicated by other websites or aggregated by a feed reader.


Any Moodle modules can '''trigger''' new events (with attached data), and other modules can elect to '''handle''' those events with custom functions that operate on the given data.
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==
==Examples==
Line 14: Line 24:
===Creating an RSS feed===
===Creating an RSS feed===


===Caching an RSS feed===
This example shows how you can create the content of an RSS Feed.


===Retrieving a cached RSS feed===
<syntaxhighlight lang="php">
require_once("lib/rsslib.php");
//create some entries (these would normally be pulled from the database table of the module you are creating an rss feed for).
for ($i=1; $i <= 10; $i++) {
    $item = NULL;
    $item->author = 'Joe Blogs';
    $item->title = 'RSS Entry Number '.$i;
    $item->pubdate = time();
    $item->link = $CFG->wwwroot.'/entry/index.php?entryid='.$i;
    $item->description = 'Summary of the RSS Entry';
    $items[] = $item;
}
$content = rss_add_items($items);  // Convert the array of items into the XML Content
$title = 'RSS Feed';
$description = 'General RSS Feed full of ENTRIES';
$header = rss_standard_header($title, $CFG->wwwroot.'/entry/index.php', $description);
$footer = rss_standard_footer();
$rssdata = $header.$content.$footer; // This is the complete RSS FEED
</syntaxhighlight>


==Database structure==
===Caching an RSS feed===
 
 
 
 
{| border="1" cellpadding="2" cellspacing="0"
|'''Field'''
|'''Type'''
|'''Info'''
|-
|id
|int(10)
|auto increment identifier
|-
|queuedeventid
|int(10)
|foreign key id corresponding to the id of the event_queues table
|-
|handlerid
|int(10)
|foreign key id corresponding to the id of the event_handlers table
|-
|status
|int(10)
|number of failed attempts to process this handler
|-
|errormessage
|text
|if an error happened last time we tried to process this event, record it here.
|-
|timemodified
|int(10)
|time stamp of the last attempt to run this from the queue
|}


==Standards for naming events==
Following on from the previous example:


==Events which exist==
<syntaxhighlight lang="php">
  $filename = $CFG->cachedir .'/rss/entry/user/0/rss.xml';
  rss_save_file('entry', $filename, $rssdata, true);
</syntaxhighlight>


===Linking to a RSS feed===


===Other===
This example shows how you can output a link to an RSS feed. (Example taken from blocks/news_items/block_news_items.php)


<syntaxhighlight lang="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);
</syntaxhighlight>


==See also==
==See also==

Latest revision as of 20:21, 14 July 2021

Moodle 2.3

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.

 require_once("lib/rsslib.php");
 //create some entries (these would normally be pulled from the database table of the module you are creating an rss feed for).
 for ($i=1; $i <= 10; $i++) {
     $item = NULL;
     $item->author = 'Joe Blogs';
     $item->title = 'RSS Entry Number '.$i;
     $item->pubdate = time();
     $item->link = $CFG->wwwroot.'/entry/index.php?entryid='.$i;
     $item->description = 'Summary of the RSS Entry';
     $items[] = $item;
 }
 $content = rss_add_items($items);   // Convert the array of items into the XML Content
 
 $title = 'RSS Feed';
 $description = 'General RSS Feed full of ENTRIES';
 $header = rss_standard_header($title, $CFG->wwwroot.'/entry/index.php', $description);
 $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/entry/user/0/rss.xml';
  rss_save_file('entry', $filename, $rssdata, 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);

See also