Exemplo n.º 1
0
        $forum = $forums[$forumid];
        $modcontext = context_module::instance($cm->id);
        $cansub = false;
        if (has_capability('mod/forum:viewdiscussion', $modcontext)) {
            $cansub = true;
        }
        if ($cansub && $cm->visible == 0 && !has_capability('mod/forum:managesubscriptions', $modcontext)) {
            $cansub = false;
        }
        if (!forum_is_forcesubscribed($forum)) {
            $subscribed = forum_is_subscribed($USER->id, $forum);
            if ((has_capability('moodle/course:manageactivities', $coursecontext, $USER->id) || $forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE) && $subscribe && !$subscribed && $cansub) {
                forum_subscribe($USER->id, $forumid, $modcontext);
            } else {
                if (!$subscribe && $subscribed) {
                    forum_unsubscribe($USER->id, $forumid, $modcontext);
                }
            }
        }
    }
    $returnto = forum_go_back_to("index.php?id={$course->id}");
    $shortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
    if ($subscribe) {
        redirect($returnto, get_string('nowallsubscribed', 'forum', $shortname), 1);
    } else {
        redirect($returnto, get_string('nowallunsubscribed', 'forum', $shortname), 1);
    }
}
/// First, let's process the general forums and build up a display
if ($generalforums) {
    foreach ($generalforums as $forum) {
Exemplo n.º 2
0
/**
 * Given a new post, subscribes or unsubscribes as appropriate.
 * Returns some text which describes what happened.
 *
 * @global objec
 * @param object $post
 * @param object $forum
 */
function forum_post_subscription($post, $forum) {

    global $USER;

    $action = '';
    $subscribed = forum_is_subscribed($USER->id, $forum);

    if ($forum->forcesubscribe == FORUM_FORCESUBSCRIBE) { // database ignored
        return "";

    } elseif (($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE)
        && !has_capability('moodle/course:manageactivities', context_course::instance($forum->course), $USER->id)) {
        if ($subscribed) {
            $action = 'unsubscribe'; // sanity check, following MDL-14558
        } else {
            return "";
        }

    } else { // go with the user's choice
        if (isset($post->subscribe)) {
            // no change
            if ((!empty($post->subscribe) && $subscribed)
                || (empty($post->subscribe) && !$subscribed)) {
                return "";

            } elseif (!empty($post->subscribe) && !$subscribed) {
                $action = 'subscribe';

            } elseif (empty($post->subscribe) && $subscribed) {
                $action = 'unsubscribe';
            }
        }
    }

    $info = new stdClass();
    $info->name  = fullname($USER);
    $info->forum = format_string($forum->name);

    switch ($action) {
        case 'subscribe':
            forum_subscribe($USER->id, $post->forum);
            return "<p>".get_string("nowsubscribed", "forum", $info)."</p>";
        case 'unsubscribe':
            forum_unsubscribe($USER->id, $post->forum);
            return "<p>".get_string("nownotsubscribed", "forum", $info)."</p>";
    }
}
Exemplo n.º 3
0
                }
                break;
        }
    }
}
/// Do course wide subscribe/unsubscribe
if (!is_null($subscribe) && !isguest()) {
    $allforums = array_merge($generalforums, $learningforums);
    if ($allforums) {
        foreach ($allforums as $forum) {
            if (!forum_is_forcesubscribed($forum->id)) {
                $subscribed = forum_is_subscribed($USER->id, $forum->id);
                if ($subscribe && !$subscribed) {
                    forum_subscribe($USER->id, $forum->id);
                } elseif (!$subscribe && $subscribed) {
                    forum_unsubscribe($USER->id, $forum->id);
                }
            }
        }
    }
    $returnto = forum_go_back_to("index.php?id={$course->id}");
    if ($subscribe) {
        add_to_log($course->id, "forum", "subscribeall", "index.php?id={$course->id}", $course->id);
        redirect($returnto, get_string("nowallsubscribed", "forum", format_string($course->shortname)), 1);
    } else {
        add_to_log($course->id, "forum", "unsubscribeall", "index.php?id={$course->id}", $course->id);
        redirect($returnto, get_string("nowallunsubscribed", "forum", format_string($course->shortname)), 1);
    }
}
/// First, let's process the general forums and build up a display
if ($generalforums) {
Exemplo n.º 4
0
/**
 * Remove subscriptions for a user in a context
 *
 * @global object
 * @global object
 * @uses CONTEXT_SYSTEM
 * @uses CONTEXT_COURSECAT
 * @uses CONTEXT_COURSE
 * @uses CONTEXT_MODULE
 * @param int $userid
 * @param object $context
 * @return bool
 */
function forum_remove_user_subscriptions($userid, $context) {

    global $CFG, $DB;

    if (empty($context->contextlevel)) {
        return false;
    }

    switch ($context->contextlevel) {

        case CONTEXT_SYSTEM:   // For the whole site
            // find all courses in which this user has a forum subscription
            if ($courses = $DB->get_records_sql("SELECT c.id
                                                  FROM {course} c,
                                                       {forum_subscriptions} fs,
                                                       {forum} f
                                                       WHERE c.id = f.course AND f.id = fs.forum AND fs.userid = ?
                                                       GROUP BY c.id", array($userid))) {

                foreach ($courses as $course) {
                    $subcontext = get_context_instance(CONTEXT_COURSE, $course->id);
                    forum_remove_user_subscriptions($userid, $subcontext);
                }
            }
            break;

        case CONTEXT_COURSECAT:   // For a whole category
             if ($courses = $DB->get_records('course', array('category' => $context->instanceid), '', 'id')) {
                 foreach ($courses as $course) {
                     $subcontext = get_context_instance(CONTEXT_COURSE, $course->id);
                     forum_remove_user_subscriptions($userid, $subcontext);
                 }
             }
             if ($categories = $DB->get_records('course_categories', array('parent' => $context->instanceid), '', 'id')) {
                 foreach ($categories as $category) {
                     $subcontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
                     forum_remove_user_subscriptions($userid, $subcontext);
                 }
             }
             break;

        case CONTEXT_COURSE:   // For a whole course
            if (!is_enrolled($context, $userid)) {
                 if ($course = $DB->get_record('course', array('id' => $context->instanceid), 'id')) {
                    // find all forums in which this user has a subscription, and its coursemodule id
                    if ($forums = $DB->get_records_sql("SELECT f.id, cm.id as coursemodule
                                                         FROM {forum} f,
                                                              {modules} m,
                                                              {course_modules} cm,
                                                              {forum_subscriptions} fs
                                                        WHERE fs.userid = ? AND f.course = ?
                                                              AND fs.forum = f.id AND cm.instance = f.id
                                                              AND cm.module = m.id AND m.name = 'forum'", array($userid, $context->instanceid))) {

                         foreach ($forums as $forum) {
                             if ($modcontext = get_context_instance(CONTEXT_MODULE, $forum->coursemodule)) {
                                 if (!has_capability('mod/forum:viewdiscussion', $modcontext, $userid)) {
                                     forum_unsubscribe($userid, $forum->id);
                                 }
                             }
                         }
                     }
                 }
            }
            break;

        case CONTEXT_MODULE:   // Just one forum
            if (!is_enrolled($context, $userid)) {
                 if ($cm = get_coursemodule_from_id('forum', $context->instanceid)) {
                     if ($forum = $DB->get_record('forum', array('id' => $cm->instance))) {
                         if (!has_capability('mod/forum:viewdiscussion', $context, $userid)) {
                             forum_unsubscribe($userid, $forum->id);
                         }
                     }
                 }
            }
            break;
    }

    return true;
}
Exemplo n.º 5
0
if ($force and has_capability('mod/forum:managesubscriptions', $context)) {
    if (forum_is_forcesubscribed($forum)) {
        forum_forcesubscribe($forum->id, 0);
        redirect($returnto, get_string("everyonecannowchoose", "forum"), 1);
    } else {
        forum_forcesubscribe($forum->id, 1);
        redirect($returnto, get_string("everyoneisnowsubscribed", "forum"), 1);
    }
}
if (forum_is_forcesubscribed($forum)) {
    redirect($returnto, get_string("everyoneisnowsubscribed", "forum"), 1);
}
$info->name = fullname($user);
$info->forum = format_string($forum->name);
if (forum_is_subscribed($user->id, $forum->id)) {
    if (forum_unsubscribe($user->id, $forum->id)) {
        add_to_log($course->id, "forum", "unsubscribe", "view.php?f={$forum->id}", $forum->id, $cm->id);
        redirect($returnto, get_string("nownotsubscribed", "forum", $info), 1);
    } else {
        print_error('cannotunsubscribe', 'forum', $_SERVER["HTTP_REFERER"]);
    }
} else {
    // subscribe
    if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE && !has_capability('mod/forum:managesubscriptions', $context)) {
        print_error('disallowsubscribe', 'forum', $_SERVER["HTTP_REFERER"]);
    }
    if (!has_capability('mod/forum:viewdiscussion', $context)) {
        print_error('cannotsubscribe', 'forum', $_SERVER["HTTP_REFERER"]);
    }
    if (forum_subscribe($user->id, $forum->id)) {
        add_to_log($course->id, "forum", "subscribe", "view.php?f={$forum->id}", $forum->id, $cm->id);
Exemplo n.º 6
0
$strshowall = get_string("showall");
$strsubscribers = get_string("subscribers", "forum");
$strforums = get_string("forums", "forum");
$searchtext = optional_param('searchtext', '', PARAM_RAW);
if ($frm = data_submitted()) {
    /// A form was submitted so process the input
    if (!empty($frm->add) and !empty($frm->addselect)) {
        foreach ($frm->addselect as $addsubscriber) {
            if (!forum_subscribe($addsubscriber, $id)) {
                print_error('cannotaddsubscriber', 'forum', '', $addsubscriber);
            }
        }
    } else {
        if (!empty($frm->remove) and !empty($frm->removeselect)) {
            foreach ($frm->removeselect as $removesubscriber) {
                if (!forum_unsubscribe($removesubscriber, $id)) {
                    print_error('cannotremovesubscriber', 'forum', '', $removesubscriber);
                }
            }
        } else {
            if (!empty($frm->showall)) {
                $searchtext = '';
            }
        }
    }
}
/// Get all existing subscribers for this forum.
if (!($subscribers = forum_subscribed_users($course, $forum, $currentgroup, $context))) {
    $subscribers = array();
}
/// Get all the potential subscribers excluding users already subscribed
Exemplo n.º 7
0
 /**
  * Test that toggling the forum-level subscription for a different user does not affect their discussion-level
  * subscriptions.
  */
 public function test_forum_subscribe_toggle_as_other()
 {
     global $DB;
     $this->resetAfterTest(true);
     // Create a course, with a forum.
     $course = $this->getDataGenerator()->create_course();
     $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
     $forum = $this->getDataGenerator()->create_module('forum', $options);
     // Create a user enrolled in the course as a student.
     list($author) = $this->helper_create_users($course, 1);
     // Post a discussion to the forum.
     list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
     // Check that the user is currently not subscribed to the forum.
     $this->assertFalse(\mod_forum\subscriptions::is_subscribed($author->id, $forum));
     // Check the deprecated function too.
     $this->assertFalse(forum_is_subscribed($author->id, $forum));
     $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
     phpunit_util::reset_debugging();
     // Check that the user is unsubscribed from the discussion too.
     $this->assertFalse(\mod_forum\subscriptions::is_subscribed($author->id, $forum, $discussion->id));
     // Check that we have no records in either of the subscription tables.
     $this->assertEquals(0, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id)));
     $this->assertEquals(0, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id)));
     // Subscribing to the forum should create a record in the subscriptions table, but not the forum discussion
     // subscriptions table.
     \mod_forum\subscriptions::subscribe_user($author->id, $forum);
     $this->assertEquals(1, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id)));
     $this->assertEquals(0, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id)));
     // Unsubscribing should remove the record from the forum subscriptions table, and not modify the forum
     // discussion subscriptions table.
     \mod_forum\subscriptions::unsubscribe_user($author->id, $forum);
     $this->assertEquals(0, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id)));
     $this->assertEquals(0, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id)));
     // The same thing should happen calling the deprecated versions of
     // these functions.
     // Subscribing to the forum should create a record in the subscriptions table, but not the forum discussion
     // subscriptions table.
     forum_subscribe($author->id, $forum->id);
     $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
     phpunit_util::reset_debugging();
     $this->assertEquals(1, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id)));
     $this->assertEquals(0, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id)));
     // Unsubscribing should remove the record from the forum subscriptions table, and not modify the forum
     // discussion subscriptions table.
     forum_unsubscribe($author->id, $forum->id);
     $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
     phpunit_util::reset_debugging();
     $this->assertEquals(0, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id)));
     $this->assertEquals(0, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id)));
     // Enroling the user in the discussion should add one record to the forum discussion table without modifying the
     // form subscriptions.
     \mod_forum\subscriptions::subscribe_user_to_discussion($author->id, $discussion);
     $this->assertEquals(0, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id)));
     $this->assertEquals(1, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id)));
     // Unsubscribing should remove the record from the forum subscriptions table, and not modify the forum
     // discussion subscriptions table.
     \mod_forum\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion);
     $this->assertEquals(0, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id)));
     $this->assertEquals(0, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id)));
     // Re-subscribe to the discussion so that we can check the effect of forum-level subscriptions.
     \mod_forum\subscriptions::subscribe_user_to_discussion($author->id, $discussion);
     $this->assertEquals(0, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id)));
     $this->assertEquals(1, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id)));
     // Subscribing to the forum should have no effect on the forum discussion subscriptions table if the user did
     // not request the change themself.
     \mod_forum\subscriptions::subscribe_user($author->id, $forum);
     $this->assertEquals(1, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id)));
     $this->assertEquals(1, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id)));
     // Unsubscribing from the forum should have no effect on the forum discussion subscriptions table if the user
     // did not request the change themself.
     \mod_forum\subscriptions::unsubscribe_user($author->id, $forum);
     $this->assertEquals(0, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id)));
     $this->assertEquals(1, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id)));
     // Subscribing to the forum should remove the per-discussion subscription preference if the user requested the
     // change themself.
     \mod_forum\subscriptions::subscribe_user($author->id, $forum, null, true);
     $this->assertEquals(1, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id)));
     $this->assertEquals(0, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id)));
     // Now unsubscribe from the current discussion whilst being subscribed to the forum as a whole.
     \mod_forum\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion);
     $this->assertEquals(1, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id)));
     $this->assertEquals(1, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id)));
     // Unsubscribing from the forum should remove the per-discussion subscription preference if the user requested the
     // change themself.
     \mod_forum\subscriptions::unsubscribe_user($author->id, $forum, null, true);
     $this->assertEquals(0, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id)));
     $this->assertEquals(0, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id)));
     // Subscribe to the discussion.
     \mod_forum\subscriptions::subscribe_user_to_discussion($author->id, $discussion);
     $this->assertEquals(0, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id)));
     $this->assertEquals(1, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id)));
     // Subscribe to the forum without removing the discussion preferences.
     \mod_forum\subscriptions::subscribe_user($author->id, $forum);
     $this->assertEquals(1, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id)));
     $this->assertEquals(1, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id)));
     // Unsubscribing from the discussion should result in a change.
     \mod_forum\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion);
     $this->assertEquals(1, $DB->count_records('forum_subscriptions', array('userid' => $author->id, 'forum' => $forum->id)));
     $this->assertEquals(1, $DB->count_records('forum_discussion_subs', array('userid' => $author->id, 'discussion' => $discussion->id)));
 }
Exemplo n.º 8
0
Arquivo: lib.php Projeto: r007/PMoodle
/**
 * Remove subscriptions for a user in a context
 */
function forum_remove_user_subscriptions($userid, $context)
{
    global $CFG;
    if (empty($context->contextlevel)) {
        return false;
    }
    switch ($context->contextlevel) {
        case CONTEXT_SYSTEM:
            // For the whole site
            //if ($courses = get_my_courses($userid)) {
            // find all courses in which this user has a forum subscription
            if ($courses = get_records_sql("SELECT c.id\n                                              FROM {$CFG->prefix}course c,\n                                                   {$CFG->prefix}forum_subscriptions fs,\n                                                   {$CFG->prefix}forum f\n                                                   WHERE c.id = f.course AND f.id = fs.forum AND fs.userid = {$userid}\n                                                   GROUP BY c.id")) {
                foreach ($courses as $course) {
                    $subcontext = get_context_instance(CONTEXT_COURSE, $course->id);
                    forum_remove_user_subscriptions($userid, $subcontext);
                }
            }
            break;
        case CONTEXT_COURSECAT:
            // For a whole category
            if ($courses = get_records('course', 'category', $context->instanceid, '', 'id')) {
                foreach ($courses as $course) {
                    $subcontext = get_context_instance(CONTEXT_COURSE, $course->id);
                    forum_remove_user_subscriptions($userid, $subcontext);
                }
            }
            if ($categories = get_records('course_categories', 'parent', $context->instanceid, '', 'id')) {
                foreach ($categories as $category) {
                    $subcontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
                    forum_remove_user_subscriptions($userid, $subcontext);
                }
            }
            break;
        case CONTEXT_COURSE:
            // For a whole course
            if ($course = get_record('course', 'id', $context->instanceid, '', '', '', '', 'id')) {
                // find all forums in which this user has a subscription, and its coursemodule id
                if ($forums = get_records_sql("SELECT f.id, cm.id as coursemodule\n                                                 FROM {$CFG->prefix}forum f,\n                                                      {$CFG->prefix}modules m,\n                                                      {$CFG->prefix}course_modules cm,\n                                                      {$CFG->prefix}forum_subscriptions fs\n                                                WHERE fs.userid = {$userid} AND f.course = {$context->instanceid}\n                                                      AND fs.forum = f.id AND cm.instance = f.id\n                                                      AND cm.module = m.id AND m.name = 'forum'")) {
                    foreach ($forums as $forum) {
                        if ($modcontext = get_context_instance(CONTEXT_MODULE, $forum->coursemodule)) {
                            if (!has_capability('mod/forum:viewdiscussion', $modcontext, $userid)) {
                                forum_unsubscribe($userid, $forum->id);
                            }
                        }
                    }
                }
            }
            break;
        case CONTEXT_MODULE:
            // Just one forum
            if ($cm = get_coursemodule_from_id('forum', $context->instanceid)) {
                if ($forum = get_record('forum', 'id', $cm->instance)) {
                    if (!has_capability('mod/forum:viewdiscussion', $context, $userid)) {
                        forum_unsubscribe($userid, $forum->id);
                    }
                }
            }
            break;
    }
    return true;
}
$PAGE->set_context(context_user::instance($USER->id));
$return = $CFG->wwwroot . '/';
if (isguestuser()) {
    redirect($return);
}
$strunsubscribeall = get_string('unsubscribeall', 'forum');
$PAGE->navbar->add(get_string('modulename', 'forum'));
$PAGE->navbar->add($strunsubscribeall);
$PAGE->set_title($strunsubscribeall);
$PAGE->set_heading($COURSE->fullname);
echo $OUTPUT->header();
echo $OUTPUT->heading($strunsubscribeall);
if (data_submitted() and $confirm and confirm_sesskey()) {
    $forums = forum_get_optional_subscribed_forums();
    foreach ($forums as $forum) {
        forum_unsubscribe($USER->id, $forum->id, context_module::instance($forum->cm));
    }
    $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 = count(forum_get_optional_subscribed_forums());
    if ($a) {
        $msg = get_string('unsubscribeallconfirm', 'forum', $a);
        echo $OUTPUT->confirm($msg, new moodle_url('unsubscribeall.php', array('confirm' => 1)), $return);
        echo $OUTPUT->footer();
        die;
    } else {
        echo $OUTPUT->box(get_string('unsubscribeallempty', 'forum'));
Exemplo n.º 10
0
    /** It has to be one or the other, not both or neither */
    if (!($subscribe xor $unsubscribe)) {
        print_error('invalidaction');
    }
    if ($subscribe) {
        $users = $subscriberselector->get_selected_users();
        foreach ($users as $user) {
            if (!forum_subscribe($user->id, $id)) {
                print_error('cannotaddsubscriber', 'forum', '', $user->id);
            }
        }
    } else {
        if ($unsubscribe) {
            $users = $existingselector->get_selected_users();
            foreach ($users as $user) {
                if (!forum_unsubscribe($user->id, $id)) {
                    print_error('cannotremovesubscriber', 'forum', '', $user->id);
                }
            }
        }
    }
    $subscriberselector->invalidate_selected_users();
    $existingselector->invalidate_selected_users();
    $subscriberselector->set_existing_subscribers($existingselector->find_users(''));
}
$strsubscribers = get_string("subscribers", "forum");
$PAGE->navbar->add($strsubscribers);
$PAGE->set_title($strsubscribers);
$PAGE->set_heading($COURSE->fullname);
if (has_capability('mod/forum:managesubscriptions', $context)) {
    $PAGE->set_button(forum_update_subscriptions_button($course->id, $id));
Exemplo n.º 11
0
/**
 * Remove subscriptions for a user in a context
 */
function forum_remove_user_subscriptions($userid, $context)
{
    if (empty($context->contextlevel)) {
        return false;
    }
    switch ($context->contextlevel) {
        case CONTEXT_SYSTEM:
            // For the whole site
            if ($courses = get_records('course')) {
                foreach ($courses as $course) {
                    $subcontext = get_context_instance(CONTEXT_COURSE, $course->id);
                    forum_remove_user_subscriptions($userid, $subcontext);
                }
            }
            break;
        case CONTEXT_COURSECAT:
            // For a whole category
            if ($courses = get_records('course', 'category', $context->instanceid)) {
                foreach ($courses as $course) {
                    $subcontext = get_context_instance(CONTEXT_COURSE, $course->id);
                    forum_remove_user_subscriptions($userid, $subcontext);
                }
            }
            if ($categories = get_records('course_categories', 'parent', $context->instanceid)) {
                foreach ($categories as $category) {
                    $subcontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
                    forum_remove_user_subscriptions($userid, $subcontext);
                }
            }
            break;
        case CONTEXT_COURSE:
            // For a whole course
            if ($course = get_record('course', 'id', $context->instanceid)) {
                if ($forums = get_all_instances_in_course('forum', $course, $userid, true)) {
                    foreach ($forums as $forum) {
                        if ($modcontext = get_context_instance(CONTEXT_MODULE, $forum->coursemodule)) {
                            if (!has_capability('mod/forum:viewdiscussion', $modcontext, $userid)) {
                                forum_unsubscribe($userid, $forum->id);
                            }
                        }
                    }
                }
            }
            break;
        case CONTEXT_MODULE:
            // Just one forum
            if ($cm = get_coursemodule_from_id('forum', $context->instanceid)) {
                if ($forum = get_record('forum', 'id', $cm->instance)) {
                    if (!has_capability('mod/forum:viewdiscussion', $context, $userid)) {
                        forum_unsubscribe($userid, $forum->id);
                    }
                }
            }
            break;
    }
    return true;
}