3 // Edit and save a new post to a discussion
6 require("../../config.php");
10 error(get_string("noguestpost", "forum"), $HTTP_REFERER);
13 if (match_referer() && isset($HTTP_POST_VARS)) { // form submitted
14 $post = (object)$HTTP_POST_VARS;
16 $post->subject = strip_tags($post->subject); // Strip all tags
17 $post->message = clean_text($post->message, $post->format); // Clean up any bad tags
19 $post->attachment = $HTTP_POST_FILES["attachment"];
21 if (!$post->subject and !$post->message) {
22 error(get_string("emptymessage", "forum"));
27 if ($post->edit) { // Updating a post
28 $post->id = $post->edit;
29 if (forum_update_post($post)) {
30 add_to_log($post->course, "forum", "update post", "discuss.php?d=$post->discussion&parent=$post->id", "$post->id");
31 redirect(forum_go_back_to("discuss.php?d=$post->discussion"), get_string("postupdated", "forum"), 1);
33 error(get_string("couldnotupdate", "forum"));
36 } else if ($post->discussion) { // Adding a new post to an existing discussion
37 if ($post->id = forum_add_new_post($post)) {
38 if ( ! forum_is_subscribed($USER->id, $post->forum) ) {
39 forum_subscribe($USER->id, $post->forum);
42 add_to_log($post->course, "forum", "add post", "discuss.php?d=$post->discussion&parent=$post->id", "$post->id");
43 redirect(forum_go_back_to("discuss.php?d=$post->discussion"),
44 get_string("postadded", "forum", format_time($CFG->maxeditingtime)), 3);
46 error(get_string("couldnotadd", "forum"));
48 } else { // Adding a new discussion
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);
56 add_to_log($post->course, "forum", "add discussion", "discuss.php?d=$discussion->id", "$discussion->id");
57 redirect(forum_go_back_to("view.php?f=$post->forum"),
58 get_string("postadded", "forum", format_time($CFG->maxeditingtime)), 3);
60 error(get_string("couldnotadd", "forum"));
66 if ($usehtmleditor = can_use_richtext_editor()) {
67 $defaultformat = FORMAT_HTML;
68 $onsubmit = "onsubmit=\"copyrichtext(theform.message);\"";
70 $defaultformat = FORMAT_MOODLE;
74 if (isset($forum)) { // User is starting a new discussion in a forum
76 $SESSION->fromurl = $HTTP_REFERER;
77 save_session("SESSION");
79 if (! $forum = get_record("forum", "id", $forum)) {
80 error("The forum number was incorrect ($forum)");
82 if (! $course = get_record("course", "id", $forum->course)) {
83 error("The course number was incorrect ($forum)");
86 if (! forum_user_can_post_discussion($forum)) {
87 error("Sorry, but you can not post a new discussion in this forum.");
90 // Load up the $post variable.
92 $post->course = $course->id;
93 $post->forum = $forum->id;
94 $post->discussion = 0; // ie discussion # not defined yet
97 $post->user = $USER->id;
99 $post->format = $defaultformat;
103 } else if (isset($reply)) { // User is writing a new reply
105 if (! $parent = forum_get_post_full($reply)) {
106 error("Parent post ID was incorrect ($reply)");
108 if (! $discussion = get_record("forum_discussions", "id", $parent->discussion)) {
109 error("This post is not part of a discussion! ($reply)");
111 if (! $forum = get_record("forum", "id", $discussion->forum)) {
112 error("The forum number was incorrect ($discussion->forum)");
114 if (! $course = get_record("course", "id", $discussion->course)) {
115 error("The course number was incorrect ($discussion->course)");
118 if (! forum_user_can_post($forum)) {
119 error("Sorry, but you can not post in this forum.");
121 // Load up the $post variable.
123 $post->course = $course->id;
124 $post->forum = $forum->id;
125 $post->discussion = $parent->discussion;
126 $post->parent = $parent->id;
127 $post->subject = $parent->subject;
128 $post->user = $USER->id;
130 $post->format = $defaultformat;
132 $strre = get_string("re", "forum");
133 if (!(substr($post->subject, 0, 3) == $strre)) {
134 $post->subject = "$strre $post->subject";
139 } else if (isset($edit)) { // User is editing their own post
141 if (! $post = forum_get_post_full($edit)) {
142 error("Post ID was incorrect");
144 if ($post->user <> $USER->id) {
145 error("You can't edit other people's posts!");
147 if ((time() - $post->created) > $CFG->maxeditingtime) {
148 error( get_string("maxtimehaspassed", "forum", format_time($CFG->maxeditingtime)) );
151 if (! $parent = forum_get_post_full($post->parent)) {
152 error("Parent post ID was incorrect ($post->parent)");
155 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
156 error("This post is not part of a discussion! ($reply)");
158 if (! $forum = get_record("forum", "id", $discussion->forum)) {
159 error("The forum number was incorrect ($discussion->forum)");
161 if (! $course = get_record("course", "id", $discussion->course)) {
162 error("The course number was incorrect ($discussion->course)");
165 // Load up the $post variable.
169 $post->course = $course->id;
170 $post->forum = $forum->id;
175 } else if (isset($delete)) { // User is deleting a post
177 if (! $post = forum_get_post_full($delete)) {
178 error("Post ID was incorrect");
180 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
181 error("This post is not part of a discussion!");
183 if (! $forum = get_record("forum", "id", $discussion->forum)) {
184 error("The forum number was incorrect ($discussion->forum)");
186 if (($post->user <> $USER->id) and !isteacher($forum->course)) {
187 error("You can't delete other people's posts!");
190 if (isset($confirm)) { // User has confirmed the delete
192 if ($post->totalscore) {
193 notice(get_string("couldnotdeleteratings", "forum"),
194 forum_go_back_to("discuss.php?d=$post->discussion"));
196 } else if (record_exists("forum_posts", "parent", $delete)) {
197 error(get_string("couldnotdeletereplies", "forum"),
198 forum_go_back_to("discuss.php?id=$post->discussion"));
201 if (! $post->parent) { // post is a discussion topic as well, so delete discussion
202 if ($forum->type == "single") {
203 notice("Sorry, but you are not allowed to delete that discussion!",
204 forum_go_back_to("discuss.php?d=$post->discussion"));
206 forum_delete_discussion($discussion);
208 add_to_log($discussion->course, "forum", "delete discussion", "view.php?id=$discussion->forum", "$post->id");
209 redirect("view.php?f=$discussion->forum",
210 get_string("deleteddiscussion", "forum"), 1);
212 } else if (forum_delete_post($post)) {
214 add_to_log($discussion->course, "forum", "delete post", "discuss.php?d=$post->discussion", "$post->id");
215 redirect(forum_go_back_to("discuss.php?d=$post->discussion"),
216 get_string("deletedpost", "forum"), 1);
218 error("An error occurred while deleting record $post->id");
223 } else { // User just asked to delete something
228 notice_yesno(get_string("deletesure", "forum"),
229 "post.php?delete=$delete&confirm=$delete",
233 forum_print_post($post, $forum->course, $ownpost=false, $reply=false, $link=false);
240 error("No operation specified");
245 // To get here they need to edit a post, and the $post
246 // variable will be loaded with all the particulars,
247 // so bring up the form.
249 // $course, $forum are defined. $discussion is for edit and reply only.
251 require_login($course->id);
253 if ($post->discussion) {
254 if (! $toppost = get_record_sql("SELECT * FROM forum_posts
255 WHERE discussion='$post->discussion'
257 error("Could not find top parent of post $post->id");
260 $toppost->subject = get_string("yournewtopic", "forum");
263 if ($post->subject) {
264 $formstart = "form.message";
266 $formstart = "form.subject";
270 $navtail = "<A HREF=\"discuss.php?d=$discussion->id\">$toppost->subject</A> -> ".get_string("editing", "forum");
272 $navtail = "$toppost->subject";
275 $strforums = get_string("modulenameplural", "forum");
278 $navmiddle = "<A HREF=\"../forum/index.php?id=$course->id\">$strforums</A> -> <A HREF=\"view.php?f=$forum->id\">$forum->name</A>";
280 if ($course->category) {
281 print_header("$course->shortname: $discussion->name: $toppost->subject", "$course->fullname",
282 "<A HREF=../../course/view.php?id=$course->id>$course->shortname</A> ->
283 $navmiddle -> $navtail", "$forumstart");
285 print_header("$course->shortname: $discussion->name: $toppost->subject", "$course->fullname",
286 "$navmiddle -> $navtail", "");
291 if (isset($parent)) {
292 forum_print_post($parent, $course->id, $ownpost=false, $reply=false, $link=false);
293 echo "<H2>".get_string("yourreply", "forum").":</H2>";
295 echo "<H2>".get_string("yournewtopic", "forum")."</H2>";
299 print_simple_box_start("center", "", "$THEME->cellheading");
300 require("post.html");
301 print_simple_box_end();
303 print_footer($course);