MDL-19676 Blog improvements
[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');
31require_once($CFG->libdir . '/magpie/rss_fetch.inc');
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);
41$returnurl = urldecode(optional_param('returnurl', $PAGE->url->out(), PARAM_RAW));
42$action = (empty($id)) ? 'add' : 'edit';
43
44$external = new stdClass();
45
46// Check that this id exists
47if (!empty($id) && !$DB->record_exists('blog_external', array('id' => $id))) {
48 print_error('wrongexternalid', 'blog');
49} elseif (!empty($id)) {
50 $external = $DB->get_record('blog_external', array('id' => $id));
51}
52
53$strformheading = ($action == 'edit') ? get_string('editexternalblog', 'blog') : get_string('addnewexternalblog', 'blog');
54$strexternalblogs = get_string('externalblogs','blog');
55$strblogs = get_string('blogs','blog');
56
57$externalblogform = new blog_edit_external_form();
58
59if ($externalblogform->is_cancelled()){
60 redirect($returnurl);
61
62} else if ($data = $externalblogform->get_data()) {
63 //save stuff in db
64 switch ($action) {
65 case 'add':
66 $rss = fetch_rss($data->url);
67 $new_external = new stdClass();
68 $new_external->name = (empty($data->name)) ? $rss->channel['title'] : $data->name;
69 $new_external->description = (empty($data->description)) ? $rss->channel['description'] : $data->description;
70 $new_external->userid = $user->id;
71 $new_external->url = $data->url;
72 $new_external->timemodified = mktime();
73
74 if ($new_external->id = $DB->insert_record('blog_external', $new_external)) {
75 tag_set('blog_external', $new_external->id, $data->tags);
76 // TODO success message
77 } else {
78 // TODO error message
79 }
80
81 break;
82
83 case 'edit':
84 if ($data->id && $DB->record_exists('blog_external', array('id' => $data->id))) {
85
86 $rss = fetch_rss($data->url);
87 $external->id = $data->id;
88 $external->name = (empty($data->name)) ? $rss->channel['title'] : $data->name;
89 $external->description = (empty($data->description)) ? $rss->channel['description'] : $data->description;
90 $external->userid = $user->id;
91 $external->url = $data->url;
92 $external->timemodified = mktime();
93
94 if ($DB->update_record('blog_external', $external)) {
95 tag_set('blog_external', $external->id, explode(',', $data->tags));
96 // TODO success message
97 } else {
98 // TODO error message
99 }
100
101 } else {
102 print_error('wrongexternalid', 'blog');
103 }
104
105 break;
106
107 default :
108 print_error('invalidaction');
109 }
110
111 redirect($returnurl);
112}
113
114$navlinks = array();
115$navlinks[] = array('name' => fullname($user), 'link' => "$CFG->wwwroot/user/view.php?id=$user->id", 'type' => 'misc');
116$navlinks[] = array('name' => $strblogs, 'link' => "$CFG->wwwroot/blog/index.php?userid=$user->id", 'type' => 'misc');
117$navlinks[] = array('name' => $strformheading, 'link' => null, 'type' => 'misc');
118$navigation = build_navigation($navlinks);
119
120$PAGE->set_heading("$SITE->shortname: $strblogs: $strexternalblogs", $SITE->fullname);
121$PAGE->set_title("$SITE->shortname: $strblogs: $strexternalblogs");
122
123echo $OUTPUT->header($navigation);
124echo $OUTPUT->heading($strformheading, 2);
125
126$external->returnurl = $returnurl;
127$externalblogform->set_data($external);
128$externalblogform->display();
129
130echo $OUTPUT->footer();