-
Notifications
You must be signed in to change notification settings - Fork 29
/
deletepost.php
297 lines (273 loc) · 12.2 KB
/
deletepost.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?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 posts
*
* @author Matt Clarkson <[email protected]>
* @package oublog
*/
require_once("../../config.php");
require_once($CFG->dirroot . '/mod/oublog/locallib.php');
require_once($CFG->libdir . '/completionlib.php');
$blog = required_param('blog', PARAM_INT); // Blog ID.
$postid = required_param('post', PARAM_INT); // Post ID for editing.
$confirm = optional_param('confirm', 0, PARAM_INT);// Confirm that it is ok to delete post.
$delete = optional_param('delete', 0, PARAM_INT);
$email = optional_param('email', 0, PARAM_INT); // Email author.
$referurl = optional_param('referurl', 0, PARAM_LOCALURL);
$cmid = optional_param('cmid', null, PARAM_INT);
if (!$oublog = $DB->get_record("oublog", array("id"=>$blog))) {
throw new moodle_exception('invalidblog', 'oublog');
}
if (!$post = oublog_get_post($postid, false)) {
throw new moodle_exception('invalidpost', 'oublog');
}
if (!$cm = get_coursemodule_from_instance('oublog', $blog)) {
throw new moodle_exception('invalidcoursemodule');
}
if (!$course = $DB->get_record("course", array("id"=>$oublog->course))) {
throw new moodle_exception('coursemisconf');
}
$url = new moodle_url('/mod/oublog/deletepost.php',
array('blog' => $blog, 'post' => $postid, '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;
$childcourse = null;
$childcm = null;
if (!empty($childdata)) {
$context = $childdata['context'];
$childoublog = $childdata['ousharedblog'];
$childcourse = $childdata['course'];
$childcm = $childdata['cm'];
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;
$postauthor=$DB->get_field_sql("
SELECT
i.userid
FROM
{oublog_posts} p
INNER JOIN {oublog_instances} i on p.oubloginstancesid=i.id
WHERE p.id = ?", array($postid));
if ($postauthor!=$USER->id) {
require_capability('mod/oublog:manageposts', $context);
}
$oublogoutput = $PAGE->get_renderer('mod_oublog');
if ($correctglobal) {
$blogtype = 'personal';
$oubloguser = $USER;
$viewurl = new moodle_url('/mod/oublog/view.php', array('user' => $postauthor));
if (isset($referurl)) {
$viewurl = new moodle_url($referurl);
}
// Print the header.
$PAGE->navbar->add(fullname($oubloguser), new moodle_url('/user/view.php',
array('id' => $oubloguser->id)));
$PAGE->navbar->add(format_string(!empty($childoublog->name) ? $childoublog->name : $oublog->name));
} else {
$blogtype = 'course';
$viewurl = new moodle_url('/mod/oublog/view.php', array('id' => $childcm ? $childcm->id : $cm->id));
if (isset($referurl)) {
$viewurl = new moodle_url($referurl);
}
}
if ($email) {
// Then open and process the form.
require_once($CFG->dirroot . '/mod/oublog/deletepost_form.php');
$customdata = (object)array('blog' => $blog, 'post' => $postid,
'delete' => $delete, 'email' => $email, 'referurl' => $viewurl, 'cmid' => $cmid);
$mform = new mod_oublog_deletepost_form('deletepost.php', $customdata);
if ($mform->is_cancelled()) {
// Form is cancelled, redirect back to the blog.
redirect($viewurl);
} else if ($submitted = $mform->get_data()) {
// Mark the post as deleted.
oublog_do_delete($course, $cm, $oublog, $post);
// We need these for the call to render post.
$canaudit = $canmanageposts = false;
// Store copy of the post for the author.
// If subject is set in this post, use it.
if (!isset($post->title) || empty($post->title)) {
$post->title = get_string('deletedblogpost', 'oublog');
}
$messagepost = $oublogoutput->render_post($cm, $oublog, $post, $viewurl, $blogtype,
$canmanageposts, $canaudit, false, false, false, true, 'top', $cm, $cmid);
// Set up the email message detail.
$messagetext = $submitted->message['text'];
$copyself = (isset($submitted->copyself)) ? true : false;
$includepost = (isset($submitted->includepost)) ? true : false;
$from = $SITE->fullname;
// Use prefered format for author of the post.
$user = (object)array(
'email' => $post->email,
'mailformat' => $post->mailformat,
'id' => $post->userid
);
$messagehtml = text_to_html($messagetext);
// Include the copy of the post in the email.
if ($includepost) {
$messagehtml .= $messagepost;
}
// Send an email to the author of the post.
if (!email_to_user($user, $from, $post->title, html_to_text($messagehtml), $messagehtml)) {
throw new moodle_exception('emailerror', 'oublog');
}
// Prepare for copies.
$emails = array();
if ($copyself) {
// Send an email copy to the current user, with prefered format.
$subject = strtoupper(get_string('copy')) . ' - '. $post->title;
if (!email_to_user($USER, $from, $subject, html_to_text($messagehtml), $messagehtml)) {
throw new moodle_exception('emailerror', 'oublog');
}
}
// Addition of 'Email address of other recipients'.
if (!empty($submitted->emailadd)) {
$emails = preg_split('~[; ]+~', $submitted->emailadd);
}
// If there are any recipients listed send them a HTML copy.
if (!empty($emails[0])) {
$subject = strtoupper(get_string('copy')) . ' - '. $post->title;
foreach ($emails as $email) {
$fakeuser = (object)array(
'email' => $email,
'mailformat' => 1,
'id' => -1
);
if (!email_to_user($fakeuser, $from, $subject, '', $messagehtml)) {
throw new moodle_exception('emailerror', 'oublog');
}
}
}
redirect($viewurl);
} else if (($delete && $email) ) {
// If subject is set in this post, use it.
if (!isset($post->title) || empty($post->title)) {
$post->title = get_string('deletedblogpost', 'oublog');
}
$displayname = oublog_get_displayname($childoublog ? $childoublog : $oublog, true);
// Prepare the object for the emailcontenthtml get_string.
$emailmessage = new stdClass;
$emailmessage->subject = $post->title;
$emailmessage->blog = $childoublog ? $childoublog->name : $oublog->name;
$emailmessage->activityname = $displayname;
$emailmessage->firstname = $USER->firstname;
$emailmessage->lastname = $USER->lastname;
$emailmessage->course = !empty($childcourse->fullname) ? $childcourse->fullname : $COURSE->fullname;
$emailmessage->deleteurl = $CFG->wwwroot . '/mod/oublog/viewpost.php?&post=' . $post->id;
$formdata = new stdClass;
$messagetext = get_string('emailcontenthtml', 'oublog', $emailmessage);
$formdata->message['text'] = $messagetext;
// Display the form.
echo $OUTPUT->header();
$mform->set_data($formdata);
$mform->display();
}
} else {
if (!$confirm) {
$PAGE->set_title(format_string(!empty($childoublog->name) ? $childoublog->name : $oublog->name));
$PAGE->set_heading(format_string(!empty($childcourse->fullname) ? $childcourse->fullname : $course->fullname));
echo $OUTPUT->header();
$confirmdeletestring = get_string('confirmdeletepost', 'oublog');
$confirmstring = get_string('deleteemailpostdescription', 'oublog');
$deletebutton = new single_button(new moodle_url('/mod/oublog/deletepost.php',
array('blog' => $blog, 'post' => $postid, 'delete' => '1',
'confirm' => '1', 'referurl' => $viewurl, 'cmid' => $cmid)), get_string('delete'), 'post');
$cancelbutton = new single_button($viewurl, get_string('cancel'), 'get');
if ($USER->id == $post->userid) {
print $OUTPUT->confirm($confirmdeletestring, $deletebutton, $cancelbutton);
} else {
// Delete - Delete and email || Cancel.
$deleteemailbutton = new single_button(new moodle_url('/mod/oublog/deletepost.php',
array('blog' => $blog, 'post' => $postid, 'email' => '1', 'delete' => '1')),
get_string('deleteemailpostbutton', 'oublog'), 'post');
print oublog_three_button($confirmstring,
$deletebutton,
$deleteemailbutton,
$cancelbutton);
}
} else {
// Mark the post as deleted.
oublog_do_delete($course, $cm, $oublog, $post);
redirect($viewurl);
}
}
echo $OUTPUT->footer();
function oublog_do_delete($course, $cm, $oublog, $post) {
global $DB, $USER;
$updatepost = (object)array(
'id' => $post->id,
'deletedby' => $USER->id,
'timedeleted' => time()
);
$transaction = $DB->start_delegated_transaction();
$DB->update_record('oublog_posts', $updatepost);
if (!oublog_update_item_tags($post->oubloginstancesid, $post->id,
array(), $post->visibility)) {
throw new moodle_exception('tagupdatefailed', 'oublog');
}
// Inform completion system, if available.
$completion = new completion_info($course);
if ($completion->is_enabled($cm) && ($oublog->completionposts)) {
$completion->update_state($cm, COMPLETION_INCOMPLETE, $post->userid);
}
$transaction->allow_commit();
// Log post deleted event.
$context = context_module::instance($cm->id);
$params = array(
'context' => $context,
'objectid' => $post->id,
'other' => array(
'oublogid' => $oublog->id
)
);
$event = \mod_oublog\event\post_deleted::create($params);
$event->trigger();
}
/**
* Print a message along with three buttons buttoneone/buttontwo/Cancel
*
* If a string or moodle_url is given instead of a single_button, method defaults to post.
*
* @param string $message The question to ask the user.
* @param single_button $buttonone The single_button component representing the buttontwo response.
* @param single_button $buttontwo The single_button component representing the buttontwo response.
* @param single_button $cancel The single_button component representing the Cancel response.
* @return string HTML fragment
*/
function oublog_three_button($message, $buttonone, $buttontwo, $cancel) {
global $OUTPUT;
if (!($buttonone instanceof single_button)) {
throw new coding_exception('The buttonone param must be an instance of a single_button.');
}
if (!($buttontwo instanceof single_button)) {
throw new coding_exception('The buttontwo param must be an instance of a single_button.');
}
if (!($cancel instanceof single_button)) {
throw new coding_exception('The cancel param must be an instance of a single_button.');
}
$output = $OUTPUT->box_start('generalbox', 'notice');
$output .= html_writer::tag('p', $message);
$buttons = $OUTPUT->render($buttonone) . $OUTPUT->render($buttontwo) . $OUTPUT->render($cancel);
$output .= html_writer::tag('div', $buttons, array('class' => 'buttons'));
$output .= $OUTPUT->box_end();
return $output;
}