Merge branch 'MDL-65515-master' of git://github.com/mihailges/moodle
[moodle.git] / blog / external_blog_edit_form.php
CommitLineData
cae83708 1<?php
cae83708 2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17
18/**
19 * Moodleform for the user interface for managing external blog links.
20 *
21 * @package moodlecore
22 * @subpackage blog
23 * @copyright 2009 Nicolas Connault
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
bfebaf64 27if (!defined('MOODLE_INTERNAL')) {
2b6e53e8 28 die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
bfebaf64
MD
29}
30
cae83708 31require_once($CFG->libdir.'/formslib.php');
b57d3959 32
cae83708 33class blog_edit_external_form extends moodleform {
34 public function definition() {
35 global $CFG;
36
37 $mform =& $this->_form;
38
5165c9f4 39 $mform->addElement('url', 'url', get_string('url', 'blog'), array('size' => 60), array('usefilepicker' => false));
a915104b 40 $mform->setType('url', PARAM_URL);
b73d1ca4 41 $mform->addRule('url', get_string('emptyurl', 'blog'), 'required', null, 'client');
2650f44d 42 $mform->addHelpButton('url', 'url', 'blog');
b73d1ca4 43
2650f44d 44 $mform->addElement('text', 'name', get_string('name', 'blog'));
a915104b 45 $mform->setType('name', PARAM_TEXT);
2650f44d 46 $mform->addHelpButton('name', 'name', 'blog');
cae83708 47
2650f44d
DM
48 $mform->addElement('textarea', 'description', get_string('description', 'blog'), array('cols' => 50, 'rows' => 7));
49 $mform->addHelpButton('description', 'description', 'blog');
cae83708 50
abea2c5d
MG
51 // To filter external blogs by their tags we do not need to check if tags in moodle are enabled.
52 $mform->addElement('text', 'filtertags', get_string('filtertags', 'blog'), array('size' => 50));
53 $mform->setType('filtertags', PARAM_TAGLIST);
54 $mform->addHelpButton('filtertags', 'filtertags', 'blog');
55
56 $mform->addElement('tags', 'autotags', get_string('autotags', 'blog'),
57 array('itemtype' => 'blog_external', 'component' => 'core'));
58 $mform->addHelpButton('autotags', 'autotags', 'blog');
cae83708 59
60 $this->add_action_buttons();
61
62 $mform->addElement('hidden', 'id');
63 $mform->setType('id', PARAM_INT);
64 $mform->setDefault('id', 0);
65
66 $mform->addElement('hidden', 'returnurl');
67 $mform->setType('returnurl', PARAM_URL);
68 $mform->setDefault('returnurl', 0);
69 }
70
71 /**
72 * Additional validation includes checking URL and tags
73 */
74 public function validation($data, $files) {
b57d3959
NC
75 global $CFG;
76
cae83708 77 $errors = parent::validation($data, $files);
78
b57d3959
NC
79 require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
80
d958e6bd
PS
81 $rss = new moodle_simplepie();
82 $rssfile = $rss->registry->create('File', array($data['url']));
83 $filetest = $rss->registry->create('Locator', array($rssfile));
b57d3959
NC
84
85 if (!$filetest->is_feed($rssfile)) {
86 $errors['url'] = get_string('feedisinvalid', 'blog');
cae83708 87 } else {
d958e6bd 88 $rss->set_feed_url($data['url']);
e14de6f9 89 if (!$rss->init()) {
cae83708 90 $errors['url'] = get_string('emptyrssfeed', 'blog');
91 }
92 }
93
94 return $errors;
95 }
96
cae83708 97 public function definition_after_data() {
98 global $CFG, $COURSE;
99 $mform =& $this->_form;
100
101 $name = trim($mform->getElementValue('name'));
102 $description = trim($mform->getElementValue('description'));
103 $url = $mform->getElementValue('url');
104
105 if (empty($name) || empty($description)) {
e14de6f9 106 $rss = new moodle_simplepie($url);
cae83708 107
e14de6f9 108 if (empty($name) && $rss->get_title()) {
109 $mform->setDefault('name', $rss->get_title());
cae83708 110 }
111
e14de6f9 112 if (empty($description) && $rss->get_description()) {
113 $mform->setDefault('description', $rss->get_description());
cae83708 114 }
115 }
116
117 if ($id = $mform->getElementValue('id')) {
b57d3959 118 $mform->freeze('url');
17af896c
SL
119 if ($mform->elementExists('filtertags')) {
120 $mform->freeze('filtertags');
121 }
b57d3959 122 // TODO change the filtertags element to a multiple select, using the tags of the external blog
2b6e53e8 123 // Use $rss->get_channel_tags().
cae83708 124 }
125 }
126}