Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/glossary/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 glossary
00005     //rss feeds generation.
00006     function glossary_rss_get_feed($context, $args) {
00007         global $CFG, $DB, $COURSE, $USER;
00008 
00009         $status = true;
00010 
00011         if (empty($CFG->glossary_enablerssfeeds)) {
00012             debugging("DISABLED (module configuration)");
00013             return null;
00014         }
00015 
00016         $glossaryid  = clean_param($args[3], PARAM_INT);
00017         $cm = get_coursemodule_from_instance('glossary', $glossaryid, 0, false, MUST_EXIST);
00018         if ($cm) {
00019             $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
00020             if ($COURSE->id == $cm->course) {
00021                 $course = $COURSE;
00022             } else {
00023                 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
00024             }
00025             //context id from db should match the submitted one
00026             //no specific capability required to view glossary entries so just check user is enrolled
00027             if ($context->id != $modcontext->id || !can_access_course($course, $USER)) {
00028                 return null;
00029             }
00030         }
00031 
00032         $glossary = $DB->get_record('glossary', array('id' => $glossaryid), '*', MUST_EXIST);
00033         if (!rss_enabled_for_mod('glossary', $glossary)) {
00034             return null;
00035         }
00036 
00037         $sql = glossary_rss_get_sql($glossary);
00038 
00039         //get the cache file info
00040         $filename = rss_get_file_name($glossary, $sql);
00041         $cachedfilepath = rss_get_file_full_name('mod_glossary', $filename);
00042 
00043         //Is the cache out of date?
00044         $cachedfilelastmodified = 0;
00045         if (file_exists($cachedfilepath)) {
00046             $cachedfilelastmodified = filemtime($cachedfilepath);
00047         }
00048         //if the cache is more than 60 seconds old and there's new stuff
00049         $dontrecheckcutoff = time()-60;
00050         if ( $dontrecheckcutoff > $cachedfilelastmodified && glossary_rss_newstuff($glossary, $cachedfilelastmodified)) {
00051             if (!$recs = $DB->get_records_sql($sql, array(), 0, $glossary->rssarticles)) {
00052                 return null;
00053             }
00054 
00055             $items = array();
00056 
00057             $formatoptions = new stdClass();
00058             $formatoptions->trusttext = true;
00059 
00060             foreach ($recs as $rec) {
00061                 $item = new stdClass();
00062                 $user = new stdClass();
00063                 $item->title = $rec->entryconcept;
00064 
00065                 if ($glossary->rsstype == 1) {//With author
00066                     $user->firstname = $rec->userfirstname;
00067                     $user->lastname = $rec->userlastname;
00068 
00069                     $item->author = fullname($user);
00070                 }
00071 
00072                 $item->pubdate = $rec->entrytimecreated;
00073                 $item->link = $CFG->wwwroot."/mod/glossary/showentry.php?courseid=".$glossary->course."&eid=".$rec->entryid;
00074                 $item->description = format_text($rec->entrydefinition,$rec->entryformat,$formatoptions,$glossary->course);
00075                 $items[] = $item;
00076             }
00077 
00078             //First all rss feeds common headers
00079             $header = rss_standard_header(format_string($glossary->name,true),
00080                                           $CFG->wwwroot."/mod/glossary/view.php?g=".$glossary->id,
00081                                           format_string($glossary->intro,true));
00082             //Now all the rss items
00083             if (!empty($header)) {
00084                 $articles = rss_add_items($items);
00085             }
00086             //Now all rss feeds common footers
00087             if (!empty($header) && !empty($articles)) {
00088                 $footer = rss_standard_footer();
00089             }
00090             //Now, if everything is ok, concatenate it
00091             if (!empty($header) && !empty($articles) && !empty($footer)) {
00092                 $rss = $header.$articles.$footer;
00093 
00094                 //Save the XML contents to file.
00095                 $status = rss_save_file('mod_glossary', $filename, $rss);
00096             }
00097         }
00098 
00099         if (!$status) {
00100             $cachedfilepath = null;
00101         }
00102 
00103         return $cachedfilepath;
00104     }
00105 
00106     function glossary_rss_get_sql($glossary, $time=0) {
00107         //do we only want new items?
00108         if ($time) {
00109             $time = "AND e.timecreated > $time";
00110         } else {
00111             $time = "";
00112         }
00113 
00114         if ($glossary->rsstype == 1) {//With author
00115             $sql = "SELECT e.id AS entryid,
00116                       e.concept AS entryconcept,
00117                       e.definition AS entrydefinition,
00118                       e.definitionformat AS entryformat,
00119                       e.definitiontrust AS entrytrust,
00120                       e.timecreated AS entrytimecreated,
00121                       u.id AS userid,
00122                       u.firstname AS userfirstname,
00123                       u.lastname AS userlastname
00124                  FROM {glossary_entries} e,
00125                       {user} u
00126                 WHERE e.glossaryid = {$glossary->id} AND
00127                       u.id = e.userid AND
00128                       e.approved = 1 $time
00129              ORDER BY e.timecreated desc";
00130         } else {//Without author
00131             $sql = "SELECT e.id AS entryid,
00132                       e.concept AS entryconcept,
00133                       e.definition AS entrydefinition,
00134                       e.definitionformat AS entryformat,
00135                       e.definitiontrust AS entrytrust,
00136                       e.timecreated AS entrytimecreated,
00137                       u.id AS userid
00138                  FROM {glossary_entries} e,
00139                       {user} u
00140                 WHERE e.glossaryid = {$glossary->id} AND
00141                       u.id = e.userid AND
00142                       e.approved = 1 $time
00143              ORDER BY e.timecreated desc";
00144         }
00145 
00146         return $sql;
00147     }
00148 
00157     function glossary_rss_newstuff($glossary, $time) {
00158         global $DB;
00159 
00160         $sql = glossary_rss_get_sql($glossary, $time);
00161 
00162         $recs = $DB->get_records_sql($sql, null, 0, 1);//limit of 1. If we get even 1 back we have new stuff
00163         return ($recs && !empty($recs));
00164     }
00165 
00166 
 All Data Structures Namespaces Files Functions Variables Enumerations