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