MDL-24561 forum subscription checks sesskey
[moodle.git] / mod / forum / index.php
CommitLineData
cd4e6b17 1<?php
f93f848a 2
8f685009
SH
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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/>.
17
18/**
19 * @package mod-forum
20 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
cd4e6b17 24require_once(dirname(__FILE__) . '/../../config.php');
25require_once($CFG->dirroot . '/course/lib.php');
26require_once($CFG->dirroot . '/mod/forum/lib.php');
27require_once($CFG->libdir . '/rsslib.php');
f93f848a 28
cd4e6b17 29$id = optional_param('id', 0, PARAM_INT); // Course id
30$subscribe = optional_param('subscribe', null, PARAM_INT); // Subscribe/Unsubscribe all forums
f93f848a 31
a6855934 32$url = new moodle_url('/mod/forum/index.php', array('id'=>$id));
cd4e6b17 33if ($subscribe !== null) {
34 $url->param('subscribe', $subscribe);
35}
36$PAGE->set_url($url);
37
38if ($id) {
39 if (! $course = $DB->get_record('course', array('id' => $id))) {
40 print_error('invalidcourseid');
41 }
42} else {
2a250a0b 43 $course = get_site();
cd4e6b17 44}
f93f848a 45
cd4e6b17 46require_course_login($course);
191b267b 47$PAGE->set_pagelayout('incourse');
cd4e6b17 48$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
583b57b4 49
ecc8403e 50
cd4e6b17 51unset($SESSION->fromdiscussion);
f93f848a 52
cd4e6b17 53add_to_log($course->id, 'forum', 'view forums', "index.php?id=$course->id");
2f3f505f 54
cd4e6b17 55$strforums = get_string('forums', 'forum');
56$strforum = get_string('forum', 'forum');
57$strdescription = get_string('description');
58$strdiscussions = get_string('discussions', 'forum');
59$strsubscribed = get_string('subscribed', 'forum');
60$strunreadposts = get_string('unreadposts', 'forum');
61$strtracking = get_string('tracking', 'forum');
62$strmarkallread = get_string('markallread', 'forum');
63$strtrackforum = get_string('trackforum', 'forum');
64$strnotrackforum = get_string('notrackforum', 'forum');
65$strsubscribe = get_string('subscribe', 'forum');
66$strunsubscribe = get_string('unsubscribe', 'forum');
67$stryes = get_string('yes');
68$strno = get_string('no');
69$strrss = get_string('rss');
7487c856 70$strsectionname = get_string('sectionname', 'format_'.$course->format);
97485d07 71
cd4e6b17 72$searchform = forum_search_form($course);
f93f848a 73
f781b794 74
cd4e6b17 75// Start of the table for General Forums
f781b794 76
cd4e6b17 77$generaltable = new html_table();
78$generaltable->head = array ($strforum, $strdescription, $strdiscussions);
79$generaltable->align = array ('left', 'left', 'center');
f37da850 80
cd4e6b17 81if ($usetracking = forum_tp_can_track_forums()) {
82 $untracked = forum_tp_get_untracked_forums($USER->id, $course->id);
c2468824 83
cd4e6b17 84 $generaltable->head[] = $strunreadposts;
85 $generaltable->align[] = 'center';
38994d6f 86
cd4e6b17 87 $generaltable->head[] = $strtracking;
88 $generaltable->align[] = 'center';
89}
f93f848a 90
cd4e6b17 91$subscribed_forums = forum_get_subscribed_forums($course);
92
1cd8c399
AD
93$can_subscribe = is_enrolled($coursecontext);
94if ($can_subscribe) {
cd4e6b17 95 $generaltable->head[] = $strsubscribed;
96 $generaltable->align[] = 'center';
97}
98
99if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
100 isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
101 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
102 $generaltable->head[] = $strrss;
103 $generaltable->align[] = 'center';
104}
90f4745c 105
7487c856
SH
106$usesections = course_format_uses_sections($course->format);
107$sections = get_all_sections($course->id);
108
109$table = new html_table();
f93f848a 110
cd4e6b17 111// Parse and organise all the forums. Most forums are course modules but
112// some special ones are not. These get placed in the general forums
113// category with the forums in section 0.
114
115$forums = $DB->get_records('forum', array('course' => $course->id));
116
117$generalforums = array();
118$learningforums = array();
119$modinfo =& get_fast_modinfo($course);
120
121if (!isset($modinfo->instances['forum'])) {
122 $modinfo->instances['forum'] = array();
123}
124
125foreach ($modinfo->instances['forum'] as $forumid=>$cm) {
126 if (!$cm->uservisible or !isset($forums[$forumid])) {
127 continue;
33f48f03 128 }
129
cd4e6b17 130 $forum = $forums[$forumid];
fa5a5b52 131
cd4e6b17 132 if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
133 continue; // Shouldn't happen
134 }
135
136 if (!has_capability('mod/forum:viewdiscussion', $context)) {
137 continue;
138 }
ecc8403e 139
cd4e6b17 140 // fill two type array - order in modinfo is the same as in course
141 if ($forum->type == 'news' or $forum->type == 'social') {
142 $generalforums[$forum->id] = $forum;
dd97c328 143
cd4e6b17 144 } else if ($course->id == SITEID or empty($cm->sectionnum)) {
145 $generalforums[$forum->id] = $forum;
ecc8403e 146
cd4e6b17 147 } else {
148 $learningforums[$forum->id] = $forum;
2f3f505f 149 }
cd4e6b17 150}
2f3f505f 151
cd4e6b17 152/// Do course wide subscribe/unsubscribe
261c6ef0 153if (!is_null($subscribe) and !isguestuser()) {
2f3f505f 154 foreach ($modinfo->instances['forum'] as $forumid=>$cm) {
2f3f505f 155 $forum = $forums[$forumid];
cd4e6b17 156 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
157 $cansub = false;
dd97c328 158
cd4e6b17 159 if (has_capability('mod/forum:viewdiscussion', $modcontext)) {
160 $cansub = true;
2f3f505f 161 }
cd4e6b17 162 if ($cansub && $cm->visible == 0 &&
163 !has_capability('mod/forum:managesubscriptions', $modcontext))
164 {
165 $cansub = false;
2f3f505f 166 }
cd4e6b17 167 if (!forum_is_forcesubscribed($forum)) {
168 $subscribed = forum_is_subscribed($USER->id, $forum);
169 if ((has_capability('moodle/course:manageactivities', $coursecontext, $USER->id) || $forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE) && $subscribe && !$subscribed && $cansub) {
170 forum_subscribe($USER->id, $forumid);
171 } else if (!$subscribe && $subscribed) {
172 forum_unsubscribe($USER->id, $forumid);
173 }
174 }
175 }
176 $returnto = forum_go_back_to("index.php?id=$course->id");
177 if ($subscribe) {
178 add_to_log($course->id, 'forum', 'subscribeall', "index.php?id=$course->id", $course->id);
179 redirect($returnto, get_string('nowallsubscribed', 'forum', format_string($course->shortname)), 1);
180 } else {
181 add_to_log($course->id, 'forum', 'unsubscribeall', "index.php?id=$course->id", $course->id);
182 redirect($returnto, get_string('nowallunsubscribed', 'forum', format_string($course->shortname)), 1);
183 }
184}
dd97c328 185
cd4e6b17 186/// First, let's process the general forums and build up a display
187
188if ($generalforums) {
189 foreach ($generalforums as $forum) {
190 $cm = $modinfo->instances['forum'][$forum->id];
191 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
192
193 $count = forum_count_discussions($forum, $cm, $course);
194
195 if ($usetracking) {
196 if ($forum->trackingtype == FORUM_TRACKING_OFF) {
197 $unreadlink = '-';
198 $trackedlink = '-';
199
200 } else {
201 if (isset($untracked[$forum->id])) {
202 $unreadlink = '-';
203 } else if ($unread = forum_tp_count_forum_unread_posts($cm, $course)) {
204 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
205 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
b5d0cafc 206 $forum->id.'&amp;mark=read"><img src="'.$OUTPUT->pix_url('t/clear') . '" alt="'.$strmarkallread.'" /></a></span>';
cd4e6b17 207 } else {
208 $unreadlink = '<span class="read">0</span>';
209 }
2f3f505f 210
cd4e6b17 211 if ($forum->trackingtype == FORUM_TRACKING_ON) {
212 $trackedlink = $stryes;
2f3f505f 213
cd4e6b17 214 } else {
a6855934 215 $aurl = new moodle_url('/mod/forum/settracking.php', array('id'=>$forum->id));
cd4e6b17 216 if (!isset($untracked[$forum->id])) {
5c2ed7e2 217 $trackedlink = $OUTPUT->single_button($aurl, $stryes, 'post', array('title'=>$strnotrackforum));
cd4e6b17 218 } else {
5c2ed7e2 219 $trackedlink = $OUTPUT->single_button($aurl, $strno, 'post', array('title'=>$strtrackforum));
cd4e6b17 220 }
221 }
222 }
223 }
224
225 $forum->intro = shorten_text(format_module_intro('forum', $forum, $cm->id), $CFG->forum_shortpost);
226 $forumname = format_string($forum->name, true);;
227
228 if ($cm->visible) {
229 $style = '';
2f3f505f 230 } else {
cd4e6b17 231 $style = 'class="dimmed"';
37b15514 232 }
cd4e6b17 233 $forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name,true)."</a>";
234 $discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
702dc57b 235
cd4e6b17 236 $row = array ($forumlink, $forum->intro, $discussionlink);
237 if ($usetracking) {
238 $row[] = $unreadlink;
239 $row[] = $trackedlink; // Tracking.
240 }
3b8b9b6a 241
cd4e6b17 242 if ($can_subscribe) {
243 if ($forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE) {
244 $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
245 'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
246 'cantsubscribe' => '-'), false, false, true, $subscribed_forums);
247 } else {
248 $row[] = '-';
2c1363e6 249 }
cd4e6b17 250 }
251
252 //If this forum has RSS activated, calculate it
253 if ($show_rss) {
254 if ($forum->rsstype and $forum->rssarticles) {
9e86f2e7 255 //Calculate the tooltip text
cd4e6b17 256 if ($forum->rsstype == 1) {
9e86f2e7 257 $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum');
cd4e6b17 258 } else {
9e86f2e7 259 $tooltiptext = get_string('rsssubscriberssposts', 'forum');
03877b04 260 }
cd4e6b17 261 //Get html code for RSS link
aa60291e 262 $row[] = rss_get_link($context->id, $USER->id, 'mod_forum', $forum->id, $tooltiptext);
cd4e6b17 263 } else {
264 $row[] = '&nbsp;';
03877b04 265 }
266 }
cd4e6b17 267
268 $generaltable->data[] = $row;
269 }
270}
271
272
273// Start of the table for Learning Forums
274$learningtable = new html_table();
275$learningtable->head = array ($strforum, $strdescription, $strdiscussions);
276$learningtable->align = array ('left', 'left', 'center');
277
278if ($usetracking) {
279 $learningtable->head[] = $strunreadposts;
280 $learningtable->align[] = 'center';
281
282 $learningtable->head[] = $strtracking;
283 $learningtable->align[] = 'center';
284}
285
286if ($can_subscribe) {
287 $learningtable->head[] = $strsubscribed;
288 $learningtable->align[] = 'center';
289}
290
291if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
292 isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
293 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
294 $learningtable->head[] = $strrss;
295 $learningtable->align[] = 'center';
296}
297
298/// Now let's process the learning forums
299
300if ($course->id != SITEID) { // Only real courses have learning forums
301 // Add extra field for section number, at the front
7487c856 302 array_unshift($learningtable->head, $strsectionname);
cd4e6b17 303 array_unshift($learningtable->align, 'center');
03877b04 304
ecc8403e 305
cd4e6b17 306 if ($learningforums) {
307 $currentsection = '';
308 foreach ($learningforums as $forum) {
2f3f505f 309 $cm = $modinfo->instances['forum'][$forum->id];
310 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
dd97c328 311
90f4745c 312 $count = forum_count_discussions($forum, $cm, $course);
8f0cd6ef 313
eaf50aef 314 if ($usetracking) {
a90e92a5 315 if ($forum->trackingtype == FORUM_TRACKING_OFF) {
316 $unreadlink = '-';
317 $trackedlink = '-';
90f4745c 318
319 } else {
320 if (isset($untracked[$forum->id])) {
cd4e6b17 321 $unreadlink = '-';
90f4745c 322 } else if ($unread = forum_tp_count_forum_unread_posts($cm, $course)) {
cd4e6b17 323 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
c39748f4 324 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
b5d0cafc 325 $forum->id.'&amp;mark=read"><img src="'.$OUTPUT->pix_url('t/clear') . '" alt="'.$strmarkallread.'" /></a></span>';
38994d6f 326 } else {
90f4745c 327 $unreadlink = '<span class="read">0</span>';
38994d6f 328 }
630bd96b 329
90f4745c 330 if ($forum->trackingtype == FORUM_TRACKING_ON) {
331 $trackedlink = $stryes;
bbbf2d40 332
a90e92a5 333 } else {
a6855934 334 $aurl = new moodle_url('/mod/forum/settracking.php', array('id'=>$forum->id));
90f4745c 335 if (!isset($untracked[$forum->id])) {
5c2ed7e2 336 $trackedlink = $OUTPUT->single_button($aurl, $stryes, 'post', array('title'=>$strnotrackforum));
90f4745c 337 } else {
5c2ed7e2 338 $trackedlink = $OUTPUT->single_button($aurl, $strno, 'post', array('title'=>$strtrackforum));
90f4745c 339 }
630bd96b 340 }
f37da850 341 }
342 }
343
4ba2221a 344 $forum->intro = shorten_text(format_module_intro('forum', $forum, $cm->id), $CFG->forum_shortpost);
cd4e6b17 345
346 if ($cm->sectionnum != $currentsection) {
7487c856 347 $printsection = get_section_name($course, $sections[$cm->sectionnum]);
cd4e6b17 348 if ($currentsection) {
349 $learningtable->data[] = 'hr';
350 }
351 $currentsection = $cm->sectionnum;
352 } else {
353 $printsection = '';
354 }
355
356 $forumname = format_string($forum->name,true);;
ecc8403e 357
90f4745c 358 if ($cm->visible) {
359 $style = '';
ecc8403e 360 } else {
90f4745c 361 $style = 'class="dimmed"';
ecc8403e 362 }
90f4745c 363 $forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name,true)."</a>";
364 $discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
ecc8403e 365
cd4e6b17 366 $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
49b53f26 367 if ($usetracking) {
368 $row[] = $unreadlink;
369 $row[] = $trackedlink; // Tracking.
f4646b44 370 }
371
ecc8403e 372 if ($can_subscribe) {
90f4745c 373 if ($forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE) {
374 $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
cd4e6b17 375 'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
376 'cantsubscribe' => '-'), false, false, true, $subscribed_forums);
90f4745c 377 } else {
378 $row[] = '-';
379 }
49b53f26 380 }
381
382 //If this forum has RSS activated, calculate it
90f4745c 383 if ($show_rss) {
384 if ($forum->rsstype and $forum->rssarticles) {
385 //Calculate the tolltip text
386 if ($forum->rsstype == 1) {
9e86f2e7 387 $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum');
90f4745c 388 } else {
9e86f2e7 389 $tooltiptext = get_string('rsssubscriberssposts', 'forum');
90f4745c 390 }
391 //Get html code for RSS link
aa60291e 392 $row[] = rss_get_link($context->id, $USER->id, 'mod_forum', $forum->id, $tooltiptext);
49b53f26 393 } else {
90f4745c 394 $row[] = '&nbsp;';
f4646b44 395 }
ecc8403e 396 }
49b53f26 397
cd4e6b17 398 $learningtable->data[] = $row;
ecc8403e 399 }
8f0cd6ef 400 }
cd4e6b17 401}
402
403
404/// Output the page
405$PAGE->navbar->add($strforums);
406$PAGE->set_title("$course->shortname: $strforums");
407$PAGE->set_heading($course->fullname);
408$PAGE->set_button($searchform);
409echo $OUTPUT->header();
410
261c6ef0 411if (!isguestuser()) {
cd4e6b17 412 echo $OUTPUT->box_start('subscription');
413 echo '<span class="helplink">';
414 echo '<a href="index.php?id='.$course->id.'&amp;subscribe=1">'.get_string('allsubscribe', 'forum').'</a>';
415 echo '</span><br /><span class="helplink">';
416 echo '<a href="index.php?id='.$course->id.'&amp;subscribe=0">'.get_string('allunsubscribe', 'forum').'</a>';
417 echo '</span>';
418 echo $OUTPUT->box_end();
419 echo $OUTPUT->box('&nbsp;', 'clearer');
420}
421
422if ($generalforums) {
423 echo $OUTPUT->heading(get_string('generalforums', 'forum'));
16be8974 424 echo html_writer::table($generaltable);
cd4e6b17 425}
426
427if ($learningforums) {
428 echo $OUTPUT->heading(get_string('learningforums', 'forum'));
16be8974 429 echo html_writer::table($learningtable);
cd4e6b17 430}
431
432echo $OUTPUT->footer();
433