MDL-20328 New capabilities for blog
[moodle.git] / blog / external.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');
30require_once('external_form.php');
e14de6f9 31require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
cae83708 32require_once($CFG->dirroot.'/tag/lib.php');
33
34require_login();
35
36$user = $USER;
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);
46be7361 41$PAGE->set_url('/blog/external.php', array('id' => $id));
42
cae83708 43$returnurl = urldecode(optional_param('returnurl', $PAGE->url->out(), PARAM_RAW));
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
cae83708 70 $new_external = new stdClass();
e14de6f9 71 $new_external->name = (empty($data->name)) ? $rss->get_title() : $data->name;
72 $new_external->description = (empty($data->description)) ? $rss->get_description() : $data->description;
cae83708 73 $new_external->userid = $user->id;
74 $new_external->url = $data->url;
75 $new_external->timemodified = mktime();
76
77 if ($new_external->id = $DB->insert_record('blog_external', $new_external)) {
78 tag_set('blog_external', $new_external->id, $data->tags);
79 // TODO success message
80 } else {
81 // TODO error message
82 }
83
84 break;
85
86 case 'edit':
87 if ($data->id && $DB->record_exists('blog_external', array('id' => $data->id))) {
88
e14de6f9 89 $rss = new moodle_simplepie($data->url);
90
cae83708 91 $external->id = $data->id;
e14de6f9 92 $external->name = (empty($data->name)) ? $rss->get_title() : $data->name;
93 $external->description = (empty($data->description)) ? $rss->get_description() : $data->description;
cae83708 94 $external->userid = $user->id;
95 $external->url = $data->url;
96 $external->timemodified = mktime();
97
98 if ($DB->update_record('blog_external', $external)) {
99 tag_set('blog_external', $external->id, explode(',', $data->tags));
100 // TODO success message
101 } else {
102 // TODO error message
103 }
104
105 } else {
106 print_error('wrongexternalid', 'blog');
107 }
108
109 break;
110
111 default :
112 print_error('invalidaction');
113 }
114
115 redirect($returnurl);
116}
117
e640790e 118$PAGE->navbar->add(fullname($user), new moodle_url($CFG->wwwroot.'/user/view.php', array('id'=>$user->id)));
119$PAGE->navbar->add($strblogs, new moodle_url($CFG->wwwroot.'/blog/index.php', array('userid'=>$user->id)));
120$PAGE->navbar->add($strformheading);
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
127$external->returnurl = $returnurl;
128$externalblogform->set_data($external);
129$externalblogform->display();
130
131echo $OUTPUT->footer();