-
Notifications
You must be signed in to change notification settings - Fork 29
/
deletecomment.php
138 lines (125 loc) · 5.24 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?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
$cmid = optional_param('cmid', null, PARAM_INT);
$referurl = optional_param('referurl', null, PARAM_LOCALURL);
if (!$comment = $DB->get_record('oublog_comments', array('id'=>$commentid))) {
throw new moodle_exception('invalidcomment', 'oublog');
}
if (!$post = oublog_get_post($comment->postid)) {
throw new moodle_exception("invalidpost", 'oublog');
}
if (!$cm = get_coursemodule_from_instance('oublog', $post->oublogid)) {
throw new moodle_exception('invalidcoursemodule');
}
if (!$course = $DB->get_record("course", array("id"=>$cm->course))) {
throw new moodle_exception('coursemisconf');
}
if (!$oublog = $DB->get_record("oublog", array("id"=>$cm->instance))) {
throw new moodle_exception('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);
$childdata = oublog_get_blog_data_base_on_cmid_of_childblog($cmid, $oublog);
$childoublog = null;
$childcm = null;
$childcourse = null;
if (!empty($childdata)) {
$context = $childdata['context'];
$childoublog = $childdata['ousharedblog'];
$childcm = $childdata['cm'];
$childcourse = $childdata['course'];
oublog_check_view_permissions($childdata['ousharedblog'], $childdata['context'], $childdata['cm']);
} else {
oublog_check_view_permissions($oublog, $context, $cm);
}
$correctglobal = isset($childoublog->global) ? $childoublog->global : $oublog->global;
// You can always delete your own comments, or any comment on your own
// personal blog
if (!($comment->userid==$USER->id ||
($correctglobal && $post->userid == $USER->id))) {
require_capability('mod/oublog:managecomments', $context);
}
if ($correctglobal) {
$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 (\core_user\fields::get_name_fields() as $field) {
$oubloguser->$field = $post->$field;
}
} else {
$blogtype = 'course';
}
$viewurl = !empty($referurl) ? $referurl : 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($childcourse ? $childcourse : $course);
$condition = $childdata ? ($completion->is_enabled($childcm) && ($childoublog->completioncomments) &&
$completion->is_enabled($cm) && ($oublog->completioncomments)) :
($completion->is_enabled($cm) && ($oublog->completioncomments));
if ($condition) {
$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(!empty($childoublog->name) ? $childoublog->name : $oublog->name));
$PAGE->set_heading(format_string(!empty($childcourse->fullname) ? $childcourse->fullname : $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',
'referurl' => $referurl, 'cmid' => $cmid)),
$viewurl);
echo $OUTPUT->footer();