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