MDL-19756 Migrated print_close_window to $OUTPUT->print_close_window($text)
[moodle.git] / blocks / rss_client / editfeed.php
CommitLineData
f1a34d8f 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 * Script to let a user edit the properties of a particular RSS feed.
20 *
21 * @package moodlecore
22 * @copyright 2009 Tim Hunt
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27require_once(dirname(__FILE__) . '/../../config.php');
28require_once($CFG->libdir . '/formslib.php');
0864a083 29require_once($CFG->libdir .'/simplepie/moodle_simplepie.php');
f1a34d8f 30
31class feed_edit_form extends moodleform {
32 protected $isadding;
33 protected $caneditshared;
34 protected $title = '';
35 protected $description = '';
36
37 function __construct($actionurl, $isadding, $caneditshared) {
38 $this->isadding = $isadding;
39 $this->caneditshared = $caneditshared;
40 parent::moodleform($actionurl);
41 }
42
43 function definition() {
44 $mform =& $this->_form;
45
46 // Then show the fields about where this block appears.
47 $mform->addElement('header', 'header', get_string('feed', 'block_rss_client'));
48
49 $mform->addElement('text', 'url', get_string('feedurl', 'block_rss_client'), array('size' => 60));
50 $mform->setType('url', PARAM_URL);
51 $mform->addRule('url', null, 'required');
52
53 $validatejs = "window.open('http://feedvalidator.org/check.cgi?url='+" .
54 "getElementById('id_url').value, 'validate', " .
55 "'width=640,height=480,scrollbars=yes,status=yes,resizable=yes'); return true;";
56 $validatelink = '<a href="#" onclick="' . $validatejs . '">' . get_string('validatefeed', 'block_rss_client') . '</a>';
57 $mform->addElement('static', 'validatelink', '', $validatelink);
58
59 $mform->addElement('text', 'preferredtitle', get_string('customtitlelabel', 'block_rss_client'), array('size' => 60));
60 $mform->setType('preferredtitle', PARAM_NOTAGS);
61
62 if ($this->caneditshared) {
63 $mform->addElement('selectyesno', 'shared', get_string('sharedfeed', 'block_rss_client'));
64 $mform->setDefault('shared', 0);
65 }
66
67 $submitlabal = null; // Default
68 if ($this->isadding) {
69 $submitlabal = get_string('addnewfeed', 'block_rss_client');
70 }
71 $this->add_action_buttons(true, $submitlabal);
72 }
73
74 function validation($data, $files) {
75 $errors = parent::validation($data, $files);
76
59664742 77 $rss = new moodle_simplepie();
78 // set timeout for longer than normal to try and grab the feed
79 $rss->set_timeout(10);
80 $rss->set_feed_url($data['url']);
81 $rss->init();
f1a34d8f 82
0864a083 83 if ($rss->error()) {
84 $errors['url'] = get_string('errorloadingfeed', 'block_rss_client', $rss->error());
f1a34d8f 85 } else {
0864a083 86 $this->title = $rss->get_title();
87 $this->description = $rss->get_description();
f1a34d8f 88 }
89
90 return $errors;
91 }
92
93 function get_data() {
94 $data = parent::get_data();
95 if ($data) {
96 $data->title = $this->title;
97 $data->description = $this->description;
98 }
99 return $data;
100 }
101}
102
103$returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
104$courseid = optional_param('courseid', 0, PARAM_INTEGER);
105$rssid = optional_param('rssid', 0, PARAM_INTEGER); // 0 mean create new.
106
6dcf276f 107if ($courseid == SITEID) {
f1a34d8f 108 $courseid = 0;
109}
110if ($courseid) {
111 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
112 $PAGE->set_course($course);
113 $context = $PAGE->context;
114} else {
115 $context = get_context_instance(CONTEXT_SYSTEM);
116 $PAGE->set_context($context);
117}
118
119$managesharedfeeds = has_capability('block/rss_client:manageanyfeeds', $context);
120if (!$managesharedfeeds) {
121 require_capability('block/rss_client:manageownfeeds', $context);
122}
123
124$urlparams = array('rssid' => $rssid);
125$manageparams = array();
126if ($courseid) {
127 $urlparams['courseid'] = $courseid;
8887711b 128 $manageparams[] = 'courseid=' . $courseid;
f1a34d8f 129}
130if ($returnurl) {
131 $urlparams['returnurl'] = $returnurl;
132 $manageparams = 'returnurl=' . $returnurl;
133}
134if ($manageparams) {
135 $manageparams = '?' . implode('&', $manageparams);
136} else {
137 $manageparams = '';
138}
139$PAGE->set_url('blocks/rss_client/editfeed.php', $urlparams);
140$PAGE->set_generaltype('form');
141
142if ($rssid) {
143 $isadding = false;
144 $rssrecord = $DB->get_record('block_rss_client', array('id' => $rssid), '*', MUST_EXIST);
145} else {
146 $isadding = true;
147 $rssrecord = new stdClass;
148}
149
150$mform = new feed_edit_form($PAGE->url, $isadding, $managesharedfeeds);
151$mform->set_data($rssrecord);
152
153if ($mform->is_cancelled()) {
154 redirect($CFG->wwwroot . '/blocks/rss_client/managefeeds.php' . $manageparams);
155
156} else if ($data = $mform->get_data()) {
157 $data->userid = $USER->id;
158 if (!$managesharedfeeds) {
159 $data->shared = 0;
160 }
161
162 if ($isadding) {
163 $DB->insert_record('block_rss_client', $data);
164 } else {
165 $data->id = $rssid;
166 $DB->update_record('block_rss_client', $data);
167 }
168
169 redirect($CFG->wwwroot . '/blocks/rss_client/managefeeds.php' . $manageparams);
170
171} else {
172 if ($isadding) {
173 $strtitle = get_string('addnewfeed', 'block_rss_client');
174 } else {
175 $strtitle = get_string('editafeed', 'block_rss_client');
176 }
177
178 $PAGE->set_title($strtitle);
179 $PAGE->set_heading($strtitle);
180
181 $navlinks = array(
182 array('name' => get_string('administration'), 'link' => "$CFG->wwwroot/$CFG->admin/index.php", 'type' => 'misc'),
183 array('name' => get_string('managemodules'), 'link' => null, 'type' => 'misc'),
184 array('name' => get_string('blocks'), 'link' => null, 'type' => 'misc'),
185 array('name' => get_string('feedstitle', 'block_rss_client'), 'link' => "$CFG->wwwroot/$CFG->admin/settings.php?section=blocksettingrss_client", 'type' => 'misc'),
186 array('name' => get_string('managefeeds', 'block_rss_client'), 'link' => $CFG->wwwroot . '/blocks/rss_client/managefeeds.php' . $manageparams, 'type' => 'misc'),
187 array('name' => $strtitle, 'link' => null, 'type' => 'misc'),
188 );
189 $navigation = build_navigation($navlinks);
190
191 echo $OUTPUT->header($navigation);
192 echo $OUTPUT->heading($strtitle, 2);
193
194 $mform->display();
195
196 echo $OUTPUT->footer();
197}
198