MDL-40062 mod_forum: read tracking events
[moodle.git] / mod / forum / settracking.php
CommitLineData
38994d6f 1<?php
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 * Set tracking option for the forum.
20 *
21 * @package mod-forum
22 * @copyright 2005 mchurch
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2f67a9b3 24 */
38994d6f 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
30$returnpage = optional_param('returnpage', 'index.php', PARAM_FILE); // Page to return to.
31
a6855934 32$url = new moodle_url('/mod/forum/settracking.php', array('id'=>$id));
cd4e6b17 33if ($returnpage !== 'index.php') {
34 $url->param('returnpage', $returnpage);
35}
36$PAGE->set_url($url);
37
38if (! $forum = $DB->get_record("forum", array("id" => $id))) {
39 print_error('invalidforumid', 'forum');
40}
41
42if (! $course = $DB->get_record("course", array("id" => $forum->course))) {
43 print_error('invalidcoursemodule');
44}
45
46if (! $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
47 print_error('invalidcoursemodule');
48}
49
50require_course_login($course, false, $cm);
51
52$returnto = forum_go_back_to($returnpage.'?id='.$course->id.'&f='.$forum->id);
53
54if (!forum_tp_can_track_forums($forum)) {
55 redirect($returnto);
56}
57
39790bd8 58$info = new stdClass();
cd4e6b17 59$info->name = fullname($USER);
60$info->forum = format_string($forum->name);
e70ebc55
DP
61
62$eventparams = array(
63 'context' => context_module::instance($cm->id),
64 'relateduserid' => $USER->id,
65 'other' => array('forumid' => $forum->id),
66);
67
cd4e6b17 68if (forum_tp_is_tracked($forum) ) {
69 if (forum_tp_stop_tracking($forum->id)) {
e70ebc55
DP
70 $event = \mod_forum\event\readtracking_disabled::create($eventparams);
71 $event->trigger();
cd4e6b17 72 redirect($returnto, get_string("nownottracking", "forum", $info), 1);
73 } else {
74 print_error('cannottrack', '', $_SERVER["HTTP_REFERER"]);
38994d6f 75 }
76
cd4e6b17 77} else { // subscribe
78 if (forum_tp_start_tracking($forum->id)) {
e70ebc55
DP
79 $event = \mod_forum\event\readtracking_enabled::create($eventparams);
80 $event->trigger();
cd4e6b17 81 redirect($returnto, get_string("nowtracking", "forum", $info), 1);
82 } else {
83 print_error('cannottrack', '', $_SERVER["HTTP_REFERER"]);
38994d6f 84 }
cd4e6b17 85}
38994d6f 86
1adbd2c3 87