Some fixes
[moodle.git] / mod / forum / discuss.php
CommitLineData
501cdbd8 1<?PHP // $Id$
2
3// Displays a post, and all the posts below it.
4// If no post is given, displays all posts in a discussion
5
b0e3a925 6 require_once("../../config.php");
7 require_once("lib.php");
501cdbd8 8
9 require_variable($d); // Discussion ID
10 optional_variable($parent); // If set, then display this post and all children.
11 optional_variable($mode); // If set, changes the layout of the thread
1fc49f00 12 optional_variable($move); // If set, moves this discussion to another forum
501cdbd8 13
14 if (! $discussion = get_record("forum_discussions", "id", $d)) {
94361e02 15 error("Discussion ID was incorrect or no longer exists");
501cdbd8 16 }
17
18 if (! $course = get_record("course", "id", $discussion->course)) {
19 error("Course ID is incorrect - discussion is faulty");
20 }
21
1fc49f00 22 if ($course->category) {
23 require_login($course->id);
24 }
25
26 if (!empty($move)) {
27 if (!isteacher($course->id)) {
28 error("Only teachers can do that!");
29 }
30 if ($forum = get_record("forum", "id", $move)) {
cc2b7ea5 31 if (!forum_move_attachments($discussion, $move)) {
32 notify("Errors occurred while moving attachment directories - check your file permissions");
33 }
1fc49f00 34 set_field("forum_discussions", "forum", $forum->id, "id", $discussion->id);
35 $discussion->forum = $forum->id;
36 add_to_log($course->id, "forum", "move discussion", "discuss.php?d=$discussion->id", "$discussion->id");
8de14dc7 37 $discussionmoved = true;
1fc49f00 38 } else {
39 error("You can't move to that forum - it doesn't exist!");
40 }
41 }
42
43 if (empty($forum)) {
44 if (! $forum = get_record("forum", "id", $discussion->forum)) {
45 notify("Bad forum ID stored in this discussion");
46 }
501cdbd8 47 }
48
dfc9ba9b 49 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
b20f74c9 50 //notify("Bad coursemodule for this discussion"); // Only affects navmenu
dfc9ba9b 51 }
52
1fc49f00 53 $logparameters = "d=$discussion->id";
54 if ($parent) {
55 $logparameters .= "&parent=$parent";
501cdbd8 56 }
1fc49f00 57 add_to_log($course->id, "forum", "view discussion", "discuss.php?$logparameters", "$discussion->id");
501cdbd8 58
59 unset($SESSION->fromdiscussion);
60
61 forum_set_display_mode($mode);
62
e92ea3d8 63 $displaymode = $USER->mode;
501cdbd8 64
e92ea3d8 65 if ($parent) {
66 if (abs($USER->mode) == 1) { // If flat AND parent, then force nested display this time
67 $displaymode = 3;
68 }
69 } else {
501cdbd8 70 $parent = $discussion->firstpost;
71 $navtail = "$discussion->name";
72 }
73
11b0c469 74 if (! $post = forum_get_post_full($parent)) {
501cdbd8 75 error("Discussion no longer exists", "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
76 }
77
61e96406 78 if (empty($navtail)) {
501cdbd8 79 $navtail = "<A HREF=\"discuss.php?d=$discussion->id\">$discussion->name</A> -> $post->subject";
80 }
81
f781b794 82 $navmiddle = "<A HREF=\"../forum/index.php?id=$course->id\">".get_string("forums", "forum")."</A> -> <A HREF=\"../forum/view.php?f=$forum->id\">$forum->name</A>";
501cdbd8 83
9c9f7d77 84 $searchform = forum_print_search_form($course, "", true, "plain");
97485d07 85
501cdbd8 86 if ($course->category) {
87 print_header("$course->shortname: $discussion->name", "$course->fullname",
88 "<A HREF=../../course/view.php?id=$course->id>$course->shortname</A> ->
97485d07 89 $navmiddle -> $navtail", "", "", true, $searchform, navmenu($course, $cm));
501cdbd8 90 } else {
91 print_header("$course->shortname: $discussion->name", "$course->fullname",
97485d07 92 "$navmiddle -> $navtail", "", "", true, $searchform, navmenu($course, $cm));
501cdbd8 93 }
94
9197e147 95/// Check to see if groups are being used in this forum
96/// If so, make sure the current person is allowed to see this discussion
97
98 $groupmode = groupmode($course, $cm);
99
100 if ($groupmode == SEPARATEGROUPS and !isteacheredit($course->id)) { // Groups must be kept separate
101 require_login();
102 if (!$toppost = get_record("forum_posts", "id", $discussion->firstpost)) {
103 error("Could not find the top post of the discussion");
104 }
105 if (!$group = user_group($course->id, $toppost->userid)) { // Find the topic's group
106 error("Could not find the appropriate group of this discussion");
107 }
108 if (mygroupid($course->id) != $group->id) {
109 print_heading("Sorry, you can't see this discussion because you are not in this group");
110 print_footer();
111 die;
112 }
113 }
114
115
02ebf404 116 echo "<table width=\"100%\"><tr><td width=\"33%\">&nbsp;</td><td width=\"33%\">";
ec9c0d44 117 forum_print_mode_form($discussion->id, $displaymode);
02ebf404 118 echo "</td><td width=\"33%\">";
1fc49f00 119 if (isteacher($course->id)) { // Popup menu to allow discussions to be moved to other forums
cccb016a 120 if ($forums = get_all_instances_in_course("forum", $course)) {
1fc49f00 121 foreach ($forums as $courseforum) {
122 if ($courseforum->id != $forum->id) {
123 $url = "discuss.php?d=$discussion->id&move=$courseforum->id";
124 $forummenu[$url] = $courseforum->name;
125 }
126 }
127 if (!empty($forummenu)) {
128 echo "<div align=\"right\">";
129 echo popup_form("$CFG->wwwroot/mod/forum/", $forummenu, "forummenu", "",
130 get_string("movethisdiscussionto", "forum"), "", "", true);
131 echo "</div>";
132 }
133 }
134 }
02ebf404 135 echo "</td></tr></table>";
1fc49f00 136
8de14dc7 137 if (isset($discussionmoved)) {
138 notify(get_string("discussionmoved", "forum", $forum->name));
139 }
140
e92ea3d8 141 forum_print_discussion($course, $forum, $discussion, $post, $displaymode);
501cdbd8 142
143 print_footer($course);
144
145?>