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