Updates and changes
[moodle.git] / mod / forum / post.php
CommitLineData
501cdbd8 1<?PHP // $Id$
2
3// Edit and save a new post to a discussion
4
5
6 require("../../config.php");
7 require("lib.php");
8
9 if (isguest()) {
10 error("Guests are not allowed to post.", $HTTP_REFERER);
11 }
12
13 if (match_referer() && isset($HTTP_POST_VARS)) { // form submitted
14 $post = (object)$HTTP_POST_VARS;
15
16 $post->subject = strip_tags($post->subject); // Strip all tags
17 $post->message = cleantext($post->message); // Clean up any bad tags
18
19 require_login();
20
21 if ($post->edit) { // Updating a post
22 $post->id = $post->edit;
11b0c469 23 if (forum_update_post($post) ) {
501cdbd8 24 add_to_log($post->course, "forum", "update post", "discuss.php?d=$post->discussion&parent=$post->id", "$post->id");
11b0c469 25 redirect(forum_go_back_to("discuss.php?d=$post->discussion"), "Your post was updated", 1);
501cdbd8 26 } else {
27 error("Could not update your post due to an unknown error");
28 }
29 } else if ($post->discussion) { // Adding a new post to an existing discussion
11b0c469 30 if ($post->id = forum_add_new_post($post)) {
501cdbd8 31 if ( ! forum_is_subscribed($USER->id, $post->forum) ) {
32 forum_subscribe($USER->id, $post->forum);
33 }
34
35 add_to_log($post->course, "forum", "add post", "discuss.php?d=$post->discussion&parent=$post->id", "$post->id");
11b0c469 36 redirect(forum_go_back_to("discuss.php?d=$post->discussion"),
501cdbd8 37 "Your post was successfully added.<P>You have ".format_time($CFG->maxeditingtime)." to edit it if you want to make any changes.", 3);
38 } else {
39 error("Could not add the post due to an unknown error");
40 }
41 } else { // Adding a new discussion
42 $discussion = $post;
43 $discussion->name = $post->subject;
44 $discussion->intro = $post->message;
45 if ($discussion->id = forum_add_discussion($discussion)) {
46 if ( ! forum_is_subscribed($USER->id, $post->forum) ) {
47 forum_subscribe($USER->id, $post->forum);
48 }
49 add_to_log($post->course, "forum", "add discussion", "discuss.php?d=$discussion->id", "$discussion->id");
11b0c469 50 redirect(forum_go_back_to("view.php?f=$post->forum"),
501cdbd8 51 "Your post was successfully added.<P>You have ".format_time($CFG->maxeditingtime)." to edit it if you want to make any changes.", 5);
52 } else {
53 error("Could not insert the new discussion.");
54 }
55 }
56 die;
57 }
58
59
60
61 if (isset($forum)) { // User is starting a new discussion in a forum
62
63 $SESSION->fromurl = $HTTP_REFERER;
8223d271 64 save_session("SESSION");
501cdbd8 65
66 if (! $forum = get_record("forum", "id", $forum)) {
67 error("The forum number was incorrect ($forum)");
68 }
69 if (! $course = get_record("course", "id", $forum->course)) {
70 error("The course number was incorrect ($forum)");
71 }
72
11b0c469 73 if (! forum_user_can_post_discussion($forum)) {
501cdbd8 74 error("Sorry, but you can not post a new discussion in this forum.");
75 }
76
77 // Load up the $post variable.
78
79 $post->course = $course->id;
80 $post->forum = $forum->id;
81 $post->discussion = 0; // ie discussion # not defined yet
82 $post->parent = 0;
83 $post->subject = "";
84 $post->user = $USER->id;
85 $post->message = "";
86
11b0c469 87 forum_set_return();
88
501cdbd8 89 } else if (isset($reply)) { // User is writing a new reply
90
11b0c469 91 if (! $parent = forum_get_post_full($reply)) {
501cdbd8 92 error("Parent post ID was incorrect ($reply)");
93 }
94 if (! $discussion = get_record("forum_discussions", "id", $parent->discussion)) {
95 error("This post is not part of a discussion! ($reply)");
96 }
97 if (! $forum = get_record("forum", "id", $discussion->forum)) {
98 error("The forum number was incorrect ($discussion->forum)");
99 }
100 if (! $course = get_record("course", "id", $discussion->course)) {
101 error("The course number was incorrect ($discussion->course)");
102 }
103 // Load up the $post variable.
104
105 $post->course = $course->id;
106 $post->forum = $forum->id;
107 $post->discussion = $parent->discussion;
108 $post->parent = $parent->id;
109 $post->subject = $parent->subject;
110 $post->user = $USER->id;
111 $post->message = "";
112
113 if (!(substr($post->subject, 0, 3) == "Re:")) {
114 $post->subject = "Re: ".$post->subject;
115 }
116
11b0c469 117 forum_set_return();
501cdbd8 118
119 } else if (isset($edit)) { // User is editing their own post
120
11b0c469 121 if (! $post = forum_get_post_full($edit)) {
501cdbd8 122 error("Post ID was incorrect");
123 }
124 if ($post->user <> $USER->id) {
125 error("You can't edit other people's posts!");
126 }
127 if ((time() - $post->created) > $CFG->maxeditingtime) {
128 error("Sorry, but the maximum time for editing this post (".format_time($CFG->maxeditingtime).") has passed!");
129 }
130 if ($post->parent) {
11b0c469 131 if (! $parent = forum_get_post_full($post->parent)) {
501cdbd8 132 error("Parent post ID was incorrect ($post->parent)");
133 }
134 }
135 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
136 error("This post is not part of a discussion! ($reply)");
137 }
138 if (! $forum = get_record("forum", "id", $discussion->forum)) {
139 error("The forum number was incorrect ($discussion->forum)");
140 }
141 if (! $course = get_record("course", "id", $discussion->course)) {
142 error("The course number was incorrect ($discussion->course)");
143 }
144
145 // Load up the $post variable.
146
147 $post->edit = $edit;
148
149 $post->course = $course->id;
150 $post->forum = $forum->id;
151
11b0c469 152 forum_set_return();
501cdbd8 153
154
155 } else if (isset($delete)) { // User is deleting a post
156
11b0c469 157 if (! $post = forum_get_post_full($delete)) {
501cdbd8 158 error("Post ID was incorrect");
159 }
501cdbd8 160 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
161 error("This post is not part of a discussion!");
162 }
64eacd6f 163 if (! $forum = get_record("forum", "id", $discussion->forum)) {
164 error("The forum number was incorrect ($discussion->forum)");
165 }
166 if (($post->user <> $USER->id) and !isteacher($forum->course)) {
167 error("You can't delete other people's posts!");
168 }
501cdbd8 169
170 if (isset($confirm)) { // User has confirmed the delete
171
172 if ($post->totalscore) {
173 notice("Sorry, that cannot be deleted as people have already rated it",
11b0c469 174 forum_go_back_to("discuss.php?d=$post->discussion"));
501cdbd8 175
176 } else if (record_exists("forum_posts", "parent", $delete)) {
177 error("Sorry, that cannot be deleted as people have
178 already responded to it",
11b0c469 179 forum_go_back_to("discuss.php?id=$post->discussion"));
501cdbd8 180
181 } else {
182 if (! $post->parent) { // post is a discussion topic as well, so delete discussion
64eacd6f 183 if ($forum->type == "single") {
184 notice("Sorry, but you are not allowed to delete that discussion!",
185 forum_go_back_to("discuss.php?d=$post->discussion"));
186 }
501cdbd8 187 forum_delete_discussion($discussion);
188
189 add_to_log($discussion->course, "forum", "delete discussion", "view.php?id=$discussion->forum", "$post->id");
190 redirect("view.php?f=$discussion->forum",
64eacd6f 191 "The discussion topic has been deleted", 1);
501cdbd8 192
64eacd6f 193 } else if (forum_delete_post($post->id)) {
501cdbd8 194
195 add_to_log($discussion->course, "forum", "delete post", "discuss.php?d=$post->discussion", "$post->id");
11b0c469 196 redirect(forum_go_back_to("discuss.php?d=$post->discussion"),
64eacd6f 197 "The post has been deleted", 1);
501cdbd8 198 } else {
199 error("An error occurred while deleting record $post->id");
200 }
201 }
202
203
204 } else { // User just asked to delete something
205
11b0c469 206 forum_set_return();
501cdbd8 207
208 print_header();
209 notice_yesno("Are you sure you want to delete this post?",
210 "post.php?delete=$delete&confirm=$delete",
211 $HTTP_REFERER);
212
213 echo "<CENTER><HR>";
11b0c469 214 forum_print_post($post, 0, $ownpost=false, $reply=false, $link=false);
501cdbd8 215
216 }
217
218 die;
219
220
221 } else {
222 error("No operation specified");
223
224 }
225
226
227 // To get here they need to edit a post, and the $post
228 // variable will be loaded with all the particulars,
229 // so bring up the form.
230
231 // $course, $forum are defined. $discussion is for edit and reply only.
232
233 require_login($course->id);
234
235 if ($post->discussion) {
236 if (! $toppost = get_record_sql("SELECT * FROM forum_posts
237 WHERE discussion='$post->discussion'
238 AND parent = 0")) {
239 error("Could not find top parent of post $post->id");
240 }
241 } else {
242 $toppost->subject = "New discussion topic";
243 }
244
245 if ($post->subject) {
246 $formstart = "form.message";
247 } else {
248 $formstart = "form.subject";
249 }
250
251 if ($post->parent) {
252 $navtail = "<A HREF=\"discuss.php?d=$discussion->id\">$toppost->subject</A> -> Editing";
253 } else {
254 $navtail = "$toppost->subject";
255 }
256
257 $navmiddle = "<A HREF=\"../forum/index.php?id=$course->id\">Forums</A> -> <A HREF=\"view.php?f=$forum->id\">$forum->name</A>";
258
259 if ($course->category) {
260 print_header("$course->shortname: $discussion->name: $toppost->subject", "$course->fullname",
261 "<A HREF=../../course/view.php?id=$course->id>$course->shortname</A> ->
262 $navmiddle -> $navtail", "$forumstart");
263 } else {
264 print_header("$course->shortname: $discussion->name: $toppost->subject", "$course->fullname",
265 "$navmiddle -> $navtail", "");
266
267 }
268
269 echo "<CENTER>";
270 if (isset($parent)) {
11b0c469 271 forum_print_post($parent, $course->id, $ownpost=false, $reply=false, $link=false);
501cdbd8 272 echo "<H2>Your reply:</H2>";
273 } else {
274 echo "<H2>Your new discussion topic:</H2>";
275 }
276 echo "</CENTER>";
277
278 print_simple_box_start("center", "", "$THEME->cellheading");
279 require("post.html");
280 print_simple_box_end();
281
282 print_footer($course);
283
284
285?>