/** * 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; }
/** * 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; }
/** * 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; }
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; }
/** * 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"; }