Merge branch 'MDL-81358' of https://github.com/paulholden/moodle
[moodle.git] / blog / external_blog_edit_form.php
blob1cf979b3aa601d9a75237de2fc376590ed0cade6
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.
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/>.
18 /**
19 * Moodleform for the user interface for managing external blog links.
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
27 if (!defined('MOODLE_INTERNAL')) {
28 die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
31 require_once($CFG->libdir.'/formslib.php');
33 class blog_edit_external_form extends moodleform {
34 public function definition() {
35 global $CFG;
37 $mform =& $this->_form;
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');
44 $mform->addElement('text', 'name', get_string('name', 'blog'));
45 $mform->setType('name', PARAM_TEXT);
46 $mform->addHelpButton('name', 'name', 'blog');
48 $mform->addElement('textarea', 'description', get_string('description', 'blog'), array('cols' => 50, 'rows' => 7));
49 $mform->addHelpButton('description', 'description', 'blog');
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');
56 $mform->addElement('tags', 'autotags', get_string('autotags', 'blog'),
57 array('itemtype' => 'blog_external', 'component' => 'core'));
58 $mform->addHelpButton('autotags', 'autotags', 'blog');
60 $this->add_action_buttons();
62 $mform->addElement('hidden', 'id');
63 $mform->setType('id', PARAM_INT);
64 $mform->setDefault('id', 0);
66 $mform->addElement('hidden', 'returnurl');
67 $mform->setType('returnurl', PARAM_LOCALURL);
68 $mform->setDefault('returnurl', 0);
71 /**
72 * Additional validation includes checking URL and tags
74 public function validation($data, $files) {
75 global $CFG;
77 $errors = parent::validation($data, $files);
79 require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
81 $rss = new moodle_simplepie();
82 $rssfile = $rss->registry->create('File', array($data['url']));
83 $filetest = $rss->registry->create('Locator', array($rssfile));
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');
94 return $errors;
97 public function definition_after_data() {
98 global $CFG, $COURSE;
99 $mform =& $this->_form;
101 $name = trim($mform->getElementValue('name') ?? '');
102 $description = trim($mform->getElementValue('description') ?? '');
103 $url = $mform->getElementValue('url');
105 if (empty($name) || empty($description)) {
106 $rss = new moodle_simplepie($url);
108 if (empty($name) && $rss->get_title()) {
109 $mform->setDefault('name', $rss->get_title());
112 if (empty($description) && $rss->get_description()) {
113 $mform->setDefault('description', $rss->get_description());
117 if ($id = $mform->getElementValue('id')) {
118 $mform->freeze('url');
119 if ($mform->elementExists('filtertags')) {
120 $mform->freeze('filtertags');
122 // TODO change the filtertags element to a multiple select, using the tags of the external blog
123 // Use $rss->get_channel_tags().