MDL-17445 course deletion cleanup and adding new event that is triggered after removi...
[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();
b57d3959 35require_capability('moodle/blog:manageexternal', get_context_instance(CONTEXT_SYSTEM));
cae83708 36
37// TODO redirect if $CFG->useexternalblogs is off, $CFG->maxexternalblogsperuser == 0, or if user doesn't have caps to manage external blogs
38
39$id = optional_param('id', null, PARAM_INT);
b57d3959
NC
40$PAGE->set_url('/blog/external_blog_edit.php', array('id' => $id));
41
a6855934 42$returnurl = new moodle_url('/blog/external_blogs.php');
46be7361 43
cae83708 44$action = (empty($id)) ? 'add' : 'edit';
45
46$external = new stdClass();
47
48// Check that this id exists
49if (!empty($id) && !$DB->record_exists('blog_external', array('id' => $id))) {
50 print_error('wrongexternalid', 'blog');
51} elseif (!empty($id)) {
52 $external = $DB->get_record('blog_external', array('id' => $id));
53}
54
55$strformheading = ($action == 'edit') ? get_string('editexternalblog', 'blog') : get_string('addnewexternalblog', 'blog');
56$strexternalblogs = get_string('externalblogs','blog');
57$strblogs = get_string('blogs','blog');
58
59$externalblogform = new blog_edit_external_form();
60
61if ($externalblogform->is_cancelled()){
62 redirect($returnurl);
63
64} else if ($data = $externalblogform->get_data()) {
65 //save stuff in db
66 switch ($action) {
67 case 'add':
e14de6f9 68 $rss = new moodle_simplepie($data->url);
69
b57d3959
NC
70 $newexternal = new stdClass();
71 $newexternal->name = (empty($data->name)) ? $rss->get_title() : $data->name;
72 $newexternal->description = (empty($data->description)) ? $rss->get_description() : $data->description;
73 $newexternal->userid = $USER->id;
74 $newexternal->url = $data->url;
75 $newexternal->filtertags = $data->filtertags;
76 $newexternal->timemodified = mktime();
77
78 if ($newexternal->id = $DB->insert_record('blog_external', $newexternal)) {
79 blog_sync_external_entries($newexternal);
80 tag_set('blog_external', $newexternal->id, $data->autotags);
cae83708 81 }
82
83 break;
84
85 case 'edit':
86 if ($data->id && $DB->record_exists('blog_external', array('id' => $data->id))) {
87
e14de6f9 88 $rss = new moodle_simplepie($data->url);
89
cae83708 90 $external->id = $data->id;
e14de6f9 91 $external->name = (empty($data->name)) ? $rss->get_title() : $data->name;
92 $external->description = (empty($data->description)) ? $rss->get_description() : $data->description;
b57d3959 93 $external->userid = $USER->id;
cae83708 94 $external->url = $data->url;
b57d3959 95 $external->filtertags = $data->filtertags;
cae83708 96 $external->timemodified = mktime();
97
98 if ($DB->update_record('blog_external', $external)) {
b57d3959 99 tag_set('blog_external', $external->id, explode(',', $data->autotags));
cae83708 100 }
101
102 } else {
103 print_error('wrongexternalid', 'blog');
104 }
105
106 break;
107
108 default :
109 print_error('invalidaction');
110 }
111
112 redirect($returnurl);
113}
114
a6855934
PS
115$PAGE->navbar->add(fullname($USER), new moodle_url('/user/view.php', array('id'=>$USER->id)));
116$PAGE->navbar->add($strblogs, new moodle_url('/blog/index.php', array('userid'=>$USER->id)));
e640790e 117$PAGE->navbar->add($strformheading);
cae83708 118$PAGE->set_heading("$SITE->shortname: $strblogs: $strexternalblogs", $SITE->fullname);
119$PAGE->set_title("$SITE->shortname: $strblogs: $strexternalblogs");
120
e640790e 121echo $OUTPUT->header();
cae83708 122echo $OUTPUT->heading($strformheading, 2);
123
cae83708 124$externalblogform->set_data($external);
125$externalblogform->display();
126
127echo $OUTPUT->footer();