MDL-30811 forum: Update use of redirect to include message and type
[moodle.git] / mod / forum / subscribe.php
CommitLineData
cd4e6b17 1<?php
f93f848a 2
8f685009
SH
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/**
6f7d65c7 19 * Subscribe to or unsubscribe from a forum or manage forum subscription mode
2f67a9b3 20 *
6f7d65c7
DM
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 *
01030f1b 29 * @package mod_forum
6f7d65c7
DM
30 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
8f685009 32 */
f93f848a 33
6f7d65c7
DM
34require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
35require_once($CFG->dirroot.'/mod/forum/lib.php');
cd4e6b17 36
ebfb73db
AN
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_RAW);
cd4e6b17 43
a6855934 44$url = new moodle_url('/mod/forum/subscribe.php', array('id'=>$id));
6f7d65c7 45if (!is_null($mode)) {
99eb51d9 46 $url->param('mode', $mode);
cd4e6b17 47}
48if ($user !== 0) {
49 $url->param('user', $user);
50}
6f7d65c7
DM
51if (!is_null($sesskey)) {
52 $url->param('sesskey', $sesskey);
cd4e6b17 53}
62208137
AN
54if (!is_null($discussionid)) {
55 $url->param('d', $discussionid);
56 $discussion = $DB->get_record('forum_discussions', array('id' => $discussionid), '*', MUST_EXIST);
57}
6f7d65c7 58$PAGE->set_url($url);
cd4e6b17 59
6f7d65c7 60$forum = $DB->get_record('forum', array('id' => $id), '*', MUST_EXIST);
74df2951 61$course = $DB->get_record('course', array('id' => $forum->course), '*', MUST_EXIST);
6f7d65c7 62$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id, false, MUST_EXIST);
bf0f06b1 63$context = context_module::instance($cm->id);
cd4e6b17 64
65if ($user) {
6f7d65c7 66 require_sesskey();
cd4e6b17 67 if (!has_capability('mod/forum:managesubscriptions', $context)) {
68 print_error('nopermissiontosubscribe', 'forum');
f93f848a 69 }
4cd5ae4f 70 $user = $DB->get_record('user', array('id' => $user), '*', MUST_EXIST);
cd4e6b17 71} else {
72 $user = $USER;
73}
74
75if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
6f7d65c7 76 $groupmode = $cm->groupmode;
cd4e6b17 77} else {
78 $groupmode = $course->groupmode;
79}
2a9f525b
AN
80
81$issubscribed = \mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussionid, $cm);
82
83// For a user to subscribe when a groupmode is set, they must have access to at least one group.
772e9dc1 84if ($groupmode && !$issubscribed && !has_capability('moodle/site:accessallgroups', $context)) {
cd4e6b17 85 if (!groups_get_all_groups($course->id, $USER->id)) {
86 print_error('cannotsubscribe', 'forum');
ec81373f 87 }
cd4e6b17 88}
ec81373f 89
cdbea7ee 90require_login($course, false, $cm);
cd4e6b17 91
72650054 92if (is_null($mode) and !is_enrolled($context, $USER, '', true)) { // Guests and visitors can't subscribe - only enrolled
cd4e6b17 93 $PAGE->set_title($course->shortname);
94 $PAGE->set_heading($course->fullname);
72650054
PS
95 if (isguestuser()) {
96 echo $OUTPUT->header();
97 echo $OUTPUT->confirm(get_string('subscribeenrolledonly', 'forum').'<br /><br />'.get_string('liketologin'),
98 get_login_url(), new moodle_url('/mod/forum/view.php', array('f'=>$id)));
99 echo $OUTPUT->footer();
100 exit;
101 } else {
d954b54f
AN
102 // There should not be any links leading to this place, just redirect.
103 redirect(
104 new moodle_url('/mod/forum/view.php', array('f'=>$id)),
105 get_string('subscribeenrolledonly', 'forum'),
106 null,
107 \core\output\notification::NOTIFY_ERROR
108 );
72650054 109 }
cd4e6b17 110}
111
112$returnto = optional_param('backtoindex',0,PARAM_INT)
113 ? "index.php?id=".$course->id
114 : "view.php?f=$id";
115
ebfb73db
AN
116if ($returnurl) {
117 $returnto = $returnurl;
118}
119
6f7d65c7
DM
120if (!is_null($mode) and has_capability('mod/forum:managesubscriptions', $context)) {
121 require_sesskey();
afef965e
SH
122 switch ($mode) {
123 case FORUM_CHOOSESUBSCRIBE : // 0
59075a43 124 \mod_forum\subscriptions::set_subscription_mode($forum->id, FORUM_CHOOSESUBSCRIBE);
d954b54f
AN
125 redirect(
126 $returnto,
127 get_string('everyonecannowchoose', 'forum'),
128 null,
129 \core\output\notification::NOTIFY_SUCCESS
130 );
afef965e
SH
131 break;
132 case FORUM_FORCESUBSCRIBE : // 1
59075a43 133 \mod_forum\subscriptions::set_subscription_mode($forum->id, FORUM_FORCESUBSCRIBE);
d954b54f
AN
134 redirect(
135 $returnto,
136 get_string('everyoneisnowsubscribed', 'forum'),
137 null,
138 \core\output\notification::NOTIFY_SUCCESS
139 );
afef965e
SH
140 break;
141 case FORUM_INITIALSUBSCRIBE : // 2
4a913724 142 if ($forum->forcesubscribe <> FORUM_INITIALSUBSCRIBE) {
59075a43 143 $users = \mod_forum\subscriptions::get_potential_subscribers($context, 0, 'u.id, u.email', '');
4a913724 144 foreach ($users as $user) {
e3bbfb52 145 \mod_forum\subscriptions::subscribe_user($user->id, $forum, $context);
4a913724
GPL
146 }
147 }
59075a43 148 \mod_forum\subscriptions::set_subscription_mode($forum->id, FORUM_INITIALSUBSCRIBE);
d954b54f
AN
149 redirect(
150 $returnto,
151 get_string('everyoneisnowsubscribed', 'forum'),
152 null,
153 \core\output\notification::NOTIFY_SUCCESS
154 );
afef965e
SH
155 break;
156 case FORUM_DISALLOWSUBSCRIBE : // 3
59075a43 157 \mod_forum\subscriptions::set_subscription_mode($forum->id, FORUM_DISALLOWSUBSCRIBE);
d954b54f
AN
158 redirect(
159 $returnto,
160 get_string('noonecansubscribenow', 'forum'),
161 null,
162 \core\output\notification::NOTIFY_SUCCESS
163 );
afef965e
SH
164 break;
165 default:
166 print_error(get_string('invalidforcesubscribe', 'forum'));
3bc9bf97 167 }
cd4e6b17 168}
3bc9bf97 169
59075a43 170if (\mod_forum\subscriptions::is_forcesubscribed($forum)) {
d954b54f
AN
171 redirect(
172 $returnto,
173 get_string('everyoneisnowsubscribed', 'forum'),
174 null,
175 \core\output\notification::NOTIFY_SUCCESS
176 );
cd4e6b17 177}
818c1b05 178
ecabd5b0 179$info = new stdClass();
cd4e6b17 180$info->name = fullname($user);
181$info->forum = format_string($forum->name);
ec81373f 182
2a9f525b 183if ($issubscribed) {
62208137
AN
184 if (is_null($sesskey)) {
185 // We came here via link in email.
6f7d65c7
DM
186 $PAGE->set_title($course->shortname);
187 $PAGE->set_heading($course->fullname);
188 echo $OUTPUT->header();
62208137
AN
189
190 $viewurl = new moodle_url('/mod/forum/view.php', array('f' => $id));
191 if ($discussionid) {
192 $a = new stdClass();
193 $a->forum = format_string($forum->name);
194 $a->discussion = format_string($discussion->name);
195 echo $OUTPUT->confirm(get_string('confirmunsubscribediscussion', 'forum', $a),
196 $PAGE->url, $viewurl);
197 } else {
198 echo $OUTPUT->confirm(get_string('confirmunsubscribe', 'forum', format_string($forum->name)),
199 $PAGE->url, $viewurl);
200 }
6f7d65c7
DM
201 echo $OUTPUT->footer();
202 exit;
203 }
204 require_sesskey();
ebfb73db
AN
205 if ($discussionid === null) {
206 if (\mod_forum\subscriptions::unsubscribe_user($user->id, $forum, $context, true)) {
d954b54f
AN
207 redirect(
208 $returnto,
209 get_string('nownotsubscribed', 'forum', $info),
210 null,
211 \core\output\notification::NOTIFY_SUCCESS
212 );
ebfb73db 213 } else {
dcee0b94 214 print_error('cannotunsubscribe', 'forum', get_local_referer(false));
ebfb73db 215 }
cd4e6b17 216 } else {
ebfb73db 217 if (\mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion, $context)) {
4c340898 218 $info->discussion = $discussion->name;
d954b54f
AN
219 redirect(
220 $returnto,
221 get_string('discussionnownotsubscribed', 'forum', $info),
222 null,
223 \core\output\notification::NOTIFY_SUCCESS
224 );
ebfb73db 225 } else {
dcee0b94 226 print_error('cannotunsubscribe', 'forum', get_local_referer(false));
ebfb73db 227 }
f93f848a 228 }
229
cd4e6b17 230} else { // subscribe
59075a43 231 if (\mod_forum\subscriptions::subscription_disabled($forum) && !has_capability('mod/forum:managesubscriptions', $context)) {
dcee0b94 232 print_error('disallowsubscribe', 'forum', get_local_referer(false));
501cdbd8 233 }
cd4e6b17 234 if (!has_capability('mod/forum:viewdiscussion', $context)) {
dcee0b94 235 print_error('noviewdiscussionspermission', 'forum', get_local_referer(false));
f93f848a 236 }
62208137
AN
237 if (is_null($sesskey)) {
238 // We came here via link in email.
6f7d65c7
DM
239 $PAGE->set_title($course->shortname);
240 $PAGE->set_heading($course->fullname);
241 echo $OUTPUT->header();
62208137
AN
242
243 $viewurl = new moodle_url('/mod/forum/view.php', array('f' => $id));
244 if ($discussionid) {
245 $a = new stdClass();
246 $a->forum = format_string($forum->name);
247 $a->discussion = format_string($discussion->name);
248 echo $OUTPUT->confirm(get_string('confirmsubscribediscussion', 'forum', $a),
249 $PAGE->url, $viewurl);
250 } else {
251 echo $OUTPUT->confirm(get_string('confirmsubscribe', 'forum', format_string($forum->name)),
252 $PAGE->url, $viewurl);
253 }
6f7d65c7
DM
254 echo $OUTPUT->footer();
255 exit;
256 }
257 require_sesskey();
ebfb73db
AN
258 if ($discussionid == null) {
259 \mod_forum\subscriptions::subscribe_user($user->id, $forum, $context, true);
d954b54f
AN
260 redirect(
261 $returnto,
262 get_string('nowsubscribed', 'forum', $info),
263 null,
264 \core\output\notification::NOTIFY_SUCCESS
265 );
ebfb73db 266 } else {
4c340898 267 $info->discussion = $discussion->name;
ebfb73db 268 \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion, $context);
d954b54f
AN
269 redirect(
270 $returnto,
271 get_string('discussionnowsubscribed', 'forum', $info),
272 null,
273 \core\output\notification::NOTIFY_SUCCESS
274 );
ebfb73db 275 }
cd4e6b17 276}