feedback MDL-19807 Updated print_header and build_navigation to OUTPUT and PAGE equiv...
[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);
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':
e14de6f9 66 $rss = new moodle_simplepie($data->url);
67
cae83708 68 $new_external = new stdClass();
e14de6f9 69 $new_external->name = (empty($data->name)) ? $rss->get_title() : $data->name;
70 $new_external->description = (empty($data->description)) ? $rss->get_description() : $data->description;
cae83708 71 $new_external->userid = $user->id;
72 $new_external->url = $data->url;
73 $new_external->timemodified = mktime();
74
75 if ($new_external->id = $DB->insert_record('blog_external', $new_external)) {
76 tag_set('blog_external', $new_external->id, $data->tags);
77 // TODO success message
78 } else {
79 // TODO error message
80 }
81
82 break;
83
84 case 'edit':
85 if ($data->id && $DB->record_exists('blog_external', array('id' => $data->id))) {
86
e14de6f9 87 $rss = new moodle_simplepie($data->url);
88
cae83708 89 $external->id = $data->id;
e14de6f9 90 $external->name = (empty($data->name)) ? $rss->get_title() : $data->name;
91 $external->description = (empty($data->description)) ? $rss->get_description() : $data->description;
cae83708 92 $external->userid = $user->id;
93 $external->url = $data->url;
94 $external->timemodified = mktime();
95
96 if ($DB->update_record('blog_external', $external)) {
97 tag_set('blog_external', $external->id, explode(',', $data->tags));
98 // TODO success message
99 } else {
100 // TODO error message
101 }
102
103 } else {
104 print_error('wrongexternalid', 'blog');
105 }
106
107 break;
108
109 default :
110 print_error('invalidaction');
111 }
112
113 redirect($returnurl);
114}
115
116$navlinks = array();
117$navlinks[] = array('name' => fullname($user), 'link' => "$CFG->wwwroot/user/view.php?id=$user->id", 'type' => 'misc');
118$navlinks[] = array('name' => $strblogs, 'link' => "$CFG->wwwroot/blog/index.php?userid=$user->id", 'type' => 'misc');
119$navlinks[] = array('name' => $strformheading, 'link' => null, 'type' => 'misc');
120$navigation = build_navigation($navlinks);
121
122$PAGE->set_heading("$SITE->shortname: $strblogs: $strexternalblogs", $SITE->fullname);
123$PAGE->set_title("$SITE->shortname: $strblogs: $strexternalblogs");
124
125echo $OUTPUT->header($navigation);
126echo $OUTPUT->heading($strformheading, 2);
127
128$external->returnurl = $returnurl;
129$externalblogform->set_data($external);
130$externalblogform->display();
131
132echo $OUTPUT->footer();