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