41905731 |
1 | <?php // $Id$ |
501cdbd8 |
2 | |
3 | // Edit and save a new post to a discussion |
4 | |
16955dc6 |
5 | require_once('../../config.php'); |
6 | require_once('lib.php'); |
7 | |
8 | $reply = optional_param('reply', 0, PARAM_INT); |
70e3da07 |
9 | $forum = optional_param('forum', 0, PARAM_INT); |
10 | $edit = optional_param('edit', 0, PARAM_INT); |
11 | $delete = optional_param('delete', 0, PARAM_INT); |
12 | $prune = optional_param('prune',0,PARAM_INT); |
13 | $name = optional_param('name','',PARAM_CLEAN); |
14 | $confirm = optional_param('confirm',0,PARAM_INT); |
bbbf2d40 |
15 | |
8d9a0ae6 |
16 | |
501cdbd8 |
17 | if (isguest()) { |
c59eb341 |
18 | $wwwroot = $CFG->wwwroot.'/login/index.php'; |
19 | if (!empty($CFG->loginhttps)) { |
bbbf2d40 |
20 | $wwwroot = str_replace('http','https', $wwwroot); |
c59eb341 |
21 | } |
556963f5 |
22 | |
bbbf2d40 |
23 | if (isset($forum)) { // User is starting a new discussion in a forum |
556963f5 |
24 | if (! $forum = get_record('forum', 'id', $forum)) { |
25 | error('The forum number was incorrect'); |
26 | } |
16955dc6 |
27 | } else if (!empty($reply)) { // User is writing a new reply |
556963f5 |
28 | if (! $parent = forum_get_post_full($reply)) { |
29 | error('Parent post ID was incorrect'); |
30 | } |
31 | if (! $discussion = get_record('forum_discussions', 'id', $parent->discussion)) { |
32 | error('This post is not part of a discussion!'); |
33 | } |
34 | if (! $forum = get_record('forum', 'id', $discussion->forum)) { |
35 | error('The forum number was incorrect'); |
36 | } |
37 | } |
38 | if (! $course = get_record('course', 'id', $forum->course)) { |
39 | error('The course number was incorrect'); |
40 | } |
e40a7a3c |
41 | |
556963f5 |
42 | if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { // For the logs |
6e89ca55 |
43 | error('Could not get the course module for the forum instance.'); |
bbbf2d40 |
44 | } else { |
45 | $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); |
556963f5 |
46 | } |
47 | |
48 | $strforums = get_string('modulenameplural', 'forum'); |
49 | if ($course->category) { |
50 | print_header($course->shortname, $course->fullname, |
51 | "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> -> |
e6157a73 |
52 | <a href=\"../forum/index.php?id=$course->id\">$strforums</a> -> |
3849dae8 |
53 | <a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>", '', '', true, "", navmenu($course, $cm)); |
556963f5 |
54 | } else { |
55 | print_header($course->shortname, $course->fullname, |
e6157a73 |
56 | "<a href=\"../forum/index.php?id=$course->id\">$strforums</a> -> |
3849dae8 |
57 | <a href=\"view.php?f=$forum->id\">".format_string($forum->name)."</a>", '', '', true, "", navmenu($course, $cm)); |
556963f5 |
58 | } |
c59eb341 |
59 | notice_yesno(get_string('noguestpost', 'forum').'<br /><br />'.get_string('liketologin'), |
60 | $wwwroot, $_SERVER['HTTP_REFERER']); |
cd8d4471 |
61 | print_footer($course); |
c59eb341 |
62 | exit; |
501cdbd8 |
63 | } |
64 | |
8e8d0524 |
65 | require_login(0, false); // Script is useless unless they're logged in |
48d38fad |
66 | |
36b4f985 |
67 | if ($post = data_submitted()) { |
bbbf2d40 |
68 | if (empty($post->course)) { |
69 | error('No course was defined!'); |
f7abd64a |
70 | } |
71 | |
bbbf2d40 |
72 | if (!$course = get_record('course', 'id', $post->course)) { |
f7abd64a |
73 | error('Could not find specified course!'); |
74 | } |
96662058 |
75 | |
76 | if (!empty($forum)) { // Check the forum id and change it to a full object |
77 | if (! $forum = get_record('forum', 'id', $forum)) { |
78 | error('The forum number was incorrect'); |
79 | } |
80 | } |
e40a7a3c |
81 | |
f7abd64a |
82 | if (!empty($course->lang)) { // Override current language |
83 | $CFG->courselang = $course->lang; |
0d1db48e |
84 | } |
501cdbd8 |
85 | |
3395f2d6 |
86 | if (empty($SESSION->fromurl)) { |
bbbf2d40 |
87 | $errordestination = "$CFG->wwwroot/mod/forum/view.php?f=$post->forum"; |
3395f2d6 |
88 | } else { |
89 | $errordestination = $SESSION->fromurl; |
90 | } |
91 | |
f2b5d7e3 |
92 | $post->subject = clean_param(strip_tags($post->subject, '<lang><span>'), PARAM_CLEAN); // Strip all tags except multilang |
db46e49c |
93 | |
f2b5d7e3 |
94 | //$post->message will be cleaned later before display |
501cdbd8 |
95 | |
36257d39 |
96 | $post->attachment = isset($_FILES['attachment']) ? $_FILES['attachment'] : NULL; |
7f6689e4 |
97 | |
bbbf2d40 |
98 | if (!$cm = get_coursemodule_from_instance("forum", $post->forum, $course->id)) { // For the logs |
6e89ca55 |
99 | error('Could not get the course module for the forum instance.'); |
69d79bc3 |
100 | } |
e40a7a3c |
101 | $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); |
f2b5d7e3 |
102 | trusttext_after_edit($post->message, $modcontext); |
69d79bc3 |
103 | |
bbbf2d40 |
104 | if (!$post->subject or !$post->message) { |
3395f2d6 |
105 | $post->error = get_string("emptymessage", "forum"); |
7f6689e4 |
106 | |
bbbf2d40 |
107 | } else if ($post->edit) { // Updating a post |
108 | $post->id = $post->edit; |
9b7bff26 |
109 | $message = ''; |
afa3507c |
110 | |
bbbf2d40 |
111 | //fix for bug #4314 |
0fa18d5a |
112 | if (!$realpost = get_record('forum_posts','id',$post->id)) { |
bbbf2d40 |
113 | $realpost = new object; |
114 | $realpost->userid = -1; |
115 | } |
116 | |
0468976c |
117 | if ( !(($realpost->userid == $USER->id && has_capability('mod/forum:replypost', $modcontext)) || |
118 | has_capability('mod/forum:editanypost', $modcontext)) ) { |
bbbf2d40 |
119 | error("You can not update this post"); |
120 | } |
121 | |
96662058 |
122 | if ($forum->type == 'news' && !$post->parent) { |
8d9a0ae6 |
123 | $updatediscussion = new object; |
bbbf2d40 |
124 | $updatediscussion->id = $post->discussion; |
fbc21e82 |
125 | if (empty($post->timestartdisabled)) { |
126 | $updatediscussion->timestart = make_timestamp($post->timestartyear, $post->timestartmonth, $post->timestartday); |
127 | } else { |
128 | $updatediscussion->timestart = 0; |
129 | } |
130 | if (empty($post->timeenddisabled)) { |
131 | $updatediscussion->timeend = make_timestamp($post->timeendyear, $post->timeendmonth, $post->timeendday); |
132 | } else { |
133 | $updatediscussion->timeend = 0; |
134 | } |
135 | if (empty($post->timeenddisabled) && $updatediscussion->timeend <= $updatediscussion->timestart) { |
136 | $post->error = get_string('timestartenderror', 'forum'); |
137 | } elseif (!update_record('forum_discussions', $updatediscussion)) { |
138 | error(get_string("couldnotupdate", "forum"), $errordestination); |
139 | } |
140 | } |
9bdedea4 |
141 | |
fbc21e82 |
142 | if (!isset($post->error)) { |
143 | |
bbbf2d40 |
144 | if (forum_update_post($post,$message)) { |
8f0cd6ef |
145 | |
9bdedea4 |
146 | add_to_log($course->id, "forum", "update post", |
bbbf2d40 |
147 | "discuss.php?d=$post->discussion&parent=$post->id", "$post->id", $cm->id); |
69d79bc3 |
148 | |
9bdedea4 |
149 | $timemessage = 2; |
150 | if (!empty($message)) { // if we're printing stuff about the file upload |
151 | $timemessage = 4; |
152 | } |
153 | $message .= '<br />'.get_string("postupdated", "forum"); |
8f0cd6ef |
154 | |
bbbf2d40 |
155 | if ($subscribemessage = forum_post_subscription($post)) { |
9bdedea4 |
156 | $timemessage = 4; |
157 | } |
bbbf2d40 |
158 | redirect(forum_go_back_to("discuss.php?d=$post->discussion#$post->id"), $message.$subscribemessage, $timemessage); |
0a9f61b5 |
159 | |
9bdedea4 |
160 | } else { |
161 | error(get_string("couldnotupdate", "forum"), $errordestination); |
162 | } |
163 | exit; |
fbc21e82 |
164 | } |
0fa18d5a |
165 | |
bbbf2d40 |
166 | } else if ($post->discussion) { // Adding a new post to an existing discussion |
db290a6e |
167 | $message = ''; |
bbbf2d40 |
168 | if ($post->id = forum_add_new_post($post,$message)) { |
69d79bc3 |
169 | |
f7abd64a |
170 | add_to_log($course->id, "forum", "add post", |
bbbf2d40 |
171 | "discuss.php?d=$post->discussion&parent=$post->id", "$post->id", $cm->id); |
69d79bc3 |
172 | |
0a9f61b5 |
173 | $timemessage = 2; |
db290a6e |
174 | if (!empty($message)) { // if we're printing stuff about the file upload |
175 | $timemessage = 4; |
176 | } |
177 | $message .= '<br />'.get_string("postadded", "forum", format_time($CFG->maxeditingtime)); |
0a9f61b5 |
178 | |
bbbf2d40 |
179 | if ($subscribemessage = forum_post_subscription($post)) { |
0a9f61b5 |
180 | $timemessage = 4; |
501cdbd8 |
181 | } |
182 | |
f2b5d7e3 |
183 | if (!empty($post->mailnow)) { |
41547057 |
184 | $message .= get_string("postmailnow", "forum"); |
185 | $timemessage = 4; |
186 | } |
187 | |
bbbf2d40 |
188 | redirect(forum_go_back_to("discuss.php?d=$post->discussion#$post->id"), $message.$subscribemessage, $timemessage); |
0a9f61b5 |
189 | |
501cdbd8 |
190 | } else { |
8f0cd6ef |
191 | error(get_string("couldnotadd", "forum"), $errordestination); |
501cdbd8 |
192 | } |
3395f2d6 |
193 | exit; |
194 | |
bbbf2d40 |
195 | } else { // Adding a new discussion |
196 | $post->mailnow = empty($post->mailnow) ? 0 : 1; |
197 | $discussion = $post; |
198 | $discussion->name = $post->subject; |
501cdbd8 |
199 | $discussion->intro = $post->message; |
fbc21e82 |
200 | $newstopic = false; |
8d9a0ae6 |
201 | |
96662058 |
202 | if ($forum->type == 'news' && !$post->parent) { |
fbc21e82 |
203 | $newstopic = true; |
204 | } |
205 | if ($newstopic && empty($post->timestartdisabled)) { |
206 | $discussion->timestart = make_timestamp($post->timestartyear, $post->timestartmonth, $post->timestartday); |
207 | } else { |
208 | $discussion->timestart = 0; |
209 | } |
210 | if ($newstopic && empty($post->timeenddisabled)) { |
211 | $discussion->timeend = make_timestamp($post->timeendyear, $post->timeendmonth, $post->timeendday); |
212 | } else { |
213 | $discussion->timeend = 0; |
214 | } |
215 | if ($newstopic && empty($post->timeenddisabled) && $discussion->timeend <= $discussion->timestart) { |
216 | $post->error = get_string('timestartenderror', 'forum'); |
217 | } else { |
9bdedea4 |
218 | $message = ''; |
219 | if ($discussion->id = forum_add_discussion($discussion,$message)) { |
fbc21e82 |
220 | |
9bdedea4 |
221 | add_to_log($course->id, "forum", "add discussion", |
222 | "discuss.php?d=$discussion->id", "$discussion->id", $cm->id); |
69d79bc3 |
223 | |
9bdedea4 |
224 | $timemessage = 2; |
225 | if (!empty($message)) { // if we're printing stuff about the file upload |
226 | $timemessage = 4; |
227 | } |
228 | $message .= '<br />'.get_string("postadded", "forum", format_time($CFG->maxeditingtime)); |
8f0cd6ef |
229 | |
bbbf2d40 |
230 | if ($post->mailnow) { |
9bdedea4 |
231 | $message .= get_string("postmailnow", "forum"); |
232 | $timemessage = 4; |
233 | } |
41547057 |
234 | |
9bdedea4 |
235 | if ($subscribemessage = forum_post_subscription($discussion)) { |
236 | $timemessage = 4; |
237 | } |
0a9f61b5 |
238 | |
bbbf2d40 |
239 | redirect(forum_go_back_to("view.php?f=$post->forum"), $message.$subscribemessage, $timemessage); |
0a9f61b5 |
240 | |
9bdedea4 |
241 | } else { |
242 | error(get_string("couldnotadd", "forum"), $errordestination); |
243 | } |
fbc21e82 |
244 | |
9bdedea4 |
245 | exit; |
fbc21e82 |
246 | } |
501cdbd8 |
247 | } |
501cdbd8 |
248 | } |
249 | |
da077b9a |
250 | if ($usehtmleditor = can_use_html_editor()) { |
213e8cc6 |
251 | $defaultformat = FORMAT_HTML; |
213e8cc6 |
252 | } else { |
253 | $defaultformat = FORMAT_MOODLE; |
254 | } |
501cdbd8 |
255 | |
bbbf2d40 |
256 | if (isset($post->error)) { // User is re-editing a failed posting |
3395f2d6 |
257 | |
258 | // Set up all the required objects again, and reuse the same $post |
259 | |
260 | if (! $forum = get_record("forum", "id", $post->forum)) { |
261 | error("The forum number was incorrect ($post->forum)"); |
262 | } |
263 | |
264 | if (! $course = get_record("course", "id", $forum->course)) { |
265 | error("The course number was incorrect ($forum->course)"); |
266 | } |
267 | |
268 | if (!empty($post->parent)) { |
269 | if (! $parent = forum_get_post_full($post->parent)) { |
270 | error("Parent post ID was incorrect ($post->parent)"); |
271 | } |
272 | } |
273 | |
274 | if (!empty($post->discussion)) { |
275 | if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) { |
276 | error("This post is not part of a discussion! ($post->discussion)"); |
277 | } |
cc86131d |
278 | } else { |
279 | $discussion = new stdClass(); |
280 | $newstopic = false; |
281 | if ($forum->type == 'news' && !$post->parent) { |
282 | $newstopic = true; |
283 | } |
284 | if ($newstopic && empty($post->timestartdisabled)) { |
285 | $discussion->timestart = make_timestamp($post->timestartyear, $post->timestartmonth, $post->timestartday); |
286 | } else { |
287 | $discussion->timestart = 0; |
288 | } |
289 | if ($newstopic && empty($post->timeenddisabled)) { |
290 | $discussion->timeend = make_timestamp($post->timeendyear, $post->timeendmonth, $post->timeendday); |
291 | } else { |
292 | $discussion->timeend = 0; |
293 | } |
3395f2d6 |
294 | } |
295 | |
bbbf2d40 |
296 | } else if (!empty($forum)) { // User is starting a new discussion in a forum |
501cdbd8 |
297 | |
bbbf2d40 |
298 | $SESSION->fromurl = $_SERVER["HTTP_REFERER"]; |
501cdbd8 |
299 | |
300 | if (! $forum = get_record("forum", "id", $forum)) { |
301 | error("The forum number was incorrect ($forum)"); |
302 | } |
303 | if (! $course = get_record("course", "id", $forum->course)) { |
3395f2d6 |
304 | error("The course number was incorrect ($forum->course)"); |
501cdbd8 |
305 | } |
e40a7a3c |
306 | $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); |
307 | |
11b0c469 |
308 | if (! forum_user_can_post_discussion($forum)) { |
501cdbd8 |
309 | error("Sorry, but you can not post a new discussion in this forum."); |
310 | } |
e40a7a3c |
311 | |
80602101 |
312 | if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) { |
0468976c |
313 | if (!$cm->visible and !has_capability('moodle/course:manageactivities', $coursecontext)) { |
80602101 |
314 | error(get_string("activityiscurrentlyhidden")); |
315 | } |
316 | } |
317 | |
501cdbd8 |
318 | // Load up the $post variable. |
319 | |
320 | $post->course = $course->id; |
321 | $post->forum = $forum->id; |
322 | $post->discussion = 0; // ie discussion # not defined yet |
323 | $post->parent = 0; |
324 | $post->subject = ""; |
ebc3bd2b |
325 | $post->userid = $USER->id; |
501cdbd8 |
326 | $post->message = ""; |
213e8cc6 |
327 | $post->format = $defaultformat; |
501cdbd8 |
328 | |
bbbf2d40 |
329 | $post->groupid = get_current_group($course->id); |
330 | if ($post->groupid == 0) { |
331 | $post->groupid = -1; |
332 | } |
333 | |
11b0c469 |
334 | forum_set_return(); |
335 | |
bbbf2d40 |
336 | } else if (!empty($reply)) { // User is writing a new reply |
501cdbd8 |
337 | |
11b0c469 |
338 | if (! $parent = forum_get_post_full($reply)) { |
29ad118c |
339 | error("Parent post ID was incorrect"); |
501cdbd8 |
340 | } |
341 | if (! $discussion = get_record("forum_discussions", "id", $parent->discussion)) { |
29ad118c |
342 | error("This post is not part of a discussion!"); |
501cdbd8 |
343 | } |
344 | if (! $forum = get_record("forum", "id", $discussion->forum)) { |
345 | error("The forum number was incorrect ($discussion->forum)"); |
346 | } |
347 | if (! $course = get_record("course", "id", $discussion->course)) { |
348 | error("The course number was incorrect ($discussion->course)"); |
349 | } |
e40a7a3c |
350 | $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); |
351 | |
6c506ca7 |
352 | if (! forum_user_can_post($forum)) { |
353 | error("Sorry, but you can not post in this forum."); |
354 | } |
e40a7a3c |
355 | |
02509fe6 |
356 | if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) { |
bbbf2d40 |
357 | if (groupmode($course, $cm)) { // Make sure user can post here |
63e20b88 |
358 | $mygroupid = mygroupid($course->id); |
fa22fd5f |
359 | if (!((empty($mygroupid) and $discussion->groupid == -1) || (ismember($discussion->groupid)/*$mygroupid == $discussion->groupid*/))) { |
02509fe6 |
360 | error("Sorry, but you can not post in this discussion."); |
361 | } |
362 | } |
0468976c |
363 | if (!$cm->visible and !has_capability('moodle/course:manageactivities', $coursecontext)) { |
80602101 |
364 | error(get_string("activityiscurrentlyhidden")); |
365 | } |
02509fe6 |
366 | } |
367 | |
501cdbd8 |
368 | // Load up the $post variable. |
369 | |
370 | $post->course = $course->id; |
371 | $post->forum = $forum->id; |
372 | $post->discussion = $parent->discussion; |
373 | $post->parent = $parent->id; |
374 | $post->subject = $parent->subject; |
ebc3bd2b |
375 | $post->userid = $USER->id; |
501cdbd8 |
376 | $post->message = ""; |
213e8cc6 |
377 | $post->format = $defaultformat; |
501cdbd8 |
378 | |
e9584ca3 |
379 | $strre = get_string('re', 'forum'); |
380 | if (!(substr($post->subject, 0, strlen($strre)) == $strre)) { |
381 | $post->subject = $strre.' '.$post->subject; |
501cdbd8 |
382 | } |
383 | |
b22b0e61 |
384 | unset($SESSION->fromdiscussion); |
501cdbd8 |
385 | |
bbbf2d40 |
386 | } else if (!empty($edit)) { // User is editing their own post |
b8be40ce |
387 | |
11b0c469 |
388 | if (! $post = forum_get_post_full($edit)) { |
501cdbd8 |
389 | error("Post ID was incorrect"); |
390 | } |
501cdbd8 |
391 | if ($post->parent) { |
11b0c469 |
392 | if (! $parent = forum_get_post_full($post->parent)) { |
501cdbd8 |
393 | error("Parent post ID was incorrect ($post->parent)"); |
394 | } |
395 | } |
396 | if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) { |
397 | error("This post is not part of a discussion! ($reply)"); |
398 | } |
399 | if (! $forum = get_record("forum", "id", $discussion->forum)) { |
400 | error("The forum number was incorrect ($discussion->forum)"); |
401 | } |
fbc21e82 |
402 | if (!($forum->type == 'news' && !$post->parent && $discussion->timestart > time())) { |
bbbf2d40 |
403 | if (((time() - $post->created) > $CFG->maxeditingtime) and |
0468976c |
404 | !has_capability('mod/forum:editanypost', $modcontext)) { |
bbbf2d40 |
405 | error( get_string("maxtimehaspassed", "forum", format_time($CFG->maxeditingtime)) ); |
fbc21e82 |
406 | } |
407 | } |
501cdbd8 |
408 | if (! $course = get_record("course", "id", $discussion->course)) { |
409 | error("The course number was incorrect ($discussion->course)"); |
410 | } |
e51e3fcb |
411 | if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) { |
6e89ca55 |
412 | error('Could not get the course module for the forum instance.'); |
413 | } else { |
414 | $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); |
415 | } |
416 | if (($post->userid <> $USER->id) and |
417 | !has_capability('mod/forum:editanypost', $modcontext)) { |
418 | error("You can't edit other people's posts!"); |
419 | } |
501cdbd8 |
420 | |
421 | // Load up the $post variable. |
501cdbd8 |
422 | $post->edit = $edit; |
423 | |
424 | $post->course = $course->id; |
425 | $post->forum = $forum->id; |
426 | |
b22b0e61 |
427 | unset($SESSION->fromdiscussion); |
501cdbd8 |
428 | |
429 | |
bbbf2d40 |
430 | } else if (!empty($delete)) { // User is deleting a post |
501cdbd8 |
431 | |
11b0c469 |
432 | if (! $post = forum_get_post_full($delete)) { |
501cdbd8 |
433 | error("Post ID was incorrect"); |
434 | } |
501cdbd8 |
435 | if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) { |
436 | error("This post is not part of a discussion!"); |
437 | } |
64eacd6f |
438 | if (! $forum = get_record("forum", "id", $discussion->forum)) { |
439 | error("The forum number was incorrect ($discussion->forum)"); |
440 | } |
e40a7a3c |
441 | if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) { |
6e89ca55 |
442 | error('Could not get the course module for the forum instance.'); |
e40a7a3c |
443 | } else { |
444 | $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); |
445 | } |
0468976c |
446 | if ( !(($post->userid == $USER->id && has_capability('mod/forum:deleteownpost', $modcontext)) |
447 | || has_capability('mod/forum:deleteanypost', $modcontext)) ) { |
bbbf2d40 |
448 | error("You can't delete this post!"); |
64eacd6f |
449 | } |
0d1db48e |
450 | if (!empty($forum->course)) { |
451 | if ($course = get_record('course', 'id', $forum->course)) { |
452 | if (!empty($course->lang)) { |
453 | $CFG->courselang = $course->lang; |
454 | } |
455 | } |
456 | } |
501cdbd8 |
457 | |
b82faacd |
458 | $replycount = forum_count_replies($post); |
459 | |
bbbf2d40 |
460 | if (!empty($confirm)) { // User has confirmed the delete |
501cdbd8 |
461 | |
462 | if ($post->totalscore) { |
8f0cd6ef |
463 | notice(get_string("couldnotdeleteratings", "forum"), |
11b0c469 |
464 | forum_go_back_to("discuss.php?d=$post->discussion")); |
501cdbd8 |
465 | |
0468976c |
466 | } else if ($replycount && !has_capability('mod/forum:deleteanypost', $modcontext)) { |
cf38360f |
467 | error(get_string("couldnotdeletereplies", "forum"), |
8203d211 |
468 | forum_go_back_to("discuss.php?d=$post->discussion")); |
501cdbd8 |
469 | |
470 | } else { |
471 | if (! $post->parent) { // post is a discussion topic as well, so delete discussion |
64eacd6f |
472 | if ($forum->type == "single") { |
8f0cd6ef |
473 | notice("Sorry, but you are not allowed to delete that discussion!", |
64eacd6f |
474 | forum_go_back_to("discuss.php?d=$post->discussion")); |
475 | } |
501cdbd8 |
476 | forum_delete_discussion($discussion); |
477 | |
8f0cd6ef |
478 | add_to_log($discussion->course, "forum", "delete discussion", |
b17333be |
479 | "view.php?id=$cm->id", "$forum->id", $cm->id); |
69d79bc3 |
480 | |
8f0cd6ef |
481 | redirect("view.php?f=$discussion->forum", |
cf38360f |
482 | get_string("deleteddiscussion", "forum"), 1); |
501cdbd8 |
483 | |
0468976c |
484 | } else if (forum_delete_post($post, has_capability('mod/forum:deleteanypost', $modcontext))) { |
501cdbd8 |
485 | |
8f0cd6ef |
486 | add_to_log($discussion->course, "forum", "delete post", |
69d79bc3 |
487 | "discuss.php?d=$post->discussion", "$post->id", $cm->id); |
488 | |
b82faacd |
489 | $feedback = $replycount ? get_string('deletedposts', 'forum') : get_string('deletedpost', 'forum'); |
490 | redirect(forum_go_back_to("discuss.php?d=$post->discussion"), $feedback, 1); |
501cdbd8 |
491 | } else { |
492 | error("An error occurred while deleting record $post->id"); |
493 | } |
494 | } |
495 | |
496 | |
bbbf2d40 |
497 | } else { // User just asked to delete something |
501cdbd8 |
498 | |
11b0c469 |
499 | forum_set_return(); |
501cdbd8 |
500 | |
b82faacd |
501 | if ($replycount) { |
6e89ca55 |
502 | if (!has_capability('mod/forum:deleteanypost', $modcontext)) { |
b82faacd |
503 | error(get_string("couldnotdeletereplies", "forum"), |
504 | forum_go_back_to("discuss.php?d=$post->discussion")); |
505 | } |
506 | print_header(); |
507 | notice_yesno(get_string("deletesureplural", "forum", $replycount+1), |
bbbf2d40 |
508 | "post.php?delete=$delete&confirm=$delete", |
b82faacd |
509 | $_SERVER["HTTP_REFERER"]); |
510 | |
511 | forum_print_post($post, $course->id, $ownpost=false, $reply=false, $link=false); |
512 | if (empty($post->edit)) { |
eaf50aef |
513 | if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) { |
b82faacd |
514 | $user_read_array = forum_tp_get_discussion_read_records($USER->id, $discussion->id); |
515 | } else { |
516 | $user_read_array = array(); |
517 | } |
518 | forum_print_posts_nested($post->id, $course->id, false, false, $user_read_array, $forum->id); |
519 | } |
520 | } else { |
521 | print_header(); |
522 | notice_yesno(get_string("deletesure", "forum", $replycount), |
bbbf2d40 |
523 | "post.php?delete=$delete&confirm=$delete", |
b82faacd |
524 | $_SERVER["HTTP_REFERER"]); |
525 | forum_print_post($post, $forum->course, $ownpost=false, $reply=false, $link=false); |
526 | } |
8f0cd6ef |
527 | |
501cdbd8 |
528 | } |
839f2456 |
529 | print_footer($course); |
501cdbd8 |
530 | die; |
531 | |
532 | |
6e89ca55 |
533 | } else if (!empty($prune)) { // Pruning |
8f0cd6ef |
534 | |
0d5da5dd |
535 | if (!$post = forum_get_post_full($prune)) { |
cf84431b |
536 | error("Post ID was incorrect"); |
537 | } |
0d5da5dd |
538 | if (!$discussion = get_record("forum_discussions", "id", $post->discussion)) { |
cf84431b |
539 | error("This post is not part of a discussion!"); |
540 | } |
0d5da5dd |
541 | if (!$forum = get_record("forum", "id", $discussion->forum)) { |
cf84431b |
542 | error("The forum number was incorrect ($discussion->forum)"); |
543 | } |
77dc6d0b |
544 | if ($forum->type == 'single') { |
545 | error('Discussions from this forum cannot be split'); |
546 | } |
cf84431b |
547 | if (!$post->parent) { |
548 | error('This is already the first post in the discussion'); |
549 | } |
0d5da5dd |
550 | if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) { // For the logs |
6e89ca55 |
551 | error('Could not get the course module for the forum instance.'); |
552 | } else { |
553 | $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); |
554 | } |
555 | if (!has_capability('mod/forum:splitdiscussions', $modcontext)) { |
556 | error("You can't split discussions!"); |
0d5da5dd |
557 | } |
cf84431b |
558 | |
bbbf2d40 |
559 | if (!empty($name)) { // User has confirmed the prune |
8f0cd6ef |
560 | |
cf84431b |
561 | $newdiscussion->course = $discussion->course; |
562 | $newdiscussion->forum = $discussion->forum; |
bbbf2d40 |
563 | $newdiscussion->name = $name; |
cf84431b |
564 | $newdiscussion->firstpost = $post->id; |
565 | $newdiscussion->userid = $discussion->userid; |
566 | $newdiscussion->groupid = $discussion->groupid; |
567 | $newdiscussion->assessed = $discussion->assessed; |
568 | $newdiscussion->usermodified = $post->userid; |
fbc21e82 |
569 | $newdiscussion->timestart = $discussion->timestart; |
570 | $newdiscussion->timeend = $discussion->timeend; |
8f0cd6ef |
571 | |
cf84431b |
572 | if (!$newid = insert_record('forum_discussions', $newdiscussion)) { |
573 | error('Could not create new discussion'); |
574 | } |
8f0cd6ef |
575 | |
13152de4 |
576 | $newpost->id = $post->id; |
577 | $newpost->parent = 0; |
bbbf2d40 |
578 | $newpost->subject = $name; |
d078ee9b |
579 | |
13152de4 |
580 | if (!update_record("forum_posts", $newpost)) { |
d078ee9b |
581 | error('Could not update the original post'); |
582 | } |
583 | |
cf84431b |
584 | forum_change_discussionid($post->id, $newid); |
8f0cd6ef |
585 | |
1da8c568 |
586 | // update last post in each discussion |
587 | forum_discussion_update_last_post($discussion->id); |
588 | forum_discussion_update_last_post($newid); |
cf84431b |
589 | |
8f0cd6ef |
590 | add_to_log($discussion->course, "forum", "prune post", |
cf84431b |
591 | "discuss.php?d=$newid", "$post->id", $cm->id); |
592 | |
d078ee9b |
593 | redirect(forum_go_back_to("discuss.php?d=$newid"), get_string("prunedpost", "forum"), 1); |
cf84431b |
594 | |
595 | } else { // User just asked to prune something |
596 | |
597 | $course = get_record('course', 'id', $forum->course); |
598 | $strforums = get_string("modulenameplural", "forum"); |
c78ac798 |
599 | print_header_simple(format_string($discussion->name).": ".format_string($post->subject), "", |
8f0cd6ef |
600 | "<a href=\"../forum/index.php?id=$course->id\">$strforums</a> -> |
3849dae8 |
601 | <a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a> -> |
17dc3f3c |
602 | <a href=\"discuss.php?d=$discussion->id\">".format_string($post->subject,true)."</a> -> ". |
cf84431b |
603 | get_string("prune", "forum"), '', "", true, "", navmenu($course, $cm)); |
8f0cd6ef |
604 | |
cf84431b |
605 | print_heading(get_string('pruneheading', 'forum')); |
d078ee9b |
606 | echo '<center>'; |
8f0cd6ef |
607 | |
cf84431b |
608 | include('prune.html'); |
8f0cd6ef |
609 | |
cf84431b |
610 | forum_print_post($post, $forum->course, $ownpost=false, $reply=false, $link=false); |
839f2456 |
611 | echo '</center>'; |
cf84431b |
612 | } |
839f2456 |
613 | print_footer($course); |
cf84431b |
614 | die; |
615 | |
616 | |
501cdbd8 |
617 | } else { |
618 | error("No operation specified"); |
619 | |
620 | } |
621 | |
622 | |
8f0cd6ef |
623 | // To get here they need to edit a post, and the $post |
501cdbd8 |
624 | // variable will be loaded with all the particulars, |
625 | // so bring up the form. |
626 | |
627 | // $course, $forum are defined. $discussion is for edit and reply only. |
628 | |
ec81373f |
629 | $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id); |
630 | |
631 | require_login($course->id, false, $cm); |
501cdbd8 |
632 | |
f2b5d7e3 |
633 | $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); |
dfc9ba9b |
634 | |
501cdbd8 |
635 | if ($post->discussion) { |
9fa49e22 |
636 | if (! $toppost = get_record("forum_posts", "discussion", $post->discussion, "parent", 0)) { |
501cdbd8 |
637 | error("Could not find top parent of post $post->id"); |
638 | } |
639 | } else { |
680afe2e |
640 | $toppost->subject = ($forum->type == "news") ? get_string("addanewtopic", "forum") : |
21da9db8 |
641 | get_string("addanewdiscussion", "forum"); |
501cdbd8 |
642 | } |
643 | |
3bbde520 |
644 | if (empty($post->subject)) { |
0ae5e5ea |
645 | $formstart = "theform.subject"; |
3bbde520 |
646 | } else { |
647 | $formstart = ""; |
501cdbd8 |
648 | } |
649 | |
650 | if ($post->parent) { |
17dc3f3c |
651 | $navtail = "<a href=\"discuss.php?d=$discussion->id\">".format_string($toppost->subject,true)."</a> -> ".get_string("editing", "forum"); |
501cdbd8 |
652 | } else { |
17dc3f3c |
653 | $navtail = format_string($toppost->subject); |
501cdbd8 |
654 | } |
655 | |
9c9f7d77 |
656 | if (empty($post->edit)) { |
657 | $post->edit = ""; |
658 | } |
659 | |
cf38360f |
660 | $strforums = get_string("modulenameplural", "forum"); |
661 | |
73bb0835 |
662 | |
3849dae8 |
663 | $navmiddle = "<a href=\"../forum/index.php?id=$course->id\">$strforums</a> -> <a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>"; |
501cdbd8 |
664 | |
f37da850 |
665 | if (empty($discussion->name)) { |
8cb091e6 |
666 | if (empty($discussion)) { |
667 | $discussion = new object; |
668 | } |
9c9f7d77 |
669 | $discussion->name = $forum->name; |
670 | } |
671 | |
501cdbd8 |
672 | if ($course->category) { |
c78ac798 |
673 | print_header("$course->shortname: ".format_string($discussion->name).": ".format_string($toppost->subject), "$course->fullname", |
325505f4 |
674 | "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> -> |
3bbde520 |
675 | $navmiddle -> $navtail", $formstart, "", true, "", navmenu($course, $cm)); |
680afe2e |
676 | |
501cdbd8 |
677 | } else { |
c78ac798 |
678 | print_header("$course->shortname: ".format_string($discussion->name).": ".format_string($toppost->subject), "$course->fullname", |
9c9f7d77 |
679 | "$navmiddle -> $navtail", "$formstart", "", true, "", navmenu($course, $cm)); |
501cdbd8 |
680 | |
681 | } |
682 | |
098d27d4 |
683 | // checkup |
951e1073 |
684 | if (!empty($parent) && !forum_user_can_see_post($forum, $discussion, $post)) { |
098d27d4 |
685 | error("You cannot reply to this post"); |
686 | } |
bbbf2d40 |
687 | if (empty($parent) && !forum_user_can_post_discussion($forum)) { |
098d27d4 |
688 | error("You cannot start a new discussion in this forum"); |
689 | } |
690 | |
0468976c |
691 | if ($forum->type == 'qanda' && !has_capability('mod/forum:viewqandawithoutposting', $modcontext) && |
bbbf2d40 |
692 | !forum_user_has_posted($forum->id,$discussion->id,$USER->id)) { |
098d27d4 |
693 | notify(get_string('qandanotify','forum')); |
694 | } |
695 | |
a4f495bf |
696 | forum_check_throttling($forum); |
697 | |
3395f2d6 |
698 | if (!empty($parent)) { |
11b0c469 |
699 | forum_print_post($parent, $course->id, $ownpost=false, $reply=false, $link=false); |
f8029045 |
700 | if (empty($post->edit)) { |
eaf50aef |
701 | if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) { |
f37da850 |
702 | $user_read_array = forum_tp_get_discussion_read_records($USER->id, $discussion->id); |
703 | } else { |
704 | $user_read_array = array(); |
705 | } |
0468976c |
706 | if ($forum->type != 'qanda' || forum_user_can_see_discussion($forum, $discussion, $modcontext)) { |
098d27d4 |
707 | forum_print_posts_threaded($parent->id, $course->id, 0, false, false, $user_read_array, $discussion->forum); |
708 | } |
f8029045 |
709 | } |
556963f5 |
710 | print_heading(get_string("yourreply", "forum").':'); |
501cdbd8 |
711 | } else { |
21da9db8 |
712 | $forum->intro = trim($forum->intro); |
713 | if (!empty($forum->intro)) { |
714 | print_simple_box(format_text($forum->intro), 'center'); |
715 | } |
098d27d4 |
716 | if ($forum->type == 'qanda') { |
717 | print_heading(get_string('yournewquestion','forum')); |
718 | } else { |
719 | print_heading(get_string('yournewtopic', 'forum')); |
720 | } |
501cdbd8 |
721 | } |
556963f5 |
722 | echo '<center>'; |
3395f2d6 |
723 | if (!empty($post->error)) { |
724 | notify($post->error); |
725 | } |
556963f5 |
726 | echo '</center>'; |
501cdbd8 |
727 | |
9bdedea4 |
728 | if ($USER->id != $post->userid) { // Not the original author, so add a message to the end |
729 | $data->date = userdate($post->modified); |
730 | if ($post->format == FORMAT_HTML) { |
731 | $data->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$USER->id.'&course='.$post->course.'">'. |
732 | fullname($USER).'</a>'; |
733 | $post->message .= '<p>(<span class="edited">'.get_string('editedby', 'forum', $data).'</span>)</p>'; |
734 | } else { |
735 | $data->name = fullname($USER); |
736 | $post->message .= "\n\n(".get_string('editedby', 'forum', $data).')'; |
737 | } |
738 | } |
739 | |
d30867b0 |
740 | print_simple_box_start("center"); |
501cdbd8 |
741 | require("post.html"); |
742 | print_simple_box_end(); |
743 | |
4b00b4b3 |
744 | if ($usehtmleditor) { |
76138908 |
745 | use_html_editor("message"); |
4b00b4b3 |
746 | } |
747 | |
501cdbd8 |
748 | print_footer($course); |
749 | |
750 | |
96662058 |
751 | ?> |