function glossary_rss_feeds()
{
    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 {
            //Iterate over all glossaries
            if ($glossaries = get_records("glossary")) {
                foreach ($glossaries as $glossary) {
                    if (!empty($glossary->rsstype) && !empty($glossary->rssarticles) && $status) {
                        $filename = rss_file_name('glossary', $glossary);
                        // 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 (!glossary_rss_newstuff($glossary, $lastmodified)) {
                                    continue;
                                }
                            }
                        }
                        //Ignore hidden forums
                        if (!instance_is_visible('glossary', $glossary)) {
                            if (file_exists($filename)) {
                                @unlink($filename);
                            }
                            continue;
                        }
                        mtrace("Updating RSS feed for " . format_string($glossary->name, true) . ", ID: {$glossary->id}");
                        //Get the XML contents
                        $result = glossary_rss_feed($glossary);
                        //Save the XML contents to file
                        if (!empty($result)) {
                            $status = rss_save_file("glossary", $glossary, $result);
                        }
                        //Some debug...
                        if (debugging()) {
                            if (empty($result)) {
                                echo "ID: {$glossary->id}-> (empty) ";
                            } else {
                                if (!empty($status)) {
                                    echo "ID: {$glossary->id}-> OK ";
                                } else {
                                    echo "ID: {$glossary->id}-> FAIL ";
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return $status;
}
Exemple #2
0
/**
 * Returns the path to the cached rss feed contents. Creates/updates the cache if necessary.
 * @param stdClass $context the context
 * @param array    $args    the arguments received in the url
 * @return string the full path to the cached RSS feed directory. Null if there is a problem.
 */
function forum_rss_get_feed($context, $args) {
    global $CFG, $DB, $USER;

    $status = true;

    //are RSS feeds enabled?
    if (empty($CFG->forum_enablerssfeeds)) {
        debugging('DISABLED (module configuration)');
        return null;
    }

    $forumid  = clean_param($args[3], PARAM_INT);
    $cm = get_coursemodule_from_instance('forum', $forumid, 0, false, MUST_EXIST);
    $modcontext = context_module::instance($cm->id);

    //context id from db should match the submitted one
    if ($context->id != $modcontext->id || !has_capability('mod/forum:viewdiscussion', $modcontext)) {
        return null;
    }

    $forum = $DB->get_record('forum', array('id' => $forumid), '*', MUST_EXIST);
    if (!rss_enabled_for_mod('forum', $forum)) {
        return null;
    }

    //the sql that will retreive the data for the feed and be hashed to get the cache filename
    list($sql, $params) = forum_rss_get_sql($forum, $cm);

    // Hash the sql to get the cache file name.
    $filename = rss_get_file_name($forum, $sql, $params);
    $cachedfilepath = rss_get_file_full_name('mod_forum', $filename);

    //Is the cache out of date?
    $cachedfilelastmodified = 0;
    if (file_exists($cachedfilepath)) {
        $cachedfilelastmodified = filemtime($cachedfilepath);
    }
    // Used to determine if we need to generate a new RSS feed.
    $dontrecheckcutoff = time() - 60; // Sixty seconds ago.

    // If it hasn't been generated we need to create it.
    // Otherwise, if it has been > 60 seconds since we last updated, check for new items.
    if (($cachedfilelastmodified == 0) || (($dontrecheckcutoff > $cachedfilelastmodified) &&
        forum_rss_newstuff($forum, $cm, $cachedfilelastmodified))) {
        // Need to regenerate the cached version.
        $result = forum_rss_feed_contents($forum, $sql, $params, $modcontext);
        $status = rss_save_file('mod_forum', $filename, $result);
    }

    //return the path to the cached version
    return $cachedfilepath;
}
Exemple #3
0
/**
 * Returns the path to the cached rss feed contents. Creates/updates the cache if necessary.
 * @param stdClass $context the context
 * @param array    $args    the arguments received in the url
 * @return string the full path to the cached RSS feed directory. Null if there is a problem.
 */
function forum_rss_get_feed($context, $args) {
    global $CFG, $DB;

    $status = true;

    //are RSS feeds enabled?
    if (empty($CFG->forum_enablerssfeeds)) {
        debugging('DISABLED (module configuration)');
        return null;
    }

    $forumid  = clean_param($args[3], PARAM_INT);
    $cm = get_coursemodule_from_instance('forum', $forumid, 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/forum:viewdiscussion', $modcontext)) {
            return null;
        }
    }

    $forum = $DB->get_record('forum', array('id' => $forumid), '*', MUST_EXIST);
    if (!rss_enabled_for_mod('forum', $forum)) {
        return null;
    }

    //the sql that will retreive the data for the feed and be hashed to get the cache filename
    $sql = forum_rss_get_sql($forum, $cm);

    //hash the sql to get the cache file name
    $filename = rss_get_file_name($forum, $sql);
    $cachedfilepath = rss_get_file_full_name('mod_forum', $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 && forum_rss_newstuff($forum, $cm, $cachedfilelastmodified)) {
        //need to regenerate the cached version
        $result = forum_rss_feed_contents($forum, $sql);
        if (!empty($result)) {
            $status = rss_save_file('mod_forum',$filename,$result);
        }
    }

    //return the path to the cached version
    return $cachedfilepath;
}
function lightboxgallery_rss_feeds()
{
    global $CFG;
    $status = true;
    if (!$CFG->enablerssfeeds) {
        debugging('DISABLED (admin variables)');
    } else {
        if (!get_config('lightboxgallery', 'enablerssfeeds')) {
            debugging('DISABLED (module configuration)');
        } else {
            if ($galleries = get_records('lightboxgallery')) {
                foreach ($galleries as $gallery) {
                    if ($gallery->rss && $status) {
                        $filename = rss_file_name('lightboxgallery', $gallery);
                        if (file_exists($filename)) {
                            if ($lastmodified = filemtime($filename)) {
                                if ($lastmodified > time() - HOURSECS) {
                                    continue;
                                }
                            }
                        }
                        if (!instance_is_visible('lightboxgallery', $gallery)) {
                            if (file_exists($filename)) {
                                @unlink($filename);
                            }
                            continue;
                        }
                        mtrace('Updating RSS feed for ' . format_string($gallery->name, true) . ', ID: ' . $gallery->id);
                        $result = lightboxgallery_rss_feed($gallery);
                        if (!empty($result)) {
                            $status = rss_save_file('lightboxgallery', $gallery, $result);
                        }
                        if (debugging()) {
                            if (empty($result)) {
                                echo 'ID: ' . $gallery->id . '-> (empty) ';
                            } else {
                                if (!empty($status)) {
                                    echo 'ID: ' . $gallery->id . '-> OK ';
                                } else {
                                    echo 'ID: ' . $gallery->id . '-> FAIL ';
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return $status;
}
/**
 * Returns the path to the cached rss feed contents. Creates/updates the cache if necessary.
 * @param stdClass $context the context
 * @param array    $args    the arguments received in the url
 * @return string the full path to the cached RSS feed directory. Null if there is a problem.
 */
function hsuforum_rss_get_feed($context, $args)
{
    global $DB;
    $config = get_config('hsuforum');
    //are RSS feeds enabled?
    if (empty($config->enablerssfeeds)) {
        debugging('DISABLED (module configuration)');
        return null;
    }
    $forumid = clean_param($args[3], PARAM_INT);
    $cm = get_coursemodule_from_instance('hsuforum', $forumid, 0, false, MUST_EXIST);
    $modcontext = context_module::instance($cm->id);
    //context id from db should match the submitted one
    if ($context->id != $modcontext->id || !has_capability('mod/hsuforum:viewdiscussion', $modcontext)) {
        return null;
    }
    $forum = $DB->get_record('hsuforum', array('id' => $forumid), '*', MUST_EXIST);
    if (!rss_enabled_for_mod('hsuforum', $forum)) {
        return null;
    }
    //the sql that will retreive the data for the feed and be hashed to get the cache filename
    list($sql, $params) = hsuforum_rss_get_sql($forum, $cm);
    // Hash the sql to get the cache file name.
    $filename = rss_get_file_name($forum, $sql, $params);
    $cachedfilepath = rss_get_file_full_name('mod_hsuforum', $filename);
    //Is the cache out of date?
    $cachedfilelastmodified = 0;
    if (file_exists($cachedfilepath)) {
        $cachedfilelastmodified = filemtime($cachedfilepath);
    }
    // Used to determine if we need to generate a new RSS feed.
    $dontrecheckcutoff = time() - 60;
    // If it hasn't been generated we will need to create it, otherwise only update
    // if there is new stuff to show and it is older than the cut off date set above.
    if ($cachedfilelastmodified == 0 || $dontrecheckcutoff > $cachedfilelastmodified && hsuforum_rss_newstuff($forum, $cm, $cachedfilelastmodified)) {
        // Need to regenerate the cached version.
        $result = hsuforum_rss_feed_contents($forum, $sql, $params, $modcontext);
        rss_save_file('mod_hsuforum', $filename, $result);
    }
    //return the path to the cached version
    return $cachedfilepath;
}
Exemple #6
0
/**
 * This function saves to file the rss feed specified in the parameters
 *
 * @param string $type     The source of the RSS feed (site/course/group/user)
 * @param int    $id       The id of the item defined by $type
 * @param int    $tagid    The id of the row in the tag table that identifies the RSS Feed
 * @param string $contents The contents of the RSS Feed file
 * @return bool whether the save was successful or not
 */
function blog_rss_save_file($type, $id, $tagid = 0, $contents = '')
{
    global $CFG;
    $status = true;
    //blog creates some additional dirs within the rss cache so make sure they all exist
    make_cache_directory('rss/blog');
    make_cache_directory('rss/blog/' . $type);
    $filename = blog_rss_file_name($type, $id, $tagid);
    $expandfilename = false;
    //we're supplying a full file path
    $status = rss_save_file('blog', $filename, $contents, $expandfilename);
    return $status;
}
Exemple #7
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;
}
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;
}
Exemple #9
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;
}
Exemple #10
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";
}
Exemple #11
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;
}
Exemple #12
0
/**
 * This function is the main entry point to pcast
 * rss feeds generation.
 *
 * @global object $CFG
 * @global object $DB
 * @param string? $context
 * @param array $args
 * @return string (path)
 */
function pcast_rss_get_feed($context, $args)
{
    global $CFG, $DB;
    if (empty($CFG->pcast_enablerssfeeds)) {
        debugging("DISABLED (module configuration)");
        return null;
    }
    $status = true;
    $token = clean_param($args[1], PARAM_ALPHANUM);
    $pcastid = clean_param($args[3], PARAM_INT);
    $groupid = clean_param($args[4], PARAM_INT);
    $uservalidated = false;
    //check capabilities
    $cm = get_coursemodule_from_instance('pcast', $pcastid, 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/pcast:view', $modcontext)) {
            $uservalidated = true;
        }
    }
    //get userid from Token
    $userid = rss_get_userid_from_token($token);
    //Check group mode 0/1/2 (All participants)
    $groupmode = groups_get_activity_groupmode($cm);
    //Using Groups, check to see if user should see all participants
    if ($groupmode == SEPARATEGROUPS) {
        //User must have the capability to see all groups or be a member of that group
        $members = get_enrolled_users($context, 'mod/pcast:write', $groupid, 'u.id', 'u.id ASC');
        // Is a member of the current group
        if (!isset($members[$userid]->id) or $members[$userid]->id != $userid) {
            // Not a member of the group, can you see all groups (from CAPS)
            if (!has_capability('moodle/site:accessallgroups', $context, $userid)) {
                $uservalidated = false;
            }
        } else {
            // Are a member of the current group
            // Is the group #0 (Group 0 is all users)
            if ($groupid == 0 and !has_capability('moodle/site:accessallgroups', $context, $userid)) {
                $uservalidated = false;
            }
        }
    }
    if (!$uservalidated) {
        return null;
    }
    // OK the user can view the RSS feed
    $pcast = $DB->get_record('pcast', array('id' => $pcastid), '*', MUST_EXIST);
    // Check to se if RSS is enabled
    // NOTE: cannot use the rss_enabled_for_mod() function due to the functions internals and naming conflicts
    if ($pcast->rssepisodes == 0 || empty($pcast->rssepisodes)) {
        return null;
    }
    $sql = pcast_rss_get_sql($pcast);
    //get the cache file info
    $filename = rss_get_file_name($pcast, $sql);
    //Append the GroupID to the end of the filename
    $filename .= '_' . $groupid;
    $cachedfilepath = rss_get_file_full_name('mod_pcast', $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 && pcast_rss_newstuff($pcast, $cachedfilelastmodified)) {
        if (!($recs = $DB->get_records_sql($sql, array(), 0, $pcast->rssepisodes))) {
            return null;
        }
        $items = array();
        $formatoptions = new stdClass();
        $formatoptions->trusttext = true;
        foreach ($recs as $rec) {
            $item = new stdClass();
            $user = new stdClass();
            $item->title = $rec->episodename;
            $item->pcastid = $rec->pcastid;
            $item->id = $rec->episodeid;
            $item->userid = $rec->userid;
            $item->course = $rec->course;
            if ($pcast->displayauthor == 1) {
                //With author
                $user->firstname = $rec->userfirstname;
                $user->lastname = $rec->userlastname;
                $item->author = fullname($user);
            }
            $item->keywords = $rec->keywords;
            $item->subtitle = $rec->subtitle;
            $item->duration = $rec->duration;
            $item->pubdate = $rec->episodetimecreated;
            $item->link = new moodle_url('/mod/pcast/showepisode.php', array('eid' => $rec->episodeid));
            $item->description = format_text($rec->episodesummary, 'HTML', NULL, $pcast->course);
            // $item->description = strip_tags($rec->episodesummary);
            if ($pcast->userscancategorize) {
                //TODO: This is very inefficient (this generates 2 DB queries per entry)
                $category = pcast_rss_category_lookup($rec);
                $item->topcategory = $category->top->name;
                $item->nestedcategory = $category->nested->name;
            }
            $items[] = $item;
        }
        //First all rss feeds common headers
        $url = new moodle_url('/mod/pcast/view.php', array('id' => $pcast->id));
        $header = pcast_rss_header(format_string($pcast->name, true), $url, format_string($pcast->intro, true), $pcast);
        // Do we need iTunes tags?
        if (isset($pcast->enablerssitunes) && $pcast->enablerssitunes == 1) {
            $itunes = true;
        } else {
            $itunes = false;
        }
        //Now all the rss items
        if (!empty($header)) {
            $episodes = pcast_rss_add_items($context, $items, $itunes, $groupid);
        }
        //Now all rss feeds common footers
        if (!empty($header) && !empty($episodes)) {
            $footer = pcast_rss_footer();
        }
        //Now, if everything is ok, concatenate it
        if (!empty($header) && !empty($episodes) && !empty($footer)) {
            $rss = $header . $episodes . $footer;
            //Save the XML contents to file.
            $status = rss_save_file('mod_pcast', $filename, $rss);
        }
    }
    if (!$status) {
        $cachedfilepath = null;
    }
    return $cachedfilepath;
}