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