Commit | Line | Data |
---|---|---|
ebfb73db AN |
1 | <?php |
2 | // This file is part of Moodle - http://moodle.org/ | |
3 | // | |
4 | // Moodle is free software: you can redistribute it and/or modify | |
5 | // it under the terms of the GNU General Public License as published by | |
6 | // the Free Software Foundation, either version 3 of the License, or | |
7 | // (at your option) any later version. | |
8 | // | |
9 | // Moodle is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | // | |
14 | // You should have received a copy of the GNU General Public License | |
15 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
17 | /** | |
18 | * Subscribe to or unsubscribe from a forum discussion. | |
19 | * | |
20 | * @package mod_forum | |
21 | * @copyright 2014 Andrew Nicols <andrew@nicols.co.uk> | |
22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
23 | */ | |
24 | ||
25 | define('AJAX_SCRIPT', true); | |
26 | require_once(dirname(dirname(__DIR__)) . '/config.php'); | |
27 | require_once($CFG->dirroot . '/mod/forum/lib.php'); | |
28 | ||
29 | $forumid = required_param('forumid', PARAM_INT); // The forum to subscribe or unsubscribe. | |
30 | $discussionid = optional_param('discussionid', null, PARAM_INT); // The discussionid to subscribe. | |
31 | $sesskey = optional_param('sesskey', null, PARAM_RAW); | |
32 | ||
33 | $forum = $DB->get_record('forum', array('id' => $forumid), '*', MUST_EXIST); | |
34 | $course = $DB->get_record('course', array('id' => $forum->course), '*', MUST_EXIST); | |
35 | $discussion = $DB->get_record('forum_discussions', array('id' => $discussionid), '*', MUST_EXIST); | |
36 | $cm = get_coursemodule_from_instance('forum', $forum->id, $course->id, false, MUST_EXIST); | |
37 | $context = context_module::instance($cm->id); | |
38 | ||
39 | require_login($course, false, $cm); | |
40 | ||
41 | $return = new stdClass(); | |
42 | ||
43 | if (!\mod_forum\subscriptions::is_subscribable($forum)) { | |
44 | // Nothing to do. We won't actually output any content here though. | |
45 | echo json_encode($return); | |
46 | die; | |
47 | } | |
48 | ||
49 | if (\mod_forum\subscriptions::is_subscribed($USER->id, $forum, $discussion->id)) { | |
50 | // The user is subscribed, unsubscribe them. | |
51 | \mod_forum\subscriptions::unsubscribe_user_from_discussion($USER->id, $discussion, $context); | |
52 | } else { | |
53 | // The user is unsubscribed, subscribe them. | |
54 | \mod_forum\subscriptions::subscribe_user_to_discussion($USER->id, $discussion, $context); | |
55 | } | |
56 | ||
57 | // Now return the updated subscription icon. | |
58 | $return->icon = forum_get_discussion_subscription_icon($forum, $discussion->id); | |
59 | echo json_encode($return); | |
60 | die; |