Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/data/rsslib.php
Go to the documentation of this file.
00001 <?php
00002     // This file adds support to rss feeds generation
00003 
00004     // This function is the main entry point to database module
00005     // rss feeds generation.
00006     function data_rss_get_feed($context, $args) {
00007         global $CFG, $DB;
00008 
00009         // Check CFG->data_enablerssfeeds.
00010         if (empty($CFG->data_enablerssfeeds)) {
00011             debugging("DISABLED (module configuration)");
00012             return null;
00013         }
00014 
00015         $dataid = clean_param($args[3], PARAM_INT);
00016         $cm = get_coursemodule_from_instance('data', $dataid, 0, false, MUST_EXIST);
00017         if ($cm) {
00018             $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
00019 
00020             //context id from db should match the submitted one
00021             if ($context->id != $modcontext->id || !has_capability('mod/data:viewentry', $modcontext)) {
00022                 return null;
00023             }
00024         }
00025 
00026         $data = $DB->get_record('data', array('id' => $dataid), '*', MUST_EXIST);
00027         if (!rss_enabled_for_mod('data', $data, false, true)) {
00028             return null;
00029         }
00030 
00031         $sql = data_rss_get_sql($data);
00032 
00033         //get the cache file info
00034         $filename = rss_get_file_name($data, $sql);
00035         $cachedfilepath = rss_get_file_full_name('mod_data', $filename);
00036 
00037         //Is the cache out of date?
00038         $cachedfilelastmodified = 0;
00039         if (file_exists($cachedfilepath)) {
00040             $cachedfilelastmodified = filemtime($cachedfilepath);
00041         }
00042         //if the cache is more than 60 seconds old and there's new stuff
00043         $dontrecheckcutoff = time()-60;
00044         if ( $dontrecheckcutoff > $cachedfilelastmodified && data_rss_newstuff($data, $cachedfilelastmodified)) {
00045             require_once($CFG->dirroot . '/mod/data/lib.php');
00046 
00047             // Get the first field in the list  (a hack for now until we have a selector)
00048             if (!$firstfield = $DB->get_record_sql('SELECT id,name FROM {data_fields} WHERE dataid = ? ORDER by id', array($data->id), true)) {
00049                 return null;
00050             }
00051 
00052             if (!$records = $DB->get_records_sql($sql, array(), 0, $data->rssarticles)) {
00053                 return null;
00054             }
00055             
00056             $firstrecord = array_shift($records);  // Get the first and put it back
00057             array_unshift($records, $firstrecord);
00058 
00059             // Now create all the articles
00060             $items = array();
00061             foreach ($records as $record) {
00062                 $recordarray = array();
00063                 array_push($recordarray, $record);
00064 
00065                 $item = null;
00066 
00067                 // guess title or not
00068                 if (!empty($data->rsstitletemplate)) {
00069                     $item->title = data_print_template('rsstitletemplate', $recordarray, $data, '', 0, true);
00070                 } else { // else we guess
00071                     $item->title   = strip_tags($DB->get_field('data_content', 'content',
00072                                                       array('fieldid'=>$firstfield->id, 'recordid'=>$record->id)));
00073                 }
00074                 $item->description = data_print_template('rsstemplate', $recordarray, $data, '', 0, true);
00075                 $item->pubdate = $record->timecreated;
00076                 $item->link = $CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&rid='.$record->id;
00077 
00078                 array_push($items, $item);
00079             }
00080             $course = $DB->get_record('course', array('id'=>$data->course));
00081             $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
00082             $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
00083 
00084             // First all rss feeds common headers.
00085             $header = rss_standard_header($courseshortname . ': ' . format_string($data->name, true, array('context' => $context)),
00086                                           $CFG->wwwroot."/mod/data/view.php?d=".$data->id,
00087                                           format_text($data->intro, $data->introformat, array('context' => $context)));
00088 
00089             if (!empty($header)) {
00090                 $articles = rss_add_items($items);
00091             }
00092 
00093             // Now all rss feeds common footers.
00094             if (!empty($header) && !empty($articles)) {
00095                 $footer = rss_standard_footer();
00096             }
00097             // Now, if everything is ok, concatenate it.
00098             if (!empty($header) && !empty($articles) && !empty($footer)) {
00099                 $rss = $header.$articles.$footer;
00100 
00101                 //Save the XML contents to file.
00102                 $status = rss_save_file('mod_data', $filename, $rss);
00103             }
00104         }
00105 
00106         return $cachedfilepath;
00107     }
00108 
00109     function data_rss_get_sql($data, $time=0) {
00110         //do we only want new posts?
00111         if ($time) {
00112             $time = " AND dr.timemodified > '$time'";
00113         } else {
00114             $time = '';
00115         }
00116 
00117         $approved = ($data->approval) ? ' AND dr.approved = 1 ' : ' ';
00118 
00119         $sql = "SELECT dr.*, u.firstname, u.lastname
00120                   FROM {data_records} dr, {user} u
00121                  WHERE dr.dataid = {$data->id} $approved
00122                        AND dr.userid = u.id $time
00123               ORDER BY dr.timecreated DESC";
00124 
00125         return $sql;
00126     }
00127 
00136     function data_rss_newstuff($data, $time) {
00137         global $DB;
00138 
00139         $sql = data_rss_get_sql($data, $time);
00140 
00141         $recs = $DB->get_records_sql($sql, null, 0, 1);//limit of 1. If we get even 1 back we have new stuff
00142         return ($recs && !empty($recs));
00143     }
00144 
 All Data Structures Namespaces Files Functions Variables Enumerations