weekly release 4.0dev
[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/**
01030f1b 19 * @package mod_forum
8f685009
SH
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
1fcf0ca8 24require_once(__DIR__ . '/../../config.php');
cd4e6b17 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
8381ac52 32$url = new moodle_url('/mod/forum/index.php', array('id' => $id));
cd4e6b17 33if ($subscribe !== null) {
c56ed7bb 34 require_sesskey();
cd4e6b17 35 $url->param('subscribe', $subscribe);
36}
37$PAGE->set_url($url);
38
39if ($id) {
8381ac52 40 if (!$course = $DB->get_record('course', array('id' => $id))) {
cd4e6b17 41 print_error('invalidcourseid');
42 }
43} else {
2a250a0b 44 $course = get_site();
cd4e6b17 45}
f93f848a 46
cd4e6b17 47require_course_login($course);
191b267b 48$PAGE->set_pagelayout('incourse');
bf0f06b1 49$coursecontext = context_course::instance($course->id);
583b57b4 50
cd4e6b17 51unset($SESSION->fromdiscussion);
f93f848a 52
22881392
DP
53$params = array(
54 'context' => context_course::instance($course->id)
55);
56$event = \mod_forum\event\course_module_instance_list_viewed::create($params);
1e242b41 57$event->add_record_snapshot('course', $course);
22881392 58$event->trigger();
2f3f505f 59
cd4e6b17 60$strforums = get_string('forums', 'forum');
61$strforum = get_string('forum', 'forum');
62$strdescription = get_string('description');
63$strdiscussions = get_string('discussions', 'forum');
64$strsubscribed = get_string('subscribed', 'forum');
65$strunreadposts = get_string('unreadposts', 'forum');
66$strtracking = get_string('tracking', 'forum');
67$strmarkallread = get_string('markallread', 'forum');
68$strtrackforum = get_string('trackforum', 'forum');
69$strnotrackforum = get_string('notrackforum', 'forum');
70$strsubscribe = get_string('subscribe', 'forum');
71$strunsubscribe = get_string('unsubscribe', 'forum');
72$stryes = get_string('yes');
73$strno = get_string('no');
74$strrss = get_string('rss');
8e08c731 75$stremaildigest = get_string('emaildigest');
97485d07 76
cd4e6b17 77$searchform = forum_search_form($course);
f93f848a 78
c42c2355 79// Start of the table for General Forums.
cd4e6b17 80$generaltable = new html_table();
81$generaltable->head = array ($strforum, $strdescription, $strdiscussions);
82$generaltable->align = array ('left', 'left', 'center');
f37da850 83
cd4e6b17 84if ($usetracking = forum_tp_can_track_forums()) {
85 $untracked = forum_tp_get_untracked_forums($USER->id, $course->id);
c2468824 86
cd4e6b17 87 $generaltable->head[] = $strunreadposts;
88 $generaltable->align[] = 'center';
38994d6f 89
cd4e6b17 90 $generaltable->head[] = $strtracking;
91 $generaltable->align[] = 'center';
92}
f93f848a 93
59075a43
AN
94// Fill the subscription cache for this course and user combination.
95\mod_forum\subscriptions::fill_subscription_cache_for_course($course->id, $USER->id);
cd4e6b17 96
7487c856 97$usesections = course_format_uses_sections($course->format);
7487c856
SH
98
99$table = new html_table();
f93f848a 100
cd4e6b17 101// Parse and organise all the forums. Most forums are course modules but
102// some special ones are not. These get placed in the general forums
103// category with the forums in section 0.
104
8e08c731
AN
105$forums = $DB->get_records_sql("
106 SELECT f.*,
107 d.maildigest
108 FROM {forum} f
109 LEFT JOIN {forum_digests} d ON d.forum = f.id AND d.userid = ?
110 WHERE f.course = ?
111 ", array($USER->id, $course->id));
cd4e6b17 112
113$generalforums = array();
114$learningforums = array();
f20edd52 115$modinfo = get_fast_modinfo($course);
8381ac52 116$showsubscriptioncolumns = false;
cd4e6b17 117
8381ac52 118foreach ($modinfo->get_instances_of('forum') as $forumid => $cm) {
cd4e6b17 119 if (!$cm->uservisible or !isset($forums[$forumid])) {
120 continue;
33f48f03 121 }
122
cd4e6b17 123 $forum = $forums[$forumid];
fa5a5b52 124
0d4d49d1 125 if (!$context = context_module::instance($cm->id, IGNORE_MISSING)) {
8381ac52
AN
126 // Shouldn't happen.
127 continue;
cd4e6b17 128 }
129
130 if (!has_capability('mod/forum:viewdiscussion', $context)) {
8381ac52 131 // User can't view this one - skip it.
cd4e6b17 132 continue;
133 }
ecc8403e 134
8381ac52
AN
135 // Determine whether subscription options should be displayed.
136 $forum->cansubscribe = mod_forum\subscriptions::is_subscribable($forum);
137 $forum->cansubscribe = $forum->cansubscribe || has_capability('mod/forum:managesubscriptions', $context);
138 $forum->issubscribed = mod_forum\subscriptions::is_subscribed($USER->id, $forum, null, $cm);
139
140 $showsubscriptioncolumns = $showsubscriptioncolumns || $forum->issubscribed || $forum->cansubscribe;
141
142 // Fill two type array - order in modinfo is the same as in course.
cd4e6b17 143 if ($forum->type == 'news' or $forum->type == 'social') {
144 $generalforums[$forum->id] = $forum;
dd97c328 145
cd4e6b17 146 } else if ($course->id == SITEID or empty($cm->sectionnum)) {
147 $generalforums[$forum->id] = $forum;
ecc8403e 148
cd4e6b17 149 } else {
150 $learningforums[$forum->id] = $forum;
2f3f505f 151 }
cd4e6b17 152}
2f3f505f 153
8381ac52
AN
154if ($showsubscriptioncolumns) {
155 // The user can subscribe to at least one forum.
156 $generaltable->head[] = $strsubscribed;
157 $generaltable->align[] = 'center';
158
159 $generaltable->head[] = $stremaildigest . ' ' . $OUTPUT->help_icon('emaildigesttype', 'mod_forum');
160 $generaltable->align[] = 'center';
161
8381ac52
AN
162}
163
164if ($show_rss = (($showsubscriptioncolumns || $course->id == SITEID) &&
165 isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
166 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
167 $generaltable->head[] = $strrss;
168 $generaltable->align[] = 'center';
169}
170
171
b688c449
EL
172// Do course wide subscribe/unsubscribe if requested
173if (!is_null($subscribe)) {
8381ac52 174 if (isguestuser() or !$showsubscriptioncolumns) {
d954b54f
AN
175 // There should not be any links leading to this place, just redirect.
176 redirect(
177 new moodle_url('/mod/forum/index.php', array('id' => $id)),
178 get_string('subscribeenrolledonly', 'forum'),
179 null,
180 \core\output\notification::NOTIFY_ERROR
181 );
b688c449
EL
182 }
183 // Can proceed now, the user is not guest and is enrolled
8381ac52 184 foreach ($modinfo->get_instances_of('forum') as $forumid => $cm) {
2f3f505f 185 $forum = $forums[$forumid];
bf0f06b1 186 $modcontext = context_module::instance($cm->id);
cd4e6b17 187 $cansub = false;
dd97c328 188
cd4e6b17 189 if (has_capability('mod/forum:viewdiscussion', $modcontext)) {
190 $cansub = true;
2f3f505f 191 }
cd4e6b17 192 if ($cansub && $cm->visible == 0 &&
193 !has_capability('mod/forum:managesubscriptions', $modcontext))
194 {
195 $cansub = false;
2f3f505f 196 }
59075a43 197 if (!\mod_forum\subscriptions::is_forcesubscribed($forum)) {
4238983e 198 $subscribed = \mod_forum\subscriptions::is_subscribed($USER->id, $forum, null, $cm);
59075a43
AN
199 $canmanageactivities = has_capability('moodle/course:manageactivities', $coursecontext, $USER->id);
200 if (($canmanageactivities || \mod_forum\subscriptions::is_subscribable($forum)) && $subscribe && !$subscribed && $cansub) {
e3bbfb52 201 \mod_forum\subscriptions::subscribe_user($USER->id, $forum, $modcontext, true);
cd4e6b17 202 } else if (!$subscribe && $subscribed) {
e3bbfb52 203 \mod_forum\subscriptions::unsubscribe_user($USER->id, $forum, $modcontext, true);
cd4e6b17 204 }
205 }
206 }
3d8d8a8c 207 $returnto = forum_go_back_to(new moodle_url('/mod/forum/index.php', array('id' => $course->id)));
bf0f06b1 208 $shortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
cd4e6b17 209 if ($subscribe) {
d954b54f
AN
210 redirect(
211 $returnto,
212 get_string('nowallsubscribed', 'forum', $shortname),
213 null,
214 \core\output\notification::NOTIFY_SUCCESS
215 );
cd4e6b17 216 } else {
d954b54f
AN
217 redirect(
218 $returnto,
219 get_string('nowallunsubscribed', 'forum', $shortname),
220 null,
221 \core\output\notification::NOTIFY_SUCCESS
222 );
cd4e6b17 223 }
224}
dd97c328 225
cd4e6b17 226if ($generalforums) {
8381ac52 227 // Process general forums.
cd4e6b17 228 foreach ($generalforums as $forum) {
229 $cm = $modinfo->instances['forum'][$forum->id];
bf0f06b1 230 $context = context_module::instance($cm->id);
cd4e6b17 231
232 $count = forum_count_discussions($forum, $cm, $course);
233
234 if ($usetracking) {
235 if ($forum->trackingtype == FORUM_TRACKING_OFF) {
236 $unreadlink = '-';
237 $trackedlink = '-';
238
239 } else {
240 if (isset($untracked[$forum->id])) {
241 $unreadlink = '-';
242 } else if ($unread = forum_tp_count_forum_unread_posts($cm, $course)) {
428db95d 243 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'#unread">'.$unread.'</a>';
663640f5 244 $icon = $OUTPUT->pix_icon('t/markasread', $strmarkallread);
cd4e6b17 245 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
663640f5 246 $forum->id.'&amp;mark=read&amp;sesskey=' . sesskey() . '">' . $icon . '</a></span>';
cd4e6b17 247 } else {
248 $unreadlink = '<span class="read">0</span>';
249 }
2f3f505f 250
bd8f5d45 251 if (($forum->trackingtype == FORUM_TRACKING_FORCED) && ($CFG->forum_allowforcedreadtracking)) {
cd4e6b17 252 $trackedlink = $stryes;
20e6782f
EM
253 } else if ($forum->trackingtype === FORUM_TRACKING_OFF || ($USER->trackforums == 0)) {
254 $trackedlink = '-';
cd4e6b17 255 } else {
7a311adb
AN
256 $aurl = new moodle_url('/mod/forum/settracking.php', array(
257 'id' => $forum->id,
258 'sesskey' => sesskey(),
259 ));
cd4e6b17 260 if (!isset($untracked[$forum->id])) {
8381ac52 261 $trackedlink = $OUTPUT->single_button($aurl, $stryes, 'post', array('title' => $strnotrackforum));
cd4e6b17 262 } else {
8381ac52 263 $trackedlink = $OUTPUT->single_button($aurl, $strno, 'post', array('title' => $strtrackforum));
cd4e6b17 264 }
265 }
266 }
267 }
268
269 $forum->intro = shorten_text(format_module_intro('forum', $forum, $cm->id), $CFG->forum_shortpost);
0e35ba6f 270 $forumname = format_string($forum->name, true);
cd4e6b17 271
272 if ($cm->visible) {
273 $style = '';
2f3f505f 274 } else {
cd4e6b17 275 $style = 'class="dimmed"';
37b15514 276 }
cd4e6b17 277 $forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name,true)."</a>";
278 $discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
702dc57b 279
cd4e6b17 280 $row = array ($forumlink, $forum->intro, $discussionlink);
281 if ($usetracking) {
282 $row[] = $unreadlink;
283 $row[] = $trackedlink; // Tracking.
284 }
3b8b9b6a 285
8381ac52 286 if ($showsubscriptioncolumns) {
f71df007
AN
287 $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
288 'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
289 'cantsubscribe' => '-'), false, false, true);
290 $row[] = forum_index_get_forum_subscription_selector($forum);
cd4e6b17 291 }
292
8381ac52 293 // If this forum has RSS activated, calculate it.
cd4e6b17 294 if ($show_rss) {
295 if ($forum->rsstype and $forum->rssarticles) {
9e86f2e7 296 //Calculate the tooltip text
cd4e6b17 297 if ($forum->rsstype == 1) {
9e86f2e7 298 $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum');
cd4e6b17 299 } else {
9e86f2e7 300 $tooltiptext = get_string('rsssubscriberssposts', 'forum');
03877b04 301 }
dbf4433d
RW
302
303 if (!isloggedin() && $course->id == SITEID) {
304 $userid = guest_user()->id;
305 } else {
306 $userid = $USER->id;
307 }
cd4e6b17 308 //Get html code for RSS link
dbf4433d 309 $row[] = rss_get_link($context->id, $userid, 'mod_forum', $forum->id, $tooltiptext);
cd4e6b17 310 } else {
311 $row[] = '&nbsp;';
03877b04 312 }
313 }
cd4e6b17 314
315 $generaltable->data[] = $row;
316 }
317}
318
319
320// Start of the table for Learning Forums
321$learningtable = new html_table();
322$learningtable->head = array ($strforum, $strdescription, $strdiscussions);
323$learningtable->align = array ('left', 'left', 'center');
324
325if ($usetracking) {
326 $learningtable->head[] = $strunreadposts;
327 $learningtable->align[] = 'center';
328
329 $learningtable->head[] = $strtracking;
330 $learningtable->align[] = 'center';
331}
332
8381ac52 333if ($showsubscriptioncolumns) {
cd4e6b17 334 $learningtable->head[] = $strsubscribed;
335 $learningtable->align[] = 'center';
8e08c731
AN
336
337 $learningtable->head[] = $stremaildigest . ' ' . $OUTPUT->help_icon('emaildigesttype', 'mod_forum');
338 $learningtable->align[] = 'center';
cd4e6b17 339}
340
8381ac52 341if ($show_rss = (($showsubscriptioncolumns || $course->id == SITEID) &&
cd4e6b17 342 isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
343 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
344 $learningtable->head[] = $strrss;
345 $learningtable->align[] = 'center';
346}
347
8381ac52 348// Now let's process the learning forums.
cd4e6b17 349if ($course->id != SITEID) { // Only real courses have learning forums
4202a917 350 // 'format_.'$course->format only applicable when not SITEID (format_site is not a format)
b489d37d 351 $strsectionname = get_string('sectionname', 'format_'.$course->format);
cd4e6b17 352 // Add extra field for section number, at the front
7487c856 353 array_unshift($learningtable->head, $strsectionname);
cd4e6b17 354 array_unshift($learningtable->align, 'center');
03877b04 355
ecc8403e 356
cd4e6b17 357 if ($learningforums) {
358 $currentsection = '';
359 foreach ($learningforums as $forum) {
2f3f505f 360 $cm = $modinfo->instances['forum'][$forum->id];
bf0f06b1 361 $context = context_module::instance($cm->id);
dd97c328 362
90f4745c 363 $count = forum_count_discussions($forum, $cm, $course);
8f0cd6ef 364
eaf50aef 365 if ($usetracking) {
a90e92a5 366 if ($forum->trackingtype == FORUM_TRACKING_OFF) {
367 $unreadlink = '-';
368 $trackedlink = '-';
90f4745c 369
370 } else {
371 if (isset($untracked[$forum->id])) {
cd4e6b17 372 $unreadlink = '-';
90f4745c 373 } else if ($unread = forum_tp_count_forum_unread_posts($cm, $course)) {
428db95d 374 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'#unread">'.$unread.'</a>';
663640f5 375 $icon = $OUTPUT->pix_icon('t/markasread', $strmarkallread);
c39748f4 376 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
663640f5 377 $forum->id.'&amp;mark=read&sesskey=' . sesskey() . '">' . $icon . '</a></span>';
38994d6f 378 } else {
90f4745c 379 $unreadlink = '<span class="read">0</span>';
38994d6f 380 }
630bd96b 381
bd8f5d45 382 if (($forum->trackingtype == FORUM_TRACKING_FORCED) && ($CFG->forum_allowforcedreadtracking)) {
90f4745c 383 $trackedlink = $stryes;
20e6782f
EM
384 } else if ($forum->trackingtype === FORUM_TRACKING_OFF || ($USER->trackforums == 0)) {
385 $trackedlink = '-';
a90e92a5 386 } else {
8381ac52 387 $aurl = new moodle_url('/mod/forum/settracking.php', array('id' => $forum->id));
90f4745c 388 if (!isset($untracked[$forum->id])) {
8381ac52 389 $trackedlink = $OUTPUT->single_button($aurl, $stryes, 'post', array('title' => $strnotrackforum));
90f4745c 390 } else {
8381ac52 391 $trackedlink = $OUTPUT->single_button($aurl, $strno, 'post', array('title' => $strtrackforum));
90f4745c 392 }
630bd96b 393 }
f37da850 394 }
395 }
396
4ba2221a 397 $forum->intro = shorten_text(format_module_intro('forum', $forum, $cm->id), $CFG->forum_shortpost);
cd4e6b17 398
399 if ($cm->sectionnum != $currentsection) {
71a56e08 400 $printsection = get_section_name($course, $cm->sectionnum);
cd4e6b17 401 if ($currentsection) {
402 $learningtable->data[] = 'hr';
403 }
404 $currentsection = $cm->sectionnum;
405 } else {
406 $printsection = '';
407 }
408
0e35ba6f 409 $forumname = format_string($forum->name,true);
ecc8403e 410
90f4745c 411 if ($cm->visible) {
412 $style = '';
ecc8403e 413 } else {
90f4745c 414 $style = 'class="dimmed"';
ecc8403e 415 }
90f4745c 416 $forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name,true)."</a>";
417 $discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
ecc8403e 418
cd4e6b17 419 $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
49b53f26 420 if ($usetracking) {
421 $row[] = $unreadlink;
422 $row[] = $trackedlink; // Tracking.
f4646b44 423 }
424
8381ac52 425 if ($showsubscriptioncolumns) {
f71df007
AN
426 $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
427 'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
428 'cantsubscribe' => '-'), false, false, true);
429 $row[] = forum_index_get_forum_subscription_selector($forum);
49b53f26 430 }
431
432 //If this forum has RSS activated, calculate it
90f4745c 433 if ($show_rss) {
434 if ($forum->rsstype and $forum->rssarticles) {
435 //Calculate the tolltip text
436 if ($forum->rsstype == 1) {
9e86f2e7 437 $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum');
90f4745c 438 } else {
9e86f2e7 439 $tooltiptext = get_string('rsssubscriberssposts', 'forum');
90f4745c 440 }
441 //Get html code for RSS link
aa60291e 442 $row[] = rss_get_link($context->id, $USER->id, 'mod_forum', $forum->id, $tooltiptext);
49b53f26 443 } else {
90f4745c 444 $row[] = '&nbsp;';
f4646b44 445 }
ecc8403e 446 }
49b53f26 447
cd4e6b17 448 $learningtable->data[] = $row;
ecc8403e 449 }
8f0cd6ef 450 }
cd4e6b17 451}
452
8381ac52 453// Output the page.
cd4e6b17 454$PAGE->navbar->add($strforums);
455$PAGE->set_title("$course->shortname: $strforums");
456$PAGE->set_heading($course->fullname);
457$PAGE->set_button($searchform);
458echo $OUTPUT->header();
459
8381ac52
AN
460if (!isguestuser() && isloggedin() && $showsubscriptioncolumns) {
461 // Show the subscribe all options only to non-guest, enrolled users.
cd4e6b17 462 echo $OUTPUT->box_start('subscription');
8381ac52
AN
463
464 $subscriptionlink = new moodle_url('/mod/forum/index.php', [
465 'id' => $course->id,
466 'sesskey' => sesskey(),
467 ]);
468
469 // Subscribe all.
470 $subscriptionlink->param('subscribe', 1);
471 echo html_writer::tag('div', html_writer::link($subscriptionlink, get_string('allsubscribe', 'forum')), [
472 'class' => 'helplink',
473 ]);
474
475 // Unsubscribe all.
476 $subscriptionlink->param('subscribe', 0);
477 echo html_writer::tag('div', html_writer::link($subscriptionlink, get_string('allunsubscribe', 'forum')), [
478 'class' => 'helplink',
479 ]);
480
cd4e6b17 481 echo $OUTPUT->box_end();
482 echo $OUTPUT->box('&nbsp;', 'clearer');
483}
484
485if ($generalforums) {
66e2b9f8 486 echo $OUTPUT->heading(get_string('generalforums', 'forum'), 2);
16be8974 487 echo html_writer::table($generaltable);
cd4e6b17 488}
489
490if ($learningforums) {
66e2b9f8 491 echo $OUTPUT->heading(get_string('learningforums', 'forum'), 2);
16be8974 492 echo html_writer::table($learningtable);
cd4e6b17 493}
494
495echo $OUTPUT->footer();
f71df007
AN
496
497/**
498 * Get the content of the forum subscription options for this forum.
499 *
500 * @param stdClass $forum The forum to return options for
501 * @return string
502 */
503function forum_index_get_forum_subscription_selector($forum) {
c42c2355 504 global $OUTPUT, $PAGE;
f71df007
AN
505
506 if ($forum->cansubscribe || $forum->issubscribed) {
c42c2355
AN
507 if ($forum->maildigest === null) {
508 $forum->maildigest = -1;
509 }
510
511 $renderer = $PAGE->get_renderer('mod_forum');
512 return $OUTPUT->render($renderer->render_digest_options($forum, $forum->maildigest));
f71df007
AN
513 } else {
514 // This user can subscribe to some forums. Add the empty fields.
515 return '';
516 }
517};