[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/mod/forum/ -> index.php (source)

   1  <?php  // $Id: index.php,v 1.104.2.12 2008/10/07 08:13:01 scyrma Exp $
   2  
   3      require_once("../../config.php");
   4      require_once ("lib.php");
   5      require_once("$CFG->libdir/rsslib.php");
   6  
   7      $id = optional_param('id', 0, PARAM_INT);                   // Course id
   8      $subscribe = optional_param('subscribe', null, PARAM_INT);  // Subscribe/Unsubscribe all forums
   9  
  10      if ($id) {
  11          if (! $course = get_record('course', 'id', $id)) {
  12              error("Course ID is incorrect");
  13          }
  14      } else {
  15          if (! $course = get_site()) {
  16              error("Could not find a top-level course!");
  17          }
  18      }
  19  
  20      require_course_login($course);
  21      $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
  22  
  23  
  24      unset($SESSION->fromdiscussion);
  25  
  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');
  37      $strnotrackforum = get_string('notrackforum', 'forum');
  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');
  45  
  46      $searchform = forum_search_form($course);
  47  
  48  
  49      // Start of the table for General Forums
  50  
  51      $generaltable->head  = array ($strforum, $strdescription, $strdiscussions);
  52      $generaltable->align = array ('left', 'left', 'center');
  53  
  54      if ($usetracking = forum_tp_can_track_forums()) {
  55          $untracked = forum_tp_get_untracked_forums($USER->id, $course->id);
  56  
  57          $generaltable->head[] = $strunreadposts;
  58          $generaltable->align[] = 'center';
  59  
  60          $generaltable->head[] = $strtracking;
  61          $generaltable->align[] = 'center';
  62      }
  63  
  64      $subscribed_forums = forum_get_subscribed_forums($course);
  65  
  66      if ($can_subscribe = (!isguestuser() && has_capability('moodle/course:view', $coursecontext))) {
  67          $generaltable->head[] = $strsubscribed;
  68          $generaltable->align[] = 'center';
  69      }
  70  
  71      if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
  72                       isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
  73                       $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
  74          $generaltable->head[] = $strrss;
  75          $generaltable->align[] = 'center';
  76      }
  77  
  78  
  79      // Parse and organise all the forums.  Most forums are course modules but
  80      // some special ones are not.  These get placed in the general forums
  81      // category with the forums in section 0.
  82  
  83      $forums = get_records('forum', 'course', $course->id);
  84  
  85      $generalforums  = array();
  86      $learningforums = array();
  87      $modinfo =& get_fast_modinfo($course);
  88  
  89      if (!isset($modinfo->instances['forum'])) {
  90          $modinfo->instances['forum'] = array();
  91      }
  92  
  93      foreach ($modinfo->instances['forum'] as $forumid=>$cm) {
  94          if (!$cm->uservisible or !isset($forums[$forumid])) {
  95              continue;
  96          }
  97  
  98          $forum = $forums[$forumid];
  99  
 100          if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
 101              continue;   // Shouldn't happen
 102          }
 103  
 104          if (!has_capability('mod/forum:viewdiscussion', $context)) {
 105              continue;
 106          }
 107  
 108          // fill two type array - order in modinfo is the same as in course
 109          if ($forum->type == 'news' or $forum->type == 'social') {
 110              $generalforums[$forum->id] = $forum;
 111  
 112          } else if ($course->id == SITEID or empty($cm->sectionnum)) {
 113              $generalforums[$forum->id] = $forum;
 114  
 115          } else {
 116              $learningforums[$forum->id] = $forum;
 117          }
 118      }
 119                                          
 120      /// Do course wide subscribe/unsubscribe
 121      if (!is_null($subscribe) and !isguestuser() and !isguest()) {
 122          foreach ($modinfo->instances['forum'] as $forumid=>$cm) {
 123              $forum = $forums[$forumid];
 124              $cansub = false;
 125              if (has_capability('mod/forum:viewdiscussion', $cm)) {
 126                  $cansub = true;
 127              }
 128              if ($cansub && $cm->visible == 0 &&
 129                  !has_capability('mod/forum:managesubscriptions', $cm)) 
 130              {
 131                  $cansub = false;
 132              }
 133              if (!forum_is_forcesubscribed($forum)) {
 134                  $subscribed = forum_is_subscribed($USER->id, $forum);
 135                  if ((has_capability('moodle/course:manageactivities', $coursecontext, $USER->id) || $forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE) && $subscribe && !$subscribed && $cansub) {
 136                      forum_subscribe($USER->id, $forumid);
 137                  } else if (!$subscribe && $subscribed) {
 138                      forum_unsubscribe($USER->id, $forumid);
 139                  }
 140              }
 141          }
 142          $returnto = forum_go_back_to("index.php?id=$course->id");
 143          if ($subscribe) {
 144              add_to_log($course->id, 'forum', 'subscribeall', "index.php?id=$course->id", $course->id);
 145              redirect($returnto, get_string('nowallsubscribed', 'forum', format_string($course->shortname)), 1);
 146          } else {
 147              add_to_log($course->id, 'forum', 'unsubscribeall', "index.php?id=$course->id", $course->id);
 148              redirect($returnto, get_string('nowallunsubscribed', 'forum', format_string($course->shortname)), 1);
 149          }
 150      }
 151  
 152      /// First, let's process the general forums and build up a display
 153  
 154      $introoptions = new object();
 155      $introoptions->para = false;
 156  
 157      if ($generalforums) {
 158          foreach ($generalforums as $forum) {
 159              $cm      = $modinfo->instances['forum'][$forum->id];
 160              $context = get_context_instance(CONTEXT_MODULE, $cm->id);
 161  
 162              $count = forum_count_discussions($forum, $cm, $course);
 163  
 164              if ($usetracking) {
 165                  if ($forum->trackingtype == FORUM_TRACKING_OFF) {
 166                      $unreadlink  = '-';
 167                      $trackedlink = '-';
 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>';
 174                          $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
 175                                         $forum->id.'&amp;mark=read"><img src="'.$CFG->pixpath.'/t/clear.gif" alt="'.$strmarkallread.'" /></a></span>';
 176                      } else {
 177                          $unreadlink = '<span class="read">0</span>';
 178                      }
 179  
 180                      if ($forum->trackingtype == FORUM_TRACKING_ON) {
 181                          $trackedlink = $stryes;
 182  
 183                      } else {
 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                          }
 190                      }
 191                  }
 192              }
 193  
 194              $forum->intro = shorten_text(trim(format_text($forum->intro, FORMAT_HTML, $introoptions)), $CFG->forum_shortpost);
 195              $forumname = format_string($forum->name, true);;
 196  
 197              if ($cm->visible) {
 198                  $style = '';
 199              } else {
 200                  $style = 'class="dimmed"';
 201              }
 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>";
 204  
 205              $row = array ($forumlink, $forum->intro, $discussionlink);
 206              if ($usetracking) {
 207                  $row[] = $unreadlink;
 208                  $row[] = $trackedlink;    // Tracking.
 209              }
 210  
 211              if ($can_subscribe) {
 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                  }
 219              }
 220  
 221              //If this forum has RSS activated, calculate it
 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);
 232                  } else {
 233                      $row[] = '&nbsp;';
 234                  }
 235              }
 236  
 237              $generaltable->data[] = $row;
 238          }
 239      }
 240  
 241  
 242      // Start of the table for Learning Forums
 243      $learningtable->head  = array ($strforum, $strdescription, $strdiscussions);
 244      $learningtable->align = array ('left', 'left', 'center');
 245  
 246      if ($usetracking) {
 247          $learningtable->head[] = $strunreadposts;
 248          $learningtable->align[] = 'center';
 249  
 250          $learningtable->head[] = $strtracking;
 251          $learningtable->align[] = 'center';
 252      }
 253  
 254      if ($can_subscribe) {
 255          $learningtable->head[] = $strsubscribed;
 256          $learningtable->align[] = 'center';
 257      }
 258  
 259      if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
 260                       isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
 261                       $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
 262          $learningtable->head[] = $strrss;
 263          $learningtable->align[] = 'center';
 264      }
 265  
 266      /// Now let's process the learning forums
 267  
 268      if ($course->id != SITEID) {    // Only real courses have learning forums
 269          // Add extra field for section number, at the front
 270          if ($course->format == 'weeks' or $course->format == 'weekscss') {
 271              array_unshift($learningtable->head, $strweek);
 272          } else {
 273              array_unshift($learningtable->head, $strsection);
 274          }
 275          array_unshift($learningtable->align, 'center');
 276  
 277  
 278          if ($learningforums) {
 279              $currentsection = '';
 280                  foreach ($learningforums as $forum) {
 281                  $cm      = $modinfo->instances['forum'][$forum->id];
 282                  $context = get_context_instance(CONTEXT_MODULE, $cm->id);
 283  
 284                  $count = forum_count_discussions($forum, $cm, $course);
 285  
 286                  if ($usetracking) {
 287                      if ($forum->trackingtype == FORUM_TRACKING_OFF) {
 288                          $unreadlink  = '-';
 289                          $trackedlink = '-';
 290  
 291                      } else {
 292                          if (isset($untracked[$forum->id])) {
 293                              $unreadlink  = '-';
 294                          } else if ($unread = forum_tp_count_forum_unread_posts($cm, $course)) {
 295                              $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
 296                              $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
 297                                             $forum->id.'&amp;mark=read"><img src="'.$CFG->pixpath.'/t/clear.gif" alt="'.$strmarkallread.'" /></a></span>';
 298                          } else {
 299                              $unreadlink = '<span class="read">0</span>';
 300                          }
 301  
 302                          if ($forum->trackingtype == FORUM_TRACKING_ON) {
 303                              $trackedlink = $stryes;
 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                              }
 312                          }
 313                      }
 314                  }
 315  
 316                  $introoptions->para=false;
 317                  $forum->intro = shorten_text(trim(format_text($forum->intro, FORMAT_HTML, $introoptions)), $CFG->forum_shortpost);
 318  
 319                  if ($cm->sectionnum != $currentsection) {
 320                      $printsection = $cm->sectionnum;
 321                      if ($currentsection) {
 322                          $learningtable->data[] = 'hr';
 323                      }
 324                      $currentsection = $cm->sectionnum;
 325                  } else {
 326                      $printsection = '';
 327                  }
 328  
 329                  $forumname = format_string($forum->name,true);;
 330  
 331                  if ($cm->visible) {
 332                      $style = '';
 333                  } else {
 334                      $style = 'class="dimmed"';
 335                  }
 336                  $forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name,true)."</a>";
 337                  $discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
 338  
 339                  $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
 340                  if ($usetracking) {
 341                      $row[] = $unreadlink;
 342                      $row[] = $trackedlink;    // Tracking.
 343                  }
 344  
 345                  if ($can_subscribe) {
 346                      if ($forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE) {
 347                          $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
 348                              'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
 349                              'cantsubscribe' => '-'), false, false, true, $subscribed_forums);
 350                      } else {
 351                          $row[] = '-';
 352                      }
 353                  }
 354  
 355                  //If this forum has RSS activated, calculate it
 356                  if ($show_rss) {
 357                      if ($forum->rsstype and $forum->rssarticles) {
 358                          //Calculate the tolltip text
 359                          if ($forum->rsstype == 1) {
 360                              $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum', format_string($forum->name));
 361                          } else {
 362                              $tooltiptext = get_string('rsssubscriberssposts', 'forum', format_string($forum->name));
 363                          }
 364                          //Get html code for RSS link
 365                          $row[] = rss_get_link($course->id, $USER->id, 'forum', $forum->id, $tooltiptext);
 366                      } else {
 367                          $row[] = '&nbsp;';
 368                      }
 369                  }
 370  
 371                  $learningtable->data[] = $row;
 372              }
 373          }
 374      }
 375  
 376  
 377      /// Output the page
 378      $navlinks = array();
 379      $navlinks[] = array('name' => $strforums, 'link' => '', 'type' => 'activity');
 380  
 381      print_header("$course->shortname: $strforums", $course->fullname,
 382                      build_navigation($navlinks),
 383                      "", "", true, $searchform, navmenu($course));
 384  
 385      if (!isguest()) {
 386          print_box_start('subscription');
 387          echo '<span class="helplink">';
 388          echo '<a href="index.php?id='.$course->id.'&amp;subscribe=1">'.get_string('allsubscribe', 'forum').'</a>';
 389          echo '</span><br /><span class="helplink">';
 390          echo '<a href="index.php?id='.$course->id.'&amp;subscribe=0">'.get_string('allunsubscribe', 'forum').'</a>';
 391          echo '</span>';
 392          print_box_end();
 393          print_box('&nbsp;', 'clearer');
 394      }
 395  
 396      if ($generalforums) {
 397          print_heading(get_string('generalforums', 'forum'));
 398          print_table($generaltable);
 399      }
 400  
 401      if ($learningforums) {
 402          print_heading(get_string('learningforums', 'forum'));
 403          print_table($learningtable);
 404      }
 405  
 406      print_footer($course);
 407  
 408  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7