Пример #1
0
/**
 * 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;
}
Пример #2
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;
}
Пример #3
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;
}