Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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   * Script to let a user edit the properties of a particular RSS feed.
  19   *
  20   * @package   block_rss_client
  21   * @copyright 2009 Tim Hunt
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  
  26  require_once(__DIR__ . '/../../config.php');
  27  require_once($CFG->libdir . '/formslib.php');
  28  require_once($CFG->libdir .'/simplepie/moodle_simplepie.php');
  29  
  30  class feed_edit_form extends moodleform {
  31      protected $isadding;
  32      protected $caneditshared;
  33      protected $title = '';
  34      protected $description = '';
  35  
  36      function __construct($actionurl, $isadding, $caneditshared) {
  37          $this->isadding = $isadding;
  38          $this->caneditshared = $caneditshared;
  39          parent::__construct($actionurl);
  40      }
  41  
  42      function definition() {
  43          $mform =& $this->_form;
  44  
  45          // Then show the fields about where this block appears.
  46          $mform->addElement('header', 'rsseditfeedheader', get_string('feed', 'block_rss_client'));
  47  
  48          $mform->addElement('text', 'url', get_string('feedurl', 'block_rss_client'), array('size' => 60));
  49          $mform->setType('url', PARAM_URL);
  50          $mform->addRule('url', null, 'required');
  51  
  52          $mform->addElement('checkbox', 'autodiscovery', get_string('enableautodiscovery', 'block_rss_client'));
  53          $mform->setDefault('autodiscovery', 1);
  54          $mform->setAdvanced('autodiscovery');
  55          $mform->addHelpButton('autodiscovery', 'enableautodiscovery', 'block_rss_client');
  56  
  57          $mform->addElement('text', 'preferredtitle', get_string('customtitlelabel', 'block_rss_client'), array('size' => 60));
  58          $mform->setType('preferredtitle', PARAM_NOTAGS);
  59  
  60          if ($this->caneditshared) {
  61              $mform->addElement('selectyesno', 'shared', get_string('sharedfeed', 'block_rss_client'));
  62              $mform->setDefault('shared', 0);
  63          }
  64  
  65          $submitlabal = null; // Default
  66          if ($this->isadding) {
  67              $submitlabal = get_string('addnewfeed', 'block_rss_client');
  68          }
  69          $this->add_action_buttons(true, $submitlabal);
  70      }
  71  
  72      function definition_after_data(){
  73          $mform =& $this->_form;
  74  
  75          if($mform->getElementValue('autodiscovery')){
  76              $mform->applyFilter('url', 'feed_edit_form::autodiscover_feed_url');
  77          }
  78      }
  79  
  80      function validation($data, $files) {
  81          $errors = parent::validation($data, $files);
  82  
  83          $rss =  new moodle_simplepie();
  84          // set timeout for longer than normal to try and grab the feed
  85          $rss->set_timeout(10);
  86          $rss->set_feed_url($data['url']);
  87          $rss->set_autodiscovery_cache_duration(0);
  88          $rss->set_autodiscovery_level(SIMPLEPIE_LOCATOR_NONE);
  89          $rss->init();
  90  
  91          if ($rss->error()) {
  92              $errors['url'] = get_string('couldnotfindloadrssfeed', 'block_rss_client');
  93          } else {
  94              $this->title = $rss->get_title();
  95              $this->description = $rss->get_description();
  96          }
  97  
  98          return $errors;
  99      }
 100  
 101      function get_data() {
 102          $data = parent::get_data();
 103          if ($data) {
 104              $data->title = '';
 105              $data->description = '';
 106  
 107              if($this->title){
 108                  $data->title = $this->title;
 109              }
 110  
 111              if($this->description){
 112                  $data->description = $this->description;
 113              }
 114          }
 115          return $data;
 116      }
 117  
 118      /**
 119       * Autodiscovers a feed url from a given url, to be used by the formslibs
 120       * filter function
 121       *
 122       * Uses simplepie with autodiscovery set to maximum level to try and find
 123       * a feed to subscribe to.
 124       * See: http://simplepie.org/wiki/reference/simplepie/set_autodiscovery_level
 125       *
 126       * @param string URL to autodiscover a url
 127       * @return string URL of feed or original url if none found
 128       */
 129      public static function autodiscover_feed_url($url){
 130              $rss =  new moodle_simplepie();
 131              $rss->set_feed_url($url);
 132              $rss->set_autodiscovery_level(SIMPLEPIE_LOCATOR_ALL);
 133              // When autodiscovering an RSS feed, simplepie will try lots of
 134              // rss links on a page, so set the timeout high
 135              $rss->set_timeout(20);
 136              $rss->init();
 137  
 138              if($rss->error()){
 139                  return $url;
 140              }
 141  
 142              // return URL without quoting..
 143              $discoveredurl = new moodle_url($rss->subscribe_url());
 144              return $discoveredurl->out(false);
 145      }
 146  }
 147  
 148  $returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
 149  $courseid = optional_param('courseid', 0, PARAM_INT);
 150  $rssid = optional_param('rssid', 0, PARAM_INT); // 0 mean create new.
 151  
 152  if ($courseid == SITEID) {
 153      $courseid = 0;
 154  }
 155  if ($courseid) {
 156      $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
 157      $PAGE->set_course($course);
 158      $context = $PAGE->context;
 159  } else {
 160      $context = context_system::instance();
 161      $PAGE->set_context($context);
 162  }
 163  
 164  $managesharedfeeds = has_capability('block/rss_client:manageanyfeeds', $context);
 165  if (!$managesharedfeeds) {
 166      require_capability('block/rss_client:manageownfeeds', $context);
 167  }
 168  
 169  $urlparams = array('rssid' => $rssid);
 170  if ($courseid) {
 171      $urlparams['courseid'] = $courseid;
 172  }
 173  if ($returnurl) {
 174      $urlparams['returnurl'] = $returnurl;
 175  }
 176  $managefeeds = new moodle_url('/blocks/rss_client/managefeeds.php', $urlparams);
 177  
 178  $PAGE->set_url('/blocks/rss_client/editfeed.php', $urlparams);
 179  $PAGE->set_pagelayout('admin');
 180  
 181  if ($rssid) {
 182      $isadding = false;
 183      $rssrecord = $DB->get_record('block_rss_client', array('id' => $rssid), '*', MUST_EXIST);
 184  } else {
 185      $isadding = true;
 186      $rssrecord = new stdClass;
 187  }
 188  
 189  $mform = new feed_edit_form($PAGE->url, $isadding, $managesharedfeeds);
 190  $mform->set_data($rssrecord);
 191  
 192  if ($mform->is_cancelled()) {
 193      redirect($managefeeds);
 194  
 195  } else if ($data = $mform->get_data()) {
 196      $data->userid = $USER->id;
 197      if (!$managesharedfeeds) {
 198          $data->shared = 0;
 199      }
 200  
 201      if ($isadding) {
 202          $DB->insert_record('block_rss_client', $data);
 203      } else {
 204          $data->id = $rssid;
 205          $DB->update_record('block_rss_client', $data);
 206      }
 207  
 208      redirect($managefeeds);
 209  
 210  } else {
 211      if ($isadding) {
 212          $strtitle = get_string('addnewfeed', 'block_rss_client');
 213      } else {
 214          $strtitle = get_string('editafeed', 'block_rss_client');
 215      }
 216  
 217      $PAGE->set_title($strtitle);
 218      $PAGE->set_heading($strtitle);
 219  
 220      $PAGE->navbar->add(get_string('blocks'));
 221      $PAGE->navbar->add(get_string('pluginname', 'block_rss_client'));
 222      $PAGE->navbar->add(get_string('managefeeds', 'block_rss_client'), $managefeeds );
 223      $PAGE->navbar->add($strtitle);
 224  
 225      echo $OUTPUT->header();
 226      echo $OUTPUT->heading($strtitle, 2);
 227  
 228      $mform->display();
 229  
 230      echo $OUTPUT->footer();
 231  }
 232