-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathdeletepost.php
270 lines (230 loc) · 9.82 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
<?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/>.
/**
* Delete or undelete a post (AJAX or standard).
* @package mod
* @subpackage forumng
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../../config.php');
require_once('mod_forumng.php');
// Get AJAX parameter which might affect error handling
$ajax = optional_param('ajax', 0, PARAM_INT);
// Post ID
$postid = required_param('p', PARAM_INT);
$cloneid = optional_param('clone', 0, PARAM_INT);
// Delete or undelete
$delete = optional_param('delete', 1, PARAM_INT);
// Email author
$email = optional_param('email', 0, PARAM_INT);
// Were the posts expanded?
$expand = optional_param('expand', 0, PARAM_INT);
$expandparam = $expand ? '&expand=1' : '';
// Where the delete came from.
$refer = optional_param('refer', '', PARAM_ALPHA);
$pageparams = array('p'=>$postid);
if ($cloneid) {
$pageparams['clone'] = $cloneid;
}
if ($delete != 1) {
$pageparams['delete'] = $delete;
}
if ($ajax) {
$pageparams['ajax'] = $ajax;
}
if ($expand) {
$pageparams['expand'] = $expand;
}
$post = mod_forumng_post::get_from_id($postid, $cloneid);
// Get convenience variables
$discussion = $post->get_discussion();
$forum = $post->get_forum();
$course = $forum->get_course();
$cm = $forum->get_course_module();
// Set up page
$pagename = get_string($delete ? 'deletepost' : 'undeletepost', 'forumng',
$post->get_effective_subject(true));
$url = new moodle_url('/mod/forumng/deletepost.php', $pageparams);
$out = $discussion->init_page($url, $pagename);
// Do all access security checks
$post->require_view();
if ($delete) {
if (!$post->can_delete($whynot)) {
throw new moodle_exception($whynot, 'forumng');
}
} else {
if (!$post->can_undelete($whynot)) {
throw new moodle_exception($whynot, 'forumng');
}
}
// Is this the actual delete?
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $email != 1) {
// Delete or undelete the post
if ($delete) {
$post->delete();
} else {
$post->undelete();
}
// Redirect back
if ($ajax) {
mod_forumng_post::print_for_ajax_and_exit($postid, $cloneid);
}
// Only include post id if user can see deleted posts
$postid = '';
if (!$delete || has_capability('mod/forumng:editanypost', $forum->get_context())) {
$postid = '#p' . $post->get_id();
}
redirect('discuss.php?' . $discussion->get_link_params(mod_forumng::PARAM_PLAIN) . $expandparam . $postid);
}
if ($email) {
require_once('deletepost_form.php');
$urlparams = array('p' => $postid, 'delete' => $delete, 'email' => $email, 'refer' => $refer);
if ($cloneid) {
$urlparams['clone'] = $cloneid;
}
$url = new moodle_url("{$CFG->wwwroot}/mod/forumng/deletepost.php", $urlparams);
$mform = new mod_forumng_deletepost_form($url);
if ($mform->is_cancelled()) {
// Form is cancelled, redirect back to the discussion.
$backurl = ($refer == 'ipud') ? $discussion->get_location(true) :
'discuss.php?' . $discussion->get_link_params(mod_forumng::PARAM_PLAIN) . $expandparam;
redirect($backurl);
} else if ($submitted = $mform->get_data()) {
// Store copy of the post for the author.
$messagepost = $post->display(true, array(mod_forumng_post::OPTION_NO_COMMANDS => true,
mod_forumng_post::OPTION_SINGLE_POST => true));
// Delete the post
$post->delete();
// Set up the email.
$messagetext = $submitted->message['text'];
$copyself = (isset($submitted->copyself))? true : false;
$includepost = (isset($submitted->includepost))? true : false;
$user = $post->get_user();
$from = $SITE->fullname;
$subject = get_string('deletedforumpost', 'forumng');
$message = html_to_text($messagetext);
// Always enable HTML version
$messagehtml = $out->deletion_email(text_to_html($messagetext));
// Include the copy of the post in the email to the author.
if ($includepost) {
$messagehtml .= $messagepost;
$message .= $post->display(false, array(mod_forumng_post::OPTION_NO_COMMANDS => true,
mod_forumng_post::OPTION_SINGLE_POST => true));
}
// Send an email to the author of the post.
if (!email_to_user($user, $from, $subject, $message, $messagehtml)) {
throw new moodle_exception(get_string('emailerror', 'forumng'));
}
// Prepare for copies.
$emails = array();
$subject = strtoupper(get_string('copy')) . ' - ' . $subject;
if ($copyself) {
// Send an email copy to the current user, with prefered format.
if (!email_to_user($USER, $from, $subject, $message, $messagehtml)) {
throw new moodle_exception(get_string('emailerror', 'forumng'));
}
}
// Addition of 'Email address of other recipients'.
if (!empty($submitted->emailadd)) {
$emails = preg_split('~[; ]+~', $submitted->emailadd);
}
// If there are any recipients listed send them an HTML copy.
if (!empty($emails[0])) {
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(get_string('emailerror', 'forumng'));
}
}
}
// redirect back to the discussion.
$backurl = ($refer == 'ipud') ? $discussion->get_location(true) :
'discuss.php?' . $discussion->get_link_params(mod_forumng::PARAM_PLAIN) . $expandparam;
redirect($backurl);
}
}
// Confirm page. Work out navigation for header
print $out->header();
// Include forum JS
$forum->print_js($cm->id, true);
if ($email) {
// prepare the object for the get_string
$emailmessage = new stdClass;
$emailmessage->subject = $post->get_effective_subject(true);
$emailmessage->firstname = $USER->firstname;
$emailmessage->lastname = $USER->lastname;
$emailmessage->course = $COURSE->fullname;
$emailmessage->forum = $post->get_forum()->get_name();
$emailmessage->deleteurl = ($refer == 'ipud') ? $discussion->get_location(true) :
$CFG->wwwroot . '/mod/forumng/discuss.php?' . $discussion->get_link_params(mod_forumng::PARAM_PLAIN);
$formdata = new stdClass;
// Use the plain
$messagetext = get_string('emailcontentplain', 'forumng', $emailmessage);
$formdata->message['text'] = $messagetext;
$formdata->expand = $expand;
$mform->set_data($formdata);
$mform->display();
// output the html for use when JS is enabled
echo $out->delete_form_html(get_string('emailcontenthtml', 'forumng', $emailmessage));
} else {
// Show confirm option
if ($delete) {
$confirmstring = get_string('confirmdelete', 'forumng');
if ($post->is_root_post()) {
$confirmstring .= ' ' . get_string('confirmdelete_nodiscussion', 'forumng');
}
$deletebutton = new single_button(new moodle_url('/mod/forumng/deletepost.php',
array('p'=>$post->get_id(), 'delete'=>$delete,
'clone'=>$cloneid, 'expand'=>$expand)),
$delete ? get_string('delete') : get_string('undelete', 'forumng'),
'post');
$cancelbutton = new single_button(new moodle_url('/mod/forumng/discuss.php',
array('d'=>$discussion->get_id(), 'clone'=>$cloneid, 'expand'=>$expand)),
get_string('cancel'), 'get');
if ($USER->id == $post->get_user()->id) {
print $out->confirm($confirmstring, $deletebutton, $cancelbutton);
} else {
print $out->confirm_three_button($confirmstring,
new single_button(new moodle_url('/mod/forumng/deletepost.php',
array('p'=>$post->get_id(), 'delete'=>$delete,
'clone'=>$cloneid, 'email' => 1, 'expand'=>$expand)),
$delete ? get_string('deleteemailpostbutton', 'forumng') :
get_string('undelete', 'forumng'), 'post'),
$deletebutton,
$cancelbutton);
}
} else {
$confirmstring = get_string('confirmundelete', 'forumng');
print $out->confirm($confirmstring,
new single_button(new moodle_url('/mod/forumng/deletepost.php',
array('p'=>$post->get_id(), 'delete'=>$delete,
'clone'=>$cloneid, 'expand'=>$expand)),
$delete ? get_string('delete') : get_string('undelete', 'forumng'), 'post'),
new single_button(new moodle_url('/mod/forumng/discuss.php',
array('d'=>$discussion->get_id(), 'clone'=>$cloneid, 'expand'=>$expand)),
get_string('cancel'), 'get'));
}
}
// Print post
print $post->display(true, array(mod_forumng_post::OPTION_NO_COMMANDS => true,
mod_forumng_post::OPTION_SINGLE_POST => true));
// Display footer
print $out->footer();