Updated the HEAD build version to 20100513
[moodle.git] / blog / external_blog_edit_form.php
CommitLineData
cae83708 1<?php
2
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.
14//
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/>.
17
18
19/**
20 * Moodleform for the user interface for managing external blog links.
21 *
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
26 */
27
28require_once($CFG->libdir.'/formslib.php');
b57d3959 29
cae83708 30class blog_edit_external_form extends moodleform {
31 public function definition() {
32 global $CFG;
33
34 $mform =& $this->_form;
35
b57d3959 36 $mform->addElement('text', 'url', get_string('url'), array('size' => 50));
b73d1ca4 37 $mform->addRule('url', get_string('emptyurl', 'blog'), 'required', null, 'client');
38 $mform->setHelpButton('url', array('url', get_string('url', 'blog'), 'blog'));
39
cae83708 40 $mform->addElement('text', 'name', get_string('name'));
cae83708 41 $mform->setHelpButton('name', array('name', get_string('name', 'blog'), 'blog'));
42
43 $mform->addElement('textarea', 'description', get_string('description'), array('cols' => 50, 'rows' => 7));
44 $mform->setHelpButton('description', array('description', get_string('description', 'blog'), 'blog'));
45
cae83708 46 if (!empty($CFG->usetags)) {
b57d3959
NC
47 $mform->addElement('text', 'filtertags', get_string('filtertags', 'blog'), array('size' => 50));
48 $mform->setHelpButton('filtertags', array('filtertags', get_string('filtertags', 'blog'), 'blog'));
49 $mform->addElement('text', 'autotags', get_string('autotags', 'blog'), array('size' => 50));
cae83708 50 }
51
52 $this->add_action_buttons();
53
54 $mform->addElement('hidden', 'id');
55 $mform->setType('id', PARAM_INT);
56 $mform->setDefault('id', 0);
57
58 $mform->addElement('hidden', 'returnurl');
59 $mform->setType('returnurl', PARAM_URL);
60 $mform->setDefault('returnurl', 0);
61 }
62
63 /**
64 * Additional validation includes checking URL and tags
65 */
66 public function validation($data, $files) {
b57d3959
NC
67 global $CFG;
68
cae83708 69 $errors = parent::validation($data, $files);
70
b57d3959
NC
71 require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
72
73 $rssfile = new moodle_simplepie_file($data['url']);
74 $filetest = new SimplePie_Locator($rssfile);
75
76 if (!$filetest->is_feed($rssfile)) {
77 $errors['url'] = get_string('feedisinvalid', 'blog');
cae83708 78 } else {
e14de6f9 79 $rss = new moodle_simplepie($data['url']);
80 if (!$rss->init()) {
cae83708 81 $errors['url'] = get_string('emptyrssfeed', 'blog');
82 }
83 }
84
85 return $errors;
86 }
87
cae83708 88 public function definition_after_data() {
89 global $CFG, $COURSE;
90 $mform =& $this->_form;
91
92 $name = trim($mform->getElementValue('name'));
93 $description = trim($mform->getElementValue('description'));
94 $url = $mform->getElementValue('url');
95
96 if (empty($name) || empty($description)) {
e14de6f9 97 $rss = new moodle_simplepie($url);
cae83708 98
e14de6f9 99 if (empty($name) && $rss->get_title()) {
100 $mform->setDefault('name', $rss->get_title());
cae83708 101 }
102
e14de6f9 103 if (empty($description) && $rss->get_description()) {
104 $mform->setDefault('description', $rss->get_description());
cae83708 105 }
106 }
107
108 if ($id = $mform->getElementValue('id')) {
b57d3959
NC
109 $mform->setDefault('autotags', implode(',', tag_get_tags_array('blog_external', $id)));
110 $mform->freeze('url');
111 $mform->freeze('filtertags');
112 // TODO change the filtertags element to a multiple select, using the tags of the external blog
113 // Use $rss->get_channel_tags()
cae83708 114 }
115 }
116}