Merge branch 'MDL-29821' of git://github.com/bostelm/moodle
[moodle.git] / blog / external_blog_edit_form.php
blob15b6b94a4f6faba7997664c33d0f94f746747763
1 <?php
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.
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/>.
19 /**
20 * Moodleform for the user interface for managing external blog links.
22 * @package moodlecore
23 * @subpackage blog
24 * @copyright 2009 Nicolas Connault
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 if (!defined('MOODLE_INTERNAL')) {
29 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
32 require_once($CFG->libdir.'/formslib.php');
34 class blog_edit_external_form extends moodleform {
35 public function definition() {
36 global $CFG;
38 $mform =& $this->_form;
40 $mform->addElement('text', 'url', get_string('url', 'blog'), array('size' => 50));
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->addHelpButton('name', 'name', 'blog');
47 $mform->addElement('textarea', 'description', get_string('description', 'blog'), array('cols' => 50, 'rows' => 7));
48 $mform->addHelpButton('description', 'description', 'blog');
50 if (!empty($CFG->usetags)) {
51 $mform->addElement('text', 'filtertags', get_string('filtertags', 'blog'), array('size' => 50));
52 $mform->addHelpButton('filtertags', 'filtertags', 'blog');
53 $mform->addElement('text', 'autotags', get_string('autotags', 'blog'), array('size' => 50));
54 $mform->addHelpButton('autotags', 'autotags', 'blog');
57 $this->add_action_buttons();
59 $mform->addElement('hidden', 'id');
60 $mform->setType('id', PARAM_INT);
61 $mform->setDefault('id', 0);
63 $mform->addElement('hidden', 'returnurl');
64 $mform->setType('returnurl', PARAM_URL);
65 $mform->setDefault('returnurl', 0);
68 /**
69 * Additional validation includes checking URL and tags
71 public function validation($data, $files) {
72 global $CFG;
74 $errors = parent::validation($data, $files);
76 require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
78 $rssfile = new moodle_simplepie_file($data['url']);
79 $filetest = new SimplePie_Locator($rssfile);
81 if (!$filetest->is_feed($rssfile)) {
82 $errors['url'] = get_string('feedisinvalid', 'blog');
83 } else {
84 $rss = new moodle_simplepie($data['url']);
85 if (!$rss->init()) {
86 $errors['url'] = get_string('emptyrssfeed', 'blog');
90 return $errors;
93 public function definition_after_data() {
94 global $CFG, $COURSE;
95 $mform =& $this->_form;
97 $name = trim($mform->getElementValue('name'));
98 $description = trim($mform->getElementValue('description'));
99 $url = $mform->getElementValue('url');
101 if (empty($name) || empty($description)) {
102 $rss = new moodle_simplepie($url);
104 if (empty($name) && $rss->get_title()) {
105 $mform->setDefault('name', $rss->get_title());
108 if (empty($description) && $rss->get_description()) {
109 $mform->setDefault('description', $rss->get_description());
113 if ($id = $mform->getElementValue('id')) {
114 $mform->setDefault('autotags', implode(',', tag_get_tags_array('blog_external', $id)));
115 $mform->freeze('url');
116 $mform->freeze('filtertags');
117 // TODO change the filtertags element to a multiple select, using the tags of the external blog
118 // Use $rss->get_channel_tags()