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