forked from moodleou/moodle-mod_oublog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeletecomment.php
121 lines (106 loc) · 4.15 KB
/
deletecomment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This page allows a user to delete a blog comments
*
* @author Matt Clarkson <[email protected]>
* @package oublog
*/
require_once("../../config.php");
require_once("locallib.php");
require_once($CFG->libdir . '/completionlib.php');
$commentid = required_param('comment', PARAM_INT); // Comment ID to delete
$confirm = optional_param('confirm', 0, PARAM_INT); // Confirm that it is ok to delete comment
if (!$comment = $DB->get_record('oublog_comments', array('id'=>$commentid))) {
print_error('invalidcomment', 'oublog');
}
if (!$post = oublog_get_post($comment->postid)) {
print_error("invalidpost", 'oublog');
}
if (!$cm = get_coursemodule_from_instance('oublog', $post->oublogid)) {
print_error('invalidcoursemodule');
}
if (!$course = $DB->get_record("course", array("id"=>$cm->course))) {
print_error('coursemisconf');
}
if (!$oublog = $DB->get_record("oublog", array("id"=>$cm->instance))) {
print_error('invalidcoursemodule');
}
$url = new moodle_url('/mod/oublog/deletepost.php', array('comment'=>$commentid, 'confirm'=>$confirm));
$PAGE->set_url($url);
// Check security.
$context = context_module::instance($cm->id);
oublog_check_view_permissions($oublog, $context, $cm);
// You can always delete your own comments, or any comment on your own
// personal blog
if (!($comment->userid==$USER->id ||
($oublog->global && $post->userid == $USER->id))) {
require_capability('mod/oublog:managecomments', $context);
}
if ($oublog->global) {
$blogtype = 'personal';
// Get blog user from the oublog_get_post result (to save making an
// extra query); this is only used to display their name anyhow
$oubloguser = new stdClass();
$oubloguser->id = $post->userid;
foreach (get_all_user_name_fields() as $field) {
$oubloguser->$field = $post->$field;
}
} else {
$blogtype = 'course';
}
$viewurl = new moodle_url('/mod/oublog/viewpost.php', array('post'=>$post->id));
if (!empty($commentid) && !empty($confirm)) {
$timedeleted = time();
$updatecomment = (object)array(
'id' => $commentid,
'deletedby' => $USER->id,
'timedeleted' => $timedeleted);
$DB->update_record('oublog_comments', $updatecomment);
// Inform completion system, if available
$completion = new completion_info($course);
if ($completion->is_enabled($cm) && ($oublog->completioncomments)) {
$completion->update_state($cm, COMPLETION_INCOMPLETE, $comment->userid);
}
// Log delete comment event.
$params = array(
'context' => $context,
'objectid' => $comment->id,
'other' => array(
'oublogid' => $oublog->id,
'postid' => $comment->postid,
)
);
$event = \mod_oublog\event\comment_deleted::create($params);
$event->trigger();
redirect($viewurl);
exit;
}
// Get Strings.
$stroublogs = get_string('modulenameplural', 'oublog');
$stroublog = get_string('modulename', 'oublog');
// Print the header.
$PAGE->set_title(format_string($oublog->name));
$PAGE->set_heading(format_string($course->fullname));
if ($blogtype == 'personal') {
$PAGE->navbar->add(fullname($oubloguser), new moodle_url('/user/view.php', array('id'=>$oubloguser->id)));
$PAGE->navbar->add(format_string($oublog->name));
}
echo $OUTPUT->header();
echo $OUTPUT->confirm(get_string('confirmdeletecomment', 'oublog'),
new moodle_url('/mod/oublog/deletecomment.php', array('comment'=>$commentid, 'confirm'=>'1')),
$viewurl);
echo $OUTPUT->footer();