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