3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Displays a post, and all the posts below it.
20 * If no post is given, displays all posts in a discussion
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once('../../config.php');
29 $d = required_param('d', PARAM_INT
); // Discussion ID
30 $parent = optional_param('parent', 0, PARAM_INT
); // If set, then display this post and all children.
31 $mode = optional_param('mode', 0, PARAM_INT
); // If set, changes the layout of the thread
32 $move = optional_param('move', 0, PARAM_INT
); // If set, moves this discussion to another forum
33 $mark = optional_param('mark', '', PARAM_ALPHA
); // Used for tracking read posts if user initiated.
34 $postid = optional_param('postid', 0, PARAM_INT
); // Used for tracking read posts if user initiated.
36 $url = new moodle_url('/mod/forum/discuss.php', array('d'=>$d));
38 $url->param('parent', $parent);
42 $discussion = $DB->get_record('forum_discussions', array('id' => $d), '*', MUST_EXIST
);
43 $course = $DB->get_record('course', array('id' => $discussion->course
), '*', MUST_EXIST
);
44 $forum = $DB->get_record('forum', array('id' => $discussion->forum
), '*', MUST_EXIST
);
45 $cm = get_coursemodule_from_instance('forum', $forum->id
, $course->id
, false, MUST_EXIST
);
47 require_course_login($course, true, $cm);
49 /// Add ajax-related libs
50 $PAGE->requires
->yui2_lib('event');
51 $PAGE->requires
->yui2_lib('connection');
52 $PAGE->requires
->yui2_lib('json');
54 // move this down fix for MDL-6926
55 require_once($CFG->dirroot
.'/mod/forum/lib.php');
57 $modcontext = get_context_instance(CONTEXT_MODULE
, $cm->id
);
58 require_capability('mod/forum:viewdiscussion', $modcontext, NULL, true, 'noviewdiscussionspermission', 'forum');
60 if (!empty($CFG->enablerssfeeds
) && !empty($CFG->forum_enablerssfeeds
) && $forum->rsstype
&& $forum->rssarticles
) {
61 require_once("$CFG->libdir/rsslib.php");
63 $rsstitle = format_string($course->shortname
) . ': %fullname%';
64 rss_add_http_header($modcontext, 'mod_forum', $forum, $rsstitle);
67 if ($forum->type
== 'news') {
68 if (!($USER->id
== $discussion->userid ||
(($discussion->timestart
== 0
69 ||
$discussion->timestart
<= time())
70 && ($discussion->timeend
== 0 ||
$discussion->timeend
> time())))) {
71 print_error('invaliddiscussionid', 'forum', "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
75 /// move discussion if requested
76 if ($move > 0 and confirm_sesskey()) {
77 $return = $CFG->wwwroot
.'/mod/forum/discuss.php?d='.$discussion->id
;
79 require_capability('mod/forum:movediscussions', $modcontext);
81 if ($forum->type
== 'single') {
82 print_error('cannotmovefromsingleforum', 'forum', $return);
85 if (!$forumto = $DB->get_record('forum', array('id' => $move))) {
86 print_error('cannotmovetonotexist', 'forum', $return);
89 if (!$cmto = get_coursemodule_from_instance('forum', $forumto->id
, $course->id
)) {
90 print_error('cannotmovetonotfound', 'forum', $return);
93 if (!coursemodule_visible_for_user($cmto)) {
94 print_error('cannotmovenotvisible', 'forum', $return);
97 require_capability('mod/forum:startdiscussion', get_context_instance(CONTEXT_MODULE
,$cmto->id
));
99 if (!forum_move_attachments($discussion, $forum->id
, $forumto->id
)) {
100 echo $OUTPUT->notification("Errors occurred while moving attachment directories - check your file permissions");
102 $DB->set_field('forum_discussions', 'forum', $forumto->id
, array('id' => $discussion->id
));
103 $DB->set_field('forum_read', 'forumid', $forumto->id
, array('discussionid' => $discussion->id
));
104 add_to_log($course->id
, 'forum', 'move discussion', "discuss.php?d=$discussion->id", $discussion->id
, $cmto->id
);
106 require_once($CFG->libdir
.'/rsslib.php');
107 require_once($CFG->dirroot
.'/mod/forum/rsslib.php');
109 // Delete the RSS files for the 2 forums to force regeneration of the feeds
110 forum_rss_delete_file($forum);
111 forum_rss_delete_file($forumto);
113 redirect($return.'&moved=-1&sesskey='.sesskey());
116 add_to_log($course->id
, 'forum', 'view discussion', $PAGE->url
->out(false), $discussion->id
, $cm->id
);
118 unset($SESSION->fromdiscussion
);
121 set_user_preference('forum_displaymode', $mode);
124 $displaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode
);
127 // If flat AND parent, then force nested display this time
128 if ($displaymode == FORUM_MODE_FLATOLDEST
or $displaymode == FORUM_MODE_FLATNEWEST
) {
129 $displaymode = FORUM_MODE_NESTED
;
132 $parent = $discussion->firstpost
;
135 if (! $post = forum_get_post_full($parent)) {
136 print_error("notexists", 'forum', "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
140 if (!forum_user_can_view_post($post, $course, $cm, $forum, $discussion)) {
141 print_error('nopermissiontoview', 'forum', "$CFG->wwwroot/mod/forum/view.php?id=$forum->id");
144 if ($mark == 'read' or $mark == 'unread') {
145 if ($CFG->forum_usermarksread
&& forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) {
146 if ($mark == 'read') {
147 forum_tp_add_read_record($USER->id
, $postid);
150 forum_tp_delete_read_records($USER->id
, $postid);
155 $searchform = forum_search_form($course);
157 $forumnode = $PAGE->navigation
->find($cm->id
, navigation_node
::TYPE_ACTIVITY
);
158 if (empty($forumnode)) {
159 $forumnode = $PAGE->navbar
;
161 $forumnode->make_active();
163 $node = $forumnode->add(format_string($discussion->name
), new moodle_url('/mod/forum/discuss.php', array('d'=>$discussion->id
)));
164 $node->display
= false;
165 if ($node && $post->id
!= $discussion->firstpost
) {
166 $node->add(format_string($post->subject
), $PAGE->url
);
169 $PAGE->set_title("$course->shortname: ".format_string($discussion->name
));
170 $PAGE->set_heading($course->fullname
);
171 $PAGE->set_button($searchform);
172 echo $OUTPUT->header();
174 /// Check to see if groups are being used in this forum
175 /// If so, make sure the current person is allowed to see this discussion
176 /// Also, if we know they should be able to reply, then explicitly set $canreply for performance reasons
178 $canreply = forum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext);
179 if (!$canreply and $forum->type
!== 'news') {
180 if (isguestuser() or !isloggedin()) {
183 if (!is_enrolled($modcontext) and !is_viewing($modcontext)) {
184 // allow guests and not-logged-in to see the link - they are prompted to log in after clicking the link
185 // normal users with temporary guest access see this link too, they are asked to enrol instead
186 $canreply = enrol_selfenrol_available($course->id
);
190 /// Print the controls across the top
191 echo '<div class="discussioncontrols clearfix">';
193 if (!empty($CFG->enableportfolios
) && has_capability('mod/forum:exportdiscussion', $modcontext)) {
194 require_once($CFG->libdir
.'/portfoliolib.php');
195 $button = new portfolio_add_button();
196 $button->set_callback_options('forum_portfolio_caller', array('discussionid' => $discussion->id
), '/mod/forum/locallib.php');
197 $button = $button->to_html(PORTFOLIO_ADD_FULL_FORM
, get_string('exportdiscussion', 'mod_forum'));
198 $buttonextraclass = '';
199 if (empty($button)) {
200 // no portfolio plugin available.
202 $buttonextraclass = ' noavailable';
204 echo html_writer
::tag('div', $button, array('class' => 'discussioncontrol exporttoportfolio'.$buttonextraclass));
206 echo html_writer
::tag('div', ' ', array('class'=>'discussioncontrol nullcontrol'));
209 // groups selector not needed here
210 echo '<div class="discussioncontrol displaymode">';
211 forum_print_mode_form($discussion->id
, $displaymode);
214 if ($forum->type
!= 'single'
215 && has_capability('mod/forum:movediscussions', $modcontext)) {
217 echo '<div class="discussioncontrol movediscussion">';
218 // Popup menu to move discussions to other forums. The discussion in a
219 // single discussion forum can't be moved.
220 $modinfo = get_fast_modinfo($course);
221 if (isset($modinfo->instances
['forum'])) {
222 $forummenu = array();
223 $sections = get_all_sections($course->id
);
224 foreach ($modinfo->instances
['forum'] as $forumcm) {
225 if (!$forumcm->uservisible ||
!has_capability('mod/forum:startdiscussion',
226 get_context_instance(CONTEXT_MODULE
,$forumcm->id
))) {
230 $section = $forumcm->sectionnum
;
231 $sectionname = get_section_name($course, $sections[$section]);
232 if (empty($forummenu[$section])) {
233 $forummenu[$section] = array($sectionname => array());
235 if ($forumcm->instance
!= $forum->id
) {
236 $url = "/mod/forum/discuss.php?d=$discussion->id&move=$forumcm->instance&sesskey=".sesskey();
237 $forummenu[$section][$sectionname][$url] = format_string($forumcm->name
);
240 if (!empty($forummenu)) {
241 echo '<div class="movediscussionoption">';
242 $select = new url_select($forummenu, '',
243 array(''=>get_string("movethisdiscussionto", "forum")),
244 'forummenu', get_string('move'));
245 echo $OUTPUT->render($select);
251 echo '<div class="clearfloat"> </div>';
254 if (!empty($forum->blockafter
) && !empty($forum->blockperiod
)) {
256 $a->blockafter
= $forum->blockafter
;
257 $a->blockperiod
= get_string('secondstotime'.$forum->blockperiod
);
258 echo $OUTPUT->notification(get_string('thisforumisthrottled','forum',$a));
261 if ($forum->type
== 'qanda' && !has_capability('mod/forum:viewqandawithoutposting', $modcontext) &&
262 !forum_user_has_posted($forum->id
,$discussion->id
,$USER->id
)) {
263 echo $OUTPUT->notification(get_string('qandanotify','forum'));
266 if ($move == -1 and confirm_sesskey()) {
267 echo $OUTPUT->notification(get_string('discussionmoved', 'forum', format_string($forum->name
,true)));
270 $canrate = has_capability('mod/forum:rate', $modcontext);
271 forum_print_discussion($course, $cm, $forum, $discussion, $post, $displaymode, $canreply, $canrate);
273 echo $OUTPUT->footer();