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)
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 $navigation = build_navigation('', $cm);
55 print_header($course->shortname
, $course->fullname
, $navigation, '', '', true, "", navmenu($course, $cm));
57 notice_yesno(get_string('noguestsubscribe', 'forum').'<br /><br />'.get_string('liketologin'),
58 $wwwroot, $_SERVER['HTTP_REFERER']);
59 print_footer($course);
63 $returnto = optional_param('backtoindex',0,PARAM_INT
)
64 ?
"index.php?id=".$course->id
67 if ($force and has_capability('mod/forum:managesubscriptions', $context)) {
68 if (forum_is_forcesubscribed($forum)) {
69 forum_forcesubscribe($forum->id
, 0);
70 redirect($returnto, get_string("everyonecannowchoose", "forum"), 1);
72 forum_forcesubscribe($forum->id
, 1);
73 redirect($returnto, get_string("everyoneisnowsubscribed", "forum"), 1);
77 if (forum_is_forcesubscribed($forum)) {
78 redirect($returnto, get_string("everyoneisnowsubscribed", "forum"), 1);
81 $info->name
= fullname($user);
82 $info->forum
= format_string($forum->name
);
84 if (forum_is_subscribed($user->id
, $forum->id
)) {
85 if (forum_unsubscribe($user->id
, $forum->id
)) {
86 add_to_log($course->id
, "forum", "unsubscribe", "view.php?f=$forum->id", $forum->id
, $cm->id
);
87 redirect($returnto, get_string("nownotsubscribed", "forum", $info), 1);
89 error("Could not unsubscribe you from that forum", $_SERVER["HTTP_REFERER"]);
93 if ($forum->forcesubscribe
== FORUM_DISALLOWSUBSCRIBE
&&
94 !has_capability('mod/forum:managesubscriptions', $context)) {
95 print_error('disallowsubscribe', 'forum', $_SERVER["HTTP_REFERER"]);
97 if (!has_capability('mod/forum:viewdiscussion', $context)) {
98 error("Could not subscribe you to that forum", $_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"]);