Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 403]

   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   * Subscribe to or unsubscribe from a forum or manage forum subscription mode
  20   *
  21   * This script can be used by either individual users to subscribe to or
  22   * unsubscribe from a forum (no 'mode' param provided), or by forum managers
  23   * to control the subscription mode (by 'mode' param).
  24   * This script can be called from a link in email so the sesskey is not
  25   * required parameter. However, if sesskey is missing, the user has to go
  26   * through a confirmation page that redirects the user back with the
  27   * sesskey.
  28   *
  29   * @package   mod_forum
  30   * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  
  34  require(__DIR__.'/../../config.php');
  35  require_once($CFG->dirroot.'/mod/forum/lib.php');
  36  
  37  $id             = required_param('id', PARAM_INT);             // The forum to set subscription on.
  38  $mode           = optional_param('mode', null, PARAM_INT);     // The forum's subscription mode.
  39  $user           = optional_param('user', 0, PARAM_INT);        // The userid of the user to subscribe, defaults to $USER.
  40  $discussionid   = optional_param('d', null, PARAM_INT);        // The discussionid to subscribe.
  41  $sesskey        = optional_param('sesskey', null, PARAM_RAW);
  42  $returnurl      = optional_param('returnurl', null, PARAM_LOCALURL);
  43  $edit           = optional_param('edit', 'off', PARAM_ALPHANUM);
  44  
  45  $url = new moodle_url('/mod/forum/subscribe.php', array('id'=>$id));
  46  if (!is_null($mode)) {
  47      $url->param('mode', $mode);
  48  }
  49  if ($user !== 0) {
  50      $url->param('user', $user);
  51  }
  52  if (!is_null($sesskey)) {
  53      $url->param('sesskey', $sesskey);
  54  }
  55  if (!is_null($discussionid)) {
  56      $url->param('d', $discussionid);
  57      if (!$discussion = $DB->get_record('forum_discussions', array('id' => $discussionid, 'forum' => $id))) {
  58          throw new \moodle_exception('invaliddiscussionid', 'forum');
  59      }
  60  }
  61  $PAGE->set_url($url);
  62  
  63  $forum   = $DB->get_record('forum', array('id' => $id), '*', MUST_EXIST);
  64  $course  = $DB->get_record('course', array('id' => $forum->course), '*', MUST_EXIST);
  65  $cm      = get_coursemodule_from_instance('forum', $forum->id, $course->id, false, MUST_EXIST);
  66  $context = context_module::instance($cm->id);
  67  
  68  if ($user) {
  69      require_sesskey();
  70      if (!has_capability('mod/forum:managesubscriptions', $context)) {
  71          throw new \moodle_exception('nopermissiontosubscribe', 'forum');
  72      }
  73      $user = $DB->get_record('user', array('id' => $user), '*', MUST_EXIST);
  74  } else {
  75      $user = $USER;
  76  }
  77  
  78  if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
  79      $groupmode = $cm->groupmode;
  80  } else {
  81      $groupmode = $course->groupmode;
  82  }
  83  
  84  $issubscribed = \mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussionid, $cm);
  85  
  86  // For a user to subscribe when a groupmode is set, they must have access to at least one group.
  87  if ($groupmode && !$issubscribed && !has_capability('moodle/site:accessallgroups', $context)) {
  88      if (!groups_get_all_groups($course->id, $USER->id)) {
  89          throw new \moodle_exception('cannotsubscribe', 'forum');
  90      }
  91  }
  92  
  93  require_login($course, false, $cm);
  94  
  95  if (is_null($mode) and !is_enrolled($context, $USER, '', true)) {   // Guests and visitors can't subscribe - only enrolled
  96      $PAGE->set_title($course->shortname);
  97      $PAGE->set_heading($course->fullname);
  98      if (isguestuser()) {
  99          echo $OUTPUT->header();
 100          echo $OUTPUT->confirm(get_string('subscribeenrolledonly', 'forum').'<br /><br />'.get_string('liketologin'),
 101                       get_login_url(), new moodle_url('/mod/forum/view.php', array('f'=>$id)));
 102          echo $OUTPUT->footer();
 103          exit;
 104      } else {
 105          // There should not be any links leading to this place, just redirect.
 106          redirect(
 107                  new moodle_url('/mod/forum/view.php', array('f'=>$id)),
 108                  get_string('subscribeenrolledonly', 'forum'),
 109                  null,
 110                  \core\output\notification::NOTIFY_ERROR
 111              );
 112      }
 113  }
 114  
 115  $returnto = optional_param('backtoindex',0,PARAM_INT)
 116      ? "index.php?id=".$course->id
 117      : "view.php?f=$id";
 118  
 119  if ($returnurl) {
 120      $returnto = $returnurl;
 121  }
 122  
 123  $subscribersurl = new moodle_url('/mod/forum/subscribers.php', ['id' => $id, 'edit' => $edit]);
 124  
 125  if (!is_null($mode) and has_capability('mod/forum:managesubscriptions', $context)) {
 126      require_sesskey();
 127      switch ($mode) {
 128          case FORUM_CHOOSESUBSCRIBE : // 0
 129              \mod_forum\subscriptions::set_subscription_mode($forum->id, FORUM_CHOOSESUBSCRIBE);
 130              redirect(
 131                      $subscribersurl,
 132                      get_string('everyonecannowchoose', 'forum'),
 133                      null,
 134                      \core\output\notification::NOTIFY_SUCCESS
 135              );
 136              break;
 137          case FORUM_FORCESUBSCRIBE : // 1
 138              \mod_forum\subscriptions::set_subscription_mode($forum->id, FORUM_FORCESUBSCRIBE);
 139              redirect(
 140                      $subscribersurl,
 141                      get_string('everyoneisnowsubscribed', 'forum'),
 142                      null,
 143                      \core\output\notification::NOTIFY_SUCCESS
 144              );
 145              break;
 146          case FORUM_INITIALSUBSCRIBE : // 2
 147              \mod_forum\subscriptions::set_subscription_mode($forum->id, FORUM_INITIALSUBSCRIBE);
 148              if ($forum->forcesubscribe <> FORUM_INITIALSUBSCRIBE) {
 149                  // Reload the forum again to get the updated forcesubscribe field.
 150                  $forum = $DB->get_record('forum', array('id' => $id), '*', MUST_EXIST);
 151                  $users = \mod_forum\subscriptions::get_potential_subscribers($context, 0, 'u.id, u.email', '');
 152                  foreach ($users as $user) {
 153                      \mod_forum\subscriptions::subscribe_user($user->id, $forum, $context);
 154                  }
 155              }
 156              redirect(
 157                      $subscribersurl,
 158                      get_string('everyoneisnowsubscribed', 'forum'),
 159                      null,
 160                      \core\output\notification::NOTIFY_SUCCESS
 161              );
 162              break;
 163          case FORUM_DISALLOWSUBSCRIBE : // 3
 164              \mod_forum\subscriptions::set_subscription_mode($forum->id, FORUM_DISALLOWSUBSCRIBE);
 165              redirect(
 166                      $subscribersurl,
 167                      get_string('noonecansubscribenow', 'forum'),
 168                      null,
 169                      \core\output\notification::NOTIFY_SUCCESS
 170              );
 171              break;
 172          default:
 173              throw new \moodle_exception(get_string('invalidforcesubscribe', 'forum'));
 174      }
 175  }
 176  
 177  if (\mod_forum\subscriptions::is_forcesubscribed($forum)) {
 178      $subscribersurl->param('notification', 'everyoneisnowsubscribed');
 179      redirect($subscribersurl);
 180  }
 181  
 182  $info = new stdClass();
 183  $info->name  = fullname($user);
 184  $info->forum = format_string($forum->name);
 185  
 186  if ($issubscribed) {
 187      if (is_null($sesskey)) {
 188          // We came here via link in email.
 189          $PAGE->set_title($course->shortname);
 190          $PAGE->set_heading($course->fullname);
 191          echo $OUTPUT->header();
 192  
 193          $viewurl = new moodle_url('/mod/forum/view.php', array('f' => $id));
 194          if ($discussionid) {
 195              $a = new stdClass();
 196              $a->forum = format_string($forum->name);
 197              $a->discussion = format_string($discussion->name);
 198              echo $OUTPUT->confirm(get_string('confirmunsubscribediscussion', 'forum', $a),
 199                      $PAGE->url, $viewurl);
 200          } else {
 201              echo $OUTPUT->confirm(get_string('confirmunsubscribe', 'forum', format_string($forum->name)),
 202                      $PAGE->url, $viewurl);
 203          }
 204          echo $OUTPUT->footer();
 205          exit;
 206      }
 207      require_sesskey();
 208      if ($discussionid === null) {
 209          if (\mod_forum\subscriptions::unsubscribe_user($user->id, $forum, $context, true)) {
 210              redirect(
 211                  $returnto,
 212                  get_string('nownotsubscribed', 'forum', $info),
 213                  null,
 214                  \core\output\notification::NOTIFY_SUCCESS
 215              );
 216          } else {
 217              throw new \moodle_exception('cannotunsubscribe', 'forum', get_local_referer(false));
 218          }
 219      } else {
 220          if (\mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion, $context)) {
 221              $info->discussion = $discussion->name;
 222              redirect(
 223                  $returnto,
 224                  get_string('discussionnownotsubscribed', 'forum', $info),
 225                  null,
 226                  \core\output\notification::NOTIFY_SUCCESS
 227              );
 228          } else {
 229              throw new \moodle_exception('cannotunsubscribe', 'forum', get_local_referer(false));
 230          }
 231      }
 232  
 233  } else {  // subscribe
 234      if (\mod_forum\subscriptions::subscription_disabled($forum) && !has_capability('mod/forum:managesubscriptions', $context)) {
 235          throw new \moodle_exception('disallowsubscribe', 'forum', get_local_referer(false));
 236      }
 237      if (!has_capability('mod/forum:viewdiscussion', $context)) {
 238          throw new \moodle_exception('noviewdiscussionspermission', 'forum', get_local_referer(false));
 239      }
 240      if (is_null($sesskey)) {
 241          // We came here via link in email.
 242          $PAGE->set_title($course->shortname);
 243          $PAGE->set_heading($course->fullname);
 244          echo $OUTPUT->header();
 245  
 246          $viewurl = new moodle_url('/mod/forum/view.php', array('f' => $id));
 247          if ($discussionid) {
 248              $a = new stdClass();
 249              $a->forum = format_string($forum->name);
 250              $a->discussion = format_string($discussion->name);
 251              echo $OUTPUT->confirm(get_string('confirmsubscribediscussion', 'forum', $a),
 252                      $PAGE->url, $viewurl);
 253          } else {
 254              echo $OUTPUT->confirm(get_string('confirmsubscribe', 'forum', format_string($forum->name)),
 255                      $PAGE->url, $viewurl);
 256          }
 257          echo $OUTPUT->footer();
 258          exit;
 259      }
 260      require_sesskey();
 261      if ($discussionid == null) {
 262          \mod_forum\subscriptions::subscribe_user($user->id, $forum, $context, true);
 263          redirect(
 264              $returnto,
 265              get_string('nowsubscribed', 'forum', $info),
 266              null,
 267              \core\output\notification::NOTIFY_SUCCESS
 268          );
 269      } else {
 270          $info->discussion = $discussion->name;
 271          \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion, $context);
 272          redirect(
 273              $returnto,
 274              get_string('discussionnowsubscribed', 'forum', $info),
 275              null,
 276              \core\output\notification::NOTIFY_SUCCESS
 277          );
 278      }
 279  }