Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 607809b3ba
Fetching contributors…

Cannot retrieve contributors at this time

file 83 lines (67 sloc) 2.764 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
<?PHP // $Id$

// Subscribe to or unsubscribe from a forum.

    require("../../config.php");
    require("lib.php");

    require_variable($id); // The forum to subscribe or unsubscribe to
    optional_variable($force); // Force everyone to be subscribed to this forum?
    optional_variable($user); // Force everyone to be subscribed to this forum?

    if (isguest()) {
        error("Guests are not allowed to subscribe to posts.", $_SERVER["HTTP_REFERER"]);
    }

    if (! $forum = get_record("forum", "id", $id)) {
        error("Forum ID was incorrect");
    }

    if (! $course = get_record("course", "id", $forum->course)) {
        error("Forum doesn't belong to a course!");
    }

    if ($user) {
        if (!isteacher($course->id)) {
            error("Only teachers can subscribe/unsubscribe other people!");
        }
        if (! $user = get_record("user", "id", $user)) {
            error("User ID was incorrect");
        }
    } else {
        $user = $USER;
    }

    if ($course->category) {
        require_login($forum->course);
    } else {
        require_login();
    }

    if ($forum->type == "teacher") {
        if (!isteacher($course->id)) {
            error("You must be a $course->teacher to subscribe to this forum");
        }
    }

    $returnto = forum_go_back_to("index.php?id=$course->id");

    if ($force and isteacher($course->id)) {
        if (forum_is_forcesubscribed($forum->id)) {
            forum_forcesubscribe($forum->id, 0);
            redirect($returnto, get_string("everyonecanchoose", "forum"), 1);
        } else {
            forum_forcesubscribe($forum->id, 1);
            redirect($returnto, get_string("everyoneissubscribed", "forum"), 1);
        }
    }

    if (forum_is_forcesubscribed($forum->id)) {
        redirect($returnto, get_string("everyoneissubscribed", "forum"), 1);
    }

    $info->name = "$user->firstname $user->lastname";
    $info->forum = $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");
            redirect($returnto, get_string("nownotsubscribed", "forum", $info), 1);
        } else {
            error("Could not unsubscribe you from that forum", $_SERVER["HTTP_REFERER"]);
        }
        
    } else { // subscribe
        if (forum_subscribe($user->id, $forum->id) ) {
            add_to_log($course->id, "forum", "subscribe", "view.php?f=$forum->id", "$forum->id");
            redirect($returnto, get_string("nowsubscribed", "forum", $info), 1);
        } else {
            error("Could not subscribe you to that forum", $_SERVER["HTTP_REFERER"]);
        }
    }

?>
Something went wrong with that request. Please try again.