moodle/mod/forum/rsslib.php

316 lines
12 KiB
PHP
Raw Normal View History

<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file adds support to rss feeds generation
*
* @package mod-forum
* @copyright 2001 Eloy Lafuente (stronk7) http://contiento.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* 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, $DB;
$status = true;
//Check CFG->enablerssfeeds
if (empty($CFG->enablerssfeeds)) {
debugging('DISABLED (admin variables)');
//Check CFG->forum_enablerssfeeds
} else if (empty($CFG->forum_enablerssfeeds)) {
debugging('DISABLED (module configuration)');
//It's working so we start...
} else {
//Iterate over all forums
if ($forums = $DB->get_records("forum")) {
foreach ($forums as $forum) {
if (!empty($forum->rsstype) && !empty($forum->rssarticles) && $status) {
$filename = rss_file_name('forum', $forum); // RSS file
//First let's make sure there is work to do by checking existing files
if (file_exists($filename)) {
if ($lastmodified = filemtime($filename)) {
if (!forum_rss_newstuff($forum, $lastmodified)) {
continue;
}
}
}
//Ignore hidden forums
if (!instance_is_visible('forum',$forum)) {
if (file_exists($filename)) {
@unlink($filename);
}
continue;
}
mtrace("Updating RSS feed for ".format_string($forum->name,true).", 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);
}
if (debugging()) {
if (empty($result)) {
echo "ID: $forum->id-> (empty) ";
} else {
if (!empty($status)) {
echo "ID: $forum->id-> OK ";
} else {
echo "ID: $forum->id-> FAIL ";
}
}
}
}
}
}
}
return $status;
}
/**
* Given a forum object, deletes the RSS file
*
* @param 0object $forum
* @return bool
*/
function forum_rss_delete_file($forum) {
global $CFG;
$rssfile = rss_file_name('forum', $forum);
if (file_exists($rssfile)) {
return unlink($rssfile);
} else {
return true;
}
}
/**
* If there is new stuff in the forum since $time then this returns
* true. Otherwise it returns false.
*
* @param object $forum
* @param int $time
* @return bool
*/
function forum_rss_newstuff($forum, $time) {
if ($forum->rsstype == 1) {
$items = forum_rss_feed_discussions($forum, $time);
} else {
$items = forum_rss_feed_posts($forum, $time);
}
return (!empty($items));
}
/**
* This function return the XML rss contents about the forum record passed as parameter
* It returns false if something is wrong
*
* @param object $forum
* @param bool
*/
function forum_rss_feed($forum) {
global $CFG;
$status = true;
//Check CFG->enablerssfeeds
if (empty($CFG->enablerssfeeds)) {
debugging("DISABLED (admin variables)");
//Check CFG->forum_enablerssfeeds
} else if (empty($CFG->forum_enablerssfeeds)) {
debugging("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
2006-04-12 08:58:20 +00:00
$header = rss_standard_header(strip_tags(format_string($forum->name,true)),
$CFG->wwwroot."/mod/forum/view.php?f=".$forum->id,
format_string($forum->intro,true)); // TODO: fix format
//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
*
* @param object $forum
* @param int $newsince
*/
function forum_rss_feed_discussions($forum, $newsince=0) {
global $CFG, $DB;
$items = array();
if ($newsince) {
$newsince = " AND p.modified > '$newsince'";
} else {
$newsince = "";
}
if ($recs = $DB->get_records_sql ("SELECT d.id AS discussionid,
d.name AS discussionname,
u.id AS userid,
2004-09-29 07:56:33 +00:00
u.firstname AS userfirstname,
u.lastname AS userlastname,
p.message AS postmessage,
p.created AS postcreated,
p.messageformat AS postformat,
p.messagetrust AS posttrust
FROM {forum_discussions} d,
{forum_posts} p,
{user} u
WHERE d.forum = '$forum->id' AND
p.discussion = d.id AND
p.parent = 0 AND
u.id = p.userid $newsince
ORDER BY p.created desc",
array($forum->id, $newsince), 0, $forum->rssarticles)) {
$item = NULL;
$user = NULL;
$formatoptions = new object;
$formatoptions->trusted = $rec->posttrust;
foreach ($recs as $rec) {
unset($item);
unset($user);
$item->title = format_string($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 = format_text($rec->postmessage,$rec->postformat,$formatoptions,$forum->course);
$items[] = $item;
}
}
return $items;
}
/**
* This function returns "items" record array to be used to build the rss feed
* for a Type=posts forum
*
* @param object $forum
* @param int $newsince
*/
function forum_rss_feed_posts($forum, $newsince=0) {
global $CFG, $DB;
$items = array();
if ($newsince) {
$newsince = " AND p.modified > '$newsince'";
} else {
$newsince = "";
}
if ($recs = $DB->get_records_sql ("SELECT p.id AS postid,
2004-09-29 07:56:33 +00:00
d.id AS discussionid,
d.name AS discussionname,
2004-09-29 07:56:33 +00:00
u.id AS userid,
u.firstname AS userfirstname,
u.lastname AS userlastname,
p.subject AS postsubject,
p.message AS postmessage,
p.created AS postcreated,
p.messageformat AS postformat,
p.messagetrust AS posttrust
FROM {forum_discussions} d,
{forum_posts} p,
{user} u
WHERE d.forum = ? AND
p.discussion = d.id AND
u.id = p.userid $newsince
ORDER BY p.created desc", array($forum->id), 0, $forum->rssarticles)) {
$item = NULL;
$user = NULL;
2006-10-21 16:00:43 +00:00
$formatoptions = new object;
$formatoptions->trusted = $rec->posttrust;
2006-10-21 16:00:43 +00:00
foreach ($recs as $rec) {
unset($item);
unset($user);
$item->category = $rec->discussionname;
$item->title = $rec->postsubject;
$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;
2006-10-21 16:00:43 +00:00
$item->description = format_text($rec->postmessage,$rec->postformat,$formatoptions,$forum->course);
2006-10-21 16:00:43 +00:00
$post_file_area_name = str_replace('//', '/', "$forum->course/$CFG->moddata/forum/$forum->id/$rec->postid");
$post_files = get_directory_list("$CFG->dataroot/$post_file_area_name");
if (!empty($post_files)) {
$item->attachments = array();
//TODO: rewrite attachment handling
}
$items[] = $item;
}
}
return $items;
}