fixed loglive paging related bug #4116, patch submitted by samulik; merged from MOODL...
[moodle.git] / mod / forum / discuss.php
CommitLineData
41905731 1<?php // $Id$
501cdbd8 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
70e3da07 9 $d = required_param('d',PARAM_INT); // Discussion ID
10 $parent = optional_param('parent',0,PARAM_INT); // If set, then display this post and all children.
11 $mode = optional_param('mode',0,PARAM_INT); // If set, changes the layout of the thread
12 $move = optional_param('move',0,PARAM_INT); // If set, moves this discussion to another forum
13 $mark = optional_param('mark',0,PARAM_INT); // Used for tracking read posts if user initiated.
14 $postid = optional_param('postid',0,PARAM_INT); // Used for tracking read posts if user initiated.
501cdbd8 15
16 if (! $discussion = get_record("forum_discussions", "id", $d)) {
94361e02 17 error("Discussion ID was incorrect or no longer exists");
501cdbd8 18 }
19
20 if (! $course = get_record("course", "id", $discussion->course)) {
21 error("Course ID is incorrect - discussion is faulty");
22 }
23
68258534 24 if (! $forum = get_record("forum", "id", $discussion->forum)) {
25 notify("Bad forum ID stored in this discussion");
26 }
27
28 if ($forum->type == "teacher") {
29 require_login($course->id);
30
31 if (!isteacher($course->id)) {
32 error("You must be a $course->teacher to view this forum");
33 }
34
35 } else {
36 if (! $cm = get_coursemodule_from_instance("forum", $discussion->forum, $course->id)) {
37 error("Course Module ID was incorrect");
38 }
39 require_course_login($course, false, $cm);
68ddf8bc 40 }
41
1fc49f00 42
8f0cd6ef 43 if (!empty($move)) {
1fc49f00 44 if (!isteacher($course->id)) {
45 error("Only teachers can do that!");
46 }
47 if ($forum = get_record("forum", "id", $move)) {
cc2b7ea5 48 if (!forum_move_attachments($discussion, $move)) {
49 notify("Errors occurred while moving attachment directories - check your file permissions");
50 }
1fc49f00 51 set_field("forum_discussions", "forum", $forum->id, "id", $discussion->id);
52 $discussion->forum = $forum->id;
69d79bc3 53 if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
54 add_to_log($course->id, "forum", "move discussion", "discuss.php?d=$discussion->id", "$discussion->id",
55 $cm->id);
56 } else {
57 add_to_log($course->id, "forum", "move discussion", "discuss.php?d=$discussion->id", "$discussion->id");
58 }
8de14dc7 59 $discussionmoved = true;
1fc49f00 60 } else {
61 error("You can't move to that forum - it doesn't exist!");
62 }
63 }
64
1fc49f00 65 $logparameters = "d=$discussion->id";
66 if ($parent) {
839f2456 67 $logparameters .= "&amp;parent=$parent";
501cdbd8 68 }
69d79bc3 69
70 if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
71 add_to_log($course->id, "forum", "view discussion", "discuss.php?$logparameters", "$discussion->id", $cm->id);
72 } else {
73 add_to_log($course->id, "forum", "view discussion", "discuss.php?$logparameters", "$discussion->id");
74 }
501cdbd8 75
76 unset($SESSION->fromdiscussion);
77
279826e2 78 if ($mode) {
acb50c1b 79 if (isguest()) {
80 $USER->preference['forum_displaymode'] = $mode; // don't save it in database
81 } else {
82 set_user_preference('forum_displaymode', $mode);
83 }
279826e2 84 }
501cdbd8 85
acb50c1b 86 $displaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
501cdbd8 87
e92ea3d8 88 if ($parent) {
279826e2 89 if (abs($displaymode) == 1) { // If flat AND parent, then force nested display this time
e92ea3d8 90 $displaymode = 3;
91 }
92 } else {
501cdbd8 93 $parent = $discussion->firstpost;
c78ac798 94 $navtail = format_string($discussion->name);
501cdbd8 95 }
96
11b0c469 97 if (! $post = forum_get_post_full($parent)) {
501cdbd8 98 error("Discussion no longer exists", "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
99 }
100
eaf50aef 101 if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum) &&
102 $CFG->forum_usermarksread) {
f37da850 103 if ($mark == 'read') {
104 forum_tp_add_read_record($USER->id, $postid, $discussion->id, $forum->id);
105 } else if ($mark == 'unread') {
106 forum_tp_delete_read_records($USER->id, $postid);
107 }
108 }
109
61e96406 110 if (empty($navtail)) {
c78ac798 111 $navtail = "<a href=\"discuss.php?d=$discussion->id\">".format_string($discussion->name,true)."</a> -> ".format_string($post->subject);
501cdbd8 112 }
113
3849dae8 114 $navmiddle = "<a href=\"../forum/index.php?id=$course->id\">".get_string("forums", "forum")."</a> -> <a href=\"../forum/view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
501cdbd8 115
6f1cc8d6 116 $searchform = forum_search_form($course);
97485d07 117
501cdbd8 118 if ($course->category) {
c78ac798 119 print_header("$course->shortname: ".format_string($discussion->name), "$course->fullname",
72b4e283 120 "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> ->
8f0cd6ef 121 $navmiddle -> $navtail", "", "", true, $searchform, navmenu($course, $cm));
501cdbd8 122 } else {
c78ac798 123 print_header("$course->shortname: ".format_string($discussion->name), "$course->fullname",
8f0cd6ef 124 "$navmiddle -> $navtail", "", "", true, $searchform, navmenu($course, $cm));
501cdbd8 125 }
126
c6d691dc 127
9197e147 128/// Check to see if groups are being used in this forum
129/// If so, make sure the current person is allowed to see this discussion
c6d691dc 130/// Also, if we know they should be able to reply, then explicitly set $canreply
131
132 $canreply = NULL; /// No override one way or the other
9197e147 133
52b201ee 134 if ($forum->type == "teacher") {
135 $groupmode = NOGROUPS;
136 } else {
137 $groupmode = groupmode($course, $cm);
138 }
9197e147 139
c6d691dc 140 if ($groupmode and !isteacheredit($course->id)) { // Groups must be kept separate
fa22fd5f 141 //change this to ismember
142 $mygroupid = mygroupid($course->id);//only useful if 0, otherwise it's an array now
c6d691dc 143 if ($groupmode == SEPARATEGROUPS) {
144 require_login();
145
fa22fd5f 146 if ((empty($mygroupid) and $discussion->groupid == -1) || (ismember($discussion->groupid) || $mygroupid == $discussion->groupid)) {
c6d691dc 147 $canreply = true;
2862b309 148 } elseif ($discussion->groupid == -1) {
149 $canreply = false;
c6d691dc 150 } else {
151 print_heading("Sorry, you can't see this discussion because you are not in this group");
cd8d4471 152 print_footer($course);
c6d691dc 153 die;
154 }
155
156 } else if ($groupmode == VISIBLEGROUPS) {
fa22fd5f 157 $canreply = ((empty($mygroupid) and $discussion->groupid == -1) || (ismember($discussion->groupid) || $mygroupid == $discussion->groupid));
9197e147 158 }
159 }
160
161
c6d691dc 162/// Print the controls across the top
163
d5bbc556 164 echo '<table width="100%"><tr><td width="33%">';
c6d691dc 165
166 if ($groupmode == VISIBLEGROUPS or ($groupmode and isteacheredit($course->id))) {
d5bbc556 167 if ($groups = get_records_menu('groups', 'courseid', $course->id, 'name ASC', 'id,name')) {
839f2456 168 print_group_menu($groups, $groupmode, $discussion->groupid, "view.php?id=$cm->id&amp;group=");
c6d691dc 169 }
170 }
171
172 echo "</td><td width=\"33%\">";
ec9c0d44 173 forum_print_mode_form($discussion->id, $displaymode);
c6d691dc 174
02ebf404 175 echo "</td><td width=\"33%\">";
768f90f6 176 if (isteacher($course->id) && $forum->type != "teacher") { // Popup menu to move discussions to other forums
cccb016a 177 if ($forums = get_all_instances_in_course("forum", $course)) {
fcc69042 178 if ($course->format == 'weeks') {
179 $strsection = get_string("week");
180 } else {
181 $strsection = get_string("topic");
182 }
183 $section = -1;
1fc49f00 184 foreach ($forums as $courseforum) {
fcc69042 185 if (!empty($courseforum->section) and $section != $courseforum->section) {
186 $forummenu[] = "-------------- $strsection $courseforum->section --------------";
187 }
188 $section = $courseforum->section;
1fc49f00 189 if ($courseforum->id != $forum->id) {
839f2456 190 $url = "discuss.php?d=$discussion->id&amp;move=$courseforum->id";
3849dae8 191 $forummenu[$url] = format_string($courseforum->name,true);
1fc49f00 192 }
193 }
194 if (!empty($forummenu)) {
195 echo "<div align=\"right\">";
8f0cd6ef 196 echo popup_form("$CFG->wwwroot/mod/forum/", $forummenu, "forummenu", "",
1fc49f00 197 get_string("movethisdiscussionto", "forum"), "", "", true);
198 echo "</div>";
199 }
200 }
201 }
02ebf404 202 echo "</td></tr></table>";
1fc49f00 203
8de14dc7 204 if (isset($discussionmoved)) {
3849dae8 205 notify(get_string("discussionmoved", "forum", format_string($forum->name,true)));
8de14dc7 206 }
207
c6d691dc 208
209/// Print the actual discussion
210
211 forum_print_discussion($course, $forum, $discussion, $post, $displaymode, $canreply);
212
501cdbd8 213 print_footer($course);
214
215?>