-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsubscribe.php
431 lines (408 loc) · 16.8 KB
/
subscribe.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This script handles requests to subscribe/unsubscribe from a forum or a discussion.
* It operates in two modes: 'go back' mode, where after subscribing it
* redirects, and 'full' mode (normally used only for links in email) where
* it displays information about the action.
*
* Specify either course (id) or (course-module) id or discussion (d). If you specify a course
* then it subscribes/unsubscribes to everything you have access to on that
* course.
* @package mod
* @subpackage forumng
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../../config.php');
require_once('mod_forumng.php');
$courseid = optional_param('course', 0, PARAM_INT);
$cmid = optional_param('id', 0, PARAM_INT);
$discussionid = optional_param('d', 0, PARAM_INT);
$groupid = optional_param('g', 0, PARAM_INT);
$cloneid = optional_param('clone', 0, PARAM_INT);
$pageparams = array();
if ($courseid) {
$pageparams['course'] = $courseid;
}
if ($cmid) {
$pageparams['id'] = $cmid;
}
if ($discussionid) {
$pageparams['d'] = $discussionid;
}
if ($groupid) {
$pageparams['g'] = $groupid;
}
if ($cloneid) {
$pageparams['clone'] = $cloneid;
}
$pageurl = new moodle_url('/mod/forumng/subscribe.php', $pageparams);
$requestingsubscribe = optional_param('submitsubscribe', '', PARAM_RAW);
$requestingunsubscribe = optional_param('submitunsubscribe', '', PARAM_RAW);
$requestingsubscribegroup = optional_param('submitsubscribe_thisgroup', '', PARAM_RAW);
$requestingunsubscribegroup = optional_param('submitunsubscribe_thisgroup', '', PARAM_RAW);
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
// Get request always does unsubscribe
$requestingunsubscribe = 'y';
$requestingsubscribe = '';
}
// Only one of the $courseid, $discussionid and $cmid must be true, also subscribe/unsubscribe.
$options = ($courseid ? 1 : 0) + ($cmid ? 1 : 0) + ($discussionid ? 1 : 0);
$subscribeoptions = ($requestingsubscribe ? 1 : 0) + ($requestingunsubscribe ? 1 : 0) +
($requestingsubscribegroup ? 1 : 0) + ($requestingunsubscribegroup ? 1 : 0);
// if group is set check that:
// - subscribe/uns-group must be set
// - cmid is set
// - discussionid is not set
// If group is not set, check that:
// - subscribe/uns_group is NOT set
if ($groupid && ($requestingsubscribegroup || $requestingunsubscribegroup) &&
$cmid && !$discussionid) {
$groupok = true;
} else if (!($groupid || $requestingunsubscribegroup || $requestingsubscribegroup)) {
$groupok = true;
} else {
$groupok = false;
}
if ($options != 1 || $subscribeoptions != 1 || !$groupok) {
throw new moodle_exception('error_subscribeparams', 'forumng');
}
// Permitted values 'index', 'view', 'discuss', nothing
$back = optional_param('back', '', PARAM_ALPHA);
if (!preg_match('~^(index|view|discuss)$~', $back)) {
$back = '';
}
if (($back=='index' && !($cmid || $courseid))) {
$back = '';
}
if (($back=='view' && !$cmid)) {
$back = '';
}
if (($back=='discuss' && !$discussionid)) {
$back = '';
}
global $USER;
$userid = $USER->id;
/**
* Check one click key if used.
* @param mod_forumng $forum
* @param int|null $groupid
* @return int User id to use in this script
* @throws coding_exception
* @throws moodle_exception
*/
function mod_forumng_check_key(\mod_forumng $forum, ?int $groupid): int {
global $USER;
// Auto unsubscribe from email. Check key (as per feed).
if ($user = optional_param('user', null, PARAM_INT)) {
$key = required_param('key', PARAM_ALPHANUM);
$correctkey = $forum->get_feed_key($groupid, $user);
if ($correctkey != $key) {
throw new \moodle_exception('feed_nopermission', 'forumng');
}
return $user;
}
return isloggedin() ? $USER->id : 0;
}
/**
* Return a list of groups the user belongs to that apply to this forum (same grouping)
* @param int $userid
* @param int $forumngid
* @return an array of group lists or an empty array
*/
function mod_forumng_get_group_list($userid, $forumngid) {
global $DB;
$sqlgroup = "
SELECT
g.id AS groupid
FROM
{forumng} f
INNER JOIN {course_modules} cm on f.id = cm.instance
INNER JOIN {modules} m on cm.module = m.id
INNER JOIN {groups_members} gm ON gm.userid = ?
INNER JOIN {groups} g ON gm.groupid = g.id AND g.courseid = cm.course
LEFT JOIN {groupings_groups} gg ON gg.groupid = g.id AND cm.groupingid = gg.groupingid
WHERE
f.id = ?
AND m.name = 'forumng'
AND (cm.groupingid = 0 or gg.id IS NOT NULL)";
$rs = $DB->get_recordset_sql($sqlgroup, array($userid, $forumngid));
$results = array();
foreach ($rs as $rec) {
$results[] = $rec->groupid;
}
$rs->close();
return $results;
}
// Decide the subscription confirmation string for not directing.
if ($requestingsubscribe) {
$subscribe = true;
} else {
$subscribe = false;
}
$confirmtext = get_string(
$subscribe ? 'subscribe_already' : 'unsubscribe_already', 'forumng');
// Handle single discussion
if ($discussionid) {
$discussion = mod_forumng_discussion::get_from_id($discussionid, $cloneid);
$forum = $discussion->get_forum();
$userid = mod_forumng_check_key($forum, $discussion->get_group_id());
$discussion->require_view($userid);
if (!$discussion->can_subscribe($userid) && !$discussion->can_unsubscribe($userid)) {
throw new moodle_exception('error_cannotchangediscussionsubscription', 'forumng');
}
if ($requestingsubscribe && $discussion->can_subscribe($userid)) {
$discussion->subscribe($userid);
$confirmtext = get_string('subscribe_confirm', 'forumng');
} else if ($requestingunsubscribe && $discussion->can_unsubscribe($userid)) {
$discussion->unsubscribe($userid);
$confirmtext = get_string('unsubscribe_confirm', 'forumng');
}
}
// Handle single forum
if ($cmid) {
$forum = mod_forumng::get_from_cmid($cmid, $cloneid);
$forumngid = $forum->get_id();
$grouplist = -1;
if ($groupid) {
$userid = mod_forumng_check_key($forum, $groupid);
$forum->require_view($groupid, $userid);
} else {
// If it is a separate groups forum and current user does not have access all groups
$userid = mod_forumng_check_key($forum, \mod_forumng::NO_GROUPS);
$context = context_module::instance($cmid);
$aaguser = has_capability('moodle/site:accessallgroups', $context, $userid);
if ($forum->get_group_mode() == SEPARATEGROUPS && !$aaguser) {
$grouplist = mod_forumng_get_group_list($userid, $forumngid);
// Get list of groups that this user belongs to that apply to this
// forum (same grouping). Call require_view on the first group in this list, or
// on NO_GROUPS if they don't have any groups
if (count($grouplist) == 0) {
$forum->require_view(mod_forumng::NO_GROUPS, $userid);
} else {
$forum->require_view($grouplist[0], $userid);
}
} else {
// Require access to all groups (if any)
$forum->require_view(mod_forumng::NO_GROUPS, $userid);
}
}
if (isguestuser($userid)) {
// This section allows users who are responding to the unsubscribe
// email link yet who may have already got guest access to the site.
// The display of the yes/no option is similar to other module behaviour
// though we could just redirect to login instead.
$wwwroot = $CFG->wwwroot.'/login/index.php';
$out = $forum->init_page($pageurl, get_string('unsubscribeshort', 'forumng'));
print $out->header();
print $out->confirm(
get_string('noguestsubscribe', 'forumng').'<br /><br />'.get_string('liketologin'),
new single_button(new moodle_url('/login/', array()),
get_string('yes'), 'get'),
new single_button(new moodle_url('/', array()),
get_string('no'), 'get'));
print $out->footer();
exit;
}
if (!$forum->can_change_subscription($userid)) {
throw new moodle_exception('error_cannotchangesubscription', 'forumng');
}
$subscriptioninfo = $forum->get_subscription_info($userid);
$discussionidcount = count($subscriptioninfo->discussionids);
$groupidcount = count($subscriptioninfo->groupids);
if (!$forum->get_group_mode()) {
// No group mode.
if ($requestingsubscribegroup || $requestingunsubscribegroup) {
throw new moodle_exception('error_cannotchangegroupsubscription', 'forumng');
}
if ($subscriptioninfo->wholeforum) {
// Subscribed to the entire forum.
$subscribed = mod_forumng::FULLY_SUBSCRIBED;
} else if ($discussionidcount == 0) {
$subscribed = mod_forumng::NOT_SUBSCRIBED;
} else {
$subscribed = mod_forumng::PARTIALLY_SUBSCRIBED;
}
if ($requestingsubscribe && $subscribed != mod_forumng::FULLY_SUBSCRIBED) {
$forum->subscribe($userid);
$confirmtext = get_string('subscribe_confirm', 'forumng');
} else if ($requestingunsubscribe && $subscribed != mod_forumng::NOT_SUBSCRIBED) {
$forum->unsubscribe($userid);
$confirmtext = get_string('unsubscribe_confirm', 'forumng');
}
} else {
if ($subscriptioninfo->wholeforum) {
if ($requestingunsubscribe) {
$forum->unsubscribe($userid);
$confirmtext = get_string('unsubscribe_confirm', 'forumng');
} else {
throw new moodle_exception('error_invalidsubscriptionrequest', 'forumng');
}
} else if ($discussionidcount != 0 || $groupidcount != 0 ) {
// Possible for subscribing to /unsubscribing from forum/group.
if ($requestingsubscribe) {
if ($grouplist == -1) {
$forum->subscribe($userid);
} else {
foreach ($grouplist as $groupid) {
$forum->subscribe($userid, $groupid);
}
}
$confirmtext = get_string('subscribe_confirm', 'forumng');
} else if ($requestingunsubscribe) {
$forum->unsubscribe($userid);
$confirmtext = get_string('unsubscribe_confirm', 'forumng');
} else if ($requestingsubscribegroup) {
// Check whether the user has subscribed to this group or not
$cansubscribetogroup = true;
foreach ($subscriptioninfo->groupids as $id) {
if ($id == $groupid) {
$cansubscribetogroup = false;
break;
}
}
if ($cansubscribetogroup) {
$forum->subscribe($userid, $groupid);
$confirmtext = get_string('subscribe_confirm_group', 'forumng');
} else {
throw new moodle_exception('subscribe_already_group', 'forumng');
}
} else if ($requestingunsubscribegroup) {
$canunsubscribefromgroup = false;
foreach ($subscriptioninfo->groupids as $id) {
if ($id == $groupid) {
$canunsubscribefromgroup = true;
break;
}
}
// Check if subscribed to any discussions belong to this group.
foreach ($subscriptioninfo->discussionids as $id => $grpid) {
if ($grpid == $groupid) {
$canunsubscribefromgroup = true;
break;
}
}
if ($canunsubscribefromgroup) {
$forum->unsubscribe($userid, $groupid);
$confirmtext = get_string('unsubscribe_confirm_group', 'forumng');
} else {
throw new moodle_exception('unsubscribe_already_group', 'forumng');
}
} else {
throw new moodle_exception('error_invalidsubscriptionrequest', 'forumng');
}
} else {
// Not subscribed yet
if ($requestingsubscribe) {
// TODO Change to take account of group list if there is one
if ($grouplist == -1) {
$forum->subscribe($userid);
} else {
foreach ($grouplist as $groupid) {
$forum->subscribe($userid, $groupid);
}
}
$confirmtext = get_string('subscribe_confirm', 'forumng');
} else if ($requestingsubscribegroup && $groupid) {
$forum->subscribe($userid, $groupid);
$confirmtext = get_string('subscribe_confirm_group', 'forumng');
} else {
throw new moodle_exception('error_invalidsubscriptionrequest', 'forumng');
}
}
}
}
// Handle whole course (user/key not supported - must be logged in).
if ($courseid) {
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
require_login($course);
$forums = mod_forumng::get_course_forums($course, 0, mod_forumng::UNREAD_NONE, array(), true);
foreach ($forums as $forum) {
if (!$forum->can_change_subscription()) {
continue;
}
$subscriptioninfo = $forum->get_subscription_info();
$discussionidcount = count($subscriptioninfo->discussionids);
if ($subscriptioninfo->wholeforum) {
// Subscribed to the entire forum.
$subscribed = mod_forumng::FULLY_SUBSCRIBED;
} else if ($discussionidcount == 0) {
$subscribed = mod_forumng::NOT_SUBSCRIBED;
} else {
$subscribed = mod_forumng::PARTIALLY_SUBSCRIBED;
}
if ($forum->can_change_subscription()) {
if ($requestingsubscribe && $subscribed != mod_forumng::FULLY_SUBSCRIBED) {
// If this is separate groups and user does not have access all groups,
// then make a group list
$grouplist = -1;
// if separate groups and not access all groups , set to list of groups
$context = $forum->get_context();
$aaguser = has_capability('moodle/site:accessallgroups', $context);
if ($forum->get_group_mode() == SEPARATEGROUPS && !$aaguser) {
$grouplist = mod_forumng_get_group_list($userid, $forumngid);
}
if ($grouplist == -1) {
$forum->subscribe();
} else {
foreach ($grouplist as $groupid) {
$forum->subscribe(0, $groupid);
}
}
$confirmtext = get_string('subscribe_confirm', 'forumng');
} else if ($requestingunsubscribe && $subscribed != mod_forumng::NOT_SUBSCRIBED) {
$forum->unsubscribe();
$confirmtext = get_string('unsubscribe_confirm', 'forumng');
}
}
}
}
// Redirect back
$backurl ='';
if ($back == 'index') {
if (!$courseid) {
$courseid = $forum->get_course()->id;
}
redirect('index.php?id=' . $courseid);
}
if ($back == 'view') {
redirect($forum->get_url(mod_forumng::PARAM_PLAIN));
}
if ($back == 'discuss') {
redirect('discuss.php?' . $discussion->get_link_params(mod_forumng::PARAM_PLAIN));
}
// Not redirecting? OK, confirm
if ($cmid || $discussionid) {
$backurl = $forum->get_url(mod_forumng::PARAM_HTML);
$out = $forum->init_page($pageurl, get_string(
$subscribe ? 'subscribeshort' : 'unsubscribeshort', 'forumng'));
print $out->header();
print $out->notification($confirmtext, 'success');
print $out->continue_button($backurl);
print $out->footer();
} else {
$backurl = $CFG->wwwroot . '/course/view.php?id=' . $courseid;
$PAGE->set_url($pageurl);
$PAGE->set_context(context_course::instance($courseid));
$PAGE->set_heading($COURSE->fullname);
$PAGE->set_title($COURSE->shortname);
$out = forum_utils::get_renderer();
print $out->header();
print $out->notification($confirmtext, 'success');
print $out->continue_button($backurl);
print $out->footer();
}