weekly release 4.0dev
[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
1fcf0ca8 34require(__DIR__.'/../../config.php');
6f7d65c7 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);
32e2e06a 42$returnurl = optional_param('returnurl', null, PARAM_LOCALURL);
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);
6b587d2b
AN
56 if (!$discussion = $DB->get_record('forum_discussions', array('id' => $discussionid, 'forum' => $id))) {
57 print_error('invaliddiscussionid', 'forum');
58 }
62208137 59}
6f7d65c7 60$PAGE->set_url($url);
cd4e6b17 61
6f7d65c7 62$forum = $DB->get_record('forum', array('id' => $id), '*', MUST_EXIST);
74df2951 63$course = $DB->get_record('course', array('id' => $forum->course), '*', MUST_EXIST);
6f7d65c7 64$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id, false, MUST_EXIST);
bf0f06b1 65$context = context_module::instance($cm->id);
cd4e6b17 66
67if ($user) {
6f7d65c7 68 require_sesskey();
cd4e6b17 69 if (!has_capability('mod/forum:managesubscriptions', $context)) {
70 print_error('nopermissiontosubscribe', 'forum');
f93f848a 71 }
4cd5ae4f 72 $user = $DB->get_record('user', array('id' => $user), '*', MUST_EXIST);
cd4e6b17 73} else {
74 $user = $USER;
75}
76
77if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
6f7d65c7 78 $groupmode = $cm->groupmode;
cd4e6b17 79} else {
80 $groupmode = $course->groupmode;
81}
2a9f525b
AN
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.
772e9dc1 86if ($groupmode && !$issubscribed && !has_capability('moodle/site:accessallgroups', $context)) {
cd4e6b17 87 if (!groups_get_all_groups($course->id, $USER->id)) {
88 print_error('cannotsubscribe', 'forum');
ec81373f 89 }
cd4e6b17 90}
ec81373f 91
cdbea7ee 92require_login($course, false, $cm);
cd4e6b17 93
72650054 94if (is_null($mode) and !is_enrolled($context, $USER, '', true)) { // Guests and visitors can't subscribe - only enrolled
cd4e6b17 95 $PAGE->set_title($course->shortname);
96 $PAGE->set_heading($course->fullname);
72650054
PS
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 {
d954b54f
AN
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 );
72650054 111 }
cd4e6b17 112}
113
114$returnto = optional_param('backtoindex',0,PARAM_INT)
115 ? "index.php?id=".$course->id
116 : "view.php?f=$id";
117
ebfb73db
AN
118if ($returnurl) {
119 $returnto = $returnurl;
120}
121
6f7d65c7
DM
122if (!is_null($mode) and has_capability('mod/forum:managesubscriptions', $context)) {
123 require_sesskey();
afef965e
SH
124 switch ($mode) {
125 case FORUM_CHOOSESUBSCRIBE : // 0
59075a43 126 \mod_forum\subscriptions::set_subscription_mode($forum->id, FORUM_CHOOSESUBSCRIBE);
d954b54f
AN
127 redirect(
128 $returnto,
129 get_string('everyonecannowchoose', 'forum'),
130 null,
131 \core\output\notification::NOTIFY_SUCCESS
132 );
afef965e
SH
133 break;
134 case FORUM_FORCESUBSCRIBE : // 1
59075a43 135 \mod_forum\subscriptions::set_subscription_mode($forum->id, FORUM_FORCESUBSCRIBE);
d954b54f
AN
136 redirect(
137 $returnto,
138 get_string('everyoneisnowsubscribed', 'forum'),
139 null,
140 \core\output\notification::NOTIFY_SUCCESS
141 );
afef965e
SH
142 break;
143 case FORUM_INITIALSUBSCRIBE : // 2
4a913724 144 if ($forum->forcesubscribe <> FORUM_INITIALSUBSCRIBE) {
59075a43 145 $users = \mod_forum\subscriptions::get_potential_subscribers($context, 0, 'u.id, u.email', '');
4a913724 146 foreach ($users as $user) {
e3bbfb52 147 \mod_forum\subscriptions::subscribe_user($user->id, $forum, $context);
4a913724
GPL
148 }
149 }
59075a43 150 \mod_forum\subscriptions::set_subscription_mode($forum->id, FORUM_INITIALSUBSCRIBE);
d954b54f
AN
151 redirect(
152 $returnto,
153 get_string('everyoneisnowsubscribed', 'forum'),
154 null,
155 \core\output\notification::NOTIFY_SUCCESS
156 );
afef965e
SH
157 break;
158 case FORUM_DISALLOWSUBSCRIBE : // 3
59075a43 159 \mod_forum\subscriptions::set_subscription_mode($forum->id, FORUM_DISALLOWSUBSCRIBE);
d954b54f
AN
160 redirect(
161 $returnto,
162 get_string('noonecansubscribenow', 'forum'),
163 null,
164 \core\output\notification::NOTIFY_SUCCESS
165 );
afef965e
SH
166 break;
167 default:
168 print_error(get_string('invalidforcesubscribe', 'forum'));
3bc9bf97 169 }
cd4e6b17 170}
3bc9bf97 171
59075a43 172if (\mod_forum\subscriptions::is_forcesubscribed($forum)) {
d954b54f
AN
173 redirect(
174 $returnto,
175 get_string('everyoneisnowsubscribed', 'forum'),
176 null,
177 \core\output\notification::NOTIFY_SUCCESS
178 );
cd4e6b17 179}
818c1b05 180
ecabd5b0 181$info = new stdClass();
cd4e6b17 182$info->name = fullname($user);
183$info->forum = format_string($forum->name);
ec81373f 184
2a9f525b 185if ($issubscribed) {
62208137
AN
186 if (is_null($sesskey)) {
187 // We came here via link in email.
6f7d65c7
DM
188 $PAGE->set_title($course->shortname);
189 $PAGE->set_heading($course->fullname);
190 echo $OUTPUT->header();
62208137
AN
191
192 $viewurl = new moodle_url('/mod/forum/view.php', array('f' => $id));
193 if ($discussionid) {
194 $a = new stdClass();
195 $a->forum = format_string($forum->name);
196 $a->discussion = format_string($discussion->name);
197 echo $OUTPUT->confirm(get_string('confirmunsubscribediscussion', 'forum', $a),
198 $PAGE->url, $viewurl);
199 } else {
200 echo $OUTPUT->confirm(get_string('confirmunsubscribe', 'forum', format_string($forum->name)),
201 $PAGE->url, $viewurl);
202 }
6f7d65c7
DM
203 echo $OUTPUT->footer();
204 exit;
205 }
206 require_sesskey();
ebfb73db
AN
207 if ($discussionid === null) {
208 if (\mod_forum\subscriptions::unsubscribe_user($user->id, $forum, $context, true)) {
d954b54f
AN
209 redirect(
210 $returnto,
211 get_string('nownotsubscribed', 'forum', $info),
212 null,
213 \core\output\notification::NOTIFY_SUCCESS
214 );
ebfb73db 215 } else {
dcee0b94 216 print_error('cannotunsubscribe', 'forum', get_local_referer(false));
ebfb73db 217 }
cd4e6b17 218 } else {
ebfb73db 219 if (\mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion, $context)) {
4c340898 220 $info->discussion = $discussion->name;
d954b54f
AN
221 redirect(
222 $returnto,
223 get_string('discussionnownotsubscribed', 'forum', $info),
224 null,
225 \core\output\notification::NOTIFY_SUCCESS
226 );
ebfb73db 227 } else {
dcee0b94 228 print_error('cannotunsubscribe', 'forum', get_local_referer(false));
ebfb73db 229 }
f93f848a 230 }
231
cd4e6b17 232} else { // subscribe
59075a43 233 if (\mod_forum\subscriptions::subscription_disabled($forum) && !has_capability('mod/forum:managesubscriptions', $context)) {
dcee0b94 234 print_error('disallowsubscribe', 'forum', get_local_referer(false));
501cdbd8 235 }
cd4e6b17 236 if (!has_capability('mod/forum:viewdiscussion', $context)) {
dcee0b94 237 print_error('noviewdiscussionspermission', 'forum', get_local_referer(false));
f93f848a 238 }
62208137
AN
239 if (is_null($sesskey)) {
240 // We came here via link in email.
6f7d65c7
DM
241 $PAGE->set_title($course->shortname);
242 $PAGE->set_heading($course->fullname);
243 echo $OUTPUT->header();
62208137
AN
244
245 $viewurl = new moodle_url('/mod/forum/view.php', array('f' => $id));
246 if ($discussionid) {
247 $a = new stdClass();
248 $a->forum = format_string($forum->name);
249 $a->discussion = format_string($discussion->name);
250 echo $OUTPUT->confirm(get_string('confirmsubscribediscussion', 'forum', $a),
251 $PAGE->url, $viewurl);
252 } else {
253 echo $OUTPUT->confirm(get_string('confirmsubscribe', 'forum', format_string($forum->name)),
254 $PAGE->url, $viewurl);
255 }
6f7d65c7
DM
256 echo $OUTPUT->footer();
257 exit;
258 }
259 require_sesskey();
ebfb73db
AN
260 if ($discussionid == null) {
261 \mod_forum\subscriptions::subscribe_user($user->id, $forum, $context, true);
d954b54f
AN
262 redirect(
263 $returnto,
264 get_string('nowsubscribed', 'forum', $info),
265 null,
266 \core\output\notification::NOTIFY_SUCCESS
267 );
ebfb73db 268 } else {
4c340898 269 $info->discussion = $discussion->name;
ebfb73db 270 \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion, $context);
d954b54f
AN
271 redirect(
272 $returnto,
273 get_string('discussionnowsubscribed', 'forum', $info),
274 null,
275 \core\output\notification::NOTIFY_SUCCESS
276 );
ebfb73db 277 }
cd4e6b17 278}