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