Пример #1
0
function blog_generate_rss_feed($type, $id, $tagid = 0)
{
    global $CFG, $SITE;
    if (empty($CFG->enablerssfeeds)) {
        debugging('Sorry, RSS feeds are disabled on this site');
        return '';
    }
    $filename = blog_rss_file_name($type, $id, $tagid);
    if (file_exists($filename)) {
        if (filemtime($filename) + 3600 > time()) {
            return $filename;
            /// It's already done so we return cached version
        }
    }
    /// Get all the posts from the database
    $blogposts = blog_fetch_entries('', 20, '', $type, $id, $tagid);
    /// Now generate an array of RSS items
    if ($blogposts) {
        $items = array();
        foreach ($blogposts as $blogpost) {
            $item = NULL;
            $item->author = fullname(get_record('user', 'id', $blogpost->userid));
            $item->title = $blogpost->subject;
            $item->pubdate = $blogpost->lastmodified;
            $item->link = $CFG->wwwroot . '/blog/index.php?postid=' . $blogpost->id;
            $item->description = format_text($blogpost->summary, $blogpost->format);
            $items[] = $item;
        }
        $articles = rss_add_items($items);
        /// Change structure to XML
    } else {
        $articles = '';
    }
    /// Get header and footer information
    switch ($type) {
        case 'user':
            $info = fullname(get_record('user', 'id', $id, '', '', '', '', 'firstname,lastname'));
            break;
        case 'course':
            $info = get_field('course', 'fullname', 'id', $id);
            break;
        case 'site':
            $info = $SITE->fullname;
            break;
        case 'group':
            $group = groups_get_group($id, false);
            $info = $group->name;
            //TODO: get_field('groups', 'name', 'id', $id)
            break;
        default:
            $info = '';
            break;
    }
    if ($tagid) {
        $info .= ': ' . get_field('tags', 'text', '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();
    /// Save the XML contents to file.
    $rssdata = $header . $articles . $footer;
    if (blog_rss_save_file($type, $id, $tagid, $rssdata)) {
        return $filename;
    } else {
        return false;
        // Couldn't find it or make it
    }
}
Пример #2
0
/**
 * Generate any blog RSS feed via one function
 *
 * @param stdClass $context The context of the blog for which the feed it being generated
 * @param array    $args    An array of arguements needed to build the feed (contextid, token, componentname, type, id, tagid)
 */
function blog_rss_get_feed($context, $args)
{
    global $CFG, $SITE, $DB;
    if (empty($CFG->bloglevel)) {
        debugging('Blogging disabled on this site, RSS feeds are not available');
        return null;
    }
    if (empty($CFG->enablerssfeeds)) {
        debugging('Sorry, RSS feeds are disabled on this site');
        return '';
    }
    if ($CFG->bloglevel == BLOG_SITE_LEVEL) {
        if (isguestuser()) {
            debugging(get_string('nopermissiontoshow', 'error'));
            return '';
        }
    }
    $sitecontext = get_context_instance(CONTEXT_SYSTEM);
    if (!has_capability('moodle/blog:view', $sitecontext)) {
        return null;
    }
    $type = clean_param($args[3], PARAM_ALPHA);
    $id = clean_param($args[4], PARAM_INT);
    // could be groupid / courseid  / userid  depending on $type
    $tagid = 0;
    if ($args[5] != 'rss.xml') {
        $tagid = clean_param($args[5], PARAM_INT);
    } else {
        $tagid = 0;
    }
    $filename = blog_rss_file_name($type, $id, $tagid);
    if (file_exists($filename)) {
        if (filemtime($filename) + 3600 > time()) {
            return $filename;
            // It's already done so we return cached version
        }
    }
    $courseid = $groupid = $userid = null;
    switch ($type) {
        case 'site':
            //$siteid = $id;
            break;
        case 'course':
            $courseid = $id;
            break;
        case 'group':
            $groupid = $id;
            break;
        case 'user':
            $userid = $id;
            break;
    }
    // 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();
    // Now generate an array of RSS items
    if ($blogentries) {
        $items = array();
        foreach ($blogentries as $blog_entry) {
            $item = NULL;
            $item->author = fullname($DB->get_record('user', array('id' => $blog_entry->userid)));
            // TODO: this is slow
            $item->title = $blog_entry->subject;
            $item->pubdate = $blog_entry->lastmodified;
            $item->link = $CFG->wwwroot . '/blog/index.php?entryid=' . $blog_entry->id;
            $summary = file_rewrite_pluginfile_urls($blog_entry->summary, 'pluginfile.php', $sitecontext->id, 'blog', 'post', $blog_entry->id);
            $item->description = format_text($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;
        }
        $articles = rss_add_items($items);
        /// Change structure to XML
    } else {
        $articles = '';
    }
    /// Get header and footer information
    switch ($type) {
        case 'user':
            $info = fullname($DB->get_record('user', array('id' => $id), 'firstname,lastname'));
            break;
        case 'course':
            $info = $DB->get_field('course', 'fullname', array('id' => $id));
            $info = format_string($info, true, array('context' => get_context_instance(CONTEXT_COURSE, $id)));
            break;
        case 'site':
            $info = format_string($SITE->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID)));
            break;
        case 'group':
            $group = groups_get_group($id);
            $info = $group->name;
            //TODO: $DB->get_field('groups', 'name', array('id'=>$id))
            break;
        default:
            $info = '';
            break;
    }
    if ($tagid) {
        $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();
    // Save the XML contents to file.
    $rssdata = $header . $articles . $footer;
    if (blog_rss_save_file($type, $id, $tagid, $rssdata)) {
        return $filename;
    } else {
        return false;
        // Couldn't find it or make it
    }
}
Пример #3
0
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
                    $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;
}
Пример #4
0
function data_rss_get_feed($context, $args)
{
    global $CFG, $DB;
    // Check CFG->data_enablerssfeeds.
    if (empty($CFG->data_enablerssfeeds)) {
        debugging("DISABLED (module configuration)");
        return null;
    }
    $dataid = clean_param($args[3], PARAM_INT);
    $cm = get_coursemodule_from_instance('data', $dataid, 0, false, MUST_EXIST);
    if ($cm) {
        $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
        //context id from db should match the submitted one
        if ($context->id != $modcontext->id || !has_capability('mod/data:viewentry', $modcontext)) {
            return null;
        }
    }
    $data = $DB->get_record('data', array('id' => $dataid), '*', MUST_EXIST);
    if (!rss_enabled_for_mod('data', $data, false, true)) {
        return null;
    }
    $sql = data_rss_get_sql($data);
    //get the cache file info
    $filename = rss_get_file_name($data, $sql);
    $cachedfilepath = rss_get_file_full_name('mod_data', $filename);
    //Is the cache out of date?
    $cachedfilelastmodified = 0;
    if (file_exists($cachedfilepath)) {
        $cachedfilelastmodified = filemtime($cachedfilepath);
    }
    //if the cache is more than 60 seconds old and there's new stuff
    $dontrecheckcutoff = time() - 60;
    if ($dontrecheckcutoff > $cachedfilelastmodified && data_rss_newstuff($data, $cachedfilelastmodified)) {
        require_once $CFG->dirroot . '/mod/data/lib.php';
        // Get the first field in the list  (a hack for now until we have a selector)
        if (!($firstfield = $DB->get_record_sql('SELECT id,name FROM {data_fields} WHERE dataid = ? ORDER by id', array($data->id), true))) {
            return null;
        }
        if (!($records = $DB->get_records_sql($sql, array(), 0, $data->rssarticles))) {
            return null;
        }
        $firstrecord = array_shift($records);
        // Get the first and put it back
        array_unshift($records, $firstrecord);
        // Now create all the articles
        $items = array();
        foreach ($records as $record) {
            $recordarray = array();
            array_push($recordarray, $record);
            $item = null;
            // guess title or not
            if (!empty($data->rsstitletemplate)) {
                $item->title = data_print_template('rsstitletemplate', $recordarray, $data, '', 0, true);
            } else {
                // else we guess
                $item->title = strip_tags($DB->get_field('data_content', 'content', array('fieldid' => $firstfield->id, 'recordid' => $record->id)));
            }
            $item->description = data_print_template('rsstemplate', $recordarray, $data, '', 0, true);
            $item->pubdate = $record->timecreated;
            $item->link = $CFG->wwwroot . '/mod/data/view.php?d=' . $data->id . '&rid=' . $record->id;
            array_push($items, $item);
        }
        $course = $DB->get_record('course', array('id' => $data->course));
        $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
        $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
        // First all rss feeds common headers.
        $header = rss_standard_header($courseshortname . ': ' . format_string($data->name, true, array('context' => $context)), $CFG->wwwroot . "/mod/data/view.php?d=" . $data->id, format_text($data->intro, $data->introformat, array('context' => $context)));
        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)) {
            $rss = $header . $articles . $footer;
            //Save the XML contents to file.
            $status = rss_save_file('mod_data', $filename, $rss);
        }
    }
    return $cachedfilepath;
}
Пример #5
0
/**
 * This function return an error xml file (string) to be sent when a rss is required (file.php) and something goes wrong
 *
 * @param string $errortype Type of error to send, default is rsserror
 * @return stdClass returns a XML Feed with an error message in it
 */
function rss_geterrorxmlfile($errortype = 'rsserror')
{
    global $CFG;
    $return = '';
    //XML Header
    $return = rss_standard_header();
    //XML item
    if ($return) {
        $item = new stdClass();
        $item->title = "RSS Error";
        $item->link = $CFG->wwwroot;
        $item->pubdate = time();
        $item->description = get_string($errortype);
        $return .= rss_add_items(array($item));
    }
    //XML Footer
    if ($return) {
        $return .= rss_standard_footer();
    }
    return $return;
}
Пример #6
0
/**
 * This function return the XML rss contents about the forum
 * It returns false if something is wrong
 *
 * @param stdClass $forum the forum object
 * @param string $sql the SQL used to retrieve the contents from the database
 * @param array $params the SQL parameters used
 * @param object $context the context this forum relates to
 * @return bool|string false if the contents is empty, otherwise the contents of the feed is returned
 *
 * @Todo MDL-31129 implement post attachment handling
 */

function forum_rss_feed_contents($forum, $sql, $params, $context) {
    global $CFG, $DB, $USER;

    $status = true;

    $recs = $DB->get_recordset_sql($sql, $params, 0, $forum->rssarticles);

    //set a flag. Are we displaying discussions or posts?
    $isdiscussion = true;
    if (!empty($forum->rsstype) && $forum->rsstype!=1) {
        $isdiscussion = false;
    }

    if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course)) {
        print_error('invalidcoursemodule');
    }

    $formatoptions = new stdClass();
    $items = array();
    foreach ($recs as $rec) {
            $item = new stdClass();
            $user = new stdClass();

            $discussion = new stdClass();
            $discussion->id = $rec->discussionid;
            $discussion->groupid = $rec->groupid;
            $discussion->timestart = $rec->timestart;
            $discussion->timeend = $rec->timeend;

            $post = null;
            if (!$isdiscussion) {
                $post = new stdClass();
                $post->id = $rec->postid;
                $post->parent = $rec->postparent;
                $post->userid = $rec->userid;
            }

            if ($isdiscussion && !forum_user_can_see_discussion($forum, $discussion, $context)) {
                // This is a discussion which the user has no permission to view
                $item->title = get_string('forumsubjecthidden', 'forum');
                $message = get_string('forumbodyhidden', 'forum');
                $item->author = get_string('forumauthorhidden', 'forum');
            } else if (!$isdiscussion && !forum_user_can_see_post($forum, $discussion, $post, $USER, $cm)) {
                // This is a post which the user has no permission to view
                $item->title = get_string('forumsubjecthidden', 'forum');
                $message = get_string('forumbodyhidden', 'forum');
                $item->author = get_string('forumauthorhidden', 'forum');
            } else {
                // The user must have permission to view
                if ($isdiscussion && !empty($rec->discussionname)) {
                    $item->title = format_string($rec->discussionname);
                } else if (!empty($rec->postsubject)) {
                    $item->title = format_string($rec->postsubject);
                } else {
                    //we should have an item title by now but if we dont somehow then substitute something somewhat meaningful
                    $item->title = format_string($forum->name.' '.userdate($rec->postcreated,get_string('strftimedatetimeshort', 'langconfig')));
                }
                $user->firstname = $rec->userfirstname;
                $user->lastname = $rec->userlastname;
                $item->author = fullname($user);
                $message = file_rewrite_pluginfile_urls($rec->postmessage, 'pluginfile.php', $context->id,
                        'mod_forum', 'post', $rec->postid);
                $formatoptions->trusted = $rec->posttrust;
            }

            if ($isdiscussion) {
                $item->link = $CFG->wwwroot."/mod/forum/discuss.php?d=".$rec->discussionid;
            } else {
                $item->link = $CFG->wwwroot."/mod/forum/discuss.php?d=".$rec->discussionid."&parent=".$rec->postid;
            }

            $formatoptions->trusted = $rec->posttrust;
            $item->description = format_text($message, $rec->postformat, $formatoptions, $forum->course);

            //TODO: MDL-31129 implement post attachment handling
            /*if (!$isdiscussion) {
                $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();
                }
            }*/
            $item->pubdate = $rec->postcreated;

            $items[] = $item;
        }
    $recs->close();

    // Create the RSS header.
    $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 there are any.
    $articles = '';
    if (!empty($items)) {
        $articles = rss_add_items($items);
    }
    // Create the RSS footer.
    $footer = rss_standard_footer();

    return $header . $articles . $footer;
}
Пример #7
0
function data_rss_feeds()
{
    global $CFG, $DB;
    $status = true;
    // Check CFG->enablerssfeeds.
    if (empty($CFG->enablerssfeeds)) {
        debugging("DISABLED (admin variables)");
    } else {
        if (empty($CFG->data_enablerssfeeds)) {
            debugging("DISABLED (module configuration)");
        } else {
            // Iterate over all data.
            if ($datas = $DB->get_records('data')) {
                foreach ($datas as $data) {
                    if ($data->rssarticles > 0) {
                        // Get the first field in the list  (a hack for now until we have a selector)
                        if (!($firstfield = $DB->get_record_sql('SELECT id,name FROM {data_fields} WHERE dataid = ? ORDER by id', array($data->id), true))) {
                            continue;
                        }
                        // Get the data_records out.
                        $approved = $data->approval ? ' AND dr.approved = 1 ' : ' ';
                        $sql = "SELECT dr.*, u.firstname, u.lastname\n                                  FROM {data_records} dr, {user} u\n                                 WHERE dr.dataid = ? {$approved}\n                                       AND dr.userid = u.id\n                              ORDER BY dr.timecreated DESC";
                        if (!($records = $DB->get_records_sql($sql, array($data->id), 0, $data->rssarticles))) {
                            continue;
                        }
                        $firstrecord = array_shift($records);
                        // Get the first and put it back
                        array_unshift($records, $firstrecord);
                        $filename = rss_file_name('data', $data);
                        if (file_exists($filename)) {
                            if (filemtime($filename) >= $firstrecord->timemodified) {
                                continue;
                            }
                        }
                        // Now create all the articles
                        mtrace('Creating feed for ' . $data->name);
                        $items = array();
                        foreach ($records as $record) {
                            $recordarray = array();
                            array_push($recordarray, $record);
                            $item = null;
                            // guess title or not
                            if (!empty($data->rsstitletemplate)) {
                                $item->title = data_print_template('rsstitletemplate', $recordarray, $data, '', 0, true);
                            } else {
                                // else we guess
                                $item->title = strip_tags($DB->get_field('data_content', 'content', array('fieldid' => $firstfield->id, 'recordid' => $record->id)));
                            }
                            $item->description = data_print_template('rsstemplate', $recordarray, $data, '', 0, true);
                            $item->pubdate = $record->timecreated;
                            $item->link = $CFG->wwwroot . '/mod/data/view.php?d=' . $data->id . '&rid=' . $record->id;
                            array_push($items, $item);
                        }
                        $course = $DB->get_record('course', array('id' => $data->course));
                        // First all rss feeds common headers.
                        $header = rss_standard_header($course->shortname . ': ' . format_string($data->name, true), $CFG->wwwroot . "/mod/data/view.php?d=" . $data->id, format_string($data->intro, true));
                        //TODO: fix format
                        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)) {
                            $rss = $header . $articles . $footer;
                            //Save the XML contents to file.
                            $status = rss_save_file('data', $data, $rss);
                        } else {
                            $status = false;
                        }
                    }
                }
            }
        }
    }
    return $status;
}
function glossary_rss_get_feed($context, $args)
{
    global $CFG, $DB;
    $status = true;
    if (empty($CFG->glossary_enablerssfeeds)) {
        debugging("DISABLED (module configuration)");
        return null;
    }
    $glossaryid = clean_param($args[3], PARAM_INT);
    $cm = get_coursemodule_from_instance('glossary', $glossaryid, 0, false, MUST_EXIST);
    if ($cm) {
        $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
        //context id from db should match the submitted one
        //no specific capability required to view glossary entries so just check user is enrolled
        if ($context->id != $modcontext->id || !is_enrolled($context)) {
            return null;
        }
    }
    $glossary = $DB->get_record('glossary', array('id' => $glossaryid), '*', MUST_EXIST);
    if (!rss_enabled_for_mod('glossary', $glossary)) {
        return null;
    }
    $sql = glossary_rss_get_sql($glossary);
    //get the cache file info
    $filename = rss_get_file_name($glossary, $sql);
    $cachedfilepath = rss_get_file_full_name('mod_glossary', $filename);
    //Is the cache out of date?
    $cachedfilelastmodified = 0;
    if (file_exists($cachedfilepath)) {
        $cachedfilelastmodified = filemtime($cachedfilepath);
    }
    //if the cache is more than 60 seconds old and there's new stuff
    $dontrecheckcutoff = time() - 60;
    if ($dontrecheckcutoff > $cachedfilelastmodified && glossary_rss_newstuff($glossary, $cachedfilelastmodified)) {
        if (!($recs = $DB->get_records_sql($sql, array(), 0, $glossary->rssarticles))) {
            return null;
        }
        $items = array();
        $formatoptions = new stdClass();
        $formatoptions->trusttext = true;
        foreach ($recs as $rec) {
            $item = new stdClass();
            $user = new stdClass();
            $item->title = $rec->entryconcept;
            if ($glossary->rsstype == 1) {
                //With author
                $user->firstname = $rec->userfirstname;
                $user->lastname = $rec->userlastname;
                $item->author = fullname($user);
            }
            $item->pubdate = $rec->entrytimecreated;
            $item->link = $CFG->wwwroot . "/mod/glossary/showentry.php?courseid=" . $glossary->course . "&eid=" . $rec->entryid;
            $item->description = format_text($rec->entrydefinition, $rec->entryformat, $formatoptions, $glossary->course);
            $items[] = $item;
        }
        //First all rss feeds common headers
        $header = rss_standard_header(format_string($glossary->name, true), $CFG->wwwroot . "/mod/glossary/view.php?g=" . $glossary->id, format_string($glossary->intro, true));
        //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)) {
            $rss = $header . $articles . $footer;
            //Save the XML contents to file.
            $status = rss_save_file('mod_glossary', $filename, $rss);
        }
    }
    if (!$status) {
        $cachedfilepath = null;
    }
    return $cachedfilepath;
}
Пример #9
0
/**
 * Generates the RSS Feed
 *
 * @param strClass $context the context the feed should be created under
 * @param array $args array of arguments to be used in the creation of the RSS Feed
 * @return NULL|string NULL if there is nothing to return, or the file path of the cached file if there is
 */
function dataform_rss_get_feed($context, $args)
{
    global $CFG, $DB;
    // Check CFG->data_enablerssfeeds.
    if (empty($CFG->dataform_enablerssfeeds)) {
        debugging("DISABLED (module configuration)");
        return null;
    }
    // Validate context.
    $dataformid = clean_param($args[3], PARAM_INT);
    $cm = get_coursemodule_from_instance('dataform', $dataformid, 0, false, MUST_EXIST);
    if ($cm) {
        $modcontext = context_module::instance($cm->id);
        // Context id from db should match the submitted one.
        if ($context->id != $modcontext->id) {
            return null;
        }
    }
    // Check RSS enbabled for instance.
    $dataform = $DB->get_record('dataform', array('id' => $dataformid), '*', MUST_EXIST);
    if (!rss_enabled_for_mod('dataform', $dataform, false, false)) {
        return null;
    }
    // Get the target view.
    $viewid = clean_param($args[4], PARAM_INT);
    $viewdata = $DB->get_record('dataform_views', array('id' => $viewid), '*', MUST_EXIST);
    $viewman = mod_dataform_view_manager::instance($dataformid);
    $view = $viewman->get_view_by_id($viewid);
    // If (!($view instanceof 'mod_dataform\interfaces\rss')) {
    // return null;
    // }.
    // Get the cache file info
    // The cached file name is formatted dfid_viewid_contentstamp,
    // where contentstamp is provided by the view.
    $componentid = $dataformid . "_{$viewid}";
    $cachedfilepath = dataform_rss_get_cached_file_path($componentid);
    $contentstamp = $cachedfilepath ? dataform_rss_get_cached_content_stamp($cachedfilepath) : null;
    $newcontentstamp = $view->get_content_stamp();
    $hasnewcontent = $newcontentstamp !== $contentstamp;
    // Neither existing nor new.
    if (!$cachedfilepath and !$hasnewcontent) {
        return null;
    }
    if ($cachedfilepath) {
        // Use cache under 60 seconds.
        $cachetime = filemtime($cachedfilepath);
        if (time() - $cachetime < 60) {
            return $cachedfilepath;
        }
        // Use cache if there is nothing new.
        if (!$hasnewcontent) {
            return $cachedfilepath;
        }
        // Cached file is outdated so delete it.
        $instance = (object) array('id' => $componentid);
        rss_delete_file('mod_dataform', $instance);
    }
    // Still here, fetch new content.
    // Each article is an stdclass {title, descrition, pubdate, entrylink}.
    if (!($items = $view->get_rss_items())) {
        return null;
    }
    // First all rss feeds common headers.
    $headertitle = $view->get_rss_header_title();
    $headerlink = $view->get_rss_header_link();
    $headerdescription = $view->get_rss_header_description();
    $header = rss_standard_header($headertitle, $headerlink, $headerdescription);
    if (empty($header)) {
        return null;
    }
    // Now all rss feeds common footers.
    $footer = rss_standard_footer();
    if (empty($footer)) {
        return null;
    }
    // All's good, save the content to file.
    $articles = rss_add_items($items);
    $rss = $header . $articles . $footer;
    $filename = $componentid . "_{$newcontentstamp}";
    $status = rss_save_file('mod_dataform', $filename, $rss);
    $dirpath = dataform_rss_get_cache_dir_path();
    return "{$dirpath}/{$filename}.xml";
}
Пример #10
0
function glossary_rss_feed($glossary)
{
    global $CFG;
    $status = true;
    //Check CFG->enablerssfeeds
    if (empty($CFG->enablerssfeeds)) {
        debugging("DISABLED (admin variables)");
        //Check CFG->glossary_enablerssfeeds
    } else {
        if (empty($CFG->glossary_enablerssfeeds)) {
            debugging("DISABLED (module configuration)");
            //It's working so we start...
        } else {
            //Check the glossary has rss activated
            if (!empty($glossary->rsstype) && !empty($glossary->rssarticles)) {
                //Depending of the glossary->rsstype, we are going to execute, different sqls
                if ($glossary->rsstype == 1) {
                    //With author RSS
                    $items = glossary_rss_feed_withauthor($glossary);
                } else {
                    //Without author RSS
                    $items = glossary_rss_feed_withoutauthor($glossary);
                }
                //Now, if items, we begin building the structure
                if (!empty($items)) {
                    //First all rss feeds common headers
                    $header = rss_standard_header(format_string($glossary->name, true), $CFG->wwwroot . "/mod/glossary/view.php?g=" . $glossary->id, format_string($glossary->intro, true));
                    //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;
}
            if ($data->title === null) {
                $data->title = '';
            }
            // ...plus discussion subject (but not for discussion feed)
            if (!$d) {
                $data->title = format_string($post->get_discussion()->get_subject()) . ': ' . $data->title;
            }
            // Remaining details straightforward
            $data->description = format_text($post->get_message(), $post->get_format());
            $data->author = $forum->display_user_name($post->get_user());
            $data->link = $post->get_url();
            $data->pubdate = $post->get_modified();
            $feeddata[] = $data;
        }
    }
    // Now output all posts
    if ($rss) {
        header('Content-type: application/rss+xml');
        echo rss_standard_header($feedname, $url, $feedsummary);
        echo rss_add_items($feeddata);
        echo rss_standard_footer();
    } else {
        header('Content-type: application/atom+xml');
        $updated = count($feeddata) == 0 ? time() : reset($feeddata)->pubdate;
        echo atom_standard_header($FULLME, $FULLME, $updated, $feedname, $feedsummary);
        echo atom_add_items($feeddata);
        echo atom_standard_footer();
    }
} catch (forum_exception $e) {
    forum_utils::handle_exception($e);
}
Пример #12
0
/**
 * This function return the XML rss contents about the forum
 * It returns false if something is wrong
 *
 * @param stdClass $forum the forum object
 * @param string   $sql   The SQL used to retrieve the contents from the database
 * @param object $context the context this forum relates to
 * @return bool|string false if the contents is empty, otherwise the contents of the feed is returned
 *
 * @Todo MDL-31129 implement post attachment handling
 */
function forum_rss_feed_contents($forum, $sql, $context)
{
    global $CFG, $DB;
    $status = true;
    $params = array();
    //$params['forumid'] = $forum->id;
    $recs = $DB->get_recordset_sql($sql, $params, 0, $forum->rssarticles);
    //set a flag. Are we displaying discussions or posts?
    $isdiscussion = true;
    if (!empty($forum->rsstype) && $forum->rsstype != 1) {
        $isdiscussion = false;
    }
    $formatoptions = new stdClass();
    $items = array();
    foreach ($recs as $rec) {
        $item = new stdClass();
        $user = new stdClass();
        if ($isdiscussion && !empty($rec->discussionname)) {
            $item->title = format_string($rec->discussionname);
        } else {
            if (!empty($rec->postsubject)) {
                $item->title = format_string($rec->postsubject);
            } else {
                //we should have an item title by now but if we dont somehow then substitute something somewhat meaningful
                $item->title = format_string($forum->name . ' ' . userdate($rec->postcreated, get_string('strftimedatetimeshort', 'langconfig')));
            }
        }
        $user->firstname = $rec->userfirstname;
        $user->lastname = $rec->userlastname;
        $item->author = fullname($user);
        $item->pubdate = $rec->postcreated;
        if ($isdiscussion) {
            $item->link = $CFG->wwwroot . "/mod/forum/discuss.php?d=" . $rec->discussionid;
        } else {
            $item->link = $CFG->wwwroot . "/mod/forum/discuss.php?d=" . $rec->discussionid . "&parent=" . $rec->postid;
        }
        $formatoptions->trusted = $rec->posttrust;
        $message = file_rewrite_pluginfile_urls($rec->postmessage, 'pluginfile.php', $context->id, 'mod_forum', 'post', $rec->postid);
        $item->description = format_text($message, $rec->postformat, $formatoptions, $forum->course);
        //TODO: MDL-31129 implement post attachment handling
        /*if (!$isdiscussion) {
                        $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();
                        }
                    }*/
        $items[] = $item;
    }
    $recs->close();
    if (!empty($items)) {
        //First the RSS header
        $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 the RSS footer
        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;
}
Пример #13
0
/**
 * Returns the path to the cached rss feed contents. Creates/updates the cache if necessary.
 * The RSS feed content is a course search result.
 * @param array $args the arguments received in the url
 * $args[0] => context id = 2 (Front page) - not used
 * $args[1] => token
 * $args[2] => module name (it was needed by the rss/file.php to call this function) - not used
 * $args[3] => downloadable - PARAM_INT
 * $args[4] => audience - PARAM_ALPHA
 * $args[5] => educationallevel - PARAM_ALPHA
 * $args[6] => subject - PARAM_ALPHA
 * $args[7] => licence - PARAM_ALPHA
 * $args[8] => language - PARAM_ALPHANUMEXT
 * $args[9] => search - PARAM_TEXT (url encoded)
 * $args[10] => orderby - PARAM_ALPHA
 * @return string the full path to the cached RSS feed directory. Null if there is a problem.
 */
function hub_rss_get_feed($context, $args)
{
    global $CFG, $DB;
    require_once $CFG->dirroot . '/local/hub/lib.php';
    //are RSS feeds enabled?
    $enablerssfeeds = get_config('local_hub', 'enablerssfeeds');
    if (empty($enablerssfeeds)) {
        debugging('DISABLED (module configuration)');
        return null;
    }
    //check capabilities
    if (!has_capability('local/hub:view', $context)) {
        return null;
    }
    //TODO: cache
    $filename = 'rsssearch_' . $args[3] . '_' . $args[4] . '_' . $args[5] . '_' . $args[6] . '_' . $args[7] . '_' . $args[8] . '_' . $args[9] . '_' . $args[10];
    $cachedfilepath = rss_get_file_full_name('local_hub', $filename);
    //get the courses from the search
    if ($args[7] != 'all') {
        $options['licenceshortname'] = $args[7];
    }
    if ($args[6] != 'all') {
        $options['subject'] = $args[6];
    }
    if ($args[4] != 'all') {
        $options['audience'] = $args[4];
    }
    if ($args[5] != 'all') {
        $options['educationallevel'] = $args[5];
    }
    if ($args[8] != 'all') {
        $options['language'] = $args[8];
    }
    if ($args[3] != 'all') {
        $options['downloadable'] = $args[3];
        $options['enrollable'] = !$args[3];
    } else {
        $options['downloadable'] = true;
        $options['enrollable'] = true;
    }
    $options['search'] = empty($args[9]) ? '' : urldecode($args[9]);
    //if the RSS invisible secret is passed as parameter, display not visible course
    $rsssecret = get_config('local_hub', 'rsssecret');
    if (!empty($rsssecret) and $rsssecret == optional_param('rsssecret', false, PARAM_RAW)) {
        $options['visibility'] = COURSEVISIBILITY_NOTVISIBLE;
    } else {
        $options['visibility'] = COURSEVISIBILITY_VISIBLE;
    }
    //order by
    switch ($args[10]) {
        case 'newest':
            $options['orderby'] = 'timemodified DESC';
            break;
        case 'eldest':
            $options['orderby'] = 'timemodified ASC';
            break;
        case 'publisher':
            $options['orderby'] = 'publishername ASC';
            break;
        case 'fullname':
            $options['orderby'] = 'fullname ASC';
            break;
        case 'ratingaverage':
            $options['orderby'] = 'ratingaverage DESC';
            break;
        default:
            break;
    }
    $hub = new local_hub();
    $courses = $hub->get_courses($options, 0, 10);
    //generate the information for rss
    $rssfeedinfo = local_hub_rss_generate_feed_info($courses);
    //generate the rss content
    require_once $CFG->libdir . "/rsslib.php";
    //First the RSS header
    $searchurl = new moodle_url($CFG->wwwroot . '/', array('downloadable' => $args[3], 'audience' => $args[4], 'educationallevel' => $args[5], 'subject' => $args[6], 'licence' => $args[7], 'language' => $args[8], 'search' => $args[9], 'orderby' => $args[10], 'submitbutton' => 'Search+for+courses'));
    $rsscontent = rss_standard_header(get_config('local_hub', 'name'), $searchurl->out(), get_string('hubcoursessearch', 'local_hub'));
    $rsscontent .= rss_add_items($rssfeedinfo);
    //Now the RSS footer
    $rsscontent .= rss_standard_footer();
    if (!empty($rsscontent)) {
        rss_save_file('local_hub', $filename, $rsscontent);
    }
    //return the path to the cached version
    return $cachedfilepath;
}