3 // Subscribe to or unsubscribe from a forum.
5 require_once("../../config.php");
6 require_once("lib.php");
8 $id = required_param('id',PARAM_INT
); // The forum to subscribe or unsubscribe to
9 $force = optional_param('force','',PARAM_ALPHA
); // Force everyone to be subscribed to this forum?
10 $user = optional_param('user',0,PARAM_INT
);
12 if (! $forum = get_record("forum", "id", $id)) {
13 error("Forum ID was incorrect");
16 if (! $course = get_record("course", "id", $forum->course
)) {
17 error("Forum doesn't belong to a course!");
20 if ($cm = get_coursemodule_from_instance("forum", $forum->id
, $course->id
)) {
21 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
24 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
28 if (!has_capability('mod/forum:managesubscriptions', $context)) {
29 error('You do not have the permission to subscribe/unsubscribe other people!');
31 if (!$user = get_record("user", "id", $user)) {
32 error("User ID was incorrect");
38 if (groupmode($course, $cm)
39 and !forum_is_subscribed($user->id
, $forum->id
)
40 and !has_capability('moodle/site:accessallgroups', $context)) {
41 if (!mygroupid($course->id
)) {
42 error('Sorry, but you must be a group member to subscribe.');
46 require_login($course->id
, false, $cm);
48 if (isguest()) { // Guests can't subscribe
49 $wwwroot = $CFG->wwwroot
.'/login/index.php';
50 if (!empty($CFG->loginhttps
)) {
51 $wwwroot = str_replace('http','https', $wwwroot);
54 $strforums = get_string('modulenameplural', 'forum');
55 $crumbs[] = array('name' => $strforums, 'link' => "index.php?id=$course->id", 'type' => 'activity');
56 $crumbs[] = array('name' => format_string($forum->name
), 'link' => "view.php?f=$forum->id", 'type' => 'activityinstance');
58 $navigation = build_navigation($crumbs);
60 print_header($course->shortname
, $course->fullname
, $navigation, '', '', true, "", navmenu($course, $cm));
62 notice_yesno(get_string('noguestsubscribe', 'forum').'<br /><br />'.get_string('liketologin'),
63 $wwwroot, $_SERVER['HTTP_REFERER']);
64 print_footer($course);
68 $returnto = forum_go_back_to("index.php?id=$course->id");
70 if ($force and has_capability('mod/forum:managesubscriptions', $context)) {
71 if (forum_is_forcesubscribed($forum->id
)) {
72 forum_forcesubscribe($forum->id
, 0);
73 redirect($returnto, get_string("everyonecannowchoose", "forum"), 1);
75 forum_forcesubscribe($forum->id
, 1);
76 redirect($returnto, get_string("everyoneisnowsubscribed", "forum"), 1);
80 if (forum_is_forcesubscribed($forum->id
)) {
81 redirect($returnto, get_string("everyoneisnowsubscribed", "forum"), 1);
84 $info->name
= fullname($user);
85 $info->forum
= format_string($forum->name
);
87 if (forum_is_subscribed($user->id
, $forum->id
)) {
88 if (forum_unsubscribe($user->id
, $forum->id
)) {
89 add_to_log($course->id
, "forum", "unsubscribe", "view.php?f=$forum->id", $forum->id
, $cm->id
);
90 redirect($returnto, get_string("nownotsubscribed", "forum", $info), 1);
92 error("Could not unsubscribe you from that forum", $_SERVER["HTTP_REFERER"]);
96 if ($forum->forcesubscribe
== FORUM_DISALLOWSUBSCRIBE
&&
97 !has_capability('mod/forum:managesubscriptions', $context)) {
98 error(get_string('disallowsubscribe'),$_SERVER["HTTP_REFERER"]);
100 if (forum_subscribe($user->id
, $forum->id
) ) {
101 add_to_log($course->id
, "forum", "subscribe", "view.php?f=$forum->id", $forum->id
, $cm->id
);
102 redirect($returnto, get_string("nowsubscribed", "forum", $info), 1);
104 error("Could not subscribe you to that forum", $_SERVER["HTTP_REFERER"]);