Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/forum/index.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00024 require_once(dirname(__FILE__) . '/../../config.php');
00025 require_once($CFG->dirroot . '/course/lib.php');
00026 require_once($CFG->dirroot . '/mod/forum/lib.php');
00027 require_once($CFG->libdir . '/rsslib.php');
00028 
00029 $id = optional_param('id', 0, PARAM_INT);                   // Course id
00030 $subscribe = optional_param('subscribe', null, PARAM_INT);  // Subscribe/Unsubscribe all forums
00031 
00032 $url = new moodle_url('/mod/forum/index.php', array('id'=>$id));
00033 if ($subscribe !== null) {
00034     require_sesskey();
00035     $url->param('subscribe', $subscribe);
00036 }
00037 $PAGE->set_url($url);
00038 
00039 if ($id) {
00040     if (! $course = $DB->get_record('course', array('id' => $id))) {
00041         print_error('invalidcourseid');
00042     }
00043 } else {
00044     $course = get_site();
00045 }
00046 
00047 require_course_login($course);
00048 $PAGE->set_pagelayout('incourse');
00049 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
00050 
00051 
00052 unset($SESSION->fromdiscussion);
00053 
00054 add_to_log($course->id, 'forum', 'view forums', "index.php?id=$course->id");
00055 
00056 $strforums       = get_string('forums', 'forum');
00057 $strforum        = get_string('forum', 'forum');
00058 $strdescription  = get_string('description');
00059 $strdiscussions  = get_string('discussions', 'forum');
00060 $strsubscribed   = get_string('subscribed', 'forum');
00061 $strunreadposts  = get_string('unreadposts', 'forum');
00062 $strtracking     = get_string('tracking', 'forum');
00063 $strmarkallread  = get_string('markallread', 'forum');
00064 $strtrackforum   = get_string('trackforum', 'forum');
00065 $strnotrackforum = get_string('notrackforum', 'forum');
00066 $strsubscribe    = get_string('subscribe', 'forum');
00067 $strunsubscribe  = get_string('unsubscribe', 'forum');
00068 $stryes          = get_string('yes');
00069 $strno           = get_string('no');
00070 $strrss          = get_string('rss');
00071 
00072 $searchform = forum_search_form($course);
00073 
00074 
00075 // Start of the table for General Forums
00076 
00077 $generaltable = new html_table();
00078 $generaltable->head  = array ($strforum, $strdescription, $strdiscussions);
00079 $generaltable->align = array ('left', 'left', 'center');
00080 
00081 if ($usetracking = forum_tp_can_track_forums()) {
00082     $untracked = forum_tp_get_untracked_forums($USER->id, $course->id);
00083 
00084     $generaltable->head[] = $strunreadposts;
00085     $generaltable->align[] = 'center';
00086 
00087     $generaltable->head[] = $strtracking;
00088     $generaltable->align[] = 'center';
00089 }
00090 
00091 $subscribed_forums = forum_get_subscribed_forums($course);
00092 
00093 $can_subscribe = is_enrolled($coursecontext);
00094 if ($can_subscribe) {
00095     $generaltable->head[] = $strsubscribed;
00096     $generaltable->align[] = 'center';
00097 }
00098 
00099 if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
00100                  isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
00101                  $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
00102     $generaltable->head[] = $strrss;
00103     $generaltable->align[] = 'center';
00104 }
00105 
00106 $usesections = course_format_uses_sections($course->format);
00107 $sections = get_all_sections($course->id);
00108 
00109 $table = new html_table();
00110 
00111 // Parse and organise all the forums.  Most forums are course modules but
00112 // some special ones are not.  These get placed in the general forums
00113 // category with the forums in section 0.
00114 
00115 $forums = $DB->get_records('forum', array('course' => $course->id));
00116 
00117 $generalforums  = array();
00118 $learningforums = array();
00119 $modinfo =& get_fast_modinfo($course);
00120 
00121 if (!isset($modinfo->instances['forum'])) {
00122     $modinfo->instances['forum'] = array();
00123 }
00124 
00125 foreach ($modinfo->instances['forum'] as $forumid=>$cm) {
00126     if (!$cm->uservisible or !isset($forums[$forumid])) {
00127         continue;
00128     }
00129 
00130     $forum = $forums[$forumid];
00131 
00132     if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
00133         continue;   // Shouldn't happen
00134     }
00135 
00136     if (!has_capability('mod/forum:viewdiscussion', $context)) {
00137         continue;
00138     }
00139 
00140     // fill two type array - order in modinfo is the same as in course
00141     if ($forum->type == 'news' or $forum->type == 'social') {
00142         $generalforums[$forum->id] = $forum;
00143 
00144     } else if ($course->id == SITEID or empty($cm->sectionnum)) {
00145         $generalforums[$forum->id] = $forum;
00146 
00147     } else {
00148         $learningforums[$forum->id] = $forum;
00149     }
00150 }
00151 
00152 // Do course wide subscribe/unsubscribe if requested
00153 if (!is_null($subscribe)) {
00154     if (isguestuser() or !$can_subscribe) {
00155         // there should not be any links leading to this place, just redirect
00156         redirect(new moodle_url('/mod/forum/index.php', array('id' => $id)), get_string('subscribeenrolledonly', 'forum'));
00157     }
00158     // Can proceed now, the user is not guest and is enrolled
00159     foreach ($modinfo->instances['forum'] as $forumid=>$cm) {
00160         $forum = $forums[$forumid];
00161         $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
00162         $cansub = false;
00163 
00164         if (has_capability('mod/forum:viewdiscussion', $modcontext)) {
00165             $cansub = true;
00166         }
00167         if ($cansub && $cm->visible == 0 &&
00168             !has_capability('mod/forum:managesubscriptions', $modcontext))
00169         {
00170             $cansub = false;
00171         }
00172         if (!forum_is_forcesubscribed($forum)) {
00173             $subscribed = forum_is_subscribed($USER->id, $forum);
00174             if ((has_capability('moodle/course:manageactivities', $coursecontext, $USER->id) || $forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE) && $subscribe && !$subscribed && $cansub) {
00175                 forum_subscribe($USER->id, $forumid);
00176             } else if (!$subscribe && $subscribed) {
00177                 forum_unsubscribe($USER->id, $forumid);
00178             }
00179         }
00180     }
00181     $returnto = forum_go_back_to("index.php?id=$course->id");
00182     $shortname = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id)));
00183     if ($subscribe) {
00184         add_to_log($course->id, 'forum', 'subscribeall', "index.php?id=$course->id", $course->id);
00185         redirect($returnto, get_string('nowallsubscribed', 'forum', $shortname), 1);
00186     } else {
00187         add_to_log($course->id, 'forum', 'unsubscribeall', "index.php?id=$course->id", $course->id);
00188         redirect($returnto, get_string('nowallunsubscribed', 'forum', $shortname), 1);
00189     }
00190 }
00191 
00193 
00194 if ($generalforums) {
00195     foreach ($generalforums as $forum) {
00196         $cm      = $modinfo->instances['forum'][$forum->id];
00197         $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00198 
00199         $count = forum_count_discussions($forum, $cm, $course);
00200 
00201         if ($usetracking) {
00202             if ($forum->trackingtype == FORUM_TRACKING_OFF) {
00203                 $unreadlink  = '-';
00204                 $trackedlink = '-';
00205 
00206             } else {
00207                 if (isset($untracked[$forum->id])) {
00208                         $unreadlink  = '-';
00209                 } else if ($unread = forum_tp_count_forum_unread_posts($cm, $course)) {
00210                         $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
00211                     $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
00212                                    $forum->id.'&amp;mark=read"><img src="'.$OUTPUT->pix_url('t/clear') . '" alt="'.$strmarkallread.'" /></a></span>';
00213                 } else {
00214                     $unreadlink = '<span class="read">0</span>';
00215                 }
00216 
00217                 if ($forum->trackingtype == FORUM_TRACKING_ON) {
00218                     $trackedlink = $stryes;
00219 
00220                 } else {
00221                     $aurl = new moodle_url('/mod/forum/settracking.php', array('id'=>$forum->id));
00222                     if (!isset($untracked[$forum->id])) {
00223                         $trackedlink = $OUTPUT->single_button($aurl, $stryes, 'post', array('title'=>$strnotrackforum));
00224                     } else {
00225                         $trackedlink = $OUTPUT->single_button($aurl, $strno, 'post', array('title'=>$strtrackforum));
00226                     }
00227                 }
00228             }
00229         }
00230 
00231         $forum->intro = shorten_text(format_module_intro('forum', $forum, $cm->id), $CFG->forum_shortpost);
00232         $forumname = format_string($forum->name, true);;
00233 
00234         if ($cm->visible) {
00235             $style = '';
00236         } else {
00237             $style = 'class="dimmed"';
00238         }
00239         $forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name,true)."</a>";
00240         $discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
00241 
00242         $row = array ($forumlink, $forum->intro, $discussionlink);
00243         if ($usetracking) {
00244             $row[] = $unreadlink;
00245             $row[] = $trackedlink;    // Tracking.
00246         }
00247 
00248         if ($can_subscribe) {
00249             if ($forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE) {
00250                 $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
00251                         'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
00252                         'cantsubscribe' => '-'), false, false, true, $subscribed_forums);
00253             } else {
00254                 $row[] = '-';
00255             }
00256         }
00257 
00258         //If this forum has RSS activated, calculate it
00259         if ($show_rss) {
00260             if ($forum->rsstype and $forum->rssarticles) {
00261                 //Calculate the tooltip text
00262                 if ($forum->rsstype == 1) {
00263                     $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum');
00264                 } else {
00265                     $tooltiptext = get_string('rsssubscriberssposts', 'forum');
00266                 }
00267 
00268                 if (!isloggedin() && $course->id == SITEID) {
00269                     $userid = guest_user()->id;
00270                 } else {
00271                     $userid = $USER->id;
00272                 }
00273                 //Get html code for RSS link
00274                 $row[] = rss_get_link($context->id, $userid, 'mod_forum', $forum->id, $tooltiptext);
00275             } else {
00276                 $row[] = '&nbsp;';
00277             }
00278         }
00279 
00280         $generaltable->data[] = $row;
00281     }
00282 }
00283 
00284 
00285 // Start of the table for Learning Forums
00286 $learningtable = new html_table();
00287 $learningtable->head  = array ($strforum, $strdescription, $strdiscussions);
00288 $learningtable->align = array ('left', 'left', 'center');
00289 
00290 if ($usetracking) {
00291     $learningtable->head[] = $strunreadposts;
00292     $learningtable->align[] = 'center';
00293 
00294     $learningtable->head[] = $strtracking;
00295     $learningtable->align[] = 'center';
00296 }
00297 
00298 if ($can_subscribe) {
00299     $learningtable->head[] = $strsubscribed;
00300     $learningtable->align[] = 'center';
00301 }
00302 
00303 if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
00304                  isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
00305                  $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
00306     $learningtable->head[] = $strrss;
00307     $learningtable->align[] = 'center';
00308 }
00309 
00311 
00312 if ($course->id != SITEID) {    // Only real courses have learning forums
00313     // 'format_.'$course->format only applicable when not SITEID (format_site is not a format)
00314     $strsectionname  = get_string('sectionname', 'format_'.$course->format);
00315     // Add extra field for section number, at the front
00316     array_unshift($learningtable->head, $strsectionname);
00317     array_unshift($learningtable->align, 'center');
00318 
00319 
00320     if ($learningforums) {
00321         $currentsection = '';
00322             foreach ($learningforums as $forum) {
00323             $cm      = $modinfo->instances['forum'][$forum->id];
00324             $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00325 
00326             $count = forum_count_discussions($forum, $cm, $course);
00327 
00328             if ($usetracking) {
00329                 if ($forum->trackingtype == FORUM_TRACKING_OFF) {
00330                     $unreadlink  = '-';
00331                     $trackedlink = '-';
00332 
00333                 } else {
00334                     if (isset($untracked[$forum->id])) {
00335                         $unreadlink  = '-';
00336                     } else if ($unread = forum_tp_count_forum_unread_posts($cm, $course)) {
00337                         $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
00338                         $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
00339                                        $forum->id.'&amp;mark=read"><img src="'.$OUTPUT->pix_url('t/clear') . '" alt="'.$strmarkallread.'" /></a></span>';
00340                     } else {
00341                         $unreadlink = '<span class="read">0</span>';
00342                     }
00343 
00344                     if ($forum->trackingtype == FORUM_TRACKING_ON) {
00345                         $trackedlink = $stryes;
00346 
00347                     } else {
00348                         $aurl = new moodle_url('/mod/forum/settracking.php', array('id'=>$forum->id));
00349                         if (!isset($untracked[$forum->id])) {
00350                             $trackedlink = $OUTPUT->single_button($aurl, $stryes, 'post', array('title'=>$strnotrackforum));
00351                         } else {
00352                             $trackedlink = $OUTPUT->single_button($aurl, $strno, 'post', array('title'=>$strtrackforum));
00353                         }
00354                     }
00355                 }
00356             }
00357 
00358             $forum->intro = shorten_text(format_module_intro('forum', $forum, $cm->id), $CFG->forum_shortpost);
00359 
00360             if ($cm->sectionnum != $currentsection) {
00361                 $printsection = get_section_name($course, $sections[$cm->sectionnum]);
00362                 if ($currentsection) {
00363                     $learningtable->data[] = 'hr';
00364                 }
00365                 $currentsection = $cm->sectionnum;
00366             } else {
00367                 $printsection = '';
00368             }
00369 
00370             $forumname = format_string($forum->name,true);;
00371 
00372             if ($cm->visible) {
00373                 $style = '';
00374             } else {
00375                 $style = 'class="dimmed"';
00376             }
00377             $forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name,true)."</a>";
00378             $discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
00379 
00380             $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
00381             if ($usetracking) {
00382                 $row[] = $unreadlink;
00383                 $row[] = $trackedlink;    // Tracking.
00384             }
00385 
00386             if ($can_subscribe) {
00387                 if ($forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE) {
00388                     $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
00389                         'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
00390                         'cantsubscribe' => '-'), false, false, true, $subscribed_forums);
00391                 } else {
00392                     $row[] = '-';
00393                 }
00394             }
00395 
00396             //If this forum has RSS activated, calculate it
00397             if ($show_rss) {
00398                 if ($forum->rsstype and $forum->rssarticles) {
00399                     //Calculate the tolltip text
00400                     if ($forum->rsstype == 1) {
00401                         $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum');
00402                     } else {
00403                         $tooltiptext = get_string('rsssubscriberssposts', 'forum');
00404                     }
00405                     //Get html code for RSS link
00406                     $row[] = rss_get_link($context->id, $USER->id, 'mod_forum', $forum->id, $tooltiptext);
00407                 } else {
00408                     $row[] = '&nbsp;';
00409                 }
00410             }
00411 
00412             $learningtable->data[] = $row;
00413         }
00414     }
00415 }
00416 
00417 
00419 $PAGE->navbar->add($strforums);
00420 $PAGE->set_title("$course->shortname: $strforums");
00421 $PAGE->set_heading($course->fullname);
00422 $PAGE->set_button($searchform);
00423 echo $OUTPUT->header();
00424 
00425 // Show the subscribe all options only to non-guest, enrolled users
00426 if (!isguestuser() && isloggedin() && $can_subscribe) {
00427     echo $OUTPUT->box_start('subscription');
00428     echo html_writer::tag('div',
00429         html_writer::link(new moodle_url('/mod/forum/index.php', array('id'=>$course->id, 'subscribe'=>1, 'sesskey'=>sesskey())),
00430             get_string('allsubscribe', 'forum')),
00431         array('class'=>'helplink'));
00432     echo html_writer::tag('div',
00433         html_writer::link(new moodle_url('/mod/forum/index.php', array('id'=>$course->id, 'subscribe'=>0, 'sesskey'=>sesskey())),
00434             get_string('allunsubscribe', 'forum')),
00435         array('class'=>'helplink'));
00436     echo $OUTPUT->box_end();
00437     echo $OUTPUT->box('&nbsp;', 'clearer');
00438 }
00439 
00440 if ($generalforums) {
00441     echo $OUTPUT->heading(get_string('generalforums', 'forum'));
00442     echo html_writer::table($generaltable);
00443 }
00444 
00445 if ($learningforums) {
00446     echo $OUTPUT->heading(get_string('learningforums', 'forum'));
00447     echo html_writer::table($learningtable);
00448 }
00449 
00450 echo $OUTPUT->footer();
00451 
 All Data Structures Namespaces Files Functions Variables Enumerations