MDL-41219 course: Make properties of course_modinfo read-only
[moodle.git] / mod / forum / index.php
1 <?php
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/>.
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  */
24 require_once(dirname(__FILE__) . '/../../config.php');
25 require_once($CFG->dirroot . '/course/lib.php');
26 require_once($CFG->dirroot . '/mod/forum/lib.php');
27 require_once($CFG->libdir . '/rsslib.php');
29 $id = optional_param('id', 0, PARAM_INT);                   // Course id
30 $subscribe = optional_param('subscribe', null, PARAM_INT);  // Subscribe/Unsubscribe all forums
32 $url = new moodle_url('/mod/forum/index.php', array('id'=>$id));
33 if ($subscribe !== null) {
34     require_sesskey();
35     $url->param('subscribe', $subscribe);
36 }
37 $PAGE->set_url($url);
39 if ($id) {
40     if (! $course = $DB->get_record('course', array('id' => $id))) {
41         print_error('invalidcourseid');
42     }
43 } else {
44     $course = get_site();
45 }
47 require_course_login($course);
48 $PAGE->set_pagelayout('incourse');
49 $coursecontext = context_course::instance($course->id);
52 unset($SESSION->fromdiscussion);
54 add_to_log($course->id, 'forum', 'view forums', "index.php?id=$course->id");
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');
71 $stremaildigest  = get_string('emaildigest');
73 $searchform = forum_search_form($course);
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';
87 // Start of the table for General Forums
89 $generaltable = new html_table();
90 $generaltable->head  = array ($strforum, $strdescription, $strdiscussions);
91 $generaltable->align = array ('left', 'left', 'center');
93 if ($usetracking = forum_tp_can_track_forums()) {
94     $untracked = forum_tp_get_untracked_forums($USER->id, $course->id);
96     $generaltable->head[] = $strunreadposts;
97     $generaltable->align[] = 'center';
99     $generaltable->head[] = $strtracking;
100     $generaltable->align[] = 'center';
103 $subscribed_forums = forum_get_subscribed_forums($course);
105 $can_subscribe = is_enrolled($coursecontext);
106 if ($can_subscribe) {
107     $generaltable->head[] = $strsubscribed;
108     $generaltable->align[] = 'center';
110     $generaltable->head[] = $stremaildigest . ' ' . $OUTPUT->help_icon('emaildigesttype', 'mod_forum');
111     $generaltable->align[] = 'center';
114 if ($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';
121 $usesections = course_format_uses_sections($course->format);
123 $table = new html_table();
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.
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));
137 $generalforums  = array();
138 $learningforums = array();
139 $modinfo = get_fast_modinfo($course);
141 foreach ($modinfo->get_instances_of('forum') as $forumid=>$cm) {
142     if (!$cm->uservisible or !isset($forums[$forumid])) {
143         continue;
144     }
146     $forum = $forums[$forumid];
148     if (!$context = context_module::instance($cm->id, IGNORE_MISSING)) {
149         continue;   // Shouldn't happen
150     }
152     if (!has_capability('mod/forum:viewdiscussion', $context)) {
153         continue;
154     }
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;
160     } else if ($course->id == SITEID or empty($cm->sectionnum)) {
161         $generalforums[$forum->id] = $forum;
163     } else {
164         $learningforums[$forum->id] = $forum;
165     }
168 // Do course wide subscribe/unsubscribe if requested
169 if (!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
175     foreach ($modinfo->get_instances_of('forum') as $forumid=>$cm) {
176         $forum = $forums[$forumid];
177         $modcontext = context_module::instance($cm->id);
178         $cansub = false;
180         if (has_capability('mod/forum:viewdiscussion', $modcontext)) {
181             $cansub = true;
182         }
183         if ($cansub && $cm->visible == 0 &&
184             !has_capability('mod/forum:managesubscriptions', $modcontext))
185         {
186             $cansub = false;
187         }
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");
198     $shortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
199     if ($subscribe) {
200         add_to_log($course->id, 'forum', 'subscribeall', "index.php?id=$course->id", $course->id);
201         redirect($returnto, get_string('nowallsubscribed', 'forum', $shortname), 1);
202     } else {
203         add_to_log($course->id, 'forum', 'unsubscribeall', "index.php?id=$course->id", $course->id);
204         redirect($returnto, get_string('nowallunsubscribed', 'forum', $shortname), 1);
205     }
208 /// First, let's process the general forums and build up a display
210 if ($generalforums) {
211     foreach ($generalforums as $forum) {
212         $cm      = $modinfo->instances['forum'][$forum->id];
213         $context = context_module::instance($cm->id);
215         $count = forum_count_discussions($forum, $cm, $course);
217         if ($usetracking) {
218             if ($forum->trackingtype == FORUM_TRACKING_OFF) {
219                 $unreadlink  = '-';
220                 $trackedlink = '-';
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='.
228                                    $forum->id.'&amp;mark=read"><img src="'.$OUTPUT->pix_url('t/markasread') . '" alt="'.$strmarkallread.'" class="iconsmall" /></a></span>';
229                 } else {
230                     $unreadlink = '<span class="read">0</span>';
231                 }
233                 if ($forum->trackingtype == FORUM_TRACKING_ON) {
234                     $trackedlink = $stryes;
236                 } else {
237                     $aurl = new moodle_url('/mod/forum/settracking.php', array('id'=>$forum->id));
238                     if (!isset($untracked[$forum->id])) {
239                         $trackedlink = $OUTPUT->single_button($aurl, $stryes, 'post', array('title'=>$strnotrackforum));
240                     } else {
241                         $trackedlink = $OUTPUT->single_button($aurl, $strno, 'post', array('title'=>$strtrackforum));
242                     }
243                 }
244             }
245         }
247         $forum->intro = shorten_text(format_module_intro('forum', $forum, $cm->id), $CFG->forum_shortpost);
248         $forumname = format_string($forum->name, true);
250         if ($cm->visible) {
251             $style = '';
252         } else {
253             $style = 'class="dimmed"';
254         }
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>";
258         $row = array ($forumlink, $forum->intro, $discussionlink);
259         if ($usetracking) {
260             $row[] = $unreadlink;
261             $row[] = $trackedlink;    // Tracking.
262         }
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[] = '-';
271             }
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);
280         }
282         //If this forum has RSS activated, calculate it
283         if ($show_rss) {
284             if ($forum->rsstype and $forum->rssarticles) {
285                 //Calculate the tooltip text
286                 if ($forum->rsstype == 1) {
287                     $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum');
288                 } else {
289                     $tooltiptext = get_string('rsssubscriberssposts', 'forum');
290                 }
292                 if (!isloggedin() && $course->id == SITEID) {
293                     $userid = guest_user()->id;
294                 } else {
295                     $userid = $USER->id;
296                 }
297                 //Get html code for RSS link
298                 $row[] = rss_get_link($context->id, $userid, 'mod_forum', $forum->id, $tooltiptext);
299             } else {
300                 $row[] = '&nbsp;';
301             }
302         }
304         $generaltable->data[] = $row;
305     }
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');
314 if ($usetracking) {
315     $learningtable->head[] = $strunreadposts;
316     $learningtable->align[] = 'center';
318     $learningtable->head[] = $strtracking;
319     $learningtable->align[] = 'center';
322 if ($can_subscribe) {
323     $learningtable->head[] = $strsubscribed;
324     $learningtable->align[] = 'center';
326     $learningtable->head[] = $stremaildigest . ' ' . $OUTPUT->help_icon('emaildigesttype', 'mod_forum');
327     $learningtable->align[] = 'center';
330 if ($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';
337 /// Now let's process the learning forums
339 if ($course->id != SITEID) {    // Only real courses have learning forums
340     // 'format_.'$course->format only applicable when not SITEID (format_site is not a format)
341     $strsectionname  = get_string('sectionname', 'format_'.$course->format);
342     // Add extra field for section number, at the front
343     array_unshift($learningtable->head, $strsectionname);
344     array_unshift($learningtable->align, 'center');
347     if ($learningforums) {
348         $currentsection = '';
349             foreach ($learningforums as $forum) {
350             $cm      = $modinfo->instances['forum'][$forum->id];
351             $context = context_module::instance($cm->id);
353             $count = forum_count_discussions($forum, $cm, $course);
355             if ($usetracking) {
356                 if ($forum->trackingtype == FORUM_TRACKING_OFF) {
357                     $unreadlink  = '-';
358                     $trackedlink = '-';
360                 } else {
361                     if (isset($untracked[$forum->id])) {
362                         $unreadlink  = '-';
363                     } else if ($unread = forum_tp_count_forum_unread_posts($cm, $course)) {
364                         $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
365                         $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
366                                        $forum->id.'&amp;mark=read"><img src="'.$OUTPUT->pix_url('t/markasread') . '" alt="'.$strmarkallread.'" class="iconsmall" /></a></span>';
367                     } else {
368                         $unreadlink = '<span class="read">0</span>';
369                     }
371                     if ($forum->trackingtype == FORUM_TRACKING_ON) {
372                         $trackedlink = $stryes;
374                     } else {
375                         $aurl = new moodle_url('/mod/forum/settracking.php', array('id'=>$forum->id));
376                         if (!isset($untracked[$forum->id])) {
377                             $trackedlink = $OUTPUT->single_button($aurl, $stryes, 'post', array('title'=>$strnotrackforum));
378                         } else {
379                             $trackedlink = $OUTPUT->single_button($aurl, $strno, 'post', array('title'=>$strtrackforum));
380                         }
381                     }
382                 }
383             }
385             $forum->intro = shorten_text(format_module_intro('forum', $forum, $cm->id), $CFG->forum_shortpost);
387             if ($cm->sectionnum != $currentsection) {
388                 $printsection = get_section_name($course, $cm->sectionnum);
389                 if ($currentsection) {
390                     $learningtable->data[] = 'hr';
391                 }
392                 $currentsection = $cm->sectionnum;
393             } else {
394                 $printsection = '';
395             }
397             $forumname = format_string($forum->name,true);
399             if ($cm->visible) {
400                 $style = '';
401             } else {
402                 $style = 'class="dimmed"';
403             }
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>";
407             $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
408             if ($usetracking) {
409                 $row[] = $unreadlink;
410                 $row[] = $trackedlink;    // Tracking.
411             }
413             if ($can_subscribe) {
414                 if ($forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE) {
415                     $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
416                         'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
417                         'cantsubscribe' => '-'), false, false, true, $subscribed_forums);
418                 } else {
419                     $row[] = '-';
420                 }
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);
429             }
431             //If this forum has RSS activated, calculate it
432             if ($show_rss) {
433                 if ($forum->rsstype and $forum->rssarticles) {
434                     //Calculate the tolltip text
435                     if ($forum->rsstype == 1) {
436                         $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum');
437                     } else {
438                         $tooltiptext = get_string('rsssubscriberssposts', 'forum');
439                     }
440                     //Get html code for RSS link
441                     $row[] = rss_get_link($context->id, $USER->id, 'mod_forum', $forum->id, $tooltiptext);
442                 } else {
443                     $row[] = '&nbsp;';
444                 }
445             }
447             $learningtable->data[] = $row;
448         }
449     }
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);
458 echo $OUTPUT->header();
460 // Show the subscribe all options only to non-guest, enrolled users
461 if (!isguestuser() && isloggedin() && $can_subscribe) {
462     echo $OUTPUT->box_start('subscription');
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'));
471     echo $OUTPUT->box_end();
472     echo $OUTPUT->box('&nbsp;', 'clearer');
475 if ($generalforums) {
476     echo $OUTPUT->heading(get_string('generalforums', 'forum'), 2);
477     echo html_writer::table($generaltable);
480 if ($learningforums) {
481     echo $OUTPUT->heading(get_string('learningforums', 'forum'), 2);
482     echo html_writer::table($learningtable);
485 echo $OUTPUT->footer();