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