[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

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

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * @package   mod_forum
  20   * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
  21   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  
  24  require_once(dirname(__FILE__) . '/../../config.php');
  25  require_once($CFG->dirroot . '/course/lib.php');
  26  require_once($CFG->dirroot . '/mod/forum/lib.php');
  27  require_once($CFG->libdir . '/rsslib.php');
  28  
  29  $id = optional_param('id', 0, PARAM_INT);                   // Course id
  30  $subscribe = optional_param('subscribe', null, PARAM_INT);  // Subscribe/Unsubscribe all forums
  31  
  32  $url = new moodle_url('/mod/forum/index.php', array('id'=>$id));
  33  if ($subscribe !== null) {
  34      require_sesskey();
  35      $url->param('subscribe', $subscribe);
  36  }
  37  $PAGE->set_url($url);
  38  
  39  if ($id) {
  40      if (! $course = $DB->get_record('course', array('id' => $id))) {
  41          print_error('invalidcourseid');
  42      }
  43  } else {
  44      $course = get_site();
  45  }
  46  
  47  require_course_login($course);
  48  $PAGE->set_pagelayout('incourse');
  49  $coursecontext = context_course::instance($course->id);
  50  
  51  
  52  unset($SESSION->fromdiscussion);
  53  
  54  $params = array(
  55      'context' => context_course::instance($course->id)
  56  );
  57  $event = \mod_forum\event\course_module_instance_list_viewed::create($params);
  58  $event->add_record_snapshot('course', $course);
  59  $event->trigger();
  60  
  61  $strforums       = get_string('forums', 'forum');
  62  $strforum        = get_string('forum', 'forum');
  63  $strdescription  = get_string('description');
  64  $strdiscussions  = get_string('discussions', 'forum');
  65  $strsubscribed   = get_string('subscribed', 'forum');
  66  $strunreadposts  = get_string('unreadposts', 'forum');
  67  $strtracking     = get_string('tracking', 'forum');
  68  $strmarkallread  = get_string('markallread', 'forum');
  69  $strtrackforum   = get_string('trackforum', 'forum');
  70  $strnotrackforum = get_string('notrackforum', 'forum');
  71  $strsubscribe    = get_string('subscribe', 'forum');
  72  $strunsubscribe  = get_string('unsubscribe', 'forum');
  73  $stryes          = get_string('yes');
  74  $strno           = get_string('no');
  75  $strrss          = get_string('rss');
  76  $stremaildigest  = get_string('emaildigest');
  77  
  78  $searchform = forum_search_form($course);
  79  
  80  // Retrieve the list of forum digest options for later.
  81  $digestoptions = forum_get_user_digest_options();
  82  $digestoptions_selector = new single_select(new moodle_url('/mod/forum/maildigest.php',
  83      array(
  84          'backtoindex' => 1,
  85      )),
  86      'maildigest',
  87      $digestoptions,
  88      null,
  89      '');
  90  $digestoptions_selector->method = 'post';
  91  
  92  // Start of the table for General Forums
  93  
  94  $generaltable = new html_table();
  95  $generaltable->head  = array ($strforum, $strdescription, $strdiscussions);
  96  $generaltable->align = array ('left', 'left', 'center');
  97  
  98  if ($usetracking = forum_tp_can_track_forums()) {
  99      $untracked = forum_tp_get_untracked_forums($USER->id, $course->id);
 100  
 101      $generaltable->head[] = $strunreadposts;
 102      $generaltable->align[] = 'center';
 103  
 104      $generaltable->head[] = $strtracking;
 105      $generaltable->align[] = 'center';
 106  }
 107  
 108  // Fill the subscription cache for this course and user combination.
 109  \mod_forum\subscriptions::fill_subscription_cache_for_course($course->id, $USER->id);
 110  
 111  $can_subscribe = is_enrolled($coursecontext);
 112  if ($can_subscribe) {
 113      $generaltable->head[] = $strsubscribed;
 114      $generaltable->align[] = 'center';
 115  
 116      $generaltable->head[] = $stremaildigest . ' ' . $OUTPUT->help_icon('emaildigesttype', 'mod_forum');
 117      $generaltable->align[] = 'center';
 118  }
 119  
 120  if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
 121                   isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
 122                   $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
 123      $generaltable->head[] = $strrss;
 124      $generaltable->align[] = 'center';
 125  }
 126  
 127  $usesections = course_format_uses_sections($course->format);
 128  
 129  $table = new html_table();
 130  
 131  // Parse and organise all the forums.  Most forums are course modules but
 132  // some special ones are not.  These get placed in the general forums
 133  // category with the forums in section 0.
 134  
 135  $forums = $DB->get_records_sql("
 136      SELECT f.*,
 137             d.maildigest
 138        FROM {forum} f
 139   LEFT JOIN {forum_digests} d ON d.forum = f.id AND d.userid = ?
 140       WHERE f.course = ?
 141      ", array($USER->id, $course->id));
 142  
 143  $generalforums  = array();
 144  $learningforums = array();
 145  $modinfo = get_fast_modinfo($course);
 146  
 147  foreach ($modinfo->get_instances_of('forum') as $forumid=>$cm) {
 148      if (!$cm->uservisible or !isset($forums[$forumid])) {
 149          continue;
 150      }
 151  
 152      $forum = $forums[$forumid];
 153  
 154      if (!$context = context_module::instance($cm->id, IGNORE_MISSING)) {
 155          continue;   // Shouldn't happen
 156      }
 157  
 158      if (!has_capability('mod/forum:viewdiscussion', $context)) {
 159          continue;
 160      }
 161  
 162      // fill two type array - order in modinfo is the same as in course
 163      if ($forum->type == 'news' or $forum->type == 'social') {
 164          $generalforums[$forum->id] = $forum;
 165  
 166      } else if ($course->id == SITEID or empty($cm->sectionnum)) {
 167          $generalforums[$forum->id] = $forum;
 168  
 169      } else {
 170          $learningforums[$forum->id] = $forum;
 171      }
 172  }
 173  
 174  // Do course wide subscribe/unsubscribe if requested
 175  if (!is_null($subscribe)) {
 176      if (isguestuser() or !$can_subscribe) {
 177          // there should not be any links leading to this place, just redirect
 178          redirect(new moodle_url('/mod/forum/index.php', array('id' => $id)), get_string('subscribeenrolledonly', 'forum'));
 179      }
 180      // Can proceed now, the user is not guest and is enrolled
 181      foreach ($modinfo->get_instances_of('forum') as $forumid=>$cm) {
 182          $forum = $forums[$forumid];
 183          $modcontext = context_module::instance($cm->id);
 184          $cansub = false;
 185  
 186          if (has_capability('mod/forum:viewdiscussion', $modcontext)) {
 187              $cansub = true;
 188          }
 189          if ($cansub && $cm->visible == 0 &&
 190              !has_capability('mod/forum:managesubscriptions', $modcontext))
 191          {
 192              $cansub = false;
 193          }
 194          if (!\mod_forum\subscriptions::is_forcesubscribed($forum)) {
 195              $subscribed = \mod_forum\subscriptions::is_subscribed($USER->id, $forum, null, $cm);
 196              $canmanageactivities = has_capability('moodle/course:manageactivities', $coursecontext, $USER->id);
 197              if (($canmanageactivities || \mod_forum\subscriptions::is_subscribable($forum)) && $subscribe && !$subscribed && $cansub) {
 198                  \mod_forum\subscriptions::subscribe_user($USER->id, $forum, $modcontext, true);
 199              } else if (!$subscribe && $subscribed) {
 200                  \mod_forum\subscriptions::unsubscribe_user($USER->id, $forum, $modcontext, true);
 201              }
 202          }
 203      }
 204      $returnto = forum_go_back_to("index.php?id=$course->id");
 205      $shortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
 206      if ($subscribe) {
 207          redirect($returnto, get_string('nowallsubscribed', 'forum', $shortname), 1);
 208      } else {
 209          redirect($returnto, get_string('nowallunsubscribed', 'forum', $shortname), 1);
 210      }
 211  }
 212  
 213  /// First, let's process the general forums and build up a display
 214  
 215  if ($generalforums) {
 216      foreach ($generalforums as $forum) {
 217          $cm      = $modinfo->instances['forum'][$forum->id];
 218          $context = context_module::instance($cm->id);
 219  
 220          $count = forum_count_discussions($forum, $cm, $course);
 221  
 222          if ($usetracking) {
 223              if ($forum->trackingtype == FORUM_TRACKING_OFF) {
 224                  $unreadlink  = '-';
 225                  $trackedlink = '-';
 226  
 227              } else {
 228                  if (isset($untracked[$forum->id])) {
 229                          $unreadlink  = '-';
 230                  } else if ($unread = forum_tp_count_forum_unread_posts($cm, $course)) {
 231                          $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
 232                      $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
 233                                     $forum->id.'&amp;mark=read"><img src="'.$OUTPUT->pix_url('t/markasread') . '" alt="'.$strmarkallread.'" class="iconsmall" /></a></span>';
 234                  } else {
 235                      $unreadlink = '<span class="read">0</span>';
 236                  }
 237  
 238                  if (($forum->trackingtype == FORUM_TRACKING_FORCED) && ($CFG->forum_allowforcedreadtracking)) {
 239                      $trackedlink = $stryes;
 240                  } else if ($forum->trackingtype === FORUM_TRACKING_OFF || ($USER->trackforums == 0)) {
 241                      $trackedlink = '-';
 242                  } else {
 243                      $aurl = new moodle_url('/mod/forum/settracking.php', array(
 244                              'id' => $forum->id,
 245                              'sesskey' => sesskey(),
 246                          ));
 247                      if (!isset($untracked[$forum->id])) {
 248                          $trackedlink = $OUTPUT->single_button($aurl, $stryes, 'post', array('title'=>$strnotrackforum));
 249                      } else {
 250                          $trackedlink = $OUTPUT->single_button($aurl, $strno, 'post', array('title'=>$strtrackforum));
 251                      }
 252                  }
 253              }
 254          }
 255  
 256          $forum->intro = shorten_text(format_module_intro('forum', $forum, $cm->id), $CFG->forum_shortpost);
 257          $forumname = format_string($forum->name, true);
 258  
 259          if ($cm->visible) {
 260              $style = '';
 261          } else {
 262              $style = 'class="dimmed"';
 263          }
 264          $forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name,true)."</a>";
 265          $discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
 266  
 267          $row = array ($forumlink, $forum->intro, $discussionlink);
 268          if ($usetracking) {
 269              $row[] = $unreadlink;
 270              $row[] = $trackedlink;    // Tracking.
 271          }
 272  
 273          if ($can_subscribe) {
 274              $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
 275                      'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
 276                      'cantsubscribe' => '-'), false, false, true);
 277  
 278              $digestoptions_selector->url->param('id', $forum->id);
 279              if ($forum->maildigest === null) {
 280                  $digestoptions_selector->selected = -1;
 281              } else {
 282                  $digestoptions_selector->selected = $forum->maildigest;
 283              }
 284              $row[] = $OUTPUT->render($digestoptions_selector);
 285          }
 286  
 287          //If this forum has RSS activated, calculate it
 288          if ($show_rss) {
 289              if ($forum->rsstype and $forum->rssarticles) {
 290                  //Calculate the tooltip text
 291                  if ($forum->rsstype == 1) {
 292                      $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum');
 293                  } else {
 294                      $tooltiptext = get_string('rsssubscriberssposts', 'forum');
 295                  }
 296  
 297                  if (!isloggedin() && $course->id == SITEID) {
 298                      $userid = guest_user()->id;
 299                  } else {
 300                      $userid = $USER->id;
 301                  }
 302                  //Get html code for RSS link
 303                  $row[] = rss_get_link($context->id, $userid, 'mod_forum', $forum->id, $tooltiptext);
 304              } else {
 305                  $row[] = '&nbsp;';
 306              }
 307          }
 308  
 309          $generaltable->data[] = $row;
 310      }
 311  }
 312  
 313  
 314  // Start of the table for Learning Forums
 315  $learningtable = new html_table();
 316  $learningtable->head  = array ($strforum, $strdescription, $strdiscussions);
 317  $learningtable->align = array ('left', 'left', 'center');
 318  
 319  if ($usetracking) {
 320      $learningtable->head[] = $strunreadposts;
 321      $learningtable->align[] = 'center';
 322  
 323      $learningtable->head[] = $strtracking;
 324      $learningtable->align[] = 'center';
 325  }
 326  
 327  if ($can_subscribe) {
 328      $learningtable->head[] = $strsubscribed;
 329      $learningtable->align[] = 'center';
 330  
 331      $learningtable->head[] = $stremaildigest . ' ' . $OUTPUT->help_icon('emaildigesttype', 'mod_forum');
 332      $learningtable->align[] = 'center';
 333  }
 334  
 335  if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
 336                   isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
 337                   $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
 338      $learningtable->head[] = $strrss;
 339      $learningtable->align[] = 'center';
 340  }
 341  
 342  /// Now let's process the learning forums
 343  
 344  if ($course->id != SITEID) {    // Only real courses have learning forums
 345      // 'format_.'$course->format only applicable when not SITEID (format_site is not a format)
 346      $strsectionname  = get_string('sectionname', 'format_'.$course->format);
 347      // Add extra field for section number, at the front
 348      array_unshift($learningtable->head, $strsectionname);
 349      array_unshift($learningtable->align, 'center');
 350  
 351  
 352      if ($learningforums) {
 353          $currentsection = '';
 354              foreach ($learningforums as $forum) {
 355              $cm      = $modinfo->instances['forum'][$forum->id];
 356              $context = context_module::instance($cm->id);
 357  
 358              $count = forum_count_discussions($forum, $cm, $course);
 359  
 360              if ($usetracking) {
 361                  if ($forum->trackingtype == FORUM_TRACKING_OFF) {
 362                      $unreadlink  = '-';
 363                      $trackedlink = '-';
 364  
 365                  } else {
 366                      if (isset($untracked[$forum->id])) {
 367                          $unreadlink  = '-';
 368                      } else if ($unread = forum_tp_count_forum_unread_posts($cm, $course)) {
 369                          $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
 370                          $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
 371                                         $forum->id.'&amp;mark=read"><img src="'.$OUTPUT->pix_url('t/markasread') . '" alt="'.$strmarkallread.'" class="iconsmall" /></a></span>';
 372                      } else {
 373                          $unreadlink = '<span class="read">0</span>';
 374                      }
 375  
 376                      if (($forum->trackingtype == FORUM_TRACKING_FORCED) && ($CFG->forum_allowforcedreadtracking)) {
 377                          $trackedlink = $stryes;
 378                      } else if ($forum->trackingtype === FORUM_TRACKING_OFF || ($USER->trackforums == 0)) {
 379                          $trackedlink = '-';
 380                      } else {
 381                          $aurl = new moodle_url('/mod/forum/settracking.php', array('id'=>$forum->id));
 382                          if (!isset($untracked[$forum->id])) {
 383                              $trackedlink = $OUTPUT->single_button($aurl, $stryes, 'post', array('title'=>$strnotrackforum));
 384                          } else {
 385                              $trackedlink = $OUTPUT->single_button($aurl, $strno, 'post', array('title'=>$strtrackforum));
 386                          }
 387                      }
 388                  }
 389              }
 390  
 391              $forum->intro = shorten_text(format_module_intro('forum', $forum, $cm->id), $CFG->forum_shortpost);
 392  
 393              if ($cm->sectionnum != $currentsection) {
 394                  $printsection = get_section_name($course, $cm->sectionnum);
 395                  if ($currentsection) {
 396                      $learningtable->data[] = 'hr';
 397                  }
 398                  $currentsection = $cm->sectionnum;
 399              } else {
 400                  $printsection = '';
 401              }
 402  
 403              $forumname = format_string($forum->name,true);
 404  
 405              if ($cm->visible) {
 406                  $style = '';
 407              } else {
 408                  $style = 'class="dimmed"';
 409              }
 410              $forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name,true)."</a>";
 411              $discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
 412  
 413              $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
 414              if ($usetracking) {
 415                  $row[] = $unreadlink;
 416                  $row[] = $trackedlink;    // Tracking.
 417              }
 418  
 419              if ($can_subscribe) {
 420                  $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
 421                      'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
 422                      'cantsubscribe' => '-'), false, false, true);
 423  
 424                  $digestoptions_selector->url->param('id', $forum->id);
 425                  if ($forum->maildigest === null) {
 426                      $digestoptions_selector->selected = -1;
 427                  } else {
 428                      $digestoptions_selector->selected = $forum->maildigest;
 429                  }
 430                  $row[] = $OUTPUT->render($digestoptions_selector);
 431              }
 432  
 433              //If this forum has RSS activated, calculate it
 434              if ($show_rss) {
 435                  if ($forum->rsstype and $forum->rssarticles) {
 436                      //Calculate the tolltip text
 437                      if ($forum->rsstype == 1) {
 438                          $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum');
 439                      } else {
 440                          $tooltiptext = get_string('rsssubscriberssposts', 'forum');
 441                      }
 442                      //Get html code for RSS link
 443                      $row[] = rss_get_link($context->id, $USER->id, 'mod_forum', $forum->id, $tooltiptext);
 444                  } else {
 445                      $row[] = '&nbsp;';
 446                  }
 447              }
 448  
 449              $learningtable->data[] = $row;
 450          }
 451      }
 452  }
 453  
 454  
 455  /// Output the page
 456  $PAGE->navbar->add($strforums);
 457  $PAGE->set_title("$course->shortname: $strforums");
 458  $PAGE->set_heading($course->fullname);
 459  $PAGE->set_button($searchform);
 460  echo $OUTPUT->header();
 461  
 462  // Show the subscribe all options only to non-guest, enrolled users
 463  if (!isguestuser() && isloggedin() && $can_subscribe) {
 464      echo $OUTPUT->box_start('subscription');
 465      echo html_writer::tag('div',
 466          html_writer::link(new moodle_url('/mod/forum/index.php', array('id'=>$course->id, 'subscribe'=>1, 'sesskey'=>sesskey())),
 467              get_string('allsubscribe', 'forum')),
 468          array('class'=>'helplink'));
 469      echo html_writer::tag('div',
 470          html_writer::link(new moodle_url('/mod/forum/index.php', array('id'=>$course->id, 'subscribe'=>0, 'sesskey'=>sesskey())),
 471              get_string('allunsubscribe', 'forum')),
 472          array('class'=>'helplink'));
 473      echo $OUTPUT->box_end();
 474      echo $OUTPUT->box('&nbsp;', 'clearer');
 475  }
 476  
 477  if ($generalforums) {
 478      echo $OUTPUT->heading(get_string('generalforums', 'forum'), 2);
 479      echo html_writer::table($generaltable);
 480  }
 481  
 482  if ($learningforums) {
 483      echo $OUTPUT->heading(get_string('learningforums', 'forum'), 2);
 484      echo html_writer::table($learningtable);
 485  }
 486  
 487  echo $OUTPUT->footer();
 488  


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1