Merged branch 'MDL-28575_master' of git://github.com/kordan/moodle.git with conflict...
[moodle.git] / blocks / rss_client / editfeed.php
CommitLineData
f1a34d8f 1<?php
2
4ca6cfbf
PS
3// This file is part of Moodle - http://moodle.org/
4//
f1a34d8f 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.
4ca6cfbf 14//
f1a34d8f 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.
15217303 47 $mform->addElement('header', 'rsseditfeedheader', get_string('feed', 'block_rss_client'));
f1a34d8f 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
28dd2513 53 $mform->addElement('checkbox', 'autodiscovery', get_string('enableautodiscovery', 'block_rss_client'));
54 $mform->setDefault('autodiscovery', 1);
55 $mform->setAdvanced('autodiscovery');
c2ba5087 56 $mform->addHelpButton('autodiscovery', 'enableautodiscovery', 'block_rss_client');
f1a34d8f 57
58 $mform->addElement('text', 'preferredtitle', get_string('customtitlelabel', 'block_rss_client'), array('size' => 60));
59 $mform->setType('preferredtitle', PARAM_NOTAGS);
60
61 if ($this->caneditshared) {
62 $mform->addElement('selectyesno', 'shared', get_string('sharedfeed', 'block_rss_client'));
63 $mform->setDefault('shared', 0);
64 }
65
66 $submitlabal = null; // Default
67 if ($this->isadding) {
68 $submitlabal = get_string('addnewfeed', 'block_rss_client');
69 }
70 $this->add_action_buttons(true, $submitlabal);
71 }
72
28dd2513 73 function definition_after_data(){
74 $mform =& $this->_form;
75
76 if($mform->getElementValue('autodiscovery')){
77 $mform->applyFilter('url', 'feed_edit_form::autodiscover_feed_url');
78 }
79 }
80
f1a34d8f 81 function validation($data, $files) {
82 $errors = parent::validation($data, $files);
83
59664742 84 $rss = new moodle_simplepie();
85 // set timeout for longer than normal to try and grab the feed
86 $rss->set_timeout(10);
87 $rss->set_feed_url($data['url']);
28dd2513 88 $rss->set_autodiscovery_cache_duration(0);
89 $rss->set_autodiscovery_level(SIMPLEPIE_LOCATOR_NONE);
59664742 90 $rss->init();
f1a34d8f 91
0864a083 92 if ($rss->error()) {
93 $errors['url'] = get_string('errorloadingfeed', 'block_rss_client', $rss->error());
f1a34d8f 94 } else {
0864a083 95 $this->title = $rss->get_title();
96 $this->description = $rss->get_description();
f1a34d8f 97 }
98
99 return $errors;
100 }
101
102 function get_data() {
103 $data = parent::get_data();
104 if ($data) {
28dd2513 105 $data->title = '';
106 $data->description = '';
107
108 if($this->title){
109 $data->title = $this->title;
110 }
111
112 if($this->description){
113 $data->description = $this->description;
114 }
f1a34d8f 115 }
116 return $data;
117 }
28dd2513 118
119 /**
4ca6cfbf 120 * Autodiscovers a feed url from a given url, to be used by the formslibs
28dd2513 121 * filter function
122 *
123 * Uses simplepie with autodiscovery set to maximum level to try and find
124 * a feed to subscribe to.
125 * See: http://simplepie.org/wiki/reference/simplepie/set_autodiscovery_level
126 *
127 * @param string URL to autodiscover a url
128 * @return string URL of feed or original url if none found
129 */
130 public static function autodiscover_feed_url($url){
131 $rss = new moodle_simplepie();
132 $rss->set_feed_url($url);
133 $rss->set_autodiscovery_level(SIMPLEPIE_LOCATOR_ALL);
134 // When autodiscovering an RSS feed, simplepie will try lots of
135 // rss links on a page, so set the timeout high
136 $rss->set_timeout(20);
137 $rss->init();
138
139 if($rss->error()){
140 return $url;
141 }
142
143 return $rss->subscribe_url();
144 }
f1a34d8f 145}
146
147$returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
148$courseid = optional_param('courseid', 0, PARAM_INTEGER);
149$rssid = optional_param('rssid', 0, PARAM_INTEGER); // 0 mean create new.
150
6dcf276f 151if ($courseid == SITEID) {
f1a34d8f 152 $courseid = 0;
153}
154if ($courseid) {
155 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
156 $PAGE->set_course($course);
157 $context = $PAGE->context;
158} else {
159 $context = get_context_instance(CONTEXT_SYSTEM);
160 $PAGE->set_context($context);
161}
162
163$managesharedfeeds = has_capability('block/rss_client:manageanyfeeds', $context);
164if (!$managesharedfeeds) {
165 require_capability('block/rss_client:manageownfeeds', $context);
166}
167
168$urlparams = array('rssid' => $rssid);
f1a34d8f 169if ($courseid) {
170 $urlparams['courseid'] = $courseid;
f1a34d8f 171}
172if ($returnurl) {
173 $urlparams['returnurl'] = $returnurl;
f1a34d8f 174}
a6855934 175$managefeeds = new moodle_url('/blocks/rss_client/managefeeds.php', $urlparams);
7d2a0492 176
a6855934 177$PAGE->set_url('/blocks/rss_client/editfeed.php', $urlparams);
191b267b 178$PAGE->set_pagelayout('base');
f1a34d8f 179
180if ($rssid) {
181 $isadding = false;
182 $rssrecord = $DB->get_record('block_rss_client', array('id' => $rssid), '*', MUST_EXIST);
183} else {
184 $isadding = true;
185 $rssrecord = new stdClass;
186}
187
188$mform = new feed_edit_form($PAGE->url, $isadding, $managesharedfeeds);
189$mform->set_data($rssrecord);
190
191if ($mform->is_cancelled()) {
7d2a0492 192 redirect($managefeeds);
f1a34d8f 193
194} else if ($data = $mform->get_data()) {
195 $data->userid = $USER->id;
196 if (!$managesharedfeeds) {
197 $data->shared = 0;
198 }
199
200 if ($isadding) {
201 $DB->insert_record('block_rss_client', $data);
202 } else {
203 $data->id = $rssid;
204 $DB->update_record('block_rss_client', $data);
205 }
206
7d2a0492 207 redirect($managefeeds);
f1a34d8f 208
209} else {
210 if ($isadding) {
211 $strtitle = get_string('addnewfeed', 'block_rss_client');
212 } else {
213 $strtitle = get_string('editafeed', 'block_rss_client');
214 }
215
216 $PAGE->set_title($strtitle);
217 $PAGE->set_heading($strtitle);
218
a6855934 219 $settingsurl = new moodle_url('/admin/settings.php?section=blocksettingrss_client');
7d2a0492 220 $PAGE->navbar->add(get_string('blocks'));
91152a35 221 $PAGE->navbar->add(get_string('feedstitle', 'block_rss_client'), $settingsurl);
7d2a0492 222 $PAGE->navbar->add(get_string('managefeeds', 'block_rss_client'));
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