HotPot module: differentiate between "guesses-correct" (JCloze) and "guess-correct...
[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
28dd2513 53 $mform->addElement('checkbox', 'autodiscovery', get_string('enableautodiscovery', 'block_rss_client'));
54 $mform->setDefault('autodiscovery', 1);
55 $mform->setAdvanced('autodiscovery');
56 $mform->setHelpButton('autodiscovery', array('feedautodiscovery', get_string('validatefeed', 'block_rss_client'), 'rss_client'), true);
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 /**
120 * Autodiscovers a feed url from a given url, to be used by the formslibs
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);
169$manageparams = array();
170if ($courseid) {
171 $urlparams['courseid'] = $courseid;
8887711b 172 $manageparams[] = 'courseid=' . $courseid;
f1a34d8f 173}
174if ($returnurl) {
175 $urlparams['returnurl'] = $returnurl;
176 $manageparams = 'returnurl=' . $returnurl;
177}
178if ($manageparams) {
179 $manageparams = '?' . implode('&', $manageparams);
180} else {
181 $manageparams = '';
182}
183$PAGE->set_url('blocks/rss_client/editfeed.php', $urlparams);
184$PAGE->set_generaltype('form');
185
186if ($rssid) {
187 $isadding = false;
188 $rssrecord = $DB->get_record('block_rss_client', array('id' => $rssid), '*', MUST_EXIST);
189} else {
190 $isadding = true;
191 $rssrecord = new stdClass;
192}
193
194$mform = new feed_edit_form($PAGE->url, $isadding, $managesharedfeeds);
195$mform->set_data($rssrecord);
196
197if ($mform->is_cancelled()) {
198 redirect($CFG->wwwroot . '/blocks/rss_client/managefeeds.php' . $manageparams);
199
200} else if ($data = $mform->get_data()) {
201 $data->userid = $USER->id;
202 if (!$managesharedfeeds) {
203 $data->shared = 0;
204 }
205
206 if ($isadding) {
207 $DB->insert_record('block_rss_client', $data);
208 } else {
209 $data->id = $rssid;
210 $DB->update_record('block_rss_client', $data);
211 }
212
213 redirect($CFG->wwwroot . '/blocks/rss_client/managefeeds.php' . $manageparams);
214
215} else {
216 if ($isadding) {
217 $strtitle = get_string('addnewfeed', 'block_rss_client');
218 } else {
219 $strtitle = get_string('editafeed', 'block_rss_client');
220 }
221
222 $PAGE->set_title($strtitle);
223 $PAGE->set_heading($strtitle);
224
225 $navlinks = array(
226 array('name' => get_string('administration'), 'link' => "$CFG->wwwroot/$CFG->admin/index.php", 'type' => 'misc'),
227 array('name' => get_string('managemodules'), 'link' => null, 'type' => 'misc'),
228 array('name' => get_string('blocks'), 'link' => null, 'type' => 'misc'),
229 array('name' => get_string('feedstitle', 'block_rss_client'), 'link' => "$CFG->wwwroot/$CFG->admin/settings.php?section=blocksettingrss_client", 'type' => 'misc'),
230 array('name' => get_string('managefeeds', 'block_rss_client'), 'link' => $CFG->wwwroot . '/blocks/rss_client/managefeeds.php' . $manageparams, 'type' => 'misc'),
231 array('name' => $strtitle, 'link' => null, 'type' => 'misc'),
232 );
233 $navigation = build_navigation($navlinks);
234
235 echo $OUTPUT->header($navigation);
236 echo $OUTPUT->heading($strtitle, 2);
237
238 $mform->display();
239
240 echo $OUTPUT->footer();
241}
242