Merge branch 'MDL-64784' of https://github.com/paulholden/moodle
[moodle.git] / blog / external_blogs.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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.
13 //
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/>.
18 /**
19  * List of external blogs for current user.
20  *
21  * @package    moodlecore
22  * @subpackage blog
23  * @copyright  2009 Nicolas Connault
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 require_once('../config.php');
28 require_once('lib.php');
30 require_login();
31 $context = context_system::instance();
32 $PAGE->set_context(context_user::instance($USER->id));
33 $PAGE->set_url(new moodle_url('/blog/external_blogs.php'));
34 require_capability('moodle/blog:manageexternal', $context);
36 $delete = optional_param('delete', null, PARAM_INT);
38 $strexternalblogs = get_string('externalblogs', 'blog');
39 $straddnewexternalblog = get_string('addnewexternalblog', 'blog');
40 $strblogs = get_string('blogs', 'blog');
41 $message = null;
43 if ($delete && confirm_sesskey()) {
44     $externalblog = $DB->get_record('blog_external', array('id' => $delete));
45     if ($externalblog->userid == $USER->id) {
46         // Delete the external blog.
47         $DB->delete_records('blog_external', array('id' => $delete));
49         // Delete the external blog's posts.
50         $deletewhere = 'module = :module
51                             AND userid = :userid
52                             AND ' . $DB->sql_isnotempty('post', 'uniquehash', false, false) . '
53                             AND ' . $DB->sql_compare_text('content') . ' = ' . $DB->sql_compare_text(':delete');
54         $DB->delete_records_select('post', $deletewhere, array('module' => 'blog_external',
55                                                                'userid' => $USER->id,
56                                                                'delete' => $delete));
58         // Log this action.
59         $eventparms = array('context' => $context, 'objectid' => $delete);
60         $event = \core\event\blog_external_removed::create($eventparms);
61         $event->add_record_snapshot('blog_external', $externalblog);
62         $event->trigger();
63         $message = get_string('externalblogdeleted', 'blog');
64     }
65 }
67 $blogs = $DB->get_records('blog_external', array('userid' => $USER->id));
69 $PAGE->set_heading(fullname($USER));
70 $PAGE->set_title("$SITE->shortname: $strblogs: $strexternalblogs");
71 $PAGE->set_pagelayout('standard');
73 echo $OUTPUT->header();
74 echo $OUTPUT->heading($strexternalblogs, 2);
76 if (!empty($message)) {
77     echo $OUTPUT->notification($message);
78 }
80 echo $OUTPUT->box_start('generalbox boxaligncenter');
82 if (!empty($blogs)) {
83     $table = new html_table();
84     $table->cellpadding = 4;
85     $table->attributes['class'] = 'generaltable boxaligncenter';
86     $table->head = array(get_string('name'),
87                          get_string('url', 'blog'),
88                          get_string('timefetched', 'blog'),
89                          get_string('valid', 'blog'),
90                          get_string('actions'));
92     foreach ($blogs as $blog) {
93         if ($blog->failedlastsync) {
94             $validicon = $OUTPUT->pix_icon('i/invalid', get_string('feedisinvalid', 'blog'));
95         } else {
96             $validicon = $OUTPUT->pix_icon('i/valid', get_string('feedisvalid', 'blog'));
97         }
99         $editurl = new moodle_url('/blog/external_blog_edit.php', array('id' => $blog->id));
100         $editicon = $OUTPUT->action_icon($editurl, new pix_icon('t/edit', get_string('editexternalblog', 'blog')));
102         $deletelink = new moodle_url('/blog/external_blogs.php', array('delete' => $blog->id, 'sesskey' => sesskey()));
103         $action = new confirm_action(get_string('externalblogdeleteconfirm', 'blog'));
104         $deleteicon = $OUTPUT->action_icon($deletelink, new pix_icon('t/delete', get_string('deleteexternalblog', 'blog')),
105                                            $action);
107         $table->data[] = new html_table_row(array($blog->name,
108                                                   $blog->url,
109                                                   userdate($blog->timefetched),
110                                                   $validicon,
111                                                   $editicon . $deleteicon));
112     }
113     echo html_writer::table($table);
116 $newexternalurl = new moodle_url('/blog/external_blog_edit.php');
117 echo html_writer::link($newexternalurl, $straddnewexternalblog);
118 echo $OUTPUT->box_end();
120 // Log this page.
121 $event = \core\event\blog_external_viewed::create(array('context' => $context));
122 $event->trigger();
123 echo $OUTPUT->footer();