-
Notifications
You must be signed in to change notification settings - Fork 23
/
markposts.php
141 lines (109 loc) · 4.96 KB
/
markposts.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* File to mark posts as read.
*
* @package mod_moodleoverflow
* @copyright 2017 Kennet Winter <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG, $DB, $PAGE, $USER, $SESSION, $OUTPUT;
// We do not need the locallib here.
require_once('../../config.php');
require_once($CFG->dirroot . '/mod/moodleoverflow/locallib.php');
// Define the parameters.
$moodleoverflowid = required_param('m', PARAM_INT); // The moodleoverflowinstance to mark.
$discussionid = optional_param('d', 0, PARAM_INT); // The discussion to mark.
$returndiscussion = optional_param('return', 0, PARAM_INT); // The page to return to.
// Prepare the array that should be used to return to this page.
$url = new moodle_url('/mod/moodleoverflow/markposts.php', ['m' => $moodleoverflowid]);
// Check the optional params.
if ($discussionid !== 0) {
$url->param('d', $discussionid);
}
if ($returndiscussion !== 0) {
$url->param('returndiscussion', $returndiscussion);
}
// Set the url that should be used to return to this page.
$PAGE->set_url($url);
// Retrieve the connected moodleoverflow instance.
$moodleoverflow = moodleoverflow_get_record_or_exception('moodleoverflow', ['id' => $moodleoverflowid], 'invalidmoodleoverflowid');
// Retrieve the connected course.
$course = moodleoverflow_get_record_or_exception('course', ['id' => $moodleoverflow->course], 'invalidcourseid', '*', true);
// Get the coursemodule.
if (!$cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id, $course->id)) {
throw new moodle_exception('invalidcoursemodule');
}
// Get the current user.
$user = $USER;
// From now on, the user must be logged in and enrolled.
require_login($course, false, $cm);
// Default relink address.
if ($returndiscussion === 0) {
// If no parameter is set, relink to the view.
$returnto = new moodle_url("/mod/moodleoverflow/view.php", ['m' => $moodleoverflow->id]);
} else {
// Else relink back to the discussion we are coming from.
$returnto = new moodle_url("/mod/moodleoverflow/discussion.php", ['d' => $returndiscussion]);
}
// Guests can't mark posts as read.
if (isguestuser()) {
// Set Page-Parameter.
$PAGE->set_title($course->shortname);
$PAGE->set_heading($course->fullname);
// Create the message.
$message = get_string('noguesttracking', 'moodleoverflow') . '<br /><br />' . get_string('liketologin');
// Display the page with a confirm-element.
echo $OUTPUT->header();
echo $OUTPUT->confirm($message, get_login_url(), $returnto);
echo $OUTPUT->footer();
exit;
}
// Delete a single discussion.
if (!empty($discussionid)) {
// Check if the discussion exists.
$options = ['id' => $discussionid, 'moodleoverflow' => $moodleoverflow->id];
$discussion = moodleoverflow_get_record_or_exception('moodleoverflow_discussions', $options, 'invaliddiscussionid');
// Mark all the discussions read.
if (!\mod_moodleoverflow\readtracking::moodleoverflow_mark_discussion_read($discussionid,
context_module::instance($cm->id), $user->id)) {
// Display an error, if something failes.
$message = get_string('markreadfailed', 'moodleoverflow');
$status = \core\output\notification::NOTIFY_ERROR;
} else {
// The discussion is successfully marked as read.
$message = get_string('markmoodleoverflowreadsuccessful', 'moodleoverflow');
$status = \core\output\notification::NOTIFY_SUCCESS;
}
// Redirect the user.
redirect(moodleoverflow_go_back_to($returnto), $message, null, $status);
exit;
} else {
// Mark all message read in the current instance.
if (!\mod_moodleoverflow\readtracking::moodleoverflow_mark_moodleoverflow_read($cm, $user->id)) {
// Display an error, if something fails.
$message = get_string('markreadfailed', 'moodleoverflow');
$status = \core\output\notification::NOTIFY_ERROR;
} else {
// All posts of the instance have been marked as read.
$message = get_string('markdiscussionreadsuccessful', 'moodleoverflow');
$status = \core\output\notification::NOTIFY_SUCCESS;
}
// Redirect the user back to the view.php.
redirect(moodleoverflow_go_back_to($returnto), $message, null, $status);
exit;
}