From 8adcb49f8e8661e46c6bc2dd49bfaee6e9ad74f5 Mon Sep 17 00:00:00 2001 From: stronk7 Date: Sun, 2 May 2004 23:08:19 +0000 Subject: [PATCH] First version of RSS System. Discussion and post forum feeds supported. Integrated in cron system. Some strings missing. Test, test, test... --- admin/config.html | 16 +++ admin/cron.php | 14 ++ lang/en/forum.php | 2 + mod/forum/config.html | 24 ++++ mod/forum/db/mysql.php | 6 + mod/forum/db/mysql.sql | 2 + mod/forum/db/oci8po.sql | 2 + mod/forum/db/postgres7.php | 6 + mod/forum/db/postgres7.sql | 2 + mod/forum/mod.html | 43 ++++++ mod/forum/rsslib.php | 212 +++++++++++++++++++++++++++++ mod/forum/version.php | 2 +- mod/forum/view.php | 13 ++ pix/i/rss.gif | Bin 0 -> 437 bytes pix/i/rsssitelogo.gif | Bin 0 -> 1785 bytes rss/file.php | 76 +++++++++++ rss/rsslib.php | 265 +++++++++++++++++++++++++++++++++++++ version.php | 2 +- 18 files changed, 685 insertions(+), 2 deletions(-) create mode 100644 mod/forum/rsslib.php create mode 100644 pix/i/rss.gif create mode 100644 pix/i/rsssitelogo.gif create mode 100644 rss/file.php create mode 100644 rss/rsslib.php diff --git a/admin/config.html b/admin/config.html index 7594621260..52b3fff01f 100644 --- a/admin/config.html +++ b/admin/config.html @@ -435,6 +435,22 @@ + +

enablerssfeeds: + + enablerssfeeds, "", "", ""); + ?> + + + + + + "> diff --git a/admin/cron.php b/admin/cron.php index 80fd628a54..5a16fd5598 100644 --- a/admin/cron.php +++ b/admin/cron.php @@ -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()); diff --git a/lang/en/forum.php b/lang/en/forum.php index 607c60143d..5a4155828f 100644 --- a/lang/en/forum.php +++ b/lang/en/forum.php @@ -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...'; diff --git a/mod/forum/config.html b/mod/forum/config.html index a374d2eeb6..7b44f470d1 100644 --- a/mod/forum/config.html +++ b/mod/forum/config.html @@ -48,6 +48,30 @@ + +

forum_enablerssfeeds: + + 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, "", "", ""); + } + ?> + + + enablerssfeeds == 0) { + print_string("configenablerssfeedsdisabled","forum"); + } + ?> + + diff --git a/mod/forum/db/mysql.php b/mod/forum/db/mysql.php index c910bfb98d..e7f4ea8880 100644 --- a/mod/forum/db/mysql.php +++ b/mod/forum/db/mysql.php @@ -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; diff --git a/mod/forum/db/mysql.sql b/mod/forum/db/mysql.sql index c5c189ab11..b3a25585fd 100644 --- a/mod/forum/db/mysql.sql +++ b/mod/forum/db/mysql.sql @@ -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) diff --git a/mod/forum/db/oci8po.sql b/mod/forum/db/oci8po.sql index 5bb50926c5..749a61b747 100755 --- a/mod/forum/db/oci8po.sql +++ b/mod/forum/db/oci8po.sql @@ -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 ); diff --git a/mod/forum/db/postgres7.php b/mod/forum/db/postgres7.php index 5d9b69b5e6..1c5babc1d7 100644 --- a/mod/forum/db/postgres7.php +++ b/mod/forum/db/postgres7.php @@ -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; } diff --git a/mod/forum/db/postgres7.sql b/mod/forum/db/postgres7.sql index 5cc0276fee..d7c716f5c0 100644 --- a/mod/forum/db/postgres7.sql +++ b/mod/forum/db/postgres7.sql @@ -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' ); # -------------------------------------------------------- diff --git a/mod/forum/mod.html b/mod/forum/mod.html index 191f84da09..18d4df98b7 100644 --- a/mod/forum/mod.html +++ b/mod/forum/mod.html @@ -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; + } ?>

action="mod.php"> @@ -109,7 +115,44 @@ ?> +enablerssfeeds && $CFG->forum_enablerssfeeds) { + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + } +?> '; } diff --git a/pix/i/rss.gif b/pix/i/rss.gif new file mode 100644 index 0000000000000000000000000000000000000000..22e88487b7f58e08fd643f5f47517b4b4739960b GIT binary patch literal 437 zcmZ?wbhEHbRAJy_xXQqw$jA6)PU81u#_N$R-ydB3c4p(9Jmvq#7JMyJtTkqOySC~7 z|NoDAf`4{7{{Qyj`NE9@{@>j2zd+#stnmLAHhey`@c*-`|K2=!*%$OGp6P#& z!{0|2{-0R#f6s*XQ-VG%EqqsN^pBB=fjFS}lZ91;!H7Wzq#WcY2DVEFg0nqzr20=R zZo86cHUGj2lUWIV>mycueHmFHJ})pmCR>@rm$p8!&6ha`20A z^9gR+vTa8|Xr2hO6d%h*JxL}lO>Ti>Cr$~h@NJpOJ4aTPk5N07?Z&M;jsE+ZTYZJO z4V78rbYz$XUvs}@e(3i&Kz^#P&=)3oW(Mas%oYv_>vbcA{5%sLHXUvkQ1+UmA?P5$ GU=0AyMXFH% literal 0 HcmV?d00001 diff --git a/pix/i/rsssitelogo.gif b/pix/i/rsssitelogo.gif new file mode 100644 index 0000000000000000000000000000000000000000..ffb0aeff05c004fa191c18c300f5cfb0cd802178 GIT binary patch literal 1785 zcmV@Nk%w1VT=GH0K@O^*5UO2->l%^-?hNf#?$AkuCeCp@%)_}>iPfq_ww1YbF_sw z-psEuGc^CQIsNO;A^8LV00000EC2ui0E_@5000I5U?7fUX`X1Ru59bRa4gSseMX+3 z10${r0Eul#EE-9P!U3pgU;@d5;i0HJkJzjhb@3FK6-%I7xBwIb@ir!0yH6KE?i?P= znd!GFBWY9y0}dS>6?%pu7Z(X&Vu^ETYHt+?2!ob`1PB=!4H<`TiHmfNU3igO6_=Nl zgc21MZJ%qQjANx|d0P{#mXwqo2otZJvY?8!j;2}?zXSxpzQCETr^Q>eqqdK^N0XS) zzRd&!)`r+sq}$at-p}9;@Pd~Frv)B~iUpo45sNw(8a}khVDAS&fh_tt3Me9hhevgi z;ad0Mz`al$R%C!T0KtGisho*)*MR_l2O0wZ9O&Xq zK!8tx2F`prsO0RjfdvPgGGG9}^pqw@B4k(;ll24B0$7*W`#~`f0Kjh>n8F}0i39qU zJYb+e0fXBfBCKRA3Q|LT36?i+{(Lx+-8HBHQ6+Q`fEcqy zumLa~WfI5(!wq+a0~55k0g5UrFo9GFOvnTS6|^`2YbjDOqe=zT=bi&OPOu}44Ke_M z2n$pMQ*${|009NSx#plE8V-QL2Q@Cj+yE1NWdQ~Qtd|=DT9rUR0Gfe6 zs0bLGl;V{MWQhTCZc@P_F(YX3z@Js1KmeryxH+f-tL~$L0Hvx*!Ay&I@LC$FEyi4q zr*MEGo|!=)ottNdIa2_x;(4b31xnn?B8PUKrePfqW@$lm!k#g~k|r2XYHp35Flz&~ z8lWw@A}rA>uJo$JD{%uzfLv0}d1}J4NlqZ0wi8s7!K=(NKtTcad<(A$R+WoR0O8Uh zffwEZFn|UShb&tLo5tzz2_5ti@4Uy!`~a-IckcicoFE6Y z08F3+vodnXq`KdVvA_bQ(lM13DOR200zsCl;RMr)K!5~7*!ptJE~`rR%mdhSFU%)A z-7C$I?TkjgBkz-89C2(;;?#hZSB~Wyh{FYuIJu+cL|Kl?^x30yu60 zYEk2er`%DE@aEm8?cFv1lYw9C_P1_ot=)aBZ`XMMi?2NPi``1IoxV=Gdb1P zi&)Em0~AHaDFv6oYQ+h8M{RoP73BVOgG2&i{q@+xV#3$(ifuOS-`hSc1j!TkBMH)N z>K+BVj;bV05WlA6uEMu#LS#sT3%v#9XGVbq01O}iF+jiqeo9R$^Pc7E)i}0^PgQ-& z4{*|?k_>3;e@FsCR!C5h3|I_i%IZ;}2IHTw2rR%F*FX(o zi^&Wc1SSFv1juR*@!_a0CM=}oA~Q%ziq0D80qx+3Zz?E2Wq`9ll?-!^F*~N^LZu$d z=%6;u$rTACu$q$`2AZp!SgV|KlEbO8IKWXQxRl9_4cNvsMu7=)aMdEE4dy4YdRqm?{Lo7Kx(nBdbasYEv6f b)u@((1)I34M$}r4w6gWAaE)sULI40e!*&PB literal 0 HcmV?d00001 diff --git a/rss/file.php b/rss/file.php new file mode 100644 index 0000000000..e295dfced2 --- /dev/null +++ b/rss/file.php @@ -0,0 +1,76 @@ +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); + } + +?> diff --git a/rss/rsslib.php b/rss/rsslib.php new file mode 100644 index 0000000000..03d06c3242 --- /dev/null +++ b/rss/rsslib.php @@ -0,0 +1,265 @@ +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 ""; + +} + +//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 .= "\n"; + $result .= "\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,"© ".$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 .= ""; + + 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)."".$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; +} + +?> diff --git a/version.php b/version.php index 82f0f2404a..0820e57921 100644 --- a/version.php +++ b/version.php @@ -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 -- 2.39.5

".get_string("rsstype", "forum")."

"; + 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 "

".get_string("rssarticles", "forum")."

"; + 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 "

:

diff --git a/mod/forum/rsslib.php b/mod/forum/rsslib.php new file mode 100644 index 0000000000..3553aac86c --- /dev/null +++ b/mod/forum/rsslib.php @@ -0,0 +1,212 @@ +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; + } +?> diff --git a/mod/forum/version.php b/mod/forum/version.php index f954f14385..bf44447c06 100644 --- a/mod/forum/version.php +++ b/mod/forum/version.php @@ -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; diff --git a/mod/forum/view.php b/mod/forum/view.php index 6bd4270885..4aa876cfae 100644 --- a/mod/forum/view.php +++ b/mod/forum/view.php @@ -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 "
"; echo "id\">$subtext"; } + + //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 "
"; + 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 '