Added navmenu in more places
[moodle.git] / mod / forum / post.php
1 <?PHP // $Id$
3 //  Edit and save a new post to a discussion
6     require("../../config.php");
7     require("lib.php");
9     if (isguest()) {
10         error(get_string("noguestpost", "forum"), $HTTP_REFERER);
11     }
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"));
23         }
25         require_login();
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);
32             } else {
33                 error(get_string("couldnotupdate", "forum")); 
34             }
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);
40                 }
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);
45             } else {
46                 error(get_string("couldnotadd", "forum")); 
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");
57                 redirect(forum_go_back_to("view.php?f=$post->forum"), 
58                          get_string("postadded", "forum", format_time($CFG->maxeditingtime)), 3);
59             } else {
60                 error(get_string("couldnotadd", "forum")); 
61             }
62         }
63         die;
64     }
66     if ($usehtmleditor = can_use_richtext_editor()) {
67         $defaultformat = FORMAT_HTML;
68         $onsubmit = "onsubmit=\"copyrichtext(theform.message);\"";
69     } else {
70         $defaultformat = FORMAT_MOODLE;
71     }
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)");
81         }
82         if (! $course = get_record("course", "id", $forum->course)) {
83             error("The course number was incorrect ($forum)");
84         }
86         if (! forum_user_can_post_discussion($forum)) {
87             error("Sorry, but you can not post a new discussion in this forum.");
88         }
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
95         $post->parent = 0;
96         $post->subject = "";
97         $post->user = $USER->id;
98         $post->message = "";
99         $post->format = $defaultformat;
101         forum_set_return();
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)");
107         }
108         if (! $discussion = get_record("forum_discussions", "id", $parent->discussion)) {
109             error("This post is not part of a discussion! ($reply)");
110         }
111         if (! $forum = get_record("forum", "id", $discussion->forum)) {
112             error("The forum number was incorrect ($discussion->forum)");
113         }
114         if (! $course = get_record("course", "id", $discussion->course)) {
115             error("The course number was incorrect ($discussion->course)");
116         }
118         if (! forum_user_can_post($forum)) {
119             error("Sorry, but you can not post in this forum.");
120         }
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;
129         $post->message = "";
130         $post->format = $defaultformat;
132         $strre = get_string("re", "forum");
133         if (!(substr($post->subject, 0, 3) == $strre)) {
134             $post->subject = "$strre $post->subject";
135         }
137         forum_set_return();
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");
143         }
144         if ($post->user <> $USER->id) {
145             error("You can't edit other people's posts!");
146         }
147         if ((time() - $post->created) > $CFG->maxeditingtime) {
148             error( get_string("maxtimehaspassed", "forum", format_time($CFG->maxeditingtime)) );
149         }
150         if ($post->parent) {
151             if (! $parent = forum_get_post_full($post->parent)) {
152                 error("Parent post ID was incorrect ($post->parent)");
153             }
154         }
155         if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
156             error("This post is not part of a discussion! ($reply)");
157         }
158         if (! $forum = get_record("forum", "id", $discussion->forum)) {
159             error("The forum number was incorrect ($discussion->forum)");
160         }
161         if (! $course = get_record("course", "id", $discussion->course)) {
162             error("The course number was incorrect ($discussion->course)");
163         }
165         // Load up the $post variable.
167         $post->edit = $edit;
169         $post->course  = $course->id;
170         $post->forum  = $forum->id;
172         forum_set_return();
175     } else if (isset($delete)) {  // User is deleting a post
177         if (! $post = forum_get_post_full($delete)) {
178             error("Post ID was incorrect");
179         }
180         if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
181             error("This post is not part of a discussion!");
182         }
183         if (! $forum = get_record("forum", "id", $discussion->forum)) {
184             error("The forum number was incorrect ($discussion->forum)");
185         }
186         if (($post->user <> $USER->id) and !isteacher($forum->course)) {
187             error("You can't delete other people's posts!");
188         }
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"));
200             } else {
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"));
205                     }
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);
217                 } else {
218                     error("An error occurred while deleting record $post->id");
219                 }
220             }
223         } else { // User just asked to delete something
225             forum_set_return();
227             print_header();
228             notice_yesno(get_string("deletesure", "forum"), 
229                          "post.php?delete=$delete&confirm=$delete",
230                          $HTTP_REFERER);
231                          
232             echo "<CENTER><HR>";
233             forum_print_post($post, $forum->course, $ownpost=false, $reply=false, $link=false);
234         }
236         die;
239     } else {
240         error("No operation specified");
242     }
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);
254     if ($post->discussion) {
255         if (! $toppost = get_record_sql("SELECT * FROM forum_posts 
256                                          WHERE discussion='$post->discussion' 
257                                          AND parent = 0")) {
258             error("Could not find top parent of post $post->id");
259         }
260     } else {
261         $toppost->subject = get_string("yournewtopic", "forum");
262     }
264     if ($post->subject) {
265         $formstart = "form.message";
266     } else {
267         $formstart = "form.subject";
268     }
270     if ($post->parent) {
271         $navtail = "<A HREF=\"discuss.php?d=$discussion->id\">$toppost->subject</A> -> ".get_string("editing", "forum");
272     } else {
273         $navtail = "$toppost->subject";
274     }
276     $strforums = get_string("modulenameplural", "forum");
279     $navmiddle = "<A HREF=\"../forum/index.php?id=$course->id\">$strforums</A> -> <A HREF=\"view.php?f=$forum->id\">$forum->name</A>";
281     $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
283     if ($course->category) {
284         print_header("$course->shortname: $discussion->name: $toppost->subject", "$course->fullname",
285                  "<A HREF=../../course/view.php?id=$course->id>$course->shortname</A> ->
286                   $navmiddle -> $navtail", "$forumstart", "", true, "", navmenu($course, $cm));
287     } else {
288         print_header("$course->shortname: $discussion->name: $toppost->subject", "$course->fullname",
289                  "$navmiddle -> $navtail", "");
291     }
293     echo "<CENTER>";
294     if (isset($parent)) {
295         forum_print_post($parent, $course->id, $ownpost=false, $reply=false, $link=false);
296         echo "<H2>".get_string("yourreply", "forum").":</H2>";
297     } else {
298         echo "<H2>".get_string("yournewtopic", "forum")."</H2>";
299     }
300     echo "</CENTER>";
302     print_simple_box_start("center", "", "$THEME->cellheading");
303     require("post.html");
304     print_simple_box_end();
306     print_footer($course);
309 ?>