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.

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

   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  /**
  19   * Moodleform for the user interface for managing external blog links.
  20   *
  21   * @package    moodlecore
  22   * @subpackage blog
  23   * @copyright  2009 Nicolas Connault
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  if (!defined('MOODLE_INTERNAL')) {
  28      die('Direct access to this script is forbidden.');    // It must be included from a Moodle page.
  29  }
  30  
  31  require_once($CFG->libdir.'/formslib.php');
  32  
  33  class blog_edit_external_form extends moodleform {
  34      public function definition() {
  35          global $CFG;
  36  
  37          $mform =& $this->_form;
  38  
  39          $mform->addElement('url', 'url', get_string('url', 'blog'), array('size' => 60), array('usefilepicker' => false));
  40          $mform->setType('url', PARAM_URL);
  41          $mform->addRule('url', get_string('emptyurl', 'blog'), 'required', null, 'client');
  42          $mform->addHelpButton('url', 'url', 'blog');
  43  
  44          $mform->addElement('text', 'name', get_string('name', 'blog'));
  45          $mform->setType('name', PARAM_TEXT);
  46          $mform->addHelpButton('name', 'name', 'blog');
  47  
  48          $mform->addElement('textarea', 'description', get_string('description', 'blog'), array('cols' => 50, 'rows' => 7));
  49          $mform->addHelpButton('description', 'description', 'blog');
  50  
  51          // To filter external blogs by their tags we do not need to check if tags in moodle are enabled.
  52          $mform->addElement('text', 'filtertags', get_string('filtertags', 'blog'), array('size' => 50));
  53          $mform->setType('filtertags', PARAM_TAGLIST);
  54          $mform->addHelpButton('filtertags', 'filtertags', 'blog');
  55  
  56          $mform->addElement('tags', 'autotags', get_string('autotags', 'blog'),
  57                  array('itemtype' => 'blog_external', 'component' => 'core'));
  58          $mform->addHelpButton('autotags', 'autotags', 'blog');
  59  
  60          $this->add_action_buttons();
  61  
  62          $mform->addElement('hidden', 'id');
  63          $mform->setType('id', PARAM_INT);
  64          $mform->setDefault('id', 0);
  65  
  66          $mform->addElement('hidden', 'returnurl');
  67          $mform->setType('returnurl', PARAM_LOCALURL);
  68          $mform->setDefault('returnurl', 0);
  69      }
  70  
  71      /**
  72       * Additional validation includes checking URL and tags
  73       */
  74      public function validation($data, $files) {
  75          global $CFG;
  76  
  77          $errors = parent::validation($data, $files);
  78  
  79          require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
  80  
  81          $rss = new moodle_simplepie();
  82          $rssfile = $rss->registry->create('File', array($data['url']));
  83          $filetest = $rss->registry->create('Locator', array($rssfile));
  84  
  85          if (!$filetest->is_feed($rssfile)) {
  86              $errors['url'] = get_string('feedisinvalid', 'blog');
  87          } else {
  88              $rss->set_feed_url($data['url']);
  89              if (!$rss->init()) {
  90                  $errors['url'] = get_string('emptyrssfeed', 'blog');
  91              }
  92          }
  93  
  94          return $errors;
  95      }
  96  
  97      public function definition_after_data() {
  98          global $CFG, $COURSE;
  99          $mform =& $this->_form;
 100  
 101          $name = trim($mform->getElementValue('name') ?? '');
 102          $description = trim($mform->getElementValue('description') ?? '');
 103          $url = $mform->getElementValue('url');
 104  
 105          if (empty($name) || empty($description)) {
 106              $rss = new moodle_simplepie($url);
 107  
 108              if (empty($name) && $rss->get_title()) {
 109                  $mform->setDefault('name', $rss->get_title());
 110              }
 111  
 112              if (empty($description) && $rss->get_description()) {
 113                  $mform->setDefault('description', $rss->get_description());
 114              }
 115          }
 116  
 117          if ($id = $mform->getElementValue('id')) {
 118              $mform->freeze('url');
 119              if ($mform->elementExists('filtertags')) {
 120                  $mform->freeze('filtertags');
 121              }
 122              // TODO change the filtertags element to a multiple select, using the tags of the external blog
 123              // Use $rss->get_channel_tags().
 124          }
 125      }
 126  }