diff --git a/mod/forum/db/access.php b/mod/forum/db/access.php index a92df579527..e14c65b1619 100644 --- a/mod/forum/db/access.php +++ b/mod/forum/db/access.php @@ -334,5 +334,15 @@ $capabilities = array( 'manager' => CAP_ALLOW ) ), + 'mod/forum:allowforcesubscribe' => array( + + 'captype' => 'read', + 'contextlevel' => CONTEXT_MODULE, + 'archetypes' => array( + 'student' => CAP_ALLOW, + 'teacher' => CAP_ALLOW, + 'editingteacher' => CAP_ALLOW + ) + ), ); diff --git a/mod/forum/db/events.php b/mod/forum/db/events.php index f11bd37aa37..ded6583575d 100644 --- a/mod/forum/db/events.php +++ b/mod/forum/db/events.php @@ -25,9 +25,9 @@ /* List of handlers */ $handlers = array ( - 'user_enrolled' => array ( + 'role_assigned' => array ( 'handlerfile' => '/mod/forum/lib.php', - 'handlerfunction' => 'forum_user_enrolled', + 'handlerfunction' => 'forum_user_role_assigned', 'schedule' => 'instant', 'internal' => 1, ), diff --git a/mod/forum/lang/en/forum.php b/mod/forum/lang/en/forum.php index 697fc0c03f7..3f995da0350 100644 --- a/mod/forum/lang/en/forum.php +++ b/mod/forum/lang/en/forum.php @@ -156,6 +156,7 @@ $string['forum'] = 'Forum'; $string['forum:addinstance'] = 'Add a new forum'; $string['forum:addnews'] = 'Add news'; $string['forum:addquestion'] = 'Add question'; +$string['forum:allowforcesubscribe'] = 'Allow force subscribe'; $string['forumauthorhidden'] = 'Author (hidden)'; $string['forumblockingalmosttoomanyposts'] = 'You are approaching the posting threshold. You have posted {$a->numposts} times in the last {$a->blockperiod} and the limit is {$a->blockafter} posts.'; $string['forumbodyhidden'] = 'This post cannot be viewed by you, probably because you have not posted in the discussion or the maximum editing time hasn\'t passed yet.'; diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 4d3a84dcdba..74af4b3dee3 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -2861,7 +2861,7 @@ function forum_get_potential_subscribers($forumcontext, $groupid, $fields, $sort global $DB; // only active enrolled users or everybody on the frontpage - list($esql, $params) = get_enrolled_sql($forumcontext, '', $groupid, true); + list($esql, $params) = get_enrolled_sql($forumcontext, 'mod/forum:allowforcesubscribe', $groupid, true); $sql = "SELECT $fields FROM {user} u @@ -4680,7 +4680,9 @@ function forum_is_subscribed($userid, $forum) { if (is_numeric($forum)) { $forum = $DB->get_record('forum', array('id' => $forum)); } - if (forum_is_forcesubscribed($forum)) { + // If forum is force subscribed and has allowforcesubscribe, then user is subscribed. + if (forum_is_forcesubscribed($forum) && + has_capability('mod/forum:allowforcesubscribe', context_module::instance($forum->id), $userid)) { return true; } return $DB->record_exists("forum_subscriptions", array("userid" => $userid, "forum" => $forum->id)); @@ -6133,6 +6135,7 @@ function forum_update_subscriptions_button($courseid, $forumid) { /** * This function gets run whenever user is enrolled into course * + * @deprecated deprecating this function as we will be using forum_user_role_assigned * @param stdClass $cp * @return void */ @@ -6155,6 +6158,38 @@ function forum_user_enrolled($cp) { } } +/** + * This function gets run whenever user is assigned role in course + * + * @param stdClass $cp + * @return void + */ +function forum_user_role_assigned($cp) { + global $DB; + + $context = context::instance_by_id($cp->contextid, MUST_EXIST); + + // If contextlevel is course then only subscribe user. Role assignment + // at course level means user is enroled in course and can subscribe to forum. + if ($context->contextlevel != CONTEXT_COURSE) { + return; + } + + $sql = "SELECT f.id + FROM {forum} f + LEFT JOIN {forum_subscriptions} fs ON (fs.forum = f.id AND fs.userid = :userid) + WHERE f.course = :courseid AND f.forcesubscribe = :initial AND fs.id IS NULL"; + $params = array('courseid'=>$context->instanceid, 'userid'=>$cp->userid, 'initial'=>FORUM_INITIALSUBSCRIBE); + + $forums = $DB->get_records_sql($sql, $params); + foreach ($forums as $forum) { + // If user doesn't have allowforcesubscribe capability then don't subscribe. + if (has_capability('mod/forum:allowforcesubscribe', context_module::instance($forum->id), $cp->userid)) { + forum_subscribe($cp->userid, $forum->id); + } + } +} + /** * This function gets run whenever user is unenrolled from course * diff --git a/mod/forum/version.php b/mod/forum/version.php index 0413ee797a0..8a1af57c51b 100644 --- a/mod/forum/version.php +++ b/mod/forum/version.php @@ -25,7 +25,7 @@ defined('MOODLE_INTERNAL') || die(); -$module->version = 2012061700; // The current module version (Date: YYYYMMDDXX) +$module->version = 2012061701; // The current module version (Date: YYYYMMDDXX) $module->requires = 2012061700; // Requires this Moodle version $module->component = 'mod_forum'; // Full name of the plugin (used for diagnostics) $module->cron = 60;