MDL-20328 New capabilities for blog
[moodle.git] / blog / external_form.php
... / ...
CommitLineData
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// TODO remove "Blogging is disabled" text from blog_menu when editing not on
30// DONE put Associations in Advanced items
31// TODO add descriptive text to Associations fieldset
32// TODO forceopen on preferences page
33// TODO Add Blog link under course navigation tree
34// DONE add string for invalidgroupid
35// DONE Restrict groupid entries to entries associated with the course
36class blog_edit_external_form extends moodleform {
37 public function definition() {
38 global $CFG;
39
40 $mform =& $this->_form;
41
42 $mform->addElement('text', 'url', get_string('url'));
43 $mform->addRule('url', get_string('emptyurl', 'blog'), 'required', null, 'client');
44 $mform->setHelpButton('url', array('url', get_string('url', 'blog'), 'blog'));
45
46 $mform->addElement('text', 'name', get_string('name'));
47 // No need to require the name, it gets prefilled with the external blog's site name if empty
48 // $mform->addRule('name', get_string('emptyname', 'blog'), 'required', null, 'client');
49 $mform->setHelpButton('name', array('name', get_string('name', 'blog'), 'blog'));
50
51 $mform->addElement('textarea', 'description', get_string('description'), array('cols' => 50, 'rows' => 7));
52 $mform->setHelpButton('description', array('description', get_string('description', 'blog'), 'blog'));
53
54 if (!empty($CFG->usetags)) {
55 $mform->addElement('text', 'tags', get_string('tags'));
56 $mform->setHelpButton('tags', array('tags', get_string('tags', 'blog'), 'blog'));
57 }
58
59 $this->add_action_buttons();
60
61 $mform->addElement('hidden', 'id');
62 $mform->setType('id', PARAM_INT);
63 $mform->setDefault('id', 0);
64
65 $mform->addElement('hidden', 'returnurl');
66 $mform->setType('returnurl', PARAM_URL);
67 $mform->setDefault('returnurl', 0);
68 }
69
70 /**
71 * Additional validation includes checking URL and tags
72 */
73 public function validation($data, $files) {
74 $errors = parent::validation($data, $files);
75
76 if (!blog_is_valid_url($data['url'])) {
77 $errors['url'] = get_string('invalidurl', 'blog');
78 } else {
79 $rss = new moodle_simplepie($data['url']);
80 if (!$rss->init()) {
81 $errors['url'] = get_string('emptyrssfeed', 'blog');
82 }
83 }
84
85 return $errors;
86 }
87
88/// tweak the form - depending on existing data
89 public function definition_after_data() {
90 global $CFG, $COURSE;
91 $mform =& $this->_form;
92
93 $name = trim($mform->getElementValue('name'));
94 $description = trim($mform->getElementValue('description'));
95 $url = $mform->getElementValue('url');
96
97 if (empty($name) || empty($description)) {
98 $rss = new moodle_simplepie($url);
99
100 if (empty($name) && $rss->get_title()) {
101 $mform->setDefault('name', $rss->get_title());
102 }
103
104 if (empty($description) && $rss->get_description()) {
105 $mform->setDefault('description', $rss->get_description());
106 }
107 }
108
109 if ($id = $mform->getElementValue('id')) {
110 $mform->setDefault('tags', implode(',', tag_get_tags_array('blog_external', $id)));
111 }
112 }
113}