MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / forum / subscribe.php
blob6baad93c689a138494f685df28f4877c3fad457a
1 <?php // $Id$
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);
22 } else {
23 $cm->id = 0;
24 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
27 if ($user) {
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");
34 } else {
35 $user = $USER;
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 $navlinks = array();
56 $navlinks[] = array('name' => $strforums, 'link' => "index.php?id=$course->id", 'type' => 'activity');
57 $navlinks[] = array('name' => format_string($forum->name), 'link' => "view.php?f=$forum->id", 'type' => 'activityinstance');
59 $navigation = build_navigation($navlinks);
61 print_header($course->shortname, $course->fullname, $navigation, '', '', true, "", navmenu($course, $cm));
63 notice_yesno(get_string('noguestsubscribe', 'forum').'<br /><br />'.get_string('liketologin'),
64 $wwwroot, $_SERVER['HTTP_REFERER']);
65 print_footer($course);
66 exit;
69 $returnto = forum_go_back_to("index.php?id=$course->id");
71 if ($force and has_capability('mod/forum:managesubscriptions', $context)) {
72 if (forum_is_forcesubscribed($forum->id)) {
73 forum_forcesubscribe($forum->id, 0);
74 redirect($returnto, get_string("everyonecannowchoose", "forum"), 1);
75 } else {
76 forum_forcesubscribe($forum->id, 1);
77 redirect($returnto, get_string("everyoneisnowsubscribed", "forum"), 1);
81 if (forum_is_forcesubscribed($forum->id)) {
82 redirect($returnto, get_string("everyoneisnowsubscribed", "forum"), 1);
85 $info->name = fullname($user);
86 $info->forum = format_string($forum->name);
88 if (forum_is_subscribed($user->id, $forum->id)) {
89 if (forum_unsubscribe($user->id, $forum->id)) {
90 add_to_log($course->id, "forum", "unsubscribe", "view.php?f=$forum->id", $forum->id, $cm->id);
91 redirect($returnto, get_string("nownotsubscribed", "forum", $info), 1);
92 } else {
93 error("Could not unsubscribe you from that forum", $_SERVER["HTTP_REFERER"]);
96 } else { // subscribe
97 if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE &&
98 !has_capability('mod/forum:managesubscriptions', $context)) {
99 error(get_string('disallowsubscribe'),$_SERVER["HTTP_REFERER"]);
101 if (forum_subscribe($user->id, $forum->id) ) {
102 add_to_log($course->id, "forum", "subscribe", "view.php?f=$forum->id", $forum->id, $cm->id);
103 redirect($returnto, get_string("nowsubscribed", "forum", $info), 1);
104 } else {
105 error("Could not subscribe you to that forum", $_SERVER["HTTP_REFERER"]);