Merge branch 'MDL-38885-master-int' of git://github.com/FMCorz/moodle
[moodle.git] / blog / external_blog_edit.php
blobe80a8b5d58bc331c877f125df3d59e7a6e4f0434
1 <?php
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.
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/>.
19 /**
20 * Form page for an external blog link.
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
28 require_once('../config.php');
29 require_once('lib.php');
30 require_once('external_blog_edit_form.php');
31 require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
32 require_once($CFG->dirroot.'/tag/lib.php');
34 require_login();
35 $context = context_system::instance();
36 require_capability('moodle/blog:manageexternal', $context);
38 // TODO redirect if $CFG->useexternalblogs is off, $CFG->maxexternalblogsperuser == 0, or if user doesn't have caps to manage external blogs
40 $id = optional_param('id', null, PARAM_INT);
41 $url = new moodle_url('/blog/external_blog_edit.php');
42 if ($id !== null) {
43 $url->param('id', $id);
45 $PAGE->set_url($url);
46 $PAGE->set_context($context);
47 $PAGE->set_pagelayout('standard');
49 $returnurl = new moodle_url('/blog/external_blogs.php');
51 $action = (empty($id)) ? 'add' : 'edit';
53 $external = new stdClass();
55 // Check that this id exists
56 if (!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));
62 $strformheading = ($action == 'edit') ? get_string('editexternalblog', 'blog') : get_string('addnewexternalblog', 'blog');
63 $strexternalblogs = get_string('externalblogs','blog');
64 $strblogs = get_string('blogs','blog');
66 $externalblogform = new blog_edit_external_form();
68 if ($externalblogform->is_cancelled()){
69 redirect($returnurl);
71 } else if ($data = $externalblogform->get_data()) {
72 //save stuff in db
73 switch ($action) {
74 case 'add':
75 $rss = new moodle_simplepie($data->url);
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;
83 $newexternal->timemodified = time();
85 $newexternal->id = $DB->insert_record('blog_external', $newexternal);
86 blog_sync_external_entries($newexternal);
87 tag_set('blog_external', $newexternal->id, explode(',', $data->autotags));
89 break;
91 case 'edit':
92 if ($data->id && $DB->record_exists('blog_external', array('id' => $data->id))) {
94 $rss = new moodle_simplepie($data->url);
96 $external->id = $data->id;
97 $external->name = (empty($data->name)) ? $rss->get_title() : $data->name;
98 $external->description = (empty($data->description)) ? $rss->get_description() : $data->description;
99 $external->userid = $USER->id;
100 $external->url = $data->url;
101 $external->filtertags = $data->filtertags;
102 $external->timemodified = time();
104 $DB->update_record('blog_external', $external);
105 tag_set('blog_external', $external->id, explode(',', $data->autotags));
107 } else {
108 print_error('wrongexternalid', 'blog');
111 break;
113 default :
114 print_error('invalidaction');
117 redirect($returnurl);
120 $PAGE->set_heading("$SITE->shortname: $strblogs: $strexternalblogs", $SITE->fullname);
121 $PAGE->set_title("$SITE->shortname: $strblogs: $strexternalblogs");
123 echo $OUTPUT->header();
124 echo $OUTPUT->heading($strformheading, 2);
126 $externalblogform->set_data($external);
127 $externalblogform->display();
129 echo $OUTPUT->footer();