MDL-14679 fixed a lot more old style uses of insert_record()
[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();
2713c6bd
SH
35$context = get_context_instance(CONTEXT_SYSTEM);
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);
897aa80c 47$PAGE->set_pagelayout('standard');
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;
83 $newexternal->timemodified = mktime();
84
a9637e7d
PS
85 $newexternal->id = $DB->insert_record('blog_external', $newexternal);
86 blog_sync_external_entries($newexternal);
87 tag_set('blog_external', $newexternal->id, $data->autotags);
cae83708 88
89 break;
90
91 case 'edit':
92 if ($data->id && $DB->record_exists('blog_external', array('id' => $data->id))) {
93
e14de6f9 94 $rss = new moodle_simplepie($data->url);
95
cae83708 96 $external->id = $data->id;
e14de6f9 97 $external->name = (empty($data->name)) ? $rss->get_title() : $data->name;
98 $external->description = (empty($data->description)) ? $rss->get_description() : $data->description;
b57d3959 99 $external->userid = $USER->id;
cae83708 100 $external->url = $data->url;
b57d3959 101 $external->filtertags = $data->filtertags;
cae83708 102 $external->timemodified = mktime();
103
104 if ($DB->update_record('blog_external', $external)) {
b57d3959 105 tag_set('blog_external', $external->id, explode(',', $data->autotags));
cae83708 106 }
107
108 } else {
109 print_error('wrongexternalid', 'blog');
110 }
111
112 break;
113
114 default :
115 print_error('invalidaction');
116 }
117
118 redirect($returnurl);
119}
120
cae83708 121$PAGE->set_heading("$SITE->shortname: $strblogs: $strexternalblogs", $SITE->fullname);
122$PAGE->set_title("$SITE->shortname: $strblogs: $strexternalblogs");
123
e640790e 124echo $OUTPUT->header();
cae83708 125echo $OUTPUT->heading($strformheading, 2);
126
cae83708 127$externalblogform->set_data($external);
128$externalblogform->display();
129
130echo $OUTPUT->footer();