Merge branch 'MDL-41041-master' of git://github.com/FMCorz/moodle
[moodle.git] / blog / external_blog_edit_form.php
blob696e79af3c6975a5c6ec5b2a201f3335e8997a24
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('url', 'url', get_string('url', 'blog'), array('size' => 50));
41 $mform->setType('url', PARAM_URL);
42 $mform->addRule('url', get_string('emptyurl', 'blog'), 'required', null, 'client');
43 $mform->addHelpButton('url', 'url', 'blog');
45 $mform->addElement('text', 'name', get_string('name', 'blog'));
46 $mform->setType('name', PARAM_TEXT);
47 $mform->addHelpButton('name', 'name', 'blog');
49 $mform->addElement('textarea', 'description', get_string('description', 'blog'), array('cols' => 50, 'rows' => 7));
50 $mform->addHelpButton('description', 'description', 'blog');
52 if (!empty($CFG->usetags)) {
53 $mform->addElement('text', 'filtertags', get_string('filtertags', 'blog'), array('size' => 50));
54 $mform->setType('filtertags', PARAM_TAGLIST);
55 $mform->addHelpButton('filtertags', 'filtertags', 'blog');
56 $mform->addElement('text', 'autotags', get_string('autotags', 'blog'), array('size' => 50));
57 $mform->setType('autotags', PARAM_TAGLIST);
58 $mform->addHelpButton('autotags', 'autotags', 'blog');
61 $this->add_action_buttons();
63 $mform->addElement('hidden', 'id');
64 $mform->setType('id', PARAM_INT);
65 $mform->setDefault('id', 0);
67 $mform->addElement('hidden', 'returnurl');
68 $mform->setType('returnurl', PARAM_URL);
69 $mform->setDefault('returnurl', 0);
72 /**
73 * Additional validation includes checking URL and tags
75 public function validation($data, $files) {
76 global $CFG;
78 $errors = parent::validation($data, $files);
80 require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
82 $rss = new moodle_simplepie();
83 $rssfile = $rss->registry->create('File', array($data['url']));
84 $filetest = $rss->registry->create('Locator', array($rssfile));
86 if (!$filetest->is_feed($rssfile)) {
87 $errors['url'] = get_string('feedisinvalid', 'blog');
88 } else {
89 $rss->set_feed_url($data['url']);
90 if (!$rss->init()) {
91 $errors['url'] = get_string('emptyrssfeed', 'blog');
95 return $errors;
98 public function definition_after_data() {
99 global $CFG, $COURSE;
100 $mform =& $this->_form;
102 $name = trim($mform->getElementValue('name'));
103 $description = trim($mform->getElementValue('description'));
104 $url = $mform->getElementValue('url');
106 if (empty($name) || empty($description)) {
107 $rss = new moodle_simplepie($url);
109 if (empty($name) && $rss->get_title()) {
110 $mform->setDefault('name', $rss->get_title());
113 if (empty($description) && $rss->get_description()) {
114 $mform->setDefault('description', $rss->get_description());
118 if ($id = $mform->getElementValue('id')) {
119 $mform->setDefault('autotags', implode(',', tag_get_tags_array('blog_external', $id)));
120 $mform->freeze('url');
121 $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()