on demand release 2.5beta+
[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
bfebaf64
MD
28if (!defined('MOODLE_INTERNAL')) {
29 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
30}
31
cae83708 32require_once($CFG->libdir.'/formslib.php');
b57d3959 33
cae83708 34class blog_edit_external_form extends moodleform {
35 public function definition() {
36 global $CFG;
37
38 $mform =& $this->_form;
39
2650f44d 40 $mform->addElement('text', 'url', get_string('url', 'blog'), array('size' => 50));
b73d1ca4 41 $mform->addRule('url', get_string('emptyurl', 'blog'), 'required', null, 'client');
2650f44d 42 $mform->addHelpButton('url', 'url', 'blog');
b73d1ca4 43
2650f44d
DM
44 $mform->addElement('text', 'name', get_string('name', 'blog'));
45 $mform->addHelpButton('name', 'name', 'blog');
cae83708 46
2650f44d
DM
47 $mform->addElement('textarea', 'description', get_string('description', 'blog'), array('cols' => 50, 'rows' => 7));
48 $mform->addHelpButton('description', 'description', 'blog');
cae83708 49
cae83708 50 if (!empty($CFG->usetags)) {
b57d3959 51 $mform->addElement('text', 'filtertags', get_string('filtertags', 'blog'), array('size' => 50));
2650f44d 52 $mform->addHelpButton('filtertags', 'filtertags', 'blog');
b57d3959 53 $mform->addElement('text', 'autotags', get_string('autotags', 'blog'), array('size' => 50));
2650f44d 54 $mform->addHelpButton('autotags', 'autotags', 'blog');
cae83708 55 }
56
57 $this->add_action_buttons();
58
59 $mform->addElement('hidden', 'id');
60 $mform->setType('id', PARAM_INT);
61 $mform->setDefault('id', 0);
62
63 $mform->addElement('hidden', 'returnurl');
64 $mform->setType('returnurl', PARAM_URL);
65 $mform->setDefault('returnurl', 0);
66 }
67
68 /**
69 * Additional validation includes checking URL and tags
70 */
71 public function validation($data, $files) {
b57d3959
NC
72 global $CFG;
73
cae83708 74 $errors = parent::validation($data, $files);
75
b57d3959
NC
76 require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
77
d958e6bd
PS
78 $rss = new moodle_simplepie();
79 $rssfile = $rss->registry->create('File', array($data['url']));
80 $filetest = $rss->registry->create('Locator', array($rssfile));
b57d3959
NC
81
82 if (!$filetest->is_feed($rssfile)) {
83 $errors['url'] = get_string('feedisinvalid', 'blog');
cae83708 84 } else {
d958e6bd 85 $rss->set_feed_url($data['url']);
e14de6f9 86 if (!$rss->init()) {
cae83708 87 $errors['url'] = get_string('emptyrssfeed', 'blog');
88 }
89 }
90
91 return $errors;
92 }
93
cae83708 94 public function definition_after_data() {
95 global $CFG, $COURSE;
96 $mform =& $this->_form;
97
98 $name = trim($mform->getElementValue('name'));
99 $description = trim($mform->getElementValue('description'));
100 $url = $mform->getElementValue('url');
101
102 if (empty($name) || empty($description)) {
e14de6f9 103 $rss = new moodle_simplepie($url);
cae83708 104
e14de6f9 105 if (empty($name) && $rss->get_title()) {
106 $mform->setDefault('name', $rss->get_title());
cae83708 107 }
108
e14de6f9 109 if (empty($description) && $rss->get_description()) {
110 $mform->setDefault('description', $rss->get_description());
cae83708 111 }
112 }
113
114 if ($id = $mform->getElementValue('id')) {
b57d3959
NC
115 $mform->setDefault('autotags', implode(',', tag_get_tags_array('blog_external', $id)));
116 $mform->freeze('url');
117 $mform->freeze('filtertags');
118 // TODO change the filtertags element to a multiple select, using the tags of the external blog
119 // Use $rss->get_channel_tags()
cae83708 120 }
121 }
122}