Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402]

   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(__DIR__ . '/../../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  $PAGE->set_secondary_active_tab('coursehome');
  39  
  40  if ($id) {
  41      if (!$course = $DB->get_record('course', array('id' => $id))) {
  42          throw new \moodle_exception('invalidcourseid');
  43      }
  44  } else {
  45      $course = get_site();
  46  }
  47  
  48  require_course_login($course);
  49  $PAGE->set_pagelayout('incourse');
  50  $coursecontext = context_course::instance($course->id);
  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  // Start of the table for General Forums.
  81  $generaltable = new html_table();
  82  $generaltable->head  = array ($strforum, $strdescription, $strdiscussions);
  83  $generaltable->align = array ('left', 'left', 'center');
  84  
  85  if ($usetracking = forum_tp_can_track_forums()) {
  86      $untracked = forum_tp_get_untracked_forums($USER->id, $course->id);
  87  
  88      $generaltable->head[] = $strunreadposts;
  89      $generaltable->align[] = 'center';
  90  
  91      $generaltable->head[] = $strtracking;
  92      $generaltable->align[] = 'center';
  93  }
  94  
  95  // Fill the subscription cache for this course and user combination.
  96  \mod_forum\subscriptions::fill_subscription_cache_for_course($course->id, $USER->id);
  97  
  98  $usesections = course_format_uses_sections($course->format);
  99  
 100  $table = new html_table();
 101  
 102  // Parse and organise all the forums.  Most forums are course modules but
 103  // some special ones are not.  These get placed in the general forums
 104  // category with the forums in section 0.
 105  
 106  $forums = $DB->get_records_sql("
 107      SELECT f.*,
 108             d.maildigest
 109        FROM {forum} f
 110   LEFT JOIN {forum_digests} d ON d.forum = f.id AND d.userid = ?
 111       WHERE f.course = ?
 112      ", array($USER->id, $course->id));
 113  
 114  $generalforums  = array();
 115  $learningforums = array();
 116  $modinfo = get_fast_modinfo($course);
 117  $showsubscriptioncolumns = false;
 118  
 119  foreach ($modinfo->get_instances_of('forum') as $forumid => $cm) {
 120      if (!$cm->uservisible or !isset($forums[$forumid])) {
 121          continue;
 122      }
 123  
 124      $forum = $forums[$forumid];
 125  
 126      if (!$context = context_module::instance($cm->id, IGNORE_MISSING)) {
 127          // Shouldn't happen.
 128          continue;
 129      }
 130  
 131      if (!has_capability('mod/forum:viewdiscussion', $context)) {
 132          // User can't view this one - skip it.
 133          continue;
 134      }
 135  
 136      // Determine whether subscription options should be displayed.
 137      $forum->cansubscribe = mod_forum\subscriptions::is_subscribable($forum);
 138      $forum->cansubscribe = $forum->cansubscribe || has_capability('mod/forum:managesubscriptions', $context);
 139      $forum->issubscribed = mod_forum\subscriptions::is_subscribed($USER->id, $forum, null, $cm);
 140  
 141      $showsubscriptioncolumns = $showsubscriptioncolumns || $forum->issubscribed || $forum->cansubscribe;
 142  
 143      // Fill two type array - order in modinfo is the same as in course.
 144      if ($forum->type == 'news' or $forum->type == 'social') {
 145          $generalforums[$forum->id] = $forum;
 146  
 147      } else if ($course->id == SITEID or empty($cm->sectionnum)) {
 148          $generalforums[$forum->id] = $forum;
 149  
 150      } else {
 151          $learningforums[$forum->id] = $forum;
 152      }
 153  }
 154  
 155  if ($showsubscriptioncolumns) {
 156      // The user can subscribe to at least one forum.
 157      $generaltable->head[] = $strsubscribed;
 158      $generaltable->align[] = 'center';
 159  
 160      $generaltable->head[] = $stremaildigest . ' ' . $OUTPUT->help_icon('emaildigesttype', 'mod_forum');
 161      $generaltable->align[] = 'center';
 162  
 163  }
 164  
 165  if ($show_rss = (($showsubscriptioncolumns || $course->id == SITEID) &&
 166                   isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
 167                   $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
 168      $generaltable->head[] = $strrss;
 169      $generaltable->align[] = 'center';
 170  }
 171  
 172  
 173  // Do course wide subscribe/unsubscribe if requested
 174  if (!is_null($subscribe)) {
 175      if (isguestuser() or !$showsubscriptioncolumns) {
 176          // There should not be any links leading to this place, just redirect.
 177          redirect(
 178                  new moodle_url('/mod/forum/index.php', array('id' => $id)),
 179                  get_string('subscribeenrolledonly', 'forum'),
 180                  null,
 181                  \core\output\notification::NOTIFY_ERROR
 182              );
 183      }
 184      // Can proceed now, the user is not guest and is enrolled
 185      foreach ($modinfo->get_instances_of('forum') as $forumid => $cm) {
 186          $forum = $forums[$forumid];
 187          $modcontext = context_module::instance($cm->id);
 188          $cansub = false;
 189  
 190          if (has_capability('mod/forum:viewdiscussion', $modcontext)) {
 191              $cansub = true;
 192          }
 193          if ($cansub && $cm->visible == 0 &&
 194              !has_capability('mod/forum:managesubscriptions', $modcontext))
 195          {
 196              $cansub = false;
 197          }
 198          if (!\mod_forum\subscriptions::is_forcesubscribed($forum)) {
 199              $subscribed = \mod_forum\subscriptions::is_subscribed($USER->id, $forum, null, $cm);
 200              $canmanageactivities = has_capability('moodle/course:manageactivities', $coursecontext, $USER->id);
 201              if (($canmanageactivities || \mod_forum\subscriptions::is_subscribable($forum)) && $subscribe && !$subscribed && $cansub) {
 202                  \mod_forum\subscriptions::subscribe_user($USER->id, $forum, $modcontext, true);
 203              } else if (!$subscribe && $subscribed) {
 204                  \mod_forum\subscriptions::unsubscribe_user($USER->id, $forum, $modcontext, true);
 205              }
 206          }
 207      }
 208      $returnto = forum_go_back_to(new moodle_url('/mod/forum/index.php', array('id' => $course->id)));
 209      $shortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
 210      if ($subscribe) {
 211          redirect(
 212                  $returnto,
 213                  get_string('nowallsubscribed', 'forum', $shortname),
 214                  null,
 215                  \core\output\notification::NOTIFY_SUCCESS
 216              );
 217      } else {
 218          redirect(
 219                  $returnto,
 220                  get_string('nowallunsubscribed', 'forum', $shortname),
 221                  null,
 222                  \core\output\notification::NOTIFY_SUCCESS
 223              );
 224      }
 225  }
 226  
 227  if ($generalforums) {
 228      // Process general forums.
 229      foreach ($generalforums as $forum) {
 230          $cm      = $modinfo->instances['forum'][$forum->id];
 231          $context = context_module::instance($cm->id);
 232  
 233          $count = forum_count_discussions($forum, $cm, $course);
 234  
 235          if ($usetracking) {
 236              if ($forum->trackingtype == FORUM_TRACKING_OFF) {
 237                  $unreadlink  = '-';
 238                  $trackedlink = '-';
 239  
 240              } else {
 241                  if (isset($untracked[$forum->id])) {
 242                          $unreadlink  = '-';
 243                  } else if ($unread = forum_tp_count_forum_unread_posts($cm, $course)) {
 244                      $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'#unread">'.$unread.'</a>';
 245                      $icon = $OUTPUT->pix_icon('t/markasread', $strmarkallread);
 246                      $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
 247                                     $forum->id.'&amp;mark=read&amp;sesskey=' . sesskey() . '">' . $icon . '</a></span>';
 248                  } else {
 249                      $unreadlink = '<span class="read">0</span>';
 250                  }
 251  
 252                  if (($forum->trackingtype == FORUM_TRACKING_FORCED) && ($CFG->forum_allowforcedreadtracking)) {
 253                      $trackedlink = $stryes;
 254                  } else if ($forum->trackingtype === FORUM_TRACKING_OFF || ($USER->trackforums == 0)) {
 255                      $trackedlink = '-';
 256                  } else {
 257                      $aurl = new moodle_url('/mod/forum/settracking.php', array(
 258                              'id' => $forum->id,
 259                              'sesskey' => sesskey(),
 260                          ));
 261                      if (!isset($untracked[$forum->id])) {
 262                          $trackedlink = $OUTPUT->single_button($aurl, $stryes, 'post', array('title' => $strnotrackforum));
 263                      } else {
 264                          $trackedlink = $OUTPUT->single_button($aurl, $strno, 'post', array('title' => $strtrackforum));
 265                      }
 266                  }
 267              }
 268          }
 269  
 270          $forum->intro = shorten_text(format_module_intro('forum', $forum, $cm->id), $CFG->forum_shortpost);
 271          $forumname = format_string($forum->name, true);
 272  
 273          if ($cm->visible) {
 274              $style = '';
 275          } else {
 276              $style = 'class="dimmed"';
 277          }
 278          $forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name,true)."</a>";
 279          $discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
 280  
 281          $row = array ($forumlink, $forum->intro, $discussionlink);
 282          if ($usetracking) {
 283              $row[] = $unreadlink;
 284              $row[] = $trackedlink;    // Tracking.
 285          }
 286  
 287          if ($showsubscriptioncolumns) {
 288              $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
 289                  'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
 290                  'cantsubscribe' => '-'), false, false, true);
 291              $row[] = forum_index_get_forum_subscription_selector($forum);
 292          }
 293  
 294          // If this forum has RSS activated, calculate it.
 295          if ($show_rss) {
 296              if ($forum->rsstype and $forum->rssarticles) {
 297                  //Calculate the tooltip text
 298                  if ($forum->rsstype == 1) {
 299                      $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum');
 300                  } else {
 301                      $tooltiptext = get_string('rsssubscriberssposts', 'forum');
 302                  }
 303  
 304                  if (!isloggedin() && $course->id == SITEID) {
 305                      $userid = guest_user()->id;
 306                  } else {
 307                      $userid = $USER->id;
 308                  }
 309                  //Get html code for RSS link
 310                  $row[] = rss_get_link($context->id, $userid, 'mod_forum', $forum->id, $tooltiptext);
 311              } else {
 312                  $row[] = '&nbsp;';
 313              }
 314          }
 315  
 316          $generaltable->data[] = $row;
 317      }
 318  }
 319  
 320  
 321  // Start of the table for Learning Forums
 322  $learningtable = new html_table();
 323  $learningtable->head  = array ($strforum, $strdescription, $strdiscussions);
 324  $learningtable->align = array ('left', 'left', 'center');
 325  
 326  if ($usetracking) {
 327      $learningtable->head[] = $strunreadposts;
 328      $learningtable->align[] = 'center';
 329  
 330      $learningtable->head[] = $strtracking;
 331      $learningtable->align[] = 'center';
 332  }
 333  
 334  if ($showsubscriptioncolumns) {
 335      $learningtable->head[] = $strsubscribed;
 336      $learningtable->align[] = 'center';
 337  
 338      $learningtable->head[] = $stremaildigest . ' ' . $OUTPUT->help_icon('emaildigesttype', 'mod_forum');
 339      $learningtable->align[] = 'center';
 340  }
 341  
 342  if ($show_rss = (($showsubscriptioncolumns || $course->id == SITEID) &&
 343                   isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
 344                   $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
 345      $learningtable->head[] = $strrss;
 346      $learningtable->align[] = 'center';
 347  }
 348  
 349  // Now let's process the learning forums.
 350  if ($course->id != SITEID) {    // Only real courses have learning forums
 351      // 'format_.'$course->format only applicable when not SITEID (format_site is not a format)
 352      $strsectionname  = get_string('sectionname', 'format_'.$course->format);
 353      // Add extra field for section number, at the front
 354      array_unshift($learningtable->head, $strsectionname);
 355      array_unshift($learningtable->align, 'center');
 356  
 357  
 358      if ($learningforums) {
 359          $currentsection = '';
 360              foreach ($learningforums as $forum) {
 361              $cm      = $modinfo->instances['forum'][$forum->id];
 362              $context = context_module::instance($cm->id);
 363  
 364              $count = forum_count_discussions($forum, $cm, $course);
 365  
 366              if ($usetracking) {
 367                  if ($forum->trackingtype == FORUM_TRACKING_OFF) {
 368                      $unreadlink  = '-';
 369                      $trackedlink = '-';
 370  
 371                  } else {
 372                      if (isset($untracked[$forum->id])) {
 373                          $unreadlink  = '-';
 374                      } else if ($unread = forum_tp_count_forum_unread_posts($cm, $course)) {
 375                          $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'#unread">'.$unread.'</a>';
 376                          $icon = $OUTPUT->pix_icon('t/markasread', $strmarkallread);
 377                          $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
 378                                         $forum->id.'&amp;mark=read&sesskey=' . sesskey() . '">' . $icon . '</a></span>';
 379                      } else {
 380                          $unreadlink = '<span class="read">0</span>';
 381                      }
 382  
 383                      if (($forum->trackingtype == FORUM_TRACKING_FORCED) && ($CFG->forum_allowforcedreadtracking)) {
 384                          $trackedlink = $stryes;
 385                      } else if ($forum->trackingtype === FORUM_TRACKING_OFF || ($USER->trackforums == 0)) {
 386                          $trackedlink = '-';
 387                      } else {
 388                          $aurl = new moodle_url('/mod/forum/settracking.php', array('id' => $forum->id));
 389                          if (!isset($untracked[$forum->id])) {
 390                              $trackedlink = $OUTPUT->single_button($aurl, $stryes, 'post', array('title' => $strnotrackforum));
 391                          } else {
 392                              $trackedlink = $OUTPUT->single_button($aurl, $strno, 'post', array('title' => $strtrackforum));
 393                          }
 394                      }
 395                  }
 396              }
 397  
 398              $forum->intro = shorten_text(format_module_intro('forum', $forum, $cm->id), $CFG->forum_shortpost);
 399  
 400              if ($cm->sectionnum != $currentsection) {
 401                  $printsection = get_section_name($course, $cm->sectionnum);
 402                  if ($currentsection) {
 403                      $learningtable->data[] = 'hr';
 404                  }
 405                  $currentsection = $cm->sectionnum;
 406              } else {
 407                  $printsection = '';
 408              }
 409  
 410              $forumname = format_string($forum->name,true);
 411  
 412              if ($cm->visible) {
 413                  $style = '';
 414              } else {
 415                  $style = 'class="dimmed"';
 416              }
 417              $forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name,true)."</a>";
 418              $discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
 419  
 420              $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
 421              if ($usetracking) {
 422                  $row[] = $unreadlink;
 423                  $row[] = $trackedlink;    // Tracking.
 424              }
 425  
 426              if ($showsubscriptioncolumns) {
 427                  $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
 428                      'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
 429                      'cantsubscribe' => '-'), false, false, true);
 430                  $row[] = forum_index_get_forum_subscription_selector($forum);
 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  // Output the page.
 455  $PAGE->navbar->add($strforums);
 456  $PAGE->set_title("$course->shortname: $strforums");
 457  $PAGE->set_heading($course->fullname);
 458  echo $OUTPUT->header();
 459  
 460  echo html_writer::start_div('input-group mr-5');
 461  echo $searchform;
 462  echo html_writer::end_div();
 463  
 464  if (!isguestuser() && isloggedin() && $showsubscriptioncolumns) {
 465      // Show the subscribe all options only to non-guest, enrolled users.
 466      echo $OUTPUT->box_start('subscription');
 467  
 468      $subscriptionlink = new moodle_url('/mod/forum/index.php', [
 469          'id'        => $course->id,
 470          'sesskey'   => sesskey(),
 471      ]);
 472  
 473      // Subscribe all.
 474      $subscriptionlink->param('subscribe', 1);
 475      echo html_writer::tag('div', html_writer::link($subscriptionlink, get_string('allsubscribe', 'forum')), [
 476              'class' => 'helplink',
 477          ]);
 478  
 479      // Unsubscribe all.
 480      $subscriptionlink->param('subscribe', 0);
 481      echo html_writer::tag('div', html_writer::link($subscriptionlink, get_string('allunsubscribe', 'forum')), [
 482              'class' => 'helplink',
 483          ]);
 484  
 485      echo $OUTPUT->box_end();
 486      echo $OUTPUT->box('&nbsp;', 'clearer');
 487  }
 488  
 489  if ($generalforums) {
 490      echo $OUTPUT->heading(get_string('generalforums', 'forum'), 2);
 491      echo html_writer::table($generaltable);
 492  }
 493  
 494  if ($learningforums) {
 495      echo $OUTPUT->heading(get_string('learningforums', 'forum'), 2);
 496      echo html_writer::table($learningtable);
 497  }
 498  
 499  echo $OUTPUT->footer();
 500  
 501  /**
 502   * Get the content of the forum subscription options for this forum.
 503   *
 504   * @param   stdClass    $forum      The forum to return options for
 505   * @return  string
 506   */
 507  function forum_index_get_forum_subscription_selector($forum) {
 508      global $OUTPUT, $PAGE;
 509  
 510      if ($forum->cansubscribe || $forum->issubscribed) {
 511          if ($forum->maildigest === null) {
 512              $forum->maildigest = -1;
 513          }
 514  
 515          $renderer = $PAGE->get_renderer('mod_forum');
 516          return $OUTPUT->render($renderer->render_digest_options($forum, $forum->maildigest));
 517      } else {
 518          // This user can subscribe to some forums. Add the empty fields.
 519          return '';
 520      }
 521  };