MDL-65788 tool_policy: Prevent two modals rendering for guests.
[moodle.git] / blocks / rss_client / editfeed.php
CommitLineData
f1a34d8f 1<?php
4ca6cfbf
PS
2// This file is part of Moodle - http://moodle.org/
3//
f1a34d8f 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.
4ca6cfbf 13//
f1a34d8f 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 * Script to let a user edit the properties of a particular RSS feed.
19 *
f25a6839 20 * @package block_rss_client
f1a34d8f 21 * @copyright 2009 Tim Hunt
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25
1fcf0ca8 26require_once(__DIR__ . '/../../config.php');
f1a34d8f 27require_once($CFG->libdir . '/formslib.php');
0864a083 28require_once($CFG->libdir .'/simplepie/moodle_simplepie.php');
f1a34d8f 29
30class feed_edit_form extends moodleform {
31 protected $isadding;
32 protected $caneditshared;
33 protected $title = '';
34 protected $description = '';
35
36 function __construct($actionurl, $isadding, $caneditshared) {
37 $this->isadding = $isadding;
38 $this->caneditshared = $caneditshared;
1a0df553 39 parent::__construct($actionurl);
f1a34d8f 40 }
41
42 function definition() {
43 $mform =& $this->_form;
44
45 // Then show the fields about where this block appears.
15217303 46 $mform->addElement('header', 'rsseditfeedheader', get_string('feed', 'block_rss_client'));
f1a34d8f 47
48 $mform->addElement('text', 'url', get_string('feedurl', 'block_rss_client'), array('size' => 60));
49 $mform->setType('url', PARAM_URL);
50 $mform->addRule('url', null, 'required');
51
28dd2513 52 $mform->addElement('checkbox', 'autodiscovery', get_string('enableautodiscovery', 'block_rss_client'));
53 $mform->setDefault('autodiscovery', 1);
54 $mform->setAdvanced('autodiscovery');
c2ba5087 55 $mform->addHelpButton('autodiscovery', 'enableautodiscovery', 'block_rss_client');
f1a34d8f 56
57 $mform->addElement('text', 'preferredtitle', get_string('customtitlelabel', 'block_rss_client'), array('size' => 60));
58 $mform->setType('preferredtitle', PARAM_NOTAGS);
59
60 if ($this->caneditshared) {
61 $mform->addElement('selectyesno', 'shared', get_string('sharedfeed', 'block_rss_client'));
62 $mform->setDefault('shared', 0);
63 }
64
65 $submitlabal = null; // Default
66 if ($this->isadding) {
67 $submitlabal = get_string('addnewfeed', 'block_rss_client');
68 }
69 $this->add_action_buttons(true, $submitlabal);
70 }
71
28dd2513 72 function definition_after_data(){
73 $mform =& $this->_form;
74
75 if($mform->getElementValue('autodiscovery')){
76 $mform->applyFilter('url', 'feed_edit_form::autodiscover_feed_url');
77 }
78 }
79
f1a34d8f 80 function validation($data, $files) {
81 $errors = parent::validation($data, $files);
82
59664742 83 $rss = new moodle_simplepie();
84 // set timeout for longer than normal to try and grab the feed
85 $rss->set_timeout(10);
86 $rss->set_feed_url($data['url']);
28dd2513 87 $rss->set_autodiscovery_cache_duration(0);
88 $rss->set_autodiscovery_level(SIMPLEPIE_LOCATOR_NONE);
59664742 89 $rss->init();
f1a34d8f 90
0864a083 91 if ($rss->error()) {
b58d8d23 92 $errors['url'] = get_string('couldnotfindloadrssfeed', 'block_rss_client');
f1a34d8f 93 } else {
0864a083 94 $this->title = $rss->get_title();
95 $this->description = $rss->get_description();
f1a34d8f 96 }
97
98 return $errors;
99 }
100
101 function get_data() {
102 $data = parent::get_data();
103 if ($data) {
28dd2513 104 $data->title = '';
105 $data->description = '';
106
107 if($this->title){
108 $data->title = $this->title;
109 }
110
111 if($this->description){
112 $data->description = $this->description;
113 }
f1a34d8f 114 }
115 return $data;
116 }
28dd2513 117
118 /**
4ca6cfbf 119 * Autodiscovers a feed url from a given url, to be used by the formslibs
28dd2513 120 * filter function
121 *
122 * Uses simplepie with autodiscovery set to maximum level to try and find
123 * a feed to subscribe to.
124 * See: http://simplepie.org/wiki/reference/simplepie/set_autodiscovery_level
125 *
126 * @param string URL to autodiscover a url
127 * @return string URL of feed or original url if none found
128 */
129 public static function autodiscover_feed_url($url){
130 $rss = new moodle_simplepie();
131 $rss->set_feed_url($url);
132 $rss->set_autodiscovery_level(SIMPLEPIE_LOCATOR_ALL);
133 // When autodiscovering an RSS feed, simplepie will try lots of
134 // rss links on a page, so set the timeout high
135 $rss->set_timeout(20);
136 $rss->init();
137
138 if($rss->error()){
139 return $url;
140 }
141
d8d1f9e3
DP
142 // return URL without quoting..
143 $discoveredurl = new moodle_url($rss->subscribe_url());
144 return $discoveredurl->out(false);
28dd2513 145 }
f1a34d8f 146}
147
148$returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
1e12c120
RT
149$courseid = optional_param('courseid', 0, PARAM_INT);
150$rssid = optional_param('rssid', 0, PARAM_INT); // 0 mean create new.
f1a34d8f 151
6dcf276f 152if ($courseid == SITEID) {
f1a34d8f 153 $courseid = 0;
154}
155if ($courseid) {
74df2951 156 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
f1a34d8f 157 $PAGE->set_course($course);
158 $context = $PAGE->context;
159} else {
fe2fdd11 160 $context = context_system::instance();
f1a34d8f 161 $PAGE->set_context($context);
162}
163
164$managesharedfeeds = has_capability('block/rss_client:manageanyfeeds', $context);
165if (!$managesharedfeeds) {
166 require_capability('block/rss_client:manageownfeeds', $context);
167}
168
169$urlparams = array('rssid' => $rssid);
f1a34d8f 170if ($courseid) {
171 $urlparams['courseid'] = $courseid;
f1a34d8f 172}
173if ($returnurl) {
174 $urlparams['returnurl'] = $returnurl;
f1a34d8f 175}
a6855934 176$managefeeds = new moodle_url('/blocks/rss_client/managefeeds.php', $urlparams);
7d2a0492 177
a6855934 178$PAGE->set_url('/blocks/rss_client/editfeed.php', $urlparams);
566889aa 179$PAGE->set_pagelayout('admin');
f1a34d8f 180
181if ($rssid) {
182 $isadding = false;
183 $rssrecord = $DB->get_record('block_rss_client', array('id' => $rssid), '*', MUST_EXIST);
184} else {
185 $isadding = true;
186 $rssrecord = new stdClass;
187}
188
189$mform = new feed_edit_form($PAGE->url, $isadding, $managesharedfeeds);
190$mform->set_data($rssrecord);
191
192if ($mform->is_cancelled()) {
7d2a0492 193 redirect($managefeeds);
f1a34d8f 194
195} else if ($data = $mform->get_data()) {
196 $data->userid = $USER->id;
197 if (!$managesharedfeeds) {
198 $data->shared = 0;
199 }
200
201 if ($isadding) {
202 $DB->insert_record('block_rss_client', $data);
203 } else {
204 $data->id = $rssid;
205 $DB->update_record('block_rss_client', $data);
206 }
207
7d2a0492 208 redirect($managefeeds);
f1a34d8f 209
210} else {
211 if ($isadding) {
212 $strtitle = get_string('addnewfeed', 'block_rss_client');
213 } else {
214 $strtitle = get_string('editafeed', 'block_rss_client');
215 }
216
217 $PAGE->set_title($strtitle);
218 $PAGE->set_heading($strtitle);
219
7d2a0492 220 $PAGE->navbar->add(get_string('blocks'));
cdc018ad
RW
221 $PAGE->navbar->add(get_string('pluginname', 'block_rss_client'));
222 $PAGE->navbar->add(get_string('managefeeds', 'block_rss_client'), $managefeeds );
7d2a0492 223 $PAGE->navbar->add($strtitle);
224
225 echo $OUTPUT->header();
f1a34d8f 226 echo $OUTPUT->heading($strtitle, 2);
227
228 $mform->display();
229
230 echo $OUTPUT->footer();
231}
232