From 6be00a7a49c1b734f43de012fd5f09b14de69522 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Fri, 11 May 2012 10:33:17 +0700 Subject: [PATCH] MDL-31460 mod_forum: added some controls to mod/forum/unsubscribeall.php to prevent the removal of subscriptions the user shouldnt be able to delete --- mod/forum/lang/en/forum.php | 10 +++---- mod/forum/lib.php | 57 ++++++++++++++++++++++++++++++++++++ mod/forum/unsubscribeall.php | 9 ++++-- 3 files changed, 69 insertions(+), 7 deletions(-) diff --git a/mod/forum/lang/en/forum.php b/mod/forum/lang/en/forum.php index 19f46b45b3f..939a72e58be 100644 --- a/mod/forum/lang/en/forum.php +++ b/mod/forum/lang/en/forum.php @@ -269,9 +269,9 @@ $string['notrackforum'] = 'Don\'t track unread posts'; $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'; @@ -355,7 +355,7 @@ $string['startedby'] = 'Started by'; $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'; @@ -401,8 +401,8 @@ $string['unreadpostsone'] = '1 unread post'; $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'; diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 84ab0214217..b8e80c3c8ac 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -4554,6 +4554,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 = get_context_instance(CONTEXT_MODULE, $forum->id); + 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 * diff --git a/mod/forum/unsubscribeall.php b/mod/forum/unsubscribeall.php index a425c6b3cfe..f66ad261b99 100644 --- a/mod/forum/unsubscribeall.php +++ b/mod/forum/unsubscribeall.php @@ -47,15 +47,20 @@ echo $OUTPUT->header(); 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);