2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Form page for an external blog link.
23 * @copyright 2009 Nicolas Connault
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once('../config.php');
28 require_once('lib.php');
29 require_once('external_blog_edit_form.php');
30 require_once($CFG->libdir
. '/simplepie/moodle_simplepie.php');
33 $context = context_system
::instance();
34 require_capability('moodle/blog:manageexternal', $context);
36 // TODO redirect if $CFG->useexternalblogs is off,
37 // $CFG->maxexternalblogsperuser == 0,
38 // 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_user
::instance($USER->id
));
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 // Retrieve the external blog record.
57 if (!$external = $DB->get_record('blog_external', array('id' => $id, 'userid' => $USER->id
))) {
58 throw new \
moodle_exception('wrongexternalid', 'blog');
60 $external->autotags
= core_tag_tag
::get_item_tags_array('core', 'blog_external', $id);
63 $strformheading = ($action == 'edit') ?
get_string('editexternalblog', 'blog') : get_string('addnewexternalblog', 'blog');
64 $strexternalblogs = get_string('externalblogs', 'blog');
65 $strblogs = get_string('blogs', 'blog');
67 $externalblogform = new blog_edit_external_form();
69 if ($externalblogform->is_cancelled()) {
72 } else if ($data = $externalblogform->get_data()) {
76 $rss = new moodle_simplepie($data->url
);
78 $newexternal = new stdClass();
79 $newexternal->name
= (empty($data->name
)) ?
$rss->get_title() : $data->name
;
80 $newexternal->description
= (empty($data->description
)) ?
$rss->get_description() : $data->description
;
81 $newexternal->userid
= $USER->id
;
82 $newexternal->url
= $data->url
;
83 $newexternal->filtertags
= (!empty($data->filtertags
)) ?
$data->filtertags
: null;
84 $newexternal->timemodified
= time();
86 $newexternal->id
= $DB->insert_record('blog_external', $newexternal);
87 core_tag_tag
::set_item_tags('core', 'blog_external', $newexternal->id
,
88 context_user
::instance($newexternal->userid
), $data->autotags
);
89 blog_sync_external_entries($newexternal);
92 $eventparms = array('context' => $context,
93 'objectid' => $newexternal->id
,
94 'other' => array('url' => $newexternal->url
));
95 $event = \core\event\blog_external_added
::create($eventparms);
101 if ($data->id
&& $DB->record_exists('blog_external', array('id' => $data->id
))) {
103 $rss = new moodle_simplepie($data->url
);
105 $external->id
= $data->id
;
106 $external->name
= (empty($data->name
)) ?
$rss->get_title() : $data->name
;
107 $external->description
= (empty($data->description
)) ?
$rss->get_description() : $data->description
;
108 $external->userid
= $USER->id
;
109 $external->url
= $data->url
;
110 $external->filtertags
= (!empty($data->filtertags
)) ?
$data->filtertags
: null;
111 $external->timemodified
= time();
113 $DB->update_record('blog_external', $external);
116 $eventparms = array('context' => $context,
117 'objectid' => $external->id
,
118 'other' => array('url' => $external->url
));
119 $event = \core\event\blog_external_updated
::create($eventparms);
122 core_tag_tag
::set_item_tags('core', 'blog_external', $external->id
,
123 context_user
::instance($external->userid
), $data->autotags
);
125 throw new \
moodle_exception('wrongexternalid', 'blog');
131 throw new \
moodle_exception('invalidaction');
134 redirect($returnurl);
137 navigation_node
::override_active_url(new moodle_url('/blog/external_blogs.php'));
138 $PAGE->navbar
->add(get_string('addnewexternalblog', 'blog'));
140 $PAGE->set_heading(fullname($USER));
141 $PAGE->set_title("$SITE->shortname: $strblogs: $strexternalblogs");
143 echo $OUTPUT->header();
144 echo $OUTPUT->heading($strformheading, 2);
146 $externalblogform->set_data($external);
147 $externalblogform->display();
149 echo $OUTPUT->footer();