First version of RSS System.

Discussion and post forum feeds supported.
Integrated in cron system.
Some strings missing.
Test, test, test...
This commit is contained in:
stronk7
2004-05-02 23:08:19 +00:00
parent 750024e833
commit 8adcb49f8e
18 changed files with 685 additions and 2 deletions
+16
View File
@@ -435,6 +435,22 @@
</td>
</tr>
<tr valign=top>
<td align=right><p>enablerssfeeds:</td>
<td>
<?php
unset($options);
$options[0] = get_string("no");
$options[1] = get_string("yes");
choose_from_menu ($options, "enablerssfeeds", $config->enablerssfeeds, "", "", "");
?>
</td>
<td>
<?php print_string("configenablerssfeeds") ?>
</td>
</tr>
<tr>
<td colspan=3 align=center>
<input type="submit" value="<?php print_string("savechanges") ?>"></td>
+14
View File
@@ -139,6 +139,20 @@
}
}
if (!empty($CFG->enablerssfeeds)) { //Defined in admin/variables page
if (file_exists("$CFG->dirroot/rss/rsslib.php")) {
include_once("$CFG->dirroot/rss/rsslib.php");
echo "Running rssfeeds if required...\n";
flush();
if ( ! cron_rss_feeds()) {
echo "Something went wrong while generating rssfeeds!!!\n";
} else {
echo "Rssfeeds finished\n";
}
}
}
echo "Cron script completed correctly\n";
$difftime = microtime_diff($starttime, microtime());
+2
View File
@@ -111,6 +111,8 @@ $string['repliesmany'] = '$a replies so far';
$string['repliesone'] = '$a reply so far';
$string['reply'] = 'Reply';
$string['replyforum'] = 'Reply to forum';
$string['rsssubscriberssdiscussions'] = 'This is a link to susbscribe to the \'$a\' forum RSS discussions channel';
$string['rsssubscriberssposts'] = 'This is a link to subscribe to the \'$a\' forum RSS posts channel';
$string['search'] = 'Search';
$string['searchforums'] = 'Search forums';
$string['searcholderposts'] = 'Search older posts...';
+24
View File
@@ -48,6 +48,30 @@
<?php print_string("configmaxbytes", "forum") ?>
</td>
</tr>
<tr valign=top>
<td align=right><p>forum_enablerssfeeds:</td>
<td>
<?php
if ($CFG->enablerssfeeds == 0) {
print_string("no");
} else {
unset($options);
$options[0] = get_string("no");
$options[1] = get_string("yes");
choose_from_menu ($options, "forum_enablerssfeeds", $CFG->forum_enablerssfeeds, "", "", "");
}
?>
</td>
<td>
<?php
print_string("configenablerssfeeds","forum");
if ($CFG->enablerssfeeds == 0) {
print_string("configenablerssfeedsdisabled","forum");
}
?>
</td>
</tr>
<tr>
<td colspan=3 align=center>
+6
View File
@@ -108,6 +108,12 @@ function forum_upgrade($oldversion) {
if ($oldversion < 2004020600) {
table_column("forum_discussions", "", "usermodified", "integer", "10", "unsigned", "0", "", "timemodified");
}
if ($oldversion < 2004050300) {
table_column("forum","","rsstype","tinyint","2", "unsigned", "0", "", "forcesubscribe");
table_column("forum","","rssarticles","tinyint","2", "unsigned", "0", "", "rsstype");
set_config("forum_enablerssfeeds",0);
}
return true;
+2
View File
@@ -16,6 +16,8 @@ CREATE TABLE prefix_forum (
scale int(10) NOT NULL default '0',
maxbytes int(10) unsigned NOT NULL default '0',
forcesubscribe tinyint(1) unsigned NOT NULL default '0',
rsstype tinyint(2) unsigned NOT NULL default '0',
rssarticles tinyint(2) unsigned NOT NULL default '0',
timemodified int(10) unsigned NOT NULL default '0',
PRIMARY KEY (id),
UNIQUE KEY id (id)
+2
View File
@@ -16,6 +16,8 @@ CREATE TABLE prefix_forum (
scale number(10) default '0' NOT NULL,
maxbytes number(10) default '0' NOT NULL,
forcesubscribe number(1) default '0' NOT NULL,
rsstype number(2) default '0' NOT NULL,
rssarticles number(2) default '0' NOT NULL,
timemodified number(10) default '0' NOT NULL
);
+6
View File
@@ -47,6 +47,12 @@ function forum_upgrade($oldversion) {
table_column("forum_discussions", "", "usermodified", "integer", "10", "unsigned", "0", "", "timemodified");
}
if ($oldversion < 2004050300) {
table_column("forum","","rsstype","integer","2", "unsigned", "0", "", "forcesubscribe");
table_column("forum","","rssarticles","integer","2", "unsigned", "0", "", "rsstype");
set_config("forum_enablerssfeeds",0);
}
return true;
}
+2
View File
@@ -16,6 +16,8 @@ CREATE TABLE prefix_forum (
scale integer NOT NULL default '0',
maxbytes integer NOT NULL default '0',
forcesubscribe integer NOT NULL default '0',
rsstype integer NOT NULL default '0',
rssarticles integer NOT NULL default '0',
timemodified integer NOT NULL default '0'
);
# --------------------------------------------------------
+43
View File
@@ -23,6 +23,12 @@
if (!isset($form->maxbytes)) {
$form->maxbytes = $CFG->forum_maxbytes;
}
if (!isset($form->rsstype)) {
$form->rsstype = 0;
}
if (!isset($form->rssarticles)) {
$form->rssarticles = 0;
}
?>
<form name="form" method="post" <?php echo $onsubmit ?> action="mod.php">
<table cellpadding=5>
@@ -109,7 +115,44 @@
?>
</td>
</tr>
<?php
//Only show rss parameters if rss is activated at site and forum levels
if ($CFG->enablerssfeeds && $CFG->forum_enablerssfeeds) {
echo "<tr valign=top>";
echo "<td align=right><p><b>".get_string("rsstype", "forum")."</b></p></td>";
echo "<td>";
unset($choices);
$choices[0] = get_string("none");
$choices[1] = get_string("discussions", "forum");
$choices[2] = get_string("posts", "forum");
choose_from_menu ($choices, "rsstype", $form->rsstype, "");
helpbutton("rsstype", get_string("rsstype", "forum"), "forum");
echo "</td>";
echo "</tr>";
echo "<tr valign=top>";
echo "<td align=right><p><b>".get_string("rssarticles", "forum")."</b></p></td>";
echo "<td>";
unset($choices);
$choices[0] = "0";
$choices[1] = "1";
$choices[2] = "2";
$choices[3] = "3";
$choices[4] = "4";
$choices[5] = "5";
$choices[10] = "10";
$choices[15] = "15";
$choices[20] = "20";
$choices[25] = "25";
$choices[30] = "30";
$choices[40] = "40";
$choices[50] = "50";
choose_from_menu ($choices, "rssarticles", $form->rssarticles, "");
helpbutton("rssarticles", get_string("rssarticles", "forum"), "forum");
echo "</td>";
echo "</tr>";
}
?>
<tr>
<td align=right valign=top><p><b><?php print_string("allowratings", "forum") ?>:</b></p></td>
<td>
+212
View File
@@ -0,0 +1,212 @@
<?PHP // $Id$
//This file adds support to rss feeds generation
//This function is the main entry point to forum
//rss feeds generation. Foreach site forum with rss enabled
//build one XML rss structure.
function forum_rss_feeds() {
global $CFG;
$status = true;
//Check CFG->enablerssfeeds
if (empty($CFG->enablerssfeeds)) {
//Some debug...
if ($CFG->debug > 7) {
echo "DISABLED (admin variables)";
}
//Check CFG->forum_enablerssfeeds
} else if (empty($CFG->forum_enablerssfeeds)) {
//Some debug...
if ($CFG->debug > 7) {
echo "DISABLED (module configuration)";
}
//It's working so we start...
} else {
//Iterate over all forums
if ($forums = get_records("forum")) {
foreach ($forums as $forum) {
if (!empty($forum->rsstype) && !empty($forum->rssarticles) && $status) {
//Some debug...
if ($CFG->debug > 7) {
echo "ID: $forum->id->";
}
//Get the XML contents
$result = forum_rss_feed($forum);
//Save the XML contents to file
if (!empty($result)) {
$status = rss_save_file("forum",$forum,$result);
}
//Some debug...
if ($CFG->debug > 7) {
if (empty($result)) {
echo "(empty) ";
} else {
if (!empty($status)) {
echo "OK ";
} else {
echo "FAIL ";
}
}
}
}
}
}
}
return $status;
}
//This function return the XML rss contents about the forum record passed as parameter
//It returns false if something is wrong
function forum_rss_feed($forum) {
global $CFG;
$status = true;
//Check CFG->enablerssfeeds
if (empty($CFG->enablerssfeeds)) {
//Some debug...
if ($CFG->debug > 7) {
echo "DISABLED (admin variables)";
}
//Check CFG->forum_enablerssfeeds
} else if (empty($CFG->forum_enablerssfeeds)) {
//Some debug...
if ($CFG->debug > 7) {
echo "DISABLED (module configuration)";
}
//It's working so we start...
} else {
//Check the forum has rss activated
if (!empty($forum->rsstype) && !empty($forum->rssarticles)) {
//Depending of the forum->rsstype, we are going to execute, different sqls
if ($forum->rsstype == 1) { //Discussion RSS
$items = forum_rss_feed_discussions($forum);
} else { //Post RSS
$items = forum_rss_feed_posts($forum);
}
//Now, if items, we begin building the structure
if (!empty($items)) {
//First all rss feeds common headers
$header = rss_standard_header($forum->name,
$CFG->wwwroot."/mod/forum/view.php?f=".$forum->id,
$forum->intro);
//Now all the rss items
if (!empty($header)) {
$articles = rss_add_items($items);
}
//Now all rss feeds common footers
if (!empty($header) && !empty($articles)) {
$footer = rss_standard_footer();
}
//Now, if everything is ok, concatenate it
if (!empty($header) && !empty($articles) && !empty($footer)) {
$status = $header.$articles.$footer;
} else {
$status = false;
}
} else {
$status = false;
}
}
}
return $status;
}
//This function returns "items" record array to be used to build the rss feed
//for a Type=discussions forum
function forum_rss_feed_discussions($forum) {
global $CFG;
$items = array();
if ($recs = get_records_sql ("SELECT d.id discussionid,
d.name discussionname,
u.id userid,
u.firstname userfirstname,
u.lastname userlastname,
p.message postmessage,
p.created postcreated
FROM {$CFG->prefix}forum_discussions d,
{$CFG->prefix}forum_posts p,
{$CFG->prefix}user u
WHERE d.forum = '$forum->id' AND
p.discussion = d.id AND
p.parent = 0 AND
u.id = p.userid
ORDER BY p.created desc")) {
//Iterate over each discussion to get forum->rssarticles records
$articlesleft = $forum->rssarticles;
$item = NULL;
$user = NULL;
foreach ($recs as $rec) {
unset($item);
unset($user);
$item->title = $rec->discussionname;
$user->firstname = $rec->userfirstname;
$user->lastname = $rec->userlastname;
$item->author = fullname($user);
$item->pubdate = $rec->postcreated;
$item->link = $CFG->wwwroot."/mod/forum/discuss.php?d=".$rec->discussionid;
$item->description = $rec->postmessage;
$items[] = $item;
$articlesleft--;
if ($articlesleft < 1) {
break;
}
}
}
return $items;
}
//This function returns "items" record array to be used to build the rss feed
//for a Type=posts forum
function forum_rss_feed_posts($forum) {
global $CFG;
$items = array();
if ($recs = get_records_sql ("SELECT p.id postid,
d.id discussionid,
d.name discussionname,
u.id userid,
u.firstname userfirstname,
u.lastname userlastname,
p.message postmessage,
p.created postcreated
FROM {$CFG->prefix}forum_discussions d,
{$CFG->prefix}forum_posts p,
{$CFG->prefix}user u
WHERE d.forum = '$forum->id' AND
p.discussion = d.id AND
u.id = p.userid
ORDER BY p.created desc")) {
//Iterate over each discussion to get forum->rssarticles records
$articlesleft = $forum->rssarticles;
$item = NULL;
$user = NULL;
foreach ($recs as $rec) {
unset($item);
unset($user);
$item->title = $rec->discussionname;
$user->firstname = $rec->userfirstname;
$user->lastname = $rec->userlastname;
$item->author = fullname($user);
$item->pubdate = $rec->postcreated;
$item->link = $CFG->wwwroot."/mod/forum/discuss.php?d=".$rec->discussionid."&parent=".$rec->postid;
$item->description = $rec->postmessage;
$items[] = $item;
$articlesleft--;
if ($articlesleft < 1) {
break;
}
}
}
return $items;
}
?>
+1 -1
View File
@@ -5,7 +5,7 @@
// This fragment is called by /admin/index.php
////////////////////////////////////////////////////////////////////////////////
$module->version = 2004031000;
$module->version = 2004050300;
$module->requires = 2004031000; // Requires this Moodle version
$module->cron = 60;
+13
View File
@@ -2,6 +2,7 @@
require_once("../../config.php");
require_once("lib.php");
require_once("$CFG->dirroot/rss/rsslib.php");
optional_variable($id); // Course Module ID
optional_variable($f); // Forum ID
@@ -153,6 +154,18 @@
echo "<br />";
echo "<font size=1><a title=\"$subtexttitle\" href=\"subscribe.php?id=$forum->id\">$subtext</a></font>";
}
//If rss are activated at site and forum level and this forum has rss defined, show link
if ($CFG->enablerssfeeds && $CFG->forum_enablerssfeeds && $forum->rsstype and $forum->rssarticles) {
echo "<br />";
if ($forum->rsstype == 1) {
$tooltiptext = get_string("rsssubscriberssdiscussions","forum",$forum->name);
} else {
$tooltiptext = get_string("rsssubscriberssposts","forum",$forum->name);
}
rss_print_link($course->id, $USER->id, "forum", $forum->id, $tooltiptext);
}
echo '</td>';
}
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 437 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

+76
View File
@@ -0,0 +1,76 @@
<?PHP //$Id
//This file returns the required rss feeds
//The URL format MUST include:
// course: the course id
// user: the user id
// name: the name of the module (forum...)
// id: the id of the module (forumid...)
//If the course has a password or it doesn't
//allow guest access then the user field is
//required to see that the user is enrolled
//in the course, else no check is performed.
//This allows to limit a bit the rss access
//to correct users. It isn't unbreakable,
//obviously, but its the best I've thought!!
require_once("../config.php");
require_once("$CFG->dirroot/files/mimetypes.php");
$allowed = true;
$error = false;
if (empty($CFG->filelifetime)) {
$CFG->filelifetime = 86400; /// Seconds for files to remain in caches
}
if (isset($file)) { // workaround for situations where / syntax doesn't work
$pathinfo = $file;
} else {
$pathinfo = get_slash_arguments("file.php");
}
if (!$pathinfo) {
$error = true;
}
$pathinfo = urldecode($pathinfo);
if (! $args = parse_slash_arguments($pathinfo)) {
$error = true;
}
$numargs = count($args);
if ($numargs < 5 or empty($args[1])) {
$error = true;
}
$courseid = (integer)$args[0];
if (! $course = get_record("course", "id", $courseid)) {
$error = true;
}
//Check for "security" if !course->guest or course->password
if (!$course->guest || $course->password) {
$allowed = (isstudent($course->id,$args[1]) || isteacher($course->id,$args[1]));
}
$pathname = $CFG->dataroot."/rss/".$args[2]."/".$args[3].".xml";
$filename = $args[$numargs-1];
//If the file exists and its allowed for me, download it!
if (file_exists($pathname) && $allowed && !$error) {
$lastmodified = filemtime($pathname);
$mimetype = mimeinfo("type", $filename);
header("Last-Modified: " . gmdate("D, d M Y H:i:s", $lastmodified) . " GMT");
header("Expires: " . gmdate("D, d M Y H:i:s", time() + $CFG->filelifetime) . " GMT");
header("Cache-control: max_age = $CFG->filelifetime");
header("Pragma: ");
header("Content-disposition: inline; filename=$filename");
header("Content-length: ".filesize($pathname));
header("Content-type: $mimetype");
readfile($pathname);
}
?>
+265
View File
@@ -0,0 +1,265 @@
<?PHP // $Id$
// This file contains all the common stuff to be used in RSS System
//This function prints the icon (from theme) with the link to rss/file.php
function rss_print_link($courseid, $userid, $modulename, $id, $tooltiptext="") {
global $CFG, $THEME, $USER;
static $pixpath = '';
static $rsspath = '';
if ($CFG->slasharguments) {
$rsspath = "$CFG->wwwroot/rss/file.php/$courseid/$userid/$modulename/$id/rss.xml";
} else {
$rsspath = "$CFG->wwwroot/rss/file.php?file=/$courseid/$userid/$modulename/$id/rss.xml";
}
if (empty($pixpath)) {
if (empty($THEME->custompix)) {
$pixpath = "$CFG->wwwroot/pix";
} else {
$pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
}
}
$rsspix = $pixpath."/i/rss.gif";
echo "<a href=\"".$rsspath."\"><img src=\"$rsspix\" title=\"$tooltiptext\"></a>";
}
//This function iterates over each module in the server to see if
//it supports generating rss feeds, searching for a MODULENAME_rss_feeds()
//function and invoking it foreach activity as necessary
function cron_rss_feeds () {
global $CFG;
$status = true;
echo " Generating rssfeeds...\n";
if ($allmods = get_records("modules") ) {
foreach ($allmods as $mod) {
echo ' '.$mod->name.': ';
$modname = $mod->name;
$modfile = "$CFG->dirroot/mod/$modname/rsslib.php";
//If file exists and we have selected to restore that type of module
if (file_exists($modfile)) {
include_once($modfile);
$generaterssfeeds = $modname.'_rss_feeds';
if (function_exists($generaterssfeeds)) {
if ($status) {
echo 'generating ';
$status = $generaterssfeeds();
if (!empty($status)) {
echo "...OK\n";
} else {
echo "...FAILED\n";
}
} else {
echo "...SKIPPED (failed above)\n";
}
} else {
echo "...NOT SUPPORTED (function)\n";
}
} else {
echo "...NOT SUPPORTED (file)\n";
}
}
}
echo " Ending rssfeeds...";
if (!empty($status)) {
echo "...OK\n";
} else {
echo "...FAILED\n";
}
return $status;
}
//This function saves to file the rss feed specified in the parameters
function rss_save_file ($modname,$mod,$result) {
global $CFG;
$status = true;
$basedir = $CFG->dataroot."/rss";
//Check dataroot/rss exists
$status = check_dir_exists($basedir,true);
$basedir .= "/".$modname;
//Check dataroot/rrs/$modname exists
$status = check_dir_exists($basedir,true);
if ($status) {
$file = $basedir .= "/".$mod->id.".xml";
$rss_file = fopen($file,"w");
if ($rss_file) {
$status = fwrite ($rss_file,$result);
fclose($rss_file);
}
}
return $status;
}
//This function return all the common headers for every rss feed in the site
function rss_standard_header($title = NULL, $link = NULL, $description = NULL) {
global $CFG, $USER;
static $pixpath = '';
$status = true;
$result = "";
if (!$site = get_site()) {
$status = false;
}
if ($status) {
//Calculate title, link and description
if (empty($title)) {
$title = $site->fullname;
}
if (empty($link)) {
$link = $CFG->wwwroot;
}
if (empty($description)) {
$description = $site->summary;
}
//xml headers
$result .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$result .= "<rss version=\"2.0\">\n";
//open the channel
$result .= rss_start_tag("channel",1,true);
//write channel info
$result .= rss_full_tag("title",2,false,$title);
$result .= rss_full_tag("link",2,false,$link);
$result .= rss_full_tag("description",2,false,$description);
$result .= rss_full_tag("language",2,false,substr($USER->lang,0,2));
$today = getdate();
$result .= rss_full_tag("copyright",2,false,"&copy; ".$today['year']." ".$site->fullname);
$result .= rss_full_tag("managingEditor",2,false,$USER->email);
$result .= rss_full_tag("webMaster",2,false,$USER->email);
//write image info
//Calculate the origin
if (empty($pixpath)) {
if (empty($THEME->custompix)) {
$pixpath = "$CFG->wwwroot/pix";
} else {
$pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
}
}
$rsspix = $pixpath."/i/rsssitelogo.gif";
//write the info
$result .= rss_start_tag("image",2,true);
$result .= rss_full_tag("url",3,false,$rsspix);
$result .= rss_full_tag("title",3,false,"moodle");
$result .= rss_full_tag("link",3,false,$CFG->wwwroot);
$result .= rss_full_tag("width",3,false,"140");
$result .= rss_full_tag("height",3,false,"35");
$result .= rss_end_tag("image",2,true);
}
if (!$status) {
return false;
} else {
return $result;
}
}
//This function returns the rss XML code for every item passed in the array
//item->title: The title of the item
//item->author: The author of the item
//item->pubdate: The pubdate of the item
//item->link: The link url of the item
//item->description: The content of the item
function rss_add_items($items) {
global $CFG;
$result = "";
if (!empty($items)) {
foreach ($items as $item) {
$result .= rss_start_tag("item",2,true);
$result .= rss_full_tag("title",3,false,$item->title);
$result .= rss_full_tag("link",3,false,$item->link);
$result .= rss_full_tag("pubDate",3,false,date("r",$item->pubdate));
$result .= rss_full_tag("description",3,false,$item->description);
$result .= rss_end_tag("item",2,true);
}
} else {
$result = false;
}
return $result;
}
//This function return all the common footers for every rss feed in the site
function rss_standard_footer($title = NULL, $link = NULL, $description = NULL) {
global $CFG, $USER;
$status = true;
$result = "";
//Close the chanel
$result .= rss_end_tag("channel",1,true);
////Close the rss tag
$result .= "</rss>";
return $result;
}
// ===== This function are used to write XML tags =========
// [stronk7]: They are similar to the glossary export and backup generation
// but I've replicated them here because they have some minor
// diferences. Someday all they should go to a common place.
//Return the xml start tag
function rss_start_tag($tag,$level=0,$endline=false) {
if ($endline) {
$endchar = "\n";
} else {
$endchar = "";
}
return str_repeat(" ",$level*2)."<".$tag.">".$endchar;
}
//Return the xml end tag
function rss_end_tag($tag,$level=0,$endline=true) {
if ($endline) {
$endchar = "\n";
} else {
$endchar = "";
}
return str_repeat(" ",$level*2)."</".$tag.">".$endchar;
}
//Return the start tag, the contents and the end tag
function rss_full_tag($tag,$level=0,$endline=true,$content,$to_utf=true) {
//Here we encode absolute links
$st = rss_start_tag($tag,$level,$endline);
$co="";
if ($to_utf) {
$co = preg_replace("/\r\n|\r/", "\n", utf8_encode(htmlspecialchars($content)));
} else {
$co = preg_replace("/\r\n|\r/", "\n", htmlspecialchars($content));
}
$et = rss_end_tag($tag,0,true);
return $st.$co.$et;
}
?>
+1 -1
View File
@@ -5,7 +5,7 @@
// database to determine whether upgrades should
// be performed (see lib/db/*.php)
$version = 2004050200; // The current version is a date (YYYYMMDDXX)
$version = 2004050300; // The current version is a date (YYYYMMDDXX)
$release = "1.3 development"; // User-friendly version number