3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
20 * Form page for an external blog link.
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');
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');
43 $url->param('id', $id);
46 $PAGE->set_context($context);
47 $PAGE->set_pagelayout('admin');
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()){
71 } else if ($data = $externalblogform->get_data()) {
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
= (!empty($data->filtertags
)) ?
$data->filtertags
: null;
83 $newexternal->timemodified
= time();
85 $newexternal->id
= $DB->insert_record('blog_external', $newexternal);
86 blog_sync_external_entries($newexternal);
88 $autotags = (!empty($data->autotags
)) ?
$data->autotags
: null;
89 tag_set('blog_external', $newexternal->id
, explode(',', $autotags), 'core',
90 context_user
::instance($newexternal->userid
)->id
);
96 if ($data->id
&& $DB->record_exists('blog_external', array('id' => $data->id
))) {
98 $rss = new moodle_simplepie($data->url
);
100 $external->id
= $data->id
;
101 $external->name
= (empty($data->name
)) ?
$rss->get_title() : $data->name
;
102 $external->description
= (empty($data->description
)) ?
$rss->get_description() : $data->description
;
103 $external->userid
= $USER->id
;
104 $external->url
= $data->url
;
105 $external->filtertags
= (!empty($data->filtertags
)) ?
$data->filtertags
: null;
106 $external->timemodified
= time();
108 $DB->update_record('blog_external', $external);
110 $autotags = (!empty($data->autotags
)) ?
$data->autotags
: null;
111 tag_set('blog_external', $external->id
, explode(',', $autotags), 'core',
112 context_user
::instance($external->userid
)->id
);
115 print_error('wrongexternalid', 'blog');
121 print_error('invalidaction');
124 redirect($returnurl);
127 $PAGE->set_heading("$SITE->shortname: $strblogs: $strexternalblogs", $SITE->fullname
);
128 $PAGE->set_title("$SITE->shortname: $strblogs: $strexternalblogs");
130 echo $OUTPUT->header();
131 echo $OUTPUT->heading($strformheading, 2);
133 $externalblogform->set_data($external);
134 $externalblogform->display();
136 echo $OUTPUT->footer();