MDL-25068 forum: fixed error string usage
[moodle.git] / mod / forum / subscribe.php
CommitLineData
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 26require_once("../../config.php");
27require_once("lib.php");
28
29$id = required_param('id',PARAM_INT); // The forum to subscribe or unsubscribe to
afef965e 30$mode = optional_param('mode',false,PARAM_INT); // Force everyone to be subscribed to this forum?
cd4e6b17 31$user = optional_param('user',0,PARAM_INT);
32
a6855934 33$url = new moodle_url('/mod/forum/subscribe.php', array('id'=>$id));
afef965e 34if ($mode !== '') {
99eb51d9 35 $url->param('mode', $mode);
cd4e6b17 36}
37if ($user !== 0) {
38 $url->param('user', $user);
39}
40$PAGE->set_url($url);
41
42if (! $forum = $DB->get_record("forum", array("id" => $id))) {
43 print_error('invalidforumid', 'forum');
44}
45
46if (! $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
53if ($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
64if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
65 $groupmode = $cm->groupmode;
66} else {
67 $groupmode = $course->groupmode;
68}
69if ($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 75require_login($course->id, false, $cm);
76
4f0c2d00 77if (!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();
9a3adb20 81 echo $OUTPUT->confirm(get_string('subscribeenrolledonly', '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
afef965e
SH
91if ($mode !== false && has_capability('mod/forum:managesubscriptions', $context)) {
92 switch ($mode) {
93 case FORUM_CHOOSESUBSCRIBE : // 0
94 forum_forcesubscribe($forum->id, 0);
95 redirect($returnto, get_string("everyonecannowchoose", "forum"), 1);
96 break;
97 case FORUM_FORCESUBSCRIBE : // 1
98 forum_forcesubscribe($forum->id, 1);
99 redirect($returnto, get_string("everyoneisnowsubscribed", "forum"), 1);
100 break;
101 case FORUM_INITIALSUBSCRIBE : // 2
102 forum_forcesubscribe($forum->id, 2);
103 redirect($returnto, get_string("everyoneisnowsubscribed", "forum"), 1);
104 break;
105 case FORUM_DISALLOWSUBSCRIBE : // 3
106 forum_forcesubscribe($forum->id, 3);
107 redirect($returnto, get_string("noonecansubscribenow", "forum"), 1);
108 break;
109 default:
110 print_error(get_string('invalidforcesubscribe', 'forum'));
3bc9bf97 111 }
cd4e6b17 112}
3bc9bf97 113
cd4e6b17 114if (forum_is_forcesubscribed($forum)) {
115 redirect($returnto, get_string("everyoneisnowsubscribed", "forum"), 1);
116}
818c1b05 117
cd4e6b17 118$info->name = fullname($user);
119$info->forum = format_string($forum->name);
ec81373f 120
cd4e6b17 121if (forum_is_subscribed($user->id, $forum->id)) {
122 if (forum_unsubscribe($user->id, $forum->id)) {
123 add_to_log($course->id, "forum", "unsubscribe", "view.php?f=$forum->id", $forum->id, $cm->id);
124 redirect($returnto, get_string("nownotsubscribed", "forum", $info), 1);
125 } else {
126 print_error('cannotunsubscribe', 'forum', $_SERVER["HTTP_REFERER"]);
f93f848a 127 }
128
cd4e6b17 129} else { // subscribe
130 if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE &&
131 !has_capability('mod/forum:managesubscriptions', $context)) {
132 print_error('disallowsubscribe', 'forum', $_SERVER["HTTP_REFERER"]);
501cdbd8 133 }
cd4e6b17 134 if (!has_capability('mod/forum:viewdiscussion', $context)) {
d1571da6 135 print_error('noviewdiscussionspermission', 'forum', $_SERVER["HTTP_REFERER"]);
f93f848a 136 }
d1571da6
DM
137 forum_subscribe($user->id, $forum->id);
138 add_to_log($course->id, "forum", "subscribe", "view.php?f=$forum->id", $forum->id, $cm->id);
139 redirect($returnto, get_string("nowsubscribed", "forum", $info), 1);
cd4e6b17 140}