Delete links on forum post now only show if post has no children
[moodle.git] / mod / forum / post.php
CommitLineData
501cdbd8 1<?PHP // $Id$
2
3// Edit and save a new post to a discussion
4
5
b0e3a925 6 require_once("../../config.php");
7 require_once("lib.php");
501cdbd8 8
9 if (isguest()) {
607809b3 10 error(get_string("noguestpost", "forum"), $_SERVER["HTTP_REFERER"]);
501cdbd8 11 }
12
48d38fad 13 require_login(); // Script is useless unless they're logged in
14
36b4f985 15 if ($post = data_submitted()) {
501cdbd8 16
3395f2d6 17 if (empty($SESSION->fromurl)) {
18 $errordestination = "$CFG->wwwroot/mod/forum/view.php?f=$post->forum";
19 } else {
20 $errordestination = $SESSION->fromurl;
21 }
22
83ec9098 23 $post->subject = strip_tags($post->subject, '<lang>'); // Strip all tags except lang
db46e49c 24 $post->subject = break_up_long_words($post->subject);
25
73bb0835 26 $post->message = clean_text($post->message, $post->format); // Clean up any bad tags
501cdbd8 27
36257d39 28 $post->attachment = isset($_FILES['attachment']) ? $_FILES['attachment'] : NULL;
7f6689e4 29
69d79bc3 30 if (!$cm = get_coursemodule_from_instance("forum", $post->forum, $post->course)) { // For the logs
31 $cm->id = 0;
32 }
33
3395f2d6 34 if (!$post->subject or !$post->message) {
35 $post->error = get_string("emptymessage", "forum");
7f6689e4 36
3395f2d6 37 } else if ($post->edit) { // Updating a post
501cdbd8 38 $post->id = $post->edit;
7f6689e4 39 if (forum_update_post($post)) {
69d79bc3 40
41 add_to_log($post->course, "forum", "update post",
42 "discuss.php?d=$post->discussion&parent=$post->id", "$post->id", $cm->id);
43
0a9f61b5 44 $message = get_string("postupdated", "forum");
45 $timemessage = 1;
46
47 if ($subscribemessage = forum_post_subscription($post)) {
48 $timemessage = 2;
49 }
b22b0e61 50 redirect(forum_go_back_to("discuss.php?d=$post->discussion#$post->id"), $message.$subscribemessage, $timemessage);
0a9f61b5 51
501cdbd8 52 } else {
3395f2d6 53 error(get_string("couldnotupdate", "forum"), $errordestination);
501cdbd8 54 }
3395f2d6 55 exit;
7f6689e4 56
501cdbd8 57 } else if ($post->discussion) { // Adding a new post to an existing discussion
11b0c469 58 if ($post->id = forum_add_new_post($post)) {
69d79bc3 59
60 add_to_log($post->course, "forum", "add post",
61 "discuss.php?d=$post->discussion&parent=$post->id", "$post->id", $cm->id);
62
0a9f61b5 63 $message = get_string("postadded", "forum", format_time($CFG->maxeditingtime));
64 $timemessage = 2;
65
66 if ($subscribemessage = forum_post_subscription($post)) {
67 $timemessage = 4;
501cdbd8 68 }
69
b22b0e61 70 redirect(forum_go_back_to("discuss.php?d=$post->discussion#$post->id"), $message.$subscribemessage, $timemessage);
0a9f61b5 71
501cdbd8 72 } else {
3395f2d6 73 error(get_string("couldnotadd", "forum"), $errordestination);
501cdbd8 74 }
3395f2d6 75 exit;
76
501cdbd8 77 } else { // Adding a new discussion
78 $discussion = $post;
79 $discussion->name = $post->subject;
80 $discussion->intro = $post->message;
81 if ($discussion->id = forum_add_discussion($discussion)) {
69d79bc3 82
83 add_to_log($post->course, "forum", "add discussion",
84 "discuss.php?d=$discussion->id", "$discussion->id", $cm->id);
85
0a9f61b5 86 $message = get_string("postadded", "forum", format_time($CFG->maxeditingtime));
87 $timemessage = 2;
88
89 if ($subscribemessage = forum_post_subscription($discussion)) {
90 $timemessage = 4;
91 }
92
93 redirect(forum_go_back_to("view.php?f=$post->forum"), $message.$subscribemessage, $timemessage);
94
501cdbd8 95 } else {
3395f2d6 96 error(get_string("couldnotadd", "forum"), $errordestination);
501cdbd8 97 }
3395f2d6 98 exit;
501cdbd8 99 }
501cdbd8 100 }
101
213e8cc6 102 if ($usehtmleditor = can_use_richtext_editor()) {
103 $defaultformat = FORMAT_HTML;
213e8cc6 104 } else {
105 $defaultformat = FORMAT_MOODLE;
106 }
501cdbd8 107
108
3395f2d6 109 if (isset($post->error)) { // User is re-editing a failed posting
110
111 // Set up all the required objects again, and reuse the same $post
112
113 if (! $forum = get_record("forum", "id", $post->forum)) {
114 error("The forum number was incorrect ($post->forum)");
115 }
116
117 if (! $course = get_record("course", "id", $forum->course)) {
118 error("The course number was incorrect ($forum->course)");
119 }
120
121 if (!empty($post->parent)) {
122 if (! $parent = forum_get_post_full($post->parent)) {
123 error("Parent post ID was incorrect ($post->parent)");
124 }
125 }
126
127 if (!empty($post->discussion)) {
128 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
129 error("This post is not part of a discussion! ($post->discussion)");
130 }
131 }
132
133 } else if (isset($forum)) { // User is starting a new discussion in a forum
501cdbd8 134
607809b3 135 $SESSION->fromurl = $_SERVER["HTTP_REFERER"];
501cdbd8 136
137 if (! $forum = get_record("forum", "id", $forum)) {
138 error("The forum number was incorrect ($forum)");
139 }
140 if (! $course = get_record("course", "id", $forum->course)) {
3395f2d6 141 error("The course number was incorrect ($forum->course)");
501cdbd8 142 }
143
11b0c469 144 if (! forum_user_can_post_discussion($forum)) {
501cdbd8 145 error("Sorry, but you can not post a new discussion in this forum.");
146 }
147
80602101 148 if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
149 if (!$cm->visible and !isteacher($course->id)) {
150 error(get_string("activityiscurrentlyhidden"));
151 }
152 }
153
501cdbd8 154 // Load up the $post variable.
155
156 $post->course = $course->id;
157 $post->forum = $forum->id;
158 $post->discussion = 0; // ie discussion # not defined yet
159 $post->parent = 0;
160 $post->subject = "";
ebc3bd2b 161 $post->userid = $USER->id;
02509fe6 162 $post->groupid = get_current_group($course->id);
501cdbd8 163 $post->message = "";
213e8cc6 164 $post->format = $defaultformat;
501cdbd8 165
11b0c469 166 forum_set_return();
167
501cdbd8 168 } else if (isset($reply)) { // User is writing a new reply
169
11b0c469 170 if (! $parent = forum_get_post_full($reply)) {
501cdbd8 171 error("Parent post ID was incorrect ($reply)");
172 }
173 if (! $discussion = get_record("forum_discussions", "id", $parent->discussion)) {
174 error("This post is not part of a discussion! ($reply)");
175 }
176 if (! $forum = get_record("forum", "id", $discussion->forum)) {
177 error("The forum number was incorrect ($discussion->forum)");
178 }
179 if (! $course = get_record("course", "id", $discussion->course)) {
180 error("The course number was incorrect ($discussion->course)");
181 }
6c506ca7 182
183 if (! forum_user_can_post($forum)) {
184 error("Sorry, but you can not post in this forum.");
185 }
02509fe6 186
187 if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
188 if (groupmode($course, $cm) and !isteacheredit($course->id)) { // Make sure user can post here
189 if (mygroupid($course->id) != $discussion->groupid) {
190 error("Sorry, but you can not post in this discussion.");
191 }
192 }
80602101 193 if (!$cm->visible and !isteacher($course->id)) {
194 error(get_string("activityiscurrentlyhidden"));
195 }
02509fe6 196 }
197
501cdbd8 198 // Load up the $post variable.
199
200 $post->course = $course->id;
201 $post->forum = $forum->id;
202 $post->discussion = $parent->discussion;
203 $post->parent = $parent->id;
204 $post->subject = $parent->subject;
ebc3bd2b 205 $post->userid = $USER->id;
501cdbd8 206 $post->message = "";
213e8cc6 207 $post->format = $defaultformat;
501cdbd8 208
e9584ca3 209 $strre = get_string('re', 'forum');
210 if (!(substr($post->subject, 0, strlen($strre)) == $strre)) {
211 $post->subject = $strre.' '.$post->subject;
501cdbd8 212 }
213
b22b0e61 214 unset($SESSION->fromdiscussion);
501cdbd8 215
216 } else if (isset($edit)) { // User is editing their own post
217
b8be40ce 218 $adminedit = (isadmin() and !empty($CFG->admineditalways));
219
11b0c469 220 if (! $post = forum_get_post_full($edit)) {
501cdbd8 221 error("Post ID was incorrect");
222 }
b8be40ce 223 if (($post->userid <> $USER->id) and !$adminedit) {
501cdbd8 224 error("You can't edit other people's posts!");
225 }
b8be40ce 226 if (((time() - $post->created) > $CFG->maxeditingtime) and !$adminedit) {
cf38360f 227 error( get_string("maxtimehaspassed", "forum", format_time($CFG->maxeditingtime)) );
501cdbd8 228 }
229 if ($post->parent) {
11b0c469 230 if (! $parent = forum_get_post_full($post->parent)) {
501cdbd8 231 error("Parent post ID was incorrect ($post->parent)");
232 }
233 }
234 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
235 error("This post is not part of a discussion! ($reply)");
236 }
237 if (! $forum = get_record("forum", "id", $discussion->forum)) {
238 error("The forum number was incorrect ($discussion->forum)");
239 }
240 if (! $course = get_record("course", "id", $discussion->course)) {
241 error("The course number was incorrect ($discussion->course)");
242 }
243
244 // Load up the $post variable.
245
246 $post->edit = $edit;
247
248 $post->course = $course->id;
249 $post->forum = $forum->id;
250
b22b0e61 251 unset($SESSION->fromdiscussion);
501cdbd8 252
253
254 } else if (isset($delete)) { // User is deleting a post
255
11b0c469 256 if (! $post = forum_get_post_full($delete)) {
501cdbd8 257 error("Post ID was incorrect");
258 }
501cdbd8 259 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
260 error("This post is not part of a discussion!");
261 }
64eacd6f 262 if (! $forum = get_record("forum", "id", $discussion->forum)) {
263 error("The forum number was incorrect ($discussion->forum)");
264 }
ebc3bd2b 265 if (($post->userid <> $USER->id) and !isteacher($forum->course)) {
64eacd6f 266 error("You can't delete other people's posts!");
267 }
501cdbd8 268
269 if (isset($confirm)) { // User has confirmed the delete
270
271 if ($post->totalscore) {
cf38360f 272 notice(get_string("couldnotdeleteratings", "forum"),
11b0c469 273 forum_go_back_to("discuss.php?d=$post->discussion"));
501cdbd8 274
275 } else if (record_exists("forum_posts", "parent", $delete)) {
cf38360f 276 error(get_string("couldnotdeletereplies", "forum"),
11b0c469 277 forum_go_back_to("discuss.php?id=$post->discussion"));
501cdbd8 278
279 } else {
69d79bc3 280 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) { // For the logs
281 $cm->id = 0;
282 }
501cdbd8 283 if (! $post->parent) { // post is a discussion topic as well, so delete discussion
64eacd6f 284 if ($forum->type == "single") {
285 notice("Sorry, but you are not allowed to delete that discussion!",
286 forum_go_back_to("discuss.php?d=$post->discussion"));
287 }
501cdbd8 288 forum_delete_discussion($discussion);
289
69d79bc3 290 add_to_log($discussion->course, "forum", "delete discussion",
b17333be 291 "view.php?id=$cm->id", "$forum->id", $cm->id);
69d79bc3 292
501cdbd8 293 redirect("view.php?f=$discussion->forum",
cf38360f 294 get_string("deleteddiscussion", "forum"), 1);
501cdbd8 295
7f6689e4 296 } else if (forum_delete_post($post)) {
501cdbd8 297
69d79bc3 298 add_to_log($discussion->course, "forum", "delete post",
299 "discuss.php?d=$post->discussion", "$post->id", $cm->id);
300
11b0c469 301 redirect(forum_go_back_to("discuss.php?d=$post->discussion"),
cf38360f 302 get_string("deletedpost", "forum"), 1);
501cdbd8 303 } else {
304 error("An error occurred while deleting record $post->id");
305 }
306 }
307
308
309 } else { // User just asked to delete something
310
11b0c469 311 forum_set_return();
501cdbd8 312
313 print_header();
cf38360f 314 notice_yesno(get_string("deletesure", "forum"),
501cdbd8 315 "post.php?delete=$delete&confirm=$delete",
607809b3 316 $_SERVER["HTTP_REFERER"]);
501cdbd8 317
318 echo "<CENTER><HR>";
8aed46c7 319 forum_print_post($post, $forum->course, $ownpost=false, $reply=false, $link=false);
501cdbd8 320 }
321
322 die;
323
324
325 } else {
326 error("No operation specified");
327
328 }
329
330
331 // To get here they need to edit a post, and the $post
332 // variable will be loaded with all the particulars,
333 // so bring up the form.
334
335 // $course, $forum are defined. $discussion is for edit and reply only.
336
337 require_login($course->id);
338
dfc9ba9b 339
501cdbd8 340 if ($post->discussion) {
9fa49e22 341 if (! $toppost = get_record("forum_posts", "discussion", $post->discussion, "parent", 0)) {
501cdbd8 342 error("Could not find top parent of post $post->id");
343 }
344 } else {
cf38360f 345 $toppost->subject = get_string("yournewtopic", "forum");
501cdbd8 346 }
347
3bbde520 348 if (empty($post->subject)) {
0ae5e5ea 349 $formstart = "theform.subject";
3bbde520 350 } else {
351 $formstart = "";
501cdbd8 352 }
353
354 if ($post->parent) {
cf38360f 355 $navtail = "<A HREF=\"discuss.php?d=$discussion->id\">$toppost->subject</A> -> ".get_string("editing", "forum");
501cdbd8 356 } else {
357 $navtail = "$toppost->subject";
358 }
359
9c9f7d77 360 if (empty($post->edit)) {
361 $post->edit = "";
362 }
363
cf38360f 364 $strforums = get_string("modulenameplural", "forum");
365
73bb0835 366
cf38360f 367 $navmiddle = "<A HREF=\"../forum/index.php?id=$course->id\">$strforums</A> -> <A HREF=\"view.php?f=$forum->id\">$forum->name</A>";
501cdbd8 368
dfc9ba9b 369 $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
370
9c9f7d77 371 if (empty($discussion->name)) {
372 $discussion->name = $forum->name;
373 }
374
501cdbd8 375 if ($course->category) {
376 print_header("$course->shortname: $discussion->name: $toppost->subject", "$course->fullname",
377 "<A HREF=../../course/view.php?id=$course->id>$course->shortname</A> ->
3bbde520 378 $navmiddle -> $navtail", $formstart, "", true, "", navmenu($course, $cm));
501cdbd8 379 } else {
380 print_header("$course->shortname: $discussion->name: $toppost->subject", "$course->fullname",
9c9f7d77 381 "$navmiddle -> $navtail", "$formstart", "", true, "", navmenu($course, $cm));
501cdbd8 382
383 }
384
3395f2d6 385 if (!empty($parent)) {
11b0c469 386 forum_print_post($parent, $course->id, $ownpost=false, $reply=false, $link=false);
f8029045 387 if (empty($post->edit)) {
388 forum_print_posts_threaded($parent->id, $course, 0, false, false);
389 }
2e82fd38 390 echo "<center>";
cf38360f 391 echo "<H2>".get_string("yourreply", "forum").":</H2>";
501cdbd8 392 } else {
2e82fd38 393 echo "<center>";
cf38360f 394 echo "<H2>".get_string("yournewtopic", "forum")."</H2>";
501cdbd8 395 }
3395f2d6 396 if (!empty($post->error)) {
397 notify($post->error);
398 }
4b00b4b3 399 echo "</center>";
501cdbd8 400
401 print_simple_box_start("center", "", "$THEME->cellheading");
402 require("post.html");
403 print_simple_box_end();
404
4b00b4b3 405 if ($usehtmleditor) {
406 use_html_editor();
407 }
408
501cdbd8 409 print_footer($course);
410
411
412?>