weekly release 2.8dev
[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
a915104b
AA
40 $mform->addElement('url', 'url', get_string('url', 'blog'), array('size' => 50));
41 $mform->setType('url', PARAM_URL);
b73d1ca4 42 $mform->addRule('url', get_string('emptyurl', 'blog'), 'required', null, 'client');
2650f44d 43 $mform->addHelpButton('url', 'url', 'blog');
b73d1ca4 44
2650f44d 45 $mform->addElement('text', 'name', get_string('name', 'blog'));
a915104b 46 $mform->setType('name', PARAM_TEXT);
2650f44d 47 $mform->addHelpButton('name', 'name', 'blog');
cae83708 48
2650f44d
DM
49 $mform->addElement('textarea', 'description', get_string('description', 'blog'), array('cols' => 50, 'rows' => 7));
50 $mform->addHelpButton('description', 'description', 'blog');
cae83708 51
cae83708 52 if (!empty($CFG->usetags)) {
b57d3959 53 $mform->addElement('text', 'filtertags', get_string('filtertags', 'blog'), array('size' => 50));
a915104b 54 $mform->setType('filtertags', PARAM_TAGLIST);
2650f44d 55 $mform->addHelpButton('filtertags', 'filtertags', 'blog');
b57d3959 56 $mform->addElement('text', 'autotags', get_string('autotags', 'blog'), array('size' => 50));
a915104b 57 $mform->setType('autotags', PARAM_TAGLIST);
2650f44d 58 $mform->addHelpButton('autotags', 'autotags', 'blog');
cae83708 59 }
60
61 $this->add_action_buttons();
62
63 $mform->addElement('hidden', 'id');
64 $mform->setType('id', PARAM_INT);
65 $mform->setDefault('id', 0);
66
67 $mform->addElement('hidden', 'returnurl');
68 $mform->setType('returnurl', PARAM_URL);
69 $mform->setDefault('returnurl', 0);
70 }
71
72 /**
73 * Additional validation includes checking URL and tags
74 */
75 public function validation($data, $files) {
b57d3959
NC
76 global $CFG;
77
cae83708 78 $errors = parent::validation($data, $files);
79
b57d3959
NC
80 require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
81
d958e6bd
PS
82 $rss = new moodle_simplepie();
83 $rssfile = $rss->registry->create('File', array($data['url']));
84 $filetest = $rss->registry->create('Locator', array($rssfile));
b57d3959
NC
85
86 if (!$filetest->is_feed($rssfile)) {
87 $errors['url'] = get_string('feedisinvalid', 'blog');
cae83708 88 } else {
d958e6bd 89 $rss->set_feed_url($data['url']);
e14de6f9 90 if (!$rss->init()) {
cae83708 91 $errors['url'] = get_string('emptyrssfeed', 'blog');
92 }
93 }
94
95 return $errors;
96 }
97
cae83708 98 public function definition_after_data() {
99 global $CFG, $COURSE;
100 $mform =& $this->_form;
101
102 $name = trim($mform->getElementValue('name'));
103 $description = trim($mform->getElementValue('description'));
104 $url = $mform->getElementValue('url');
105
106 if (empty($name) || empty($description)) {
e14de6f9 107 $rss = new moodle_simplepie($url);
cae83708 108
e14de6f9 109 if (empty($name) && $rss->get_title()) {
110 $mform->setDefault('name', $rss->get_title());
cae83708 111 }
112
e14de6f9 113 if (empty($description) && $rss->get_description()) {
114 $mform->setDefault('description', $rss->get_description());
cae83708 115 }
116 }
117
118 if ($id = $mform->getElementValue('id')) {
b57d3959
NC
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()
cae83708 124 }
125 }
126}