Commit | Line | Data |
---|---|---|
cd4e6b17 | 1 | <?php |
f93f848a | 2 | |
8f685009 SH |
3 | // This file is part of Moodle - http://moodle.org/ |
4 | // | |
5 | // Moodle is free software: you can redistribute it and/or modify | |
6 | // it under the terms of the GNU General Public License as published by | |
7 | // the Free Software Foundation, either version 3 of the License, or | |
8 | // (at your option) any later version. | |
9 | // | |
10 | // Moodle is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | // GNU General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU General Public License | |
16 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
18 | /** | |
19 | * Subscribe to or unsubscribe from a forum. | |
2f67a9b3 | 20 | * |
8f685009 SH |
21 | * @package mod-forum |
22 | * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} | |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | */ | |
f93f848a | 25 | |
cd4e6b17 | 26 | require_once("../../config.php"); |
27 | require_once("lib.php"); | |
28 | ||
29 | $id = required_param('id',PARAM_INT); // The forum to subscribe or unsubscribe to | |
30 | $force = optional_param('force','',PARAM_ALPHA); // Force everyone to be subscribed to this forum? | |
31 | $user = optional_param('user',0,PARAM_INT); | |
32 | ||
a6855934 | 33 | $url = new moodle_url('/mod/forum/subscribe.php', array('id'=>$id)); |
cd4e6b17 | 34 | if ($force !== '') { |
35 | $url->param('force', $force); | |
36 | } | |
37 | if ($user !== 0) { | |
38 | $url->param('user', $user); | |
39 | } | |
40 | $PAGE->set_url($url); | |
41 | ||
42 | if (! $forum = $DB->get_record("forum", array("id" => $id))) { | |
43 | print_error('invalidforumid', 'forum'); | |
44 | } | |
45 | ||
46 | if (! $course = $DB->get_record("course", array("id" => $forum->course))) { | |
47 | print_error('invalidcoursemodule'); | |
48 | } | |
49 | ||
a6855934 PS |
50 | $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id, false, MUST_EXIST); |
51 | $context = get_context_instance(CONTEXT_MODULE, $cm->id); | |
cd4e6b17 | 52 | |
53 | if ($user) { | |
54 | if (!has_capability('mod/forum:managesubscriptions', $context)) { | |
55 | print_error('nopermissiontosubscribe', 'forum'); | |
f93f848a | 56 | } |
cd4e6b17 | 57 | if (!$user = $DB->get_record("user", array("id" => $user))) { |
58 | print_error('invaliduserid'); | |
f93f848a | 59 | } |
cd4e6b17 | 60 | } else { |
61 | $user = $USER; | |
62 | } | |
63 | ||
64 | if (isset($cm->groupmode) && empty($course->groupmodeforce)) { | |
65 | $groupmode = $cm->groupmode; | |
66 | } else { | |
67 | $groupmode = $course->groupmode; | |
68 | } | |
69 | if ($groupmode && !forum_is_subscribed($user->id, $forum) && !has_capability('moodle/site:accessallgroups', $context)) { | |
70 | if (!groups_get_all_groups($course->id, $USER->id)) { | |
71 | print_error('cannotsubscribe', 'forum'); | |
ec81373f | 72 | } |
cd4e6b17 | 73 | } |
ec81373f | 74 | |
cd4e6b17 | 75 | require_login($course->id, false, $cm); |
76 | ||
4f0c2d00 | 77 | if (!is_enrolled($context)) { // Guests and visitors can't subscribe - only enrolled |
cd4e6b17 | 78 | $PAGE->set_title($course->shortname); |
79 | $PAGE->set_heading($course->fullname); | |
80 | echo $OUTPUT->header(); | |
81 | echo $OUTPUT->confirm(get_string('noguestsubscribe', 'forum').'<br /><br />'.get_string('liketologin'), | |
a6855934 | 82 | get_login_url(), new moodle_url('/mod/forum/view.php', array('f'=>$id))); |
cd4e6b17 | 83 | echo $OUTPUT->footer(); |
84 | exit; | |
85 | } | |
86 | ||
87 | $returnto = optional_param('backtoindex',0,PARAM_INT) | |
88 | ? "index.php?id=".$course->id | |
89 | : "view.php?f=$id"; | |
90 | ||
91 | if ($force and has_capability('mod/forum:managesubscriptions', $context)) { | |
92 | if (forum_is_forcesubscribed($forum)) { | |
93 | forum_forcesubscribe($forum->id, 0); | |
94 | redirect($returnto, get_string("everyonecannowchoose", "forum"), 1); | |
3bc9bf97 | 95 | } else { |
cd4e6b17 | 96 | forum_forcesubscribe($forum->id, 1); |
97 | redirect($returnto, get_string("everyoneisnowsubscribed", "forum"), 1); | |
3bc9bf97 | 98 | } |
cd4e6b17 | 99 | } |
3bc9bf97 | 100 | |
cd4e6b17 | 101 | if (forum_is_forcesubscribed($forum)) { |
102 | redirect($returnto, get_string("everyoneisnowsubscribed", "forum"), 1); | |
103 | } | |
818c1b05 | 104 | |
cd4e6b17 | 105 | $info->name = fullname($user); |
106 | $info->forum = format_string($forum->name); | |
ec81373f | 107 | |
cd4e6b17 | 108 | if (forum_is_subscribed($user->id, $forum->id)) { |
109 | if (forum_unsubscribe($user->id, $forum->id)) { | |
110 | add_to_log($course->id, "forum", "unsubscribe", "view.php?f=$forum->id", $forum->id, $cm->id); | |
111 | redirect($returnto, get_string("nownotsubscribed", "forum", $info), 1); | |
112 | } else { | |
113 | print_error('cannotunsubscribe', 'forum', $_SERVER["HTTP_REFERER"]); | |
f93f848a | 114 | } |
115 | ||
cd4e6b17 | 116 | } else { // subscribe |
117 | if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE && | |
118 | !has_capability('mod/forum:managesubscriptions', $context)) { | |
119 | print_error('disallowsubscribe', 'forum', $_SERVER["HTTP_REFERER"]); | |
501cdbd8 | 120 | } |
cd4e6b17 | 121 | if (!has_capability('mod/forum:viewdiscussion', $context)) { |
122 | print_error('cannotsubscribe', 'forum', $_SERVER["HTTP_REFERER"]); | |
501cdbd8 | 123 | } |
cd4e6b17 | 124 | if (forum_subscribe($user->id, $forum->id) ) { |
125 | add_to_log($course->id, "forum", "subscribe", "view.php?f=$forum->id", $forum->id, $cm->id); | |
126 | redirect($returnto, get_string("nowsubscribed", "forum", $info), 1); | |
127 | } else { | |
128 | print_error('cannotsubscribe', 'forum', $_SERVER["HTTP_REFERER"]); | |
f93f848a | 129 | } |
cd4e6b17 | 130 | } |
f93f848a | 131 |