[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/blog/ -> rsslib.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Blog RSS Management
  19   *
  20   * @package    core_blog
  21   * @category   rss
  22   * @copyright  2010 Andrew Davis
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  require_once($CFG->dirroot.'/lib/rsslib.php');
  26  require_once($CFG->dirroot .'/blog/lib.php');
  27  
  28  /**
  29   * Build the URL for the RSS feed
  30   *
  31   * @param int    $contextid    The context under which the URL should be created
  32   * @param int    $userid       The id of the user requesting the RSS Feed
  33   * @param string $filtertype   The source of the RSS feed (site/course/group/user)
  34   * @param int    $filterselect The id of the item defined by $filtertype
  35   * @param int    $tagid        The id of the row in the tag table that identifies the RSS Feed
  36   * @return string
  37   */
  38  function blog_rss_get_url($contextid, $userid, $filtertype, $filterselect = 0, $tagid = 0) {
  39      $componentname = 'blog';
  40  
  41      $additionalargs = null;
  42      switch ($filtertype) {
  43          case 'site':
  44              $additionalargs = 'site/'.SITEID;
  45          break;
  46          case 'course':
  47              $additionalargs = 'course/'.$filterselect;
  48          break;
  49          case 'group':
  50              $additionalargs = 'group/'.$filterselect;
  51          break;
  52          case 'user':
  53              $additionalargs = 'user/'.$filterselect;
  54          break;
  55      }
  56  
  57      if ($tagid) {
  58          $additionalargs .= '/'.$tagid;
  59      }
  60  
  61      return rss_get_url($contextid, $userid, $componentname, $additionalargs);
  62  }
  63  
  64  /**
  65   * Print the link for the RSS feed with the correct RSS icon (Theme based)
  66   *
  67   * @param stdClass    $context      The context under which the URL should be created
  68   * @param string      $filtertype   The source of the RSS feed (site/course/group/user)
  69   * @param int         $filterselect The id of the item defined by $filtertype
  70   * @param int         $tagid        The id of the row in the tag table that identifies the RSS Feed
  71   * @param string      $tooltiptext  The tooltip to be displayed with the link
  72   */
  73  function blog_rss_print_link($context, $filtertype, $filterselect = 0, $tagid = 0, $tooltiptext = '') {
  74      global $CFG, $USER, $OUTPUT;
  75  
  76      if (!isloggedin()) {
  77          $userid = $CFG->siteguest;
  78      } else {
  79          $userid = $USER->id;
  80      }
  81  
  82      $url = blog_rss_get_url($context->id, $userid, $filtertype, $filterselect, $tagid);
  83      $rsspix = $OUTPUT->pix_url('i/rss');
  84      print '<div class="mdl-right"><a href="'. $url .'"><img src="'. $rsspix .'" title="'. strip_tags($tooltiptext) .'" alt="'.get_string('rss').'" /></a></div>';
  85  }
  86  
  87  /**
  88   * Build the URL for the RSS feed amd add it as a header
  89   *
  90   * @param stdClass    $context      The context under which the URL should be created
  91   * @param string      $title        Name for the link to be added to the page header
  92   * @param string      $filtertype   The source of the RSS feed (site/course/group/user)
  93   * @param int         $filterselect The id of the item defined by $filtertype
  94   * @param int         $tagid        The id of the row in the tag table that identifies the RSS Feed
  95   */
  96  function blog_rss_add_http_header($context, $title, $filtertype, $filterselect = 0, $tagid = 0) {
  97      global $PAGE, $USER, $CFG;
  98  
  99      if (!isloggedin()) {
 100          $userid = $CFG->siteguest;
 101      } else {
 102          $userid = $USER->id;
 103      }
 104  
 105      $rsspath = blog_rss_get_url($context->id, $userid, $filtertype, $filterselect, $tagid);
 106      $PAGE->add_alternate_version($title, $rsspath, 'application/rss+xml');
 107  }
 108  
 109  /**
 110   * Utility function to extract parameters needed to generate RSS URLs from the blog filters
 111   *
 112   * @param  array $filters filters for the blog
 113   * @return array array containing the id of the user/course/group, the relevant context and the filter type: site/user/course/group
 114   */
 115  function blog_rss_get_params($filters) {
 116      $thingid = $rsscontext = $filtertype = null;
 117  
 118      $sitecontext = context_system::instance();
 119  
 120      if (!$filters) {
 121          $thingid = SITEID;
 122          $filtertype = 'site';
 123      } else if (array_key_exists('course', $filters)) {
 124          $thingid = $filters['course'];
 125          $filtertype = 'course';
 126      } else if (array_key_exists('user', $filters)) {
 127          $thingid = $filters['user'];
 128          $filtertype = 'user';
 129      } else if (array_key_exists('group', $filters)) {
 130          $thingid = $filters['group'];
 131          $filtertype = 'group';
 132      }
 133  
 134      return array($thingid, $rsscontext, $filtertype);
 135  }
 136  
 137  /**
 138   * Generate any blog RSS feed via one function
 139   *
 140   * @param stdClass $context The context of the blog for which the feed it being generated
 141   * @param array    $args    An array of arguements needed to build the feed (contextid, token, componentname, type, id, tagid)
 142   */
 143  function blog_rss_get_feed($context, $args) {
 144      global $CFG, $SITE, $DB;
 145  
 146      if (empty($CFG->enableblogs)) {
 147          debugging('Blogging disabled on this site, RSS feeds are not available');
 148          return null;
 149      }
 150  
 151      if (empty($CFG->enablerssfeeds)) {
 152          debugging('Sorry, RSS feeds are disabled on this site');
 153          return '';
 154      }
 155  
 156      if ($CFG->bloglevel == BLOG_SITE_LEVEL) {
 157          if (isguestuser()) {
 158              debugging(get_string('nopermissiontoshow', 'error'));
 159              return '';
 160          }
 161      }
 162  
 163      $sitecontext = context_system::instance();
 164      if (!has_capability('moodle/blog:view', $sitecontext)) {
 165          return null;
 166      }
 167  
 168      $type  = clean_param($args[3], PARAM_ALPHA);
 169      $id = clean_param($args[4], PARAM_INT);  // Could be groupid / courseid  / userid  depending on $type.
 170  
 171      $tagid = 0;
 172      if ($args[5] != 'rss.xml') {
 173          $tagid = clean_param($args[5], PARAM_INT);
 174      } else {
 175          $tagid = 0;
 176      }
 177  
 178      $filename = blog_rss_file_name($type, $id, $tagid);
 179  
 180      if (file_exists($filename)) {
 181          if (filemtime($filename) + 3600 > time()) {
 182              return $filename;   // It's already done so we return cached version.
 183          }
 184      }
 185  
 186      $courseid = $groupid = $userid = null;
 187      switch ($type) {
 188          case 'site':
 189              break;
 190          case 'course':
 191              $courseid = $id;
 192              break;
 193          case 'group':
 194              $groupid = $id;
 195              break;
 196          case 'user':
 197              $userid = $id;
 198              break;
 199      }
 200  
 201      // Get all the entries from the database.
 202      require_once($CFG->dirroot .'/blog/locallib.php');
 203      $blogheaders = blog_get_headers($courseid, $groupid, $userid, $tagid);
 204  
 205      $bloglisting = new blog_listing($blogheaders['filters']);
 206      $blogentries = $bloglisting->get_entries();
 207  
 208      // Now generate an array of RSS items.
 209      if ($blogentries) {
 210          $items = array();
 211          foreach ($blogentries as $blogentry) {
 212              $item = null;
 213              $item->author = fullname($DB->get_record('user', array('id' => $blogentry->userid))); // TODO: this is slow.
 214              $item->title = $blogentry->subject;
 215              $item->pubdate = $blogentry->lastmodified;
 216              $item->link = $CFG->wwwroot.'/blog/index.php?entryid='.$blogentry->id;
 217              $summary = file_rewrite_pluginfile_urls($blogentry->summary, 'pluginfile.php',
 218                  $sitecontext->id, 'blog', 'post', $blogentry->id);
 219              $item->description = format_text($summary, $blogentry->format);
 220              if ($blogtags = core_tag_tag::get_item_tags_array('core', 'post', $blogentry->id)) {
 221                  $item->tags = $blogtags;
 222                  $item->tagscheme = $CFG->wwwroot . '/tag';
 223              }
 224              $items[] = $item;
 225          }
 226          $articles = rss_add_items($items);   // Change structure to XML.
 227      } else {
 228          $articles = '';
 229      }
 230  
 231      // Get header and footer information.
 232  
 233      switch ($type) {
 234          case 'user':
 235              $info = fullname($DB->get_record('user', array('id' => $id), 'firstname,lastname'));
 236              break;
 237          case 'course':
 238              $info = $DB->get_field('course', 'fullname', array('id' => $id));
 239              $info = format_string($info, true, array('context' => context_course::instance($id)));
 240              break;
 241          case 'site':
 242              $info = format_string($SITE->fullname, true, array('context' => context_course::instance(SITEID)));
 243              break;
 244          case 'group':
 245              $group = groups_get_group($id);
 246              $info = $group->name; // TODO: $DB->get_field('groups', 'name', array('id' => $id)).
 247              break;
 248          default:
 249              $info = '';
 250              break;
 251      }
 252  
 253      if ($tagid) {
 254          $info .= ': '.$DB->get_field('tags', 'text', array('id' => $tagid));
 255      }
 256  
 257      $header = rss_standard_header(get_string($type.'blog', 'blog', $info),
 258                                    $CFG->wwwroot.'/blog/index.php',
 259                                    get_string('intro', 'blog'));
 260  
 261      $footer = rss_standard_footer();
 262  
 263      // Save the XML contents to file.
 264      $rssdata = $header.$articles.$footer;
 265      if (blog_rss_save_file($type, $id, $tagid, $rssdata)) {
 266          return $filename;
 267      } else {
 268          return false;   // Couldn't find it or make it.
 269      }
 270  }
 271  
 272  /**
 273   * Retrieve the location and file name of a cached RSS feed
 274   *
 275   * @param string $type  The source of the RSS feed (site/course/group/user)
 276   * @param int    $id    The id of the item defined by $type
 277   * @param int    $tagid The id of the row in the tag table that identifies the RSS Feed
 278   * @return string
 279   */
 280  function blog_rss_file_name($type, $id, $tagid = 0) {
 281      global $CFG;
 282  
 283      if ($tagid) {
 284          return "$CFG->cachedir/rss/blog/$type/$id/$tagid.xml";
 285      } else {
 286          return "$CFG->cachedir/rss/blog/$type/$id.xml";
 287      }
 288  }
 289  
 290  /**
 291   * This function saves to file the rss feed specified in the parameters
 292   *
 293   * @param string $type     The source of the RSS feed (site/course/group/user)
 294   * @param int    $id       The id of the item defined by $type
 295   * @param int    $tagid    The id of the row in the tag table that identifies the RSS Feed
 296   * @param string $contents The contents of the RSS Feed file
 297   * @return bool whether the save was successful or not
 298   */
 299  function blog_rss_save_file($type, $id, $tagid = 0, $contents = '') {
 300      global $CFG;
 301  
 302      $status = true;
 303  
 304      // Blog creates some additional dirs within the rss cache so make sure they all exist.
 305      make_cache_directory('rss/blog');
 306      make_cache_directory('rss/blog/'.$type);
 307  
 308      $filename = blog_rss_file_name($type, $id, $tagid);
 309      $expandfilename = false; // We are supplying a full file path.
 310      $status = rss_save_file('blog', $filename, $contents, $expandfilename);
 311  
 312      return $status;
 313  }
 314  
 315  /**
 316   * Delete the supplied user's cached blog post RSS feed.
 317   * Only user blogs are available by RSS.
 318   * This doesn't call rss_delete_file() as blog RSS caching uses it's own file structure.
 319   *
 320   * @param int $userid
 321   */
 322  function blog_rss_delete_file($userid) {
 323      $filename = blog_rss_file_name('user', $userid);
 324      if (file_exists($filename)) {
 325          unlink($filename);
 326      }
 327  }
 328  


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1