3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Subscribe to or unsubscribe from a forum or manage forum subscription mode
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
30 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 require(__DIR__
.'/../../config.php');
35 require_once($CFG->dirroot
.'/mod/forum/lib.php');
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
);
44 $url = new moodle_url('/mod/forum/subscribe.php', array('id'=>$id));
45 if (!is_null($mode)) {
46 $url->param('mode', $mode);
49 $url->param('user', $user);
51 if (!is_null($sesskey)) {
52 $url->param('sesskey', $sesskey);
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');
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
);
69 if (!has_capability('mod/forum:managesubscriptions', $context)) {
70 print_error('nopermissiontosubscribe', 'forum');
72 $user = $DB->get_record('user', array('id' => $user), '*', MUST_EXIST
);
77 if (isset($cm->groupmode
) && empty($course->groupmodeforce
)) {
78 $groupmode = $cm->groupmode
;
80 $groupmode = $course->groupmode
;
83 $issubscribed = \mod_forum\subscriptions
::is_subscribed($user->id
, $forum, $discussionid, $cm);
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');
92 require_login($course, false, $cm);
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
);
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();
104 // There should not be any links leading to this place, just redirect.
106 new moodle_url('/mod/forum/view.php', array('f'=>$id)),
107 get_string('subscribeenrolledonly', 'forum'),
109 \core\output\notification
::NOTIFY_ERROR
114 $returnto = optional_param('backtoindex',0,PARAM_INT
)
115 ?
"index.php?id=".$course->id
119 $returnto = $returnurl;
122 if (!is_null($mode) and has_capability('mod/forum:managesubscriptions', $context)) {
125 case FORUM_CHOOSESUBSCRIBE
: // 0
126 \mod_forum\subscriptions
::set_subscription_mode($forum->id
, FORUM_CHOOSESUBSCRIBE
);
129 get_string('everyonecannowchoose', 'forum'),
131 \core\output\notification
::NOTIFY_SUCCESS
134 case FORUM_FORCESUBSCRIBE
: // 1
135 \mod_forum\subscriptions
::set_subscription_mode($forum->id
, FORUM_FORCESUBSCRIBE
);
138 get_string('everyoneisnowsubscribed', 'forum'),
140 \core\output\notification
::NOTIFY_SUCCESS
143 case FORUM_INITIALSUBSCRIBE
: // 2
144 if ($forum->forcesubscribe
<> FORUM_INITIALSUBSCRIBE
) {
145 $users = \mod_forum\subscriptions
::get_potential_subscribers($context, 0, 'u.id, u.email', '');
146 foreach ($users as $user) {
147 \mod_forum\subscriptions
::subscribe_user($user->id
, $forum, $context);
150 \mod_forum\subscriptions
::set_subscription_mode($forum->id
, FORUM_INITIALSUBSCRIBE
);
153 get_string('everyoneisnowsubscribed', 'forum'),
155 \core\output\notification
::NOTIFY_SUCCESS
158 case FORUM_DISALLOWSUBSCRIBE
: // 3
159 \mod_forum\subscriptions
::set_subscription_mode($forum->id
, FORUM_DISALLOWSUBSCRIBE
);
162 get_string('noonecansubscribenow', 'forum'),
164 \core\output\notification
::NOTIFY_SUCCESS
168 print_error(get_string('invalidforcesubscribe', 'forum'));
172 if (\mod_forum\subscriptions
::is_forcesubscribed($forum)) {
175 get_string('everyoneisnowsubscribed', 'forum'),
177 \core\output\notification
::NOTIFY_SUCCESS
181 $info = new stdClass();
182 $info->name
= fullname($user);
183 $info->forum
= format_string($forum->name
);
186 if (is_null($sesskey)) {
187 // We came here via link in email.
188 $PAGE->set_title($course->shortname
);
189 $PAGE->set_heading($course->fullname
);
190 echo $OUTPUT->header();
192 $viewurl = new moodle_url('/mod/forum/view.php', array('f' => $id));
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);
200 echo $OUTPUT->confirm(get_string('confirmunsubscribe', 'forum', format_string($forum->name
)),
201 $PAGE->url
, $viewurl);
203 echo $OUTPUT->footer();
207 if ($discussionid === null) {
208 if (\mod_forum\subscriptions
::unsubscribe_user($user->id
, $forum, $context, true)) {
211 get_string('nownotsubscribed', 'forum', $info),
213 \core\output\notification
::NOTIFY_SUCCESS
216 print_error('cannotunsubscribe', 'forum', get_local_referer(false));
219 if (\mod_forum\subscriptions
::unsubscribe_user_from_discussion($user->id
, $discussion, $context)) {
220 $info->discussion
= $discussion->name
;
223 get_string('discussionnownotsubscribed', 'forum', $info),
225 \core\output\notification
::NOTIFY_SUCCESS
228 print_error('cannotunsubscribe', 'forum', get_local_referer(false));
232 } else { // subscribe
233 if (\mod_forum\subscriptions
::subscription_disabled($forum) && !has_capability('mod/forum:managesubscriptions', $context)) {
234 print_error('disallowsubscribe', 'forum', get_local_referer(false));
236 if (!has_capability('mod/forum:viewdiscussion', $context)) {
237 print_error('noviewdiscussionspermission', 'forum', get_local_referer(false));
239 if (is_null($sesskey)) {
240 // We came here via link in email.
241 $PAGE->set_title($course->shortname
);
242 $PAGE->set_heading($course->fullname
);
243 echo $OUTPUT->header();
245 $viewurl = new moodle_url('/mod/forum/view.php', array('f' => $id));
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);
253 echo $OUTPUT->confirm(get_string('confirmsubscribe', 'forum', format_string($forum->name
)),
254 $PAGE->url
, $viewurl);
256 echo $OUTPUT->footer();
260 if ($discussionid == null) {
261 \mod_forum\subscriptions
::subscribe_user($user->id
, $forum, $context, true);
264 get_string('nowsubscribed', 'forum', $info),
266 \core\output\notification
::NOTIFY_SUCCESS
269 $info->discussion
= $discussion->name
;
270 \mod_forum\subscriptions
::subscribe_user_to_discussion($user->id
, $discussion, $context);
273 get_string('discussionnowsubscribed', 'forum', $info),
275 \core\output\notification
::NOTIFY_SUCCESS