MDL-19756 Fixed link to help.php
[moodle.git] / blog / external_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');
29
30class blog_edit_external_form extends moodleform {
31 public function definition() {
32 global $CFG;
33
34 $mform =& $this->_form;
35
36 $mform->addElement('text', 'name', get_string('name'));
37 // No need to require the name, it gets prefilled with the external blog's site name if empty
38 // $mform->addRule('name', get_string('emptyname', 'blog'), 'required', null, 'client');
39 $mform->setHelpButton('name', array('name', get_string('name', 'blog'), 'blog'));
40
41 $mform->addElement('textarea', 'description', get_string('description'), array('cols' => 50, 'rows' => 7));
42 $mform->setHelpButton('description', array('description', get_string('description', 'blog'), 'blog'));
43
44 $mform->addElement('text', 'url', get_string('url'));
45 $mform->addRule('url', get_string('emptyurl', 'blog'), 'required', null, 'client');
46 $mform->setHelpButton('url', array('url', get_string('url', 'blog'), 'blog'));
47
48 if (!empty($CFG->usetags)) {
49 $mform->addElement('text', 'tags', get_string('tags'));
50 $mform->setHelpButton('tags', array('tags', get_string('tags', 'blog'), 'blog'));
51 }
52
53 $this->add_action_buttons();
54
55 $mform->addElement('hidden', 'id');
56 $mform->setType('id', PARAM_INT);
57 $mform->setDefault('id', 0);
58
59 $mform->addElement('hidden', 'returnurl');
60 $mform->setType('returnurl', PARAM_URL);
61 $mform->setDefault('returnurl', 0);
62 }
63
64 /**
65 * Additional validation includes checking URL and tags
66 */
67 public function validation($data, $files) {
68 $errors = parent::validation($data, $files);
69
70 if (!blog_is_valid_url($data['url'])) {
71 $errors['url'] = get_string('invalidurl', 'blog');
72 } else {
e14de6f9 73 $rss = new moodle_simplepie($data['url']);
74 if (!$rss->init()) {
cae83708 75 $errors['url'] = get_string('emptyrssfeed', 'blog');
76 }
77 }
78
79 return $errors;
80 }
81
82/// tweak the form - depending on existing data
83 public function definition_after_data() {
84 global $CFG, $COURSE;
85 $mform =& $this->_form;
86
87 $name = trim($mform->getElementValue('name'));
88 $description = trim($mform->getElementValue('description'));
89 $url = $mform->getElementValue('url');
90
91 if (empty($name) || empty($description)) {
e14de6f9 92 $rss = new moodle_simplepie($url);
cae83708 93
e14de6f9 94 if (empty($name) && $rss->get_title()) {
95 $mform->setDefault('name', $rss->get_title());
cae83708 96 }
97
e14de6f9 98 if (empty($description) && $rss->get_description()) {
99 $mform->setDefault('description', $rss->get_description());
cae83708 100 }
101 }
102
103 if ($id = $mform->getElementValue('id')) {
104 $mform->setDefault('tags', implode(',', tag_get_tags_array('blog_external', $id)));
105 }
106 }
107}