MDL-41219 course: Make properties of course_modinfo read-only
[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) {
c56ed7bb 34 require_sesskey();
cd4e6b17 35 $url->param('subscribe', $subscribe);
36}
37$PAGE->set_url($url);
38
39if ($id) {
40 if (! $course = $DB->get_record('course', array('id' => $id))) {
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
ecc8403e 51
cd4e6b17 52unset($SESSION->fromdiscussion);
f93f848a 53
cd4e6b17 54add_to_log($course->id, 'forum', 'view forums', "index.php?id=$course->id");
2f3f505f 55
cd4e6b17 56$strforums = get_string('forums', 'forum');
57$strforum = get_string('forum', 'forum');
58$strdescription = get_string('description');
59$strdiscussions = get_string('discussions', 'forum');
60$strsubscribed = get_string('subscribed', 'forum');
61$strunreadposts = get_string('unreadposts', 'forum');
62$strtracking = get_string('tracking', 'forum');
63$strmarkallread = get_string('markallread', 'forum');
64$strtrackforum = get_string('trackforum', 'forum');
65$strnotrackforum = get_string('notrackforum', 'forum');
66$strsubscribe = get_string('subscribe', 'forum');
67$strunsubscribe = get_string('unsubscribe', 'forum');
68$stryes = get_string('yes');
69$strno = get_string('no');
70$strrss = get_string('rss');
8e08c731 71$stremaildigest = get_string('emaildigest');
97485d07 72
cd4e6b17 73$searchform = forum_search_form($course);
f93f848a 74
8e08c731
AN
75// Retrieve the list of forum digest options for later.
76$digestoptions = forum_get_user_digest_options();
77$digestoptions_selector = new single_select(new moodle_url('/mod/forum/maildigest.php',
78 array(
79 'backtoindex' => 1,
80 )),
81 'maildigest',
82 $digestoptions,
83 null,
84 '');
85$digestoptions_selector->method = 'post';
f781b794 86
cd4e6b17 87// Start of the table for General Forums
f781b794 88
cd4e6b17 89$generaltable = new html_table();
90$generaltable->head = array ($strforum, $strdescription, $strdiscussions);
91$generaltable->align = array ('left', 'left', 'center');
f37da850 92
cd4e6b17 93if ($usetracking = forum_tp_can_track_forums()) {
94 $untracked = forum_tp_get_untracked_forums($USER->id, $course->id);
c2468824 95
cd4e6b17 96 $generaltable->head[] = $strunreadposts;
97 $generaltable->align[] = 'center';
38994d6f 98
cd4e6b17 99 $generaltable->head[] = $strtracking;
100 $generaltable->align[] = 'center';
101}
f93f848a 102
cd4e6b17 103$subscribed_forums = forum_get_subscribed_forums($course);
104
1cd8c399
AD
105$can_subscribe = is_enrolled($coursecontext);
106if ($can_subscribe) {
cd4e6b17 107 $generaltable->head[] = $strsubscribed;
108 $generaltable->align[] = 'center';
8e08c731
AN
109
110 $generaltable->head[] = $stremaildigest . ' ' . $OUTPUT->help_icon('emaildigesttype', 'mod_forum');
111 $generaltable->align[] = 'center';
cd4e6b17 112}
113
114if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
115 isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
116 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
117 $generaltable->head[] = $strrss;
118 $generaltable->align[] = 'center';
119}
90f4745c 120
7487c856 121$usesections = course_format_uses_sections($course->format);
7487c856
SH
122
123$table = new html_table();
f93f848a 124
cd4e6b17 125// Parse and organise all the forums. Most forums are course modules but
126// some special ones are not. These get placed in the general forums
127// category with the forums in section 0.
128
8e08c731
AN
129$forums = $DB->get_records_sql("
130 SELECT f.*,
131 d.maildigest
132 FROM {forum} f
133 LEFT JOIN {forum_digests} d ON d.forum = f.id AND d.userid = ?
134 WHERE f.course = ?
135 ", array($USER->id, $course->id));
cd4e6b17 136
137$generalforums = array();
138$learningforums = array();
f20edd52 139$modinfo = get_fast_modinfo($course);
cd4e6b17 140
44ddd2a1 141foreach ($modinfo->get_instances_of('forum') as $forumid=>$cm) {
cd4e6b17 142 if (!$cm->uservisible or !isset($forums[$forumid])) {
143 continue;
33f48f03 144 }
145
cd4e6b17 146 $forum = $forums[$forumid];
fa5a5b52 147
0d4d49d1 148 if (!$context = context_module::instance($cm->id, IGNORE_MISSING)) {
cd4e6b17 149 continue; // Shouldn't happen
150 }
151
152 if (!has_capability('mod/forum:viewdiscussion', $context)) {
153 continue;
154 }
ecc8403e 155
cd4e6b17 156 // fill two type array - order in modinfo is the same as in course
157 if ($forum->type == 'news' or $forum->type == 'social') {
158 $generalforums[$forum->id] = $forum;
dd97c328 159
cd4e6b17 160 } else if ($course->id == SITEID or empty($cm->sectionnum)) {
161 $generalforums[$forum->id] = $forum;
ecc8403e 162
cd4e6b17 163 } else {
164 $learningforums[$forum->id] = $forum;
2f3f505f 165 }
cd4e6b17 166}
2f3f505f 167
b688c449
EL
168// Do course wide subscribe/unsubscribe if requested
169if (!is_null($subscribe)) {
170 if (isguestuser() or !$can_subscribe) {
171 // there should not be any links leading to this place, just redirect
172 redirect(new moodle_url('/mod/forum/index.php', array('id' => $id)), get_string('subscribeenrolledonly', 'forum'));
173 }
174 // Can proceed now, the user is not guest and is enrolled
44ddd2a1 175 foreach ($modinfo->get_instances_of('forum') as $forumid=>$cm) {
2f3f505f 176 $forum = $forums[$forumid];
bf0f06b1 177 $modcontext = context_module::instance($cm->id);
cd4e6b17 178 $cansub = false;
dd97c328 179
cd4e6b17 180 if (has_capability('mod/forum:viewdiscussion', $modcontext)) {
181 $cansub = true;
2f3f505f 182 }
cd4e6b17 183 if ($cansub && $cm->visible == 0 &&
184 !has_capability('mod/forum:managesubscriptions', $modcontext))
185 {
186 $cansub = false;
2f3f505f 187 }
cd4e6b17 188 if (!forum_is_forcesubscribed($forum)) {
189 $subscribed = forum_is_subscribed($USER->id, $forum);
190 if ((has_capability('moodle/course:manageactivities', $coursecontext, $USER->id) || $forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE) && $subscribe && !$subscribed && $cansub) {
191 forum_subscribe($USER->id, $forumid);
192 } else if (!$subscribe && $subscribed) {
193 forum_unsubscribe($USER->id, $forumid);
194 }
195 }
196 }
197 $returnto = forum_go_back_to("index.php?id=$course->id");
bf0f06b1 198 $shortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
cd4e6b17 199 if ($subscribe) {
200 add_to_log($course->id, 'forum', 'subscribeall', "index.php?id=$course->id", $course->id);
8ebbb06a 201 redirect($returnto, get_string('nowallsubscribed', 'forum', $shortname), 1);
cd4e6b17 202 } else {
203 add_to_log($course->id, 'forum', 'unsubscribeall', "index.php?id=$course->id", $course->id);
8ebbb06a 204 redirect($returnto, get_string('nowallunsubscribed', 'forum', $shortname), 1);
cd4e6b17 205 }
206}
dd97c328 207
cd4e6b17 208/// First, let's process the general forums and build up a display
209
210if ($generalforums) {
211 foreach ($generalforums as $forum) {
212 $cm = $modinfo->instances['forum'][$forum->id];
bf0f06b1 213 $context = context_module::instance($cm->id);
cd4e6b17 214
215 $count = forum_count_discussions($forum, $cm, $course);
216
217 if ($usetracking) {
218 if ($forum->trackingtype == FORUM_TRACKING_OFF) {
219 $unreadlink = '-';
220 $trackedlink = '-';
221
222 } else {
223 if (isset($untracked[$forum->id])) {
224 $unreadlink = '-';
225 } else if ($unread = forum_tp_count_forum_unread_posts($cm, $course)) {
226 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
227 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
99938034 228 $forum->id.'&amp;mark=read"><img src="'.$OUTPUT->pix_url('t/markasread') . '" alt="'.$strmarkallread.'" class="iconsmall" /></a></span>';
cd4e6b17 229 } else {
230 $unreadlink = '<span class="read">0</span>';
231 }
2f3f505f 232
cd4e6b17 233 if ($forum->trackingtype == FORUM_TRACKING_ON) {
234 $trackedlink = $stryes;
2f3f505f 235
cd4e6b17 236 } else {
a6855934 237 $aurl = new moodle_url('/mod/forum/settracking.php', array('id'=>$forum->id));
cd4e6b17 238 if (!isset($untracked[$forum->id])) {
5c2ed7e2 239 $trackedlink = $OUTPUT->single_button($aurl, $stryes, 'post', array('title'=>$strnotrackforum));
cd4e6b17 240 } else {
5c2ed7e2 241 $trackedlink = $OUTPUT->single_button($aurl, $strno, 'post', array('title'=>$strtrackforum));
cd4e6b17 242 }
243 }
244 }
245 }
246
247 $forum->intro = shorten_text(format_module_intro('forum', $forum, $cm->id), $CFG->forum_shortpost);
0e35ba6f 248 $forumname = format_string($forum->name, true);
cd4e6b17 249
250 if ($cm->visible) {
251 $style = '';
2f3f505f 252 } else {
cd4e6b17 253 $style = 'class="dimmed"';
37b15514 254 }
cd4e6b17 255 $forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name,true)."</a>";
256 $discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
702dc57b 257
cd4e6b17 258 $row = array ($forumlink, $forum->intro, $discussionlink);
259 if ($usetracking) {
260 $row[] = $unreadlink;
261 $row[] = $trackedlink; // Tracking.
262 }
3b8b9b6a 263
cd4e6b17 264 if ($can_subscribe) {
265 if ($forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE) {
266 $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
267 'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
268 'cantsubscribe' => '-'), false, false, true, $subscribed_forums);
269 } else {
270 $row[] = '-';
2c1363e6 271 }
8e08c731
AN
272
273 $digestoptions_selector->url->param('id', $forum->id);
274 if ($forum->maildigest === null) {
275 $digestoptions_selector->selected = -1;
276 } else {
277 $digestoptions_selector->selected = $forum->maildigest;
278 }
279 $row[] = $OUTPUT->render($digestoptions_selector);
cd4e6b17 280 }
281
282 //If this forum has RSS activated, calculate it
283 if ($show_rss) {
284 if ($forum->rsstype and $forum->rssarticles) {
9e86f2e7 285 //Calculate the tooltip text
cd4e6b17 286 if ($forum->rsstype == 1) {
9e86f2e7 287 $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum');
cd4e6b17 288 } else {
9e86f2e7 289 $tooltiptext = get_string('rsssubscriberssposts', 'forum');
03877b04 290 }
dbf4433d
RW
291
292 if (!isloggedin() && $course->id == SITEID) {
293 $userid = guest_user()->id;
294 } else {
295 $userid = $USER->id;
296 }
cd4e6b17 297 //Get html code for RSS link
dbf4433d 298 $row[] = rss_get_link($context->id, $userid, 'mod_forum', $forum->id, $tooltiptext);
cd4e6b17 299 } else {
300 $row[] = '&nbsp;';
03877b04 301 }
302 }
cd4e6b17 303
304 $generaltable->data[] = $row;
305 }
306}
307
308
309// Start of the table for Learning Forums
310$learningtable = new html_table();
311$learningtable->head = array ($strforum, $strdescription, $strdiscussions);
312$learningtable->align = array ('left', 'left', 'center');
313
314if ($usetracking) {
315 $learningtable->head[] = $strunreadposts;
316 $learningtable->align[] = 'center';
317
318 $learningtable->head[] = $strtracking;
319 $learningtable->align[] = 'center';
320}
321
322if ($can_subscribe) {
323 $learningtable->head[] = $strsubscribed;
324 $learningtable->align[] = 'center';
8e08c731
AN
325
326 $learningtable->head[] = $stremaildigest . ' ' . $OUTPUT->help_icon('emaildigesttype', 'mod_forum');
327 $learningtable->align[] = 'center';
cd4e6b17 328}
329
330if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
331 isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
332 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
333 $learningtable->head[] = $strrss;
334 $learningtable->align[] = 'center';
335}
336
337/// Now let's process the learning forums
338
339if ($course->id != SITEID) { // Only real courses have learning forums
4202a917 340 // 'format_.'$course->format only applicable when not SITEID (format_site is not a format)
b489d37d 341 $strsectionname = get_string('sectionname', 'format_'.$course->format);
cd4e6b17 342 // Add extra field for section number, at the front
7487c856 343 array_unshift($learningtable->head, $strsectionname);
cd4e6b17 344 array_unshift($learningtable->align, 'center');
03877b04 345
ecc8403e 346
cd4e6b17 347 if ($learningforums) {
348 $currentsection = '';
349 foreach ($learningforums as $forum) {
2f3f505f 350 $cm = $modinfo->instances['forum'][$forum->id];
bf0f06b1 351 $context = context_module::instance($cm->id);
dd97c328 352
90f4745c 353 $count = forum_count_discussions($forum, $cm, $course);
8f0cd6ef 354
eaf50aef 355 if ($usetracking) {
a90e92a5 356 if ($forum->trackingtype == FORUM_TRACKING_OFF) {
357 $unreadlink = '-';
358 $trackedlink = '-';
90f4745c 359
360 } else {
361 if (isset($untracked[$forum->id])) {
cd4e6b17 362 $unreadlink = '-';
90f4745c 363 } else if ($unread = forum_tp_count_forum_unread_posts($cm, $course)) {
cd4e6b17 364 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
c39748f4 365 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
99938034 366 $forum->id.'&amp;mark=read"><img src="'.$OUTPUT->pix_url('t/markasread') . '" alt="'.$strmarkallread.'" class="iconsmall" /></a></span>';
38994d6f 367 } else {
90f4745c 368 $unreadlink = '<span class="read">0</span>';
38994d6f 369 }
630bd96b 370
90f4745c 371 if ($forum->trackingtype == FORUM_TRACKING_ON) {
372 $trackedlink = $stryes;
bbbf2d40 373
a90e92a5 374 } else {
a6855934 375 $aurl = new moodle_url('/mod/forum/settracking.php', array('id'=>$forum->id));
90f4745c 376 if (!isset($untracked[$forum->id])) {
5c2ed7e2 377 $trackedlink = $OUTPUT->single_button($aurl, $stryes, 'post', array('title'=>$strnotrackforum));
90f4745c 378 } else {
5c2ed7e2 379 $trackedlink = $OUTPUT->single_button($aurl, $strno, 'post', array('title'=>$strtrackforum));
90f4745c 380 }
630bd96b 381 }
f37da850 382 }
383 }
384
4ba2221a 385 $forum->intro = shorten_text(format_module_intro('forum', $forum, $cm->id), $CFG->forum_shortpost);
cd4e6b17 386
387 if ($cm->sectionnum != $currentsection) {
71a56e08 388 $printsection = get_section_name($course, $cm->sectionnum);
cd4e6b17 389 if ($currentsection) {
390 $learningtable->data[] = 'hr';
391 }
392 $currentsection = $cm->sectionnum;
393 } else {
394 $printsection = '';
395 }
396
0e35ba6f 397 $forumname = format_string($forum->name,true);
ecc8403e 398
90f4745c 399 if ($cm->visible) {
400 $style = '';
ecc8403e 401 } else {
90f4745c 402 $style = 'class="dimmed"';
ecc8403e 403 }
90f4745c 404 $forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name,true)."</a>";
405 $discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
ecc8403e 406
cd4e6b17 407 $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
49b53f26 408 if ($usetracking) {
409 $row[] = $unreadlink;
410 $row[] = $trackedlink; // Tracking.
f4646b44 411 }
412
ecc8403e 413 if ($can_subscribe) {
90f4745c 414 if ($forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE) {
415 $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
cd4e6b17 416 'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
417 'cantsubscribe' => '-'), false, false, true, $subscribed_forums);
90f4745c 418 } else {
419 $row[] = '-';
420 }
8e08c731
AN
421
422 $digestoptions_selector->url->param('id', $forum->id);
423 if ($forum->maildigest === null) {
424 $digestoptions_selector->selected = -1;
425 } else {
426 $digestoptions_selector->selected = $forum->maildigest;
427 }
428 $row[] = $OUTPUT->render($digestoptions_selector);
49b53f26 429 }
430
431 //If this forum has RSS activated, calculate it
90f4745c 432 if ($show_rss) {
433 if ($forum->rsstype and $forum->rssarticles) {
434 //Calculate the tolltip text
435 if ($forum->rsstype == 1) {
9e86f2e7 436 $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum');
90f4745c 437 } else {
9e86f2e7 438 $tooltiptext = get_string('rsssubscriberssposts', 'forum');
90f4745c 439 }
440 //Get html code for RSS link
aa60291e 441 $row[] = rss_get_link($context->id, $USER->id, 'mod_forum', $forum->id, $tooltiptext);
49b53f26 442 } else {
90f4745c 443 $row[] = '&nbsp;';
f4646b44 444 }
ecc8403e 445 }
49b53f26 446
cd4e6b17 447 $learningtable->data[] = $row;
ecc8403e 448 }
8f0cd6ef 449 }
cd4e6b17 450}
451
452
453/// Output the page
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
b688c449
EL
460// Show the subscribe all options only to non-guest, enrolled users
461if (!isguestuser() && isloggedin() && $can_subscribe) {
cd4e6b17 462 echo $OUTPUT->box_start('subscription');
c56ed7bb
DM
463 echo html_writer::tag('div',
464 html_writer::link(new moodle_url('/mod/forum/index.php', array('id'=>$course->id, 'subscribe'=>1, 'sesskey'=>sesskey())),
465 get_string('allsubscribe', 'forum')),
466 array('class'=>'helplink'));
467 echo html_writer::tag('div',
468 html_writer::link(new moodle_url('/mod/forum/index.php', array('id'=>$course->id, 'subscribe'=>0, 'sesskey'=>sesskey())),
469 get_string('allunsubscribe', 'forum')),
470 array('class'=>'helplink'));
cd4e6b17 471 echo $OUTPUT->box_end();
472 echo $OUTPUT->box('&nbsp;', 'clearer');
473}
474
475if ($generalforums) {
66e2b9f8 476 echo $OUTPUT->heading(get_string('generalforums', 'forum'), 2);
16be8974 477 echo html_writer::table($generaltable);
cd4e6b17 478}
479
480if ($learningforums) {
66e2b9f8 481 echo $OUTPUT->heading(get_string('learningforums', 'forum'), 2);
16be8974 482 echo html_writer::table($learningtable);
cd4e6b17 483}
484
485echo $OUTPUT->footer();
486