Merge branch 'MDL-69553-m310' of https://github.com/NeillM/moodle into MOODLE_310_STABLE
[moodle.git] / blog / external_blog_edit.php
CommitLineData
cae83708 1<?php
cae83708 2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17
18/**
19 * Form page for an external blog link.
20 *
21 * @package moodlecore
22 * @subpackage blog
23 * @copyright 2009 Nicolas Connault
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27require_once('../config.php');
28require_once('lib.php');
b57d3959 29require_once('external_blog_edit_form.php');
e14de6f9 30require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
cae83708 31
32require_login();
41b38360 33$context = context_system::instance();
2713c6bd 34require_capability('moodle/blog:manageexternal', $context);
cae83708 35
2b6e53e8
AD
36// TODO redirect if $CFG->useexternalblogs is off,
37// $CFG->maxexternalblogsperuser == 0,
38// or if user doesn't have caps to manage external blogs.
cae83708 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);
880c5073 46$PAGE->set_context(context_user::instance($USER->id));
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
b7fcd095
MG
55// Retrieve the external blog record.
56if (!empty($id)) {
57 if (!$external = $DB->get_record('blog_external', array('id' => $id, 'userid' => $USER->id))) {
58 print_error('wrongexternalid', 'blog');
59 }
abea2c5d 60 $external->autotags = core_tag_tag::get_item_tags_array('core', 'blog_external', $id);
cae83708 61}
62
63$strformheading = ($action == 'edit') ? get_string('editexternalblog', 'blog') : get_string('addnewexternalblog', 'blog');
2b6e53e8
AD
64$strexternalblogs = get_string('externalblogs', 'blog');
65$strblogs = get_string('blogs', 'blog');
cae83708 66
67$externalblogform = new blog_edit_external_form();
68
2b6e53e8 69if ($externalblogform->is_cancelled()) {
cae83708 70 redirect($returnurl);
71
72} else if ($data = $externalblogform->get_data()) {
2b6e53e8 73 // Save stuff in db.
cae83708 74 switch ($action) {
75 case 'add':
e14de6f9 76 $rss = new moodle_simplepie($data->url);
77
b57d3959
NC
78 $newexternal = new stdClass();
79 $newexternal->name = (empty($data->name)) ? $rss->get_title() : $data->name;
80 $newexternal->description = (empty($data->description)) ? $rss->get_description() : $data->description;
81 $newexternal->userid = $USER->id;
82 $newexternal->url = $data->url;
17af896c 83 $newexternal->filtertags = (!empty($data->filtertags)) ? $data->filtertags : null;
caee6e6c 84 $newexternal->timemodified = time();
b57d3959 85
a9637e7d 86 $newexternal->id = $DB->insert_record('blog_external', $newexternal);
abea2c5d
MG
87 core_tag_tag::set_item_tags('core', 'blog_external', $newexternal->id,
88 context_user::instance($newexternal->userid), $data->autotags);
a9637e7d 89 blog_sync_external_entries($newexternal);
cae83708 90
02ce2e41
SB
91 // Log this action.
92 $eventparms = array('context' => $context,
93 'objectid' => $newexternal->id,
94 'other' => array('url' => $newexternal->url));
95 $event = \core\event\blog_external_added::create($eventparms);
96 $event->trigger();
97
cae83708 98 break;
99
100 case 'edit':
101 if ($data->id && $DB->record_exists('blog_external', array('id' => $data->id))) {
102
e14de6f9 103 $rss = new moodle_simplepie($data->url);
104
cae83708 105 $external->id = $data->id;
e14de6f9 106 $external->name = (empty($data->name)) ? $rss->get_title() : $data->name;
107 $external->description = (empty($data->description)) ? $rss->get_description() : $data->description;
b57d3959 108 $external->userid = $USER->id;
cae83708 109 $external->url = $data->url;
17af896c 110 $external->filtertags = (!empty($data->filtertags)) ? $data->filtertags : null;
caee6e6c 111 $external->timemodified = time();
cae83708 112
dd88de0e 113 $DB->update_record('blog_external', $external);
02ce2e41
SB
114
115 // Log this action.
116 $eventparms = array('context' => $context,
117 'objectid' => $external->id,
118 'other' => array('url' => $external->url));
119 $event = \core\event\blog_external_updated::create($eventparms);
120 $event->trigger();
121
abea2c5d
MG
122 core_tag_tag::set_item_tags('core', 'blog_external', $external->id,
123 context_user::instance($external->userid), $data->autotags);
cae83708 124 } else {
125 print_error('wrongexternalid', 'blog');
126 }
127
128 break;
129
130 default :
131 print_error('invalidaction');
132 }
133
134 redirect($returnurl);
135}
136
abea2c5d
MG
137navigation_node::override_active_url(new moodle_url('/blog/external_blogs.php'));
138$PAGE->navbar->add(get_string('addnewexternalblog', 'blog'));
139
880c5073 140$PAGE->set_heading(fullname($USER));
cae83708 141$PAGE->set_title("$SITE->shortname: $strblogs: $strexternalblogs");
142
e640790e 143echo $OUTPUT->header();
cae83708 144echo $OUTPUT->heading($strformheading, 2);
145
cae83708 146$externalblogform->set_data($external);
147$externalblogform->display();
148
149echo $OUTPUT->footer();