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