[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/data/ -> rsslib.php (source)

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  
  19  /**
  20   * This file contains all the common stuff to be used for RSS in the Database Module
  21   *
  22   * @package    mod_data
  23   * @category   rss
  24   * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  /**
  29   * Generates the Database modules RSS Feed
  30   *
  31   * @param strClass $context the context the feed should be created under
  32   * @param array $args array of arguments to be used in the creation of the RSS Feed
  33   * @return NULL|string NULL if there is nothing to return, or the file path of the cached file if there is
  34   */
  35      function data_rss_get_feed($context, $args) {
  36          global $CFG, $DB;
  37  
  38          // Check CFG->data_enablerssfeeds.
  39          if (empty($CFG->data_enablerssfeeds)) {
  40              debugging("DISABLED (module configuration)");
  41              return null;
  42          }
  43  
  44          $dataid = clean_param($args[3], PARAM_INT);
  45          $cm = get_coursemodule_from_instance('data', $dataid, 0, false, MUST_EXIST);
  46          if ($cm) {
  47              $modcontext = context_module::instance($cm->id);
  48  
  49              //context id from db should match the submitted one
  50              if ($context->id != $modcontext->id || !has_capability('mod/data:viewentry', $modcontext)) {
  51                  return null;
  52              }
  53          }
  54  
  55          $data = $DB->get_record('data', array('id' => $dataid), '*', MUST_EXIST);
  56          if (!rss_enabled_for_mod('data', $data, false, true)) {
  57              return null;
  58          }
  59  
  60          $sql = data_rss_get_sql($data);
  61  
  62          //get the cache file info
  63          $filename = rss_get_file_name($data, $sql);
  64          $cachedfilepath = rss_get_file_full_name('mod_data', $filename);
  65  
  66          //Is the cache out of date?
  67          $cachedfilelastmodified = 0;
  68          if (file_exists($cachedfilepath)) {
  69              $cachedfilelastmodified = filemtime($cachedfilepath);
  70          }
  71          //if the cache is more than 60 seconds old and there's new stuff
  72          $dontrecheckcutoff = time()-60;
  73          if ( $dontrecheckcutoff > $cachedfilelastmodified && data_rss_newstuff($data, $cachedfilelastmodified)) {
  74              require_once($CFG->dirroot . '/mod/data/lib.php');
  75  
  76              // Get the first field in the list  (a hack for now until we have a selector)
  77              if (!$firstfield = $DB->get_record_sql('SELECT id,name FROM {data_fields} WHERE dataid = ? ORDER by id', array($data->id), true)) {
  78                  return null;
  79              }
  80  
  81              if (!$records = $DB->get_records_sql($sql, array(), 0, $data->rssarticles)) {
  82                  return null;
  83              }
  84  
  85              $firstrecord = array_shift($records);  // Get the first and put it back
  86              array_unshift($records, $firstrecord);
  87  
  88              // Now create all the articles
  89              $items = array();
  90              foreach ($records as $record) {
  91                  $recordarray = array();
  92                  array_push($recordarray, $record);
  93  
  94                  $item = null;
  95  
  96                  // guess title or not
  97                  if (!empty($data->rsstitletemplate)) {
  98                      $item->title = data_print_template('rsstitletemplate', $recordarray, $data, '', 0, true);
  99                  } else { // else we guess
 100                      $item->title   = strip_tags($DB->get_field('data_content', 'content',
 101                                                        array('fieldid'=>$firstfield->id, 'recordid'=>$record->id)));
 102                  }
 103                  $item->description = data_print_template('rsstemplate', $recordarray, $data, '', 0, true);
 104                  $item->pubdate = $record->timecreated;
 105                  $item->link = $CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&rid='.$record->id;
 106  
 107                  array_push($items, $item);
 108              }
 109              $course = $DB->get_record('course', array('id'=>$data->course));
 110              $coursecontext = context_course::instance($course->id);
 111              $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
 112  
 113              // First all rss feeds common headers.
 114              $header = rss_standard_header($courseshortname . ': ' . format_string($data->name, true, array('context' => $context)),
 115                                            $CFG->wwwroot."/mod/data/view.php?d=".$data->id,
 116                                            format_text($data->intro, $data->introformat, array('context' => $context)));
 117  
 118              if (!empty($header)) {
 119                  $articles = rss_add_items($items);
 120              }
 121  
 122              // Now all rss feeds common footers.
 123              if (!empty($header) && !empty($articles)) {
 124                  $footer = rss_standard_footer();
 125              }
 126              // Now, if everything is ok, concatenate it.
 127              if (!empty($header) && !empty($articles) && !empty($footer)) {
 128                  $rss = $header.$articles.$footer;
 129  
 130                  //Save the XML contents to file.
 131                  $status = rss_save_file('mod_data', $filename, $rss);
 132              }
 133          }
 134  
 135          return $cachedfilepath;
 136      }
 137  /**
 138   * Creates and returns a SQL query for the items that would be included in the RSS Feed
 139   *
 140   * @param stdClass $data database instance object
 141   * @param int      $time epoch timestamp to compare time-modified to, 0 for all or a proper value
 142   * @return string SQL query string to get the items for the databse RSS Feed
 143   */
 144      function data_rss_get_sql($data, $time=0) {
 145          //do we only want new posts?
 146          if ($time) {
 147              $time = " AND dr.timemodified > '$time'";
 148          } else {
 149              $time = '';
 150          }
 151  
 152          $approved = ($data->approval) ? ' AND dr.approved = 1 ' : ' ';
 153  
 154          $sql = "SELECT dr.*, u.firstname, u.lastname
 155                    FROM {data_records} dr, {user} u
 156                   WHERE dr.dataid = {$data->id} $approved
 157                         AND dr.userid = u.id $time
 158                ORDER BY dr.timecreated DESC";
 159  
 160          return $sql;
 161      }
 162  
 163      /**
 164       * If there is new stuff in since $time this returns true
 165       * Otherwise it returns false.
 166       *
 167       * @param stdClass $data the data activity object
 168       * @param int      $time timestamp
 169       * @return bool true if there is new stuff for the RSS Feed
 170       */
 171      function data_rss_newstuff($data, $time) {
 172          global $DB;
 173  
 174          $sql = data_rss_get_sql($data, $time);
 175  
 176          $recs = $DB->get_records_sql($sql, null, 0, 1);//limit of 1. If we get even 1 back we have new stuff
 177          return ($recs && !empty($recs));
 178      }
 179  
 180      /**
 181       * Given a database object, deletes all cached RSS files associated with it.
 182       *
 183       * @param stdClass $data
 184       */
 185      function data_rss_delete_file($data) {
 186          global $CFG;
 187          require_once("$CFG->libdir/rsslib.php");
 188  
 189          rss_delete_file('mod_data', $data);
 190      }
 191  


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1