blocks/rss_client: MDL-13932 fix config_shownumentries typo
[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
0864a083 77 $rss = new moodle_simplepie($data['url']);
f1a34d8f 78
0864a083 79 if ($rss->error()) {
80 $errors['url'] = get_string('errorloadingfeed', 'block_rss_client', $rss->error());
f1a34d8f 81 } else {
0864a083 82 $this->title = $rss->get_title();
83 $this->description = $rss->get_description();
f1a34d8f 84 }
85
86 return $errors;
87 }
88
89 function get_data() {
90 $data = parent::get_data();
91 if ($data) {
92 $data->title = $this->title;
93 $data->description = $this->description;
94 }
95 return $data;
96 }
97}
98
99$returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
100$courseid = optional_param('courseid', 0, PARAM_INTEGER);
101$rssid = optional_param('rssid', 0, PARAM_INTEGER); // 0 mean create new.
102
6dcf276f 103if ($courseid == SITEID) {
f1a34d8f 104 $courseid = 0;
105}
106if ($courseid) {
107 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
108 $PAGE->set_course($course);
109 $context = $PAGE->context;
110} else {
111 $context = get_context_instance(CONTEXT_SYSTEM);
112 $PAGE->set_context($context);
113}
114
115$managesharedfeeds = has_capability('block/rss_client:manageanyfeeds', $context);
116if (!$managesharedfeeds) {
117 require_capability('block/rss_client:manageownfeeds', $context);
118}
119
120$urlparams = array('rssid' => $rssid);
121$manageparams = array();
122if ($courseid) {
123 $urlparams['courseid'] = $courseid;
124 $manageparams[] = 'courseid=$courseid';
125}
126if ($returnurl) {
127 $urlparams['returnurl'] = $returnurl;
128 $manageparams = 'returnurl=' . $returnurl;
129}
130if ($manageparams) {
131 $manageparams = '?' . implode('&', $manageparams);
132} else {
133 $manageparams = '';
134}
135$PAGE->set_url('blocks/rss_client/editfeed.php', $urlparams);
136$PAGE->set_generaltype('form');
137
138if ($rssid) {
139 $isadding = false;
140 $rssrecord = $DB->get_record('block_rss_client', array('id' => $rssid), '*', MUST_EXIST);
141} else {
142 $isadding = true;
143 $rssrecord = new stdClass;
144}
145
146$mform = new feed_edit_form($PAGE->url, $isadding, $managesharedfeeds);
147$mform->set_data($rssrecord);
148
149if ($mform->is_cancelled()) {
150 redirect($CFG->wwwroot . '/blocks/rss_client/managefeeds.php' . $manageparams);
151
152} else if ($data = $mform->get_data()) {
153 $data->userid = $USER->id;
154 if (!$managesharedfeeds) {
155 $data->shared = 0;
156 }
157
158 if ($isadding) {
159 $DB->insert_record('block_rss_client', $data);
160 } else {
161 $data->id = $rssid;
162 $DB->update_record('block_rss_client', $data);
163 }
164
165 redirect($CFG->wwwroot . '/blocks/rss_client/managefeeds.php' . $manageparams);
166
167} else {
168 if ($isadding) {
169 $strtitle = get_string('addnewfeed', 'block_rss_client');
170 } else {
171 $strtitle = get_string('editafeed', 'block_rss_client');
172 }
173
174 $PAGE->set_title($strtitle);
175 $PAGE->set_heading($strtitle);
176
177 $navlinks = array(
178 array('name' => get_string('administration'), 'link' => "$CFG->wwwroot/$CFG->admin/index.php", 'type' => 'misc'),
179 array('name' => get_string('managemodules'), 'link' => null, 'type' => 'misc'),
180 array('name' => get_string('blocks'), 'link' => null, 'type' => 'misc'),
181 array('name' => get_string('feedstitle', 'block_rss_client'), 'link' => "$CFG->wwwroot/$CFG->admin/settings.php?section=blocksettingrss_client", 'type' => 'misc'),
182 array('name' => get_string('managefeeds', 'block_rss_client'), 'link' => $CFG->wwwroot . '/blocks/rss_client/managefeeds.php' . $manageparams, 'type' => 'misc'),
183 array('name' => $strtitle, 'link' => null, 'type' => 'misc'),
184 );
185 $navigation = build_navigation($navlinks);
186
187 echo $OUTPUT->header($navigation);
188 echo $OUTPUT->heading($strtitle, 2);
189
190 $mform->display();
191
192 echo $OUTPUT->footer();
193}
194