Skip to content

Commit

Permalink
MDL-31460 mod_forum: added some controls to mod/forum/unsubscribeall.…
Browse files Browse the repository at this point in the history
…php to prevent the removal of subscriptions the user shouldnt be able to delete
  • Loading branch information
andyjdavis committed Jun 4, 2012
1 parent 4db0616 commit 74a37e1
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
10 changes: 5 additions & 5 deletions mod/forum/lang/en/forum.php
Expand Up @@ -290,9 +290,9 @@
$string['noviewdiscussionspermission'] = 'You do not have the permission to view discussions in this forum';
$string['nowallsubscribed'] = 'All forums in {$a} are subscribed.';
$string['nowallunsubscribed'] = 'All forums in {$a} are not subscribed.';
$string['nownotsubscribed'] = '{$a->name} will NOT receive copies of \'{$a->forum}\' by email.';
$string['nownotsubscribed'] = '{$a->name} will NOT be notified of new posts in \'{$a->forum}\'';
$string['nownottracking'] = '{$a->name} is no longer tracking \'{$a->forum}\'.';
$string['nowsubscribed'] = '{$a->name} will receive copies of \'{$a->forum}\' by email.';
$string['nowsubscribed'] = '{$a->name} will be notified of new posts in \'{$a->forum}\'';
$string['nowtracking'] = '{$a->name} is now tracking \'{$a->forum}\'.';
$string['numposts'] = '{$a} posts';
$string['olderdiscussions'] = 'Older discussions';
Expand Down Expand Up @@ -376,7 +376,7 @@
$string['subject'] = 'Subject';
$string['subscribe'] = 'Subscribe to this forum';
$string['subscribeall'] = 'Subscribe everyone to this forum';
$string['subscribeenrolledonly'] = 'Sorry, only enrolled users are allowed to subscribe to receive forum postings by email.';
$string['subscribeenrolledonly'] = 'Sorry, only enrolled users are allowed to subscribe to forum post notifications.';
$string['subscribed'] = 'Subscribed';
$string['subscribenone'] = 'Unsubscribe everyone from this forum';
$string['subscribers'] = 'Subscribers';
Expand Down Expand Up @@ -422,8 +422,8 @@
$string['unsubscribe'] = 'Unsubscribe from this forum';
$string['unsubscribeall'] = 'Unsubscribe from all forums';
$string['unsubscribeallconfirm'] = 'You are subscribed to {$a} forums now. Do you really want to unsubscribe from all forums and disable forum auto-subscribe?';
$string['unsubscribealldone'] = 'All your forum subscriptions were removed, you might still receive notifications from forums with forced subscription. If you do not want to receive any emails from this server please go to your profile and disable email address there.';
$string['unsubscribeallempty'] = 'Sorry, you are not subscribed to any forums. If you do not want to receive any emails from this server please go to your profile and disable email address there.';
$string['unsubscribealldone'] = 'All optional forum subscriptions were removed. You will still receive notifications from forums with forced subscription. To manage forum notifications go to Messaging in My Profile Settings.';
$string['unsubscribeallempty'] = 'You are not subscribed to any forums. To disable all notifications from this server go to Messaging in My Profile Settings.';
$string['unsubscribed'] = 'Unsubscribed';
$string['unsubscribeshort'] = 'Unsubscribe';
$string['usermarksread'] = 'Manual message read marking';
Expand Down
57 changes: 57 additions & 0 deletions mod/forum/lib.php
Expand Up @@ -4611,6 +4611,63 @@ function forum_get_subscribed_forums($course) {
}
}

/**
* Returns an array of forums that the current user is subscribed to and is allowed to unsubscribe from
*
* @return array An array of unsubscribable forums
*/
function forum_get_optional_subscribed_forums() {
global $USER, $DB;

// Get courses that $USER is enrolled in and can see
$courses = enrol_get_my_courses();
if (empty($courses)) {
return array();
}

$courseids = array();
foreach($courses as $course) {
$courseids[] = $course->id;
}
list($coursesql, $courseparams) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED, 'c');

// get all forums from the user's courses that they are subscribed to and which are not set to forced
$sql = "SELECT f.id, cm.id as cm, cm.visible
FROM {forum} f
JOIN {course_modules} cm ON cm.instance = f.id
JOIN {modules} m ON m.name = :modulename AND m.id = cm.module
LEFT JOIN {forum_subscriptions} fs ON (fs.forum = f.id AND fs.userid = :userid)
WHERE f.forcesubscribe <> :forcesubscribe AND fs.id IS NOT NULL
AND cm.course $coursesql";
$params = array_merge($courseparams, array('modulename'=>'forum', 'userid'=>$USER->id, 'forcesubscribe'=>FORUM_FORCESUBSCRIBE));
if (!$forums = $DB->get_records_sql($sql, $params)) {
return array();
}

$unsubscribableforums = array(); // Array to return

foreach($forums as $forum) {

if (empty($forum->visible)) {
// the forum is hidden
$context = context_module::instance($forum->cm);
if (!has_capability('moodle/course:viewhiddenactivities', $context)) {
// the user can't see the hidden forum
continue;
}
}

// subscribe.php only requires 'mod/forum:managesubscriptions' when
// unsubscribing a user other than yourself so we don't require it here either

// A check for whether the forum has subscription set to forced is built into the SQL above

$unsubscribableforums[] = $forum;
}

return $unsubscribableforums;
}

/**
* Adds user to the subscriber list
*
Expand Down
9 changes: 7 additions & 2 deletions mod/forum/unsubscribeall.php
Expand Up @@ -47,15 +47,20 @@
echo $OUTPUT->heading($strunsubscribeall);

if (data_submitted() and $confirm and confirm_sesskey()) {
$DB->delete_records('forum_subscriptions', array('userid'=>$USER->id));
$forums = forum_get_optional_subscribed_forums();

foreach($forums as $forum) {
forum_unsubscribe($USER->id, $forum->id);
}
$DB->set_field('user', 'autosubscribe', 0, array('id'=>$USER->id));

echo $OUTPUT->box(get_string('unsubscribealldone', 'forum'));
echo $OUTPUT->continue_button($return);
echo $OUTPUT->footer();
die;

} else {
$a = $DB->count_records('forum_subscriptions', array('userid'=>$USER->id));
$a = count(forum_get_optional_subscribed_forums());

if ($a) {
$msg = get_string('unsubscribeallconfirm', 'forum', $a);
Expand Down

0 comments on commit 74a37e1

Please sign in to comment.