MDL-46218 reshuffled access checks in blog and notes
[moodle.git] / blog / external_blog_edit.php
CommitLineData
cae83708 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/**
20 * Form page for an external blog link.
21 *
22 * @package moodlecore
23 * @subpackage blog
24 * @copyright 2009 Nicolas Connault
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28require_once('../config.php');
29require_once('lib.php');
b57d3959 30require_once('external_blog_edit_form.php');
e14de6f9 31require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
cae83708 32require_once($CFG->dirroot.'/tag/lib.php');
33
34require_login();
41b38360 35$context = context_system::instance();
2713c6bd 36require_capability('moodle/blog:manageexternal', $context);
cae83708 37
38// TODO redirect if $CFG->useexternalblogs is off, $CFG->maxexternalblogsperuser == 0, or if user doesn't have caps to manage external blogs
39
40$id = optional_param('id', null, PARAM_INT);
2713c6bd
SH
41$url = new moodle_url('/blog/external_blog_edit.php');
42if ($id !== null) {
43 $url->param('id', $id);
44}
45$PAGE->set_url($url);
46$PAGE->set_context($context);
566889aa 47$PAGE->set_pagelayout('admin');
b57d3959 48
a6855934 49$returnurl = new moodle_url('/blog/external_blogs.php');
46be7361 50
cae83708 51$action = (empty($id)) ? 'add' : 'edit';
52
53$external = new stdClass();
54
55// Check that this id exists
56if (!empty($id) && !$DB->record_exists('blog_external', array('id' => $id))) {
57 print_error('wrongexternalid', 'blog');
58} elseif (!empty($id)) {
59 $external = $DB->get_record('blog_external', array('id' => $id));
60}
61
62$strformheading = ($action == 'edit') ? get_string('editexternalblog', 'blog') : get_string('addnewexternalblog', 'blog');
63$strexternalblogs = get_string('externalblogs','blog');
64$strblogs = get_string('blogs','blog');
65
66$externalblogform = new blog_edit_external_form();
67
68if ($externalblogform->is_cancelled()){
69 redirect($returnurl);
70
71} else if ($data = $externalblogform->get_data()) {
72 //save stuff in db
73 switch ($action) {
74 case 'add':
e14de6f9 75 $rss = new moodle_simplepie($data->url);
76
b57d3959
NC
77 $newexternal = new stdClass();
78 $newexternal->name = (empty($data->name)) ? $rss->get_title() : $data->name;
79 $newexternal->description = (empty($data->description)) ? $rss->get_description() : $data->description;
80 $newexternal->userid = $USER->id;
81 $newexternal->url = $data->url;
82 $newexternal->filtertags = $data->filtertags;
caee6e6c 83 $newexternal->timemodified = time();
b57d3959 84
a9637e7d
PS
85 $newexternal->id = $DB->insert_record('blog_external', $newexternal);
86 blog_sync_external_entries($newexternal);
cc033d48
MN
87 tag_set('blog_external', $newexternal->id, explode(',', $data->autotags), 'core',
88 context_user::instance($newexternal->userid)->id);
cae83708 89
90 break;
91
92 case 'edit':
93 if ($data->id && $DB->record_exists('blog_external', array('id' => $data->id))) {
94
e14de6f9 95 $rss = new moodle_simplepie($data->url);
96
cae83708 97 $external->id = $data->id;
e14de6f9 98 $external->name = (empty($data->name)) ? $rss->get_title() : $data->name;
99 $external->description = (empty($data->description)) ? $rss->get_description() : $data->description;
b57d3959 100 $external->userid = $USER->id;
cae83708 101 $external->url = $data->url;
b57d3959 102 $external->filtertags = $data->filtertags;
caee6e6c 103 $external->timemodified = time();
cae83708 104
dd88de0e 105 $DB->update_record('blog_external', $external);
cc033d48
MN
106 tag_set('blog_external', $external->id, explode(',', $data->autotags), 'core',
107 context_user::instance($newexternal->userid)->id);
cae83708 108
109 } else {
110 print_error('wrongexternalid', 'blog');
111 }
112
113 break;
114
115 default :
116 print_error('invalidaction');
117 }
118
119 redirect($returnurl);
120}
121
cae83708 122$PAGE->set_heading("$SITE->shortname: $strblogs: $strexternalblogs", $SITE->fullname);
123$PAGE->set_title("$SITE->shortname: $strblogs: $strexternalblogs");
124
e640790e 125echo $OUTPUT->header();
cae83708 126echo $OUTPUT->heading($strformheading, 2);
127
cae83708 128$externalblogform->set_data($external);
129$externalblogform->display();
130
131echo $OUTPUT->footer();