Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

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