Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402]

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