Load overlib.js as an external Javascript file, on all pages
[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;
501cdbd8 141 $post->message = "";
213e8cc6 142 $post->format = $defaultformat;
501cdbd8 143
11b0c469 144 forum_set_return();
145
501cdbd8 146 } else if (isset($reply)) { // User is writing a new reply
147
11b0c469 148 if (! $parent = forum_get_post_full($reply)) {
501cdbd8 149 error("Parent post ID was incorrect ($reply)");
150 }
151 if (! $discussion = get_record("forum_discussions", "id", $parent->discussion)) {
152 error("This post is not part of a discussion! ($reply)");
153 }
154 if (! $forum = get_record("forum", "id", $discussion->forum)) {
155 error("The forum number was incorrect ($discussion->forum)");
156 }
157 if (! $course = get_record("course", "id", $discussion->course)) {
158 error("The course number was incorrect ($discussion->course)");
159 }
6c506ca7 160
161 if (! forum_user_can_post($forum)) {
162 error("Sorry, but you can not post in this forum.");
163 }
501cdbd8 164 // Load up the $post variable.
165
166 $post->course = $course->id;
167 $post->forum = $forum->id;
168 $post->discussion = $parent->discussion;
169 $post->parent = $parent->id;
170 $post->subject = $parent->subject;
ebc3bd2b 171 $post->userid = $USER->id;
501cdbd8 172 $post->message = "";
213e8cc6 173 $post->format = $defaultformat;
501cdbd8 174
cf38360f 175 $strre = get_string("re", "forum");
176 if (!(substr($post->subject, 0, 3) == $strre)) {
177 $post->subject = "$strre $post->subject";
501cdbd8 178 }
179
b22b0e61 180 unset($SESSION->fromdiscussion);
501cdbd8 181
182 } else if (isset($edit)) { // User is editing their own post
183
11b0c469 184 if (! $post = forum_get_post_full($edit)) {
501cdbd8 185 error("Post ID was incorrect");
186 }
ebc3bd2b 187 if ($post->userid <> $USER->id) {
501cdbd8 188 error("You can't edit other people's posts!");
189 }
190 if ((time() - $post->created) > $CFG->maxeditingtime) {
cf38360f 191 error( get_string("maxtimehaspassed", "forum", format_time($CFG->maxeditingtime)) );
501cdbd8 192 }
193 if ($post->parent) {
11b0c469 194 if (! $parent = forum_get_post_full($post->parent)) {
501cdbd8 195 error("Parent post ID was incorrect ($post->parent)");
196 }
197 }
198 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
199 error("This post is not part of a discussion! ($reply)");
200 }
201 if (! $forum = get_record("forum", "id", $discussion->forum)) {
202 error("The forum number was incorrect ($discussion->forum)");
203 }
204 if (! $course = get_record("course", "id", $discussion->course)) {
205 error("The course number was incorrect ($discussion->course)");
206 }
207
208 // Load up the $post variable.
209
210 $post->edit = $edit;
211
212 $post->course = $course->id;
213 $post->forum = $forum->id;
214
b22b0e61 215 unset($SESSION->fromdiscussion);
501cdbd8 216
217
218 } else if (isset($delete)) { // User is deleting a post
219
11b0c469 220 if (! $post = forum_get_post_full($delete)) {
501cdbd8 221 error("Post ID was incorrect");
222 }
501cdbd8 223 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
224 error("This post is not part of a discussion!");
225 }
64eacd6f 226 if (! $forum = get_record("forum", "id", $discussion->forum)) {
227 error("The forum number was incorrect ($discussion->forum)");
228 }
ebc3bd2b 229 if (($post->userid <> $USER->id) and !isteacher($forum->course)) {
64eacd6f 230 error("You can't delete other people's posts!");
231 }
501cdbd8 232
233 if (isset($confirm)) { // User has confirmed the delete
234
235 if ($post->totalscore) {
cf38360f 236 notice(get_string("couldnotdeleteratings", "forum"),
11b0c469 237 forum_go_back_to("discuss.php?d=$post->discussion"));
501cdbd8 238
239 } else if (record_exists("forum_posts", "parent", $delete)) {
cf38360f 240 error(get_string("couldnotdeletereplies", "forum"),
11b0c469 241 forum_go_back_to("discuss.php?id=$post->discussion"));
501cdbd8 242
243 } else {
244 if (! $post->parent) { // post is a discussion topic as well, so delete discussion
64eacd6f 245 if ($forum->type == "single") {
246 notice("Sorry, but you are not allowed to delete that discussion!",
247 forum_go_back_to("discuss.php?d=$post->discussion"));
248 }
501cdbd8 249 forum_delete_discussion($discussion);
250
251 add_to_log($discussion->course, "forum", "delete discussion", "view.php?id=$discussion->forum", "$post->id");
252 redirect("view.php?f=$discussion->forum",
cf38360f 253 get_string("deleteddiscussion", "forum"), 1);
501cdbd8 254
7f6689e4 255 } else if (forum_delete_post($post)) {
501cdbd8 256
257 add_to_log($discussion->course, "forum", "delete post", "discuss.php?d=$post->discussion", "$post->id");
11b0c469 258 redirect(forum_go_back_to("discuss.php?d=$post->discussion"),
cf38360f 259 get_string("deletedpost", "forum"), 1);
501cdbd8 260 } else {
261 error("An error occurred while deleting record $post->id");
262 }
263 }
264
265
266 } else { // User just asked to delete something
267
11b0c469 268 forum_set_return();
501cdbd8 269
270 print_header();
cf38360f 271 notice_yesno(get_string("deletesure", "forum"),
501cdbd8 272 "post.php?delete=$delete&confirm=$delete",
607809b3 273 $_SERVER["HTTP_REFERER"]);
501cdbd8 274
275 echo "<CENTER><HR>";
8aed46c7 276 forum_print_post($post, $forum->course, $ownpost=false, $reply=false, $link=false);
501cdbd8 277 }
278
279 die;
280
281
282 } else {
283 error("No operation specified");
284
285 }
286
287
288 // To get here they need to edit a post, and the $post
289 // variable will be loaded with all the particulars,
290 // so bring up the form.
291
292 // $course, $forum are defined. $discussion is for edit and reply only.
293
294 require_login($course->id);
295
dfc9ba9b 296
501cdbd8 297 if ($post->discussion) {
9fa49e22 298 if (! $toppost = get_record("forum_posts", "discussion", $post->discussion, "parent", 0)) {
501cdbd8 299 error("Could not find top parent of post $post->id");
300 }
301 } else {
cf38360f 302 $toppost->subject = get_string("yournewtopic", "forum");
501cdbd8 303 }
304
3bbde520 305 if (empty($post->subject)) {
0ae5e5ea 306 $formstart = "theform.subject";
3bbde520 307 } else {
308 $formstart = "";
501cdbd8 309 }
310
311 if ($post->parent) {
cf38360f 312 $navtail = "<A HREF=\"discuss.php?d=$discussion->id\">$toppost->subject</A> -> ".get_string("editing", "forum");
501cdbd8 313 } else {
314 $navtail = "$toppost->subject";
315 }
316
9c9f7d77 317 if (empty($post->edit)) {
318 $post->edit = "";
319 }
320
cf38360f 321 $strforums = get_string("modulenameplural", "forum");
322
73bb0835 323
cf38360f 324 $navmiddle = "<A HREF=\"../forum/index.php?id=$course->id\">$strforums</A> -> <A HREF=\"view.php?f=$forum->id\">$forum->name</A>";
501cdbd8 325
dfc9ba9b 326 $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
327
9c9f7d77 328 if (empty($discussion->name)) {
329 $discussion->name = $forum->name;
330 }
331
501cdbd8 332 if ($course->category) {
333 print_header("$course->shortname: $discussion->name: $toppost->subject", "$course->fullname",
334 "<A HREF=../../course/view.php?id=$course->id>$course->shortname</A> ->
3bbde520 335 $navmiddle -> $navtail", $formstart, "", true, "", navmenu($course, $cm));
501cdbd8 336 } else {
337 print_header("$course->shortname: $discussion->name: $toppost->subject", "$course->fullname",
9c9f7d77 338 "$navmiddle -> $navtail", "$formstart", "", true, "", navmenu($course, $cm));
501cdbd8 339
340 }
341
3395f2d6 342 if (!empty($parent)) {
11b0c469 343 forum_print_post($parent, $course->id, $ownpost=false, $reply=false, $link=false);
f8029045 344 if (empty($post->edit)) {
345 forum_print_posts_threaded($parent->id, $course, 0, false, false);
346 }
2e82fd38 347 echo "<center>";
cf38360f 348 echo "<H2>".get_string("yourreply", "forum").":</H2>";
501cdbd8 349 } else {
2e82fd38 350 echo "<center>";
cf38360f 351 echo "<H2>".get_string("yournewtopic", "forum")."</H2>";
501cdbd8 352 }
3395f2d6 353 if (!empty($post->error)) {
354 notify($post->error);
355 }
4b00b4b3 356 echo "</center>";
501cdbd8 357
358 print_simple_box_start("center", "", "$THEME->cellheading");
359 require("post.html");
360 print_simple_box_end();
361
4b00b4b3 362 if ($usehtmleditor) {
363 use_html_editor();
364 }
365
501cdbd8 366 print_footer($course);
367
368
369?>