Permalink
Browse files

Merge branch 'MDL-31460_unsubscribe_21' of git://github.com/andyjdavi…

…s/moodle into MOODLE_21_STABLE
  • Loading branch information...
2 parents 1f0856d + 6be00a7 commit f83ecb3721d11323910fe974429c4fc04a2afbf7 @danpoltawski danpoltawski committed Jun 5, 2012
Showing with 69 additions and 7 deletions.
  1. +5 −5 mod/forum/lang/en/forum.php
  2. +57 −0 mod/forum/lib.php
  3. +7 −2 mod/forum/unsubscribeall.php
@@ -269,9 +269,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';
@@ -355,7 +355,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';
@@ -401,8 +401,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';
View
@@ -4553,6 +4553,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
*
* @global object
@@ -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);

0 comments on commit f83ecb3

Please sign in to comment.