-
Notifications
You must be signed in to change notification settings - Fork 29
/
editcomment.php
208 lines (180 loc) · 7.86 KB
/
editcomment.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?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/>.
/**
* This page allows a user to add and edit blog comments
*
* @author Matt Clarkson <[email protected]>
* @author Sam Marshall <[email protected]>
* @package oublog
*/
require_once("../../config.php");
require_once("locallib.php");
require_once('comment_form.php');
define('OUBLOG_CONFIRMED_COOKIE', 'OUBLOG_REALPERSON');
$blog = required_param('blog', PARAM_INT); // Blog ID
$postid = required_param('post', PARAM_INT); // Post ID for editing
$commentid = optional_param('comment', 0, PARAM_INT); // Comment ID for editing
$cmid = optional_param('cmid', null, PARAM_INT);
$referurl = optional_param('referurl', 0, PARAM_LOCALURL);
if (!$oublog = $DB->get_record("oublog", array("id"=>$blog))) {
throw new moodle_exception('invalidblog', 'oublog');
}
if (!$cm = get_coursemodule_from_instance('oublog', $blog)) {
throw new moodle_exception('invalidcoursemodule');
}
if (!$course = $DB->get_record("course", array("id"=>$oublog->course))) {
throw new moodle_exception('coursemisconf');
}
if (!$post = oublog_get_post($postid)) {
throw new moodle_exception('invalidpost', 'oublog');
}
if (!$oubloginstance = $DB->get_record('oublog_instances', array('id'=>$post->oubloginstancesid))) {
throw new moodle_exception('invalidblog', 'oublog');
}
$url = new moodle_url('/mod/oublog/editcomment.php', array('blog'=>$blog, 'post'=>$postid, 'comment'=>$commentid));
$PAGE->set_url($url);
// Check security.
$context = context_module::instance($cm->id);
$childdata = oublog_get_blog_data_base_on_cmid_of_childblog($cmid, $oublog);
$childcm = null;
$childcourse = null;
$childoublog = null;
if (!empty($childdata)) {
$context = $childdata['context'];
$childcm = $childdata['cm'];
$childcourse = $childdata['course'];
$childoublog = $childdata['ousharedblog'];
oublog_check_view_permissions($childdata['ousharedblog'], $childdata['context'], $childdata['cm']);
} else {
oublog_check_view_permissions($oublog, $context, $cm);
}
$correctglobal = isset($childoublog->global) ? $childoublog->global : $oublog->global;
$post->userid=$oubloginstance->userid; // oublog_can_view_post needs this
if (!oublog_can_view_post($post, $USER, $context, $cm, $oublog, $childcm, $childoublog)) {
throw new moodle_exception('accessdenied', 'oublog');
}
oublog_get_activity_groupmode($childcm ? $childcm : $cm, $childcourse ? $childcourse : $course);
if (!oublog_can_comment($childcm ? $childcm : $cm, $childoublog ? $childoublog : $oublog, $post)) {
throw new moodle_exception('accessdenied', 'oublog');
}
if ($oublog->allowcomments == OUBLOG_COMMENTS_PREVENT || $post->allowcomments == OUBLOG_COMMENTS_PREVENT ||
(!empty($childoublog->allowcomments) && $childoublog->allowcomments == OUBLOG_COMMENTS_PREVENT)) {
throw new moodle_exception('commentsnotallowed', 'oublog');
}
$viewurl = !empty($referurl) ? $referurl : new moodle_url('/mod/oublog/viewpost.php', array('post' => $post->id));
if ($correctglobal) {
$blogtype = 'personal';
if (!$oubloguser = $DB->get_record('user', array('id'=>$oubloginstance->userid))) {
throw new moodle_exception('invaliduserid');
}
} else {
$blogtype = 'course';
}
$renderer = $PAGE->get_renderer('mod_oublog');
// Get strings.
$stroublogs = get_string('modulenameplural', 'oublog');
$stroublog = get_string('modulename', 'oublog');
$straddcomment = get_string('newcomment', 'oublog');
$moderated = !(isloggedin() && !isguestuser());
$confirmed = isset($_COOKIE[OUBLOG_CONFIRMED_COOKIE]) &&
$_COOKIE[OUBLOG_CONFIRMED_COOKIE] == get_string(
'moderated_confirmvalue', 'oublog');
$mform = new mod_oublog_comment_form('editcomment.php', array(
'maxvisibility' => $childoublog ? $childoublog->maxvisibility : $oublog->maxvisibility,
'edit' => !empty($commentid),
'blogid' => $blog,
'postid' => $postid,
'moderated' => $moderated,
'confirmed' => $confirmed,
'maxbytes' => $childoublog ? $childoublog->maxbytes : $oublog->maxbytes,
'referurl' => $referurl,
'cmid' => $cmid,
'postrender' => $renderer->render_post($childcm ? $childcm : $cm, $childoublog ? $childoublog : $oublog,
$post, $url, $blogtype, false, false, false,
false, false, true, 'top', $cm, $cmid),
));
if ($mform->is_cancelled()) {
redirect($viewurl);
exit;
}
$PAGE->set_title(format_string(!empty($childoublog->name) ? $childoublog->name : $oublog->name));
$PAGE->set_heading(format_string(!empty($childcourse->fullname) ? $childcourse->fullname : $course->fullname));
if (!$comment = $mform->get_data()) {
$comment = new stdClass;
$comment->general = $straddcomment;
$comment->blog = $blog;
$comment->post = $postid;
$mform->set_data($comment);
// Print the header
if ($blogtype == 'personal') {
oublog_build_navigation($oublog, $oubloginstance, $oubloguser);
} else {
oublog_build_navigation($childoublog ? $childoublog : $oublog, $oubloginstance, null);
$url = new moodle_url("$CFG->wwwroot/course/mod.php", array('update' => $cm->id, 'return' => true, 'sesskey' => sesskey()));
}
oublog_get_post_extranav($post, true, $cmid);
$PAGE->navbar->add($comment->general);
echo $OUTPUT->header();
echo '<br />';
$mform->display();
echo $OUTPUT->footer();
} else {
// Prepare comment for database
unset($comment->id);
$comment->userid = $USER->id;
$comment->postid = $postid;
// Special behaviour for moderated users
if ($moderated) {
// Check IP address
if (oublog_too_many_comments_from_ip()) {
throw new moodle_exception('error_toomanycomments', 'oublog');
}
// Set the confirmed cookie if they haven't got it yet
if (!$confirmed) {
setcookie(OUBLOG_CONFIRMED_COOKIE, $comment->confirm,
time() + 365 * 24 * 3600); // Expire in 1 year
}
if (!oublog_add_comment_moderated($oublog, $oubloginstance, $post, $comment)) {
throw new moodle_exception('couldnotaddcomment', 'oublog');
}
$approvaltime = oublog_get_typical_approval_time($post->userid);
oublog_build_navigation($childoublog ? $childoublog : $oublog, $oubloginstance, isset($oubloguser) ? $oubloguser : null);
oublog_get_post_extranav($post, true, $cmid);
$PAGE->navbar->add(get_string('moderated_submitted', 'oublog'));
echo $OUTPUT->header();
notice(get_string('moderated_addedcomment', 'oublog') .
($approvaltime ? ' ' .
get_string('moderated_typicaltime', 'oublog', $approvaltime)
: ''), 'viewpost.php?post=' . $postid, $course);
// Does not continue.
}
$comment->userid = $USER->id;
if (!oublog_add_comment($course, $cm, $oublog, $comment)) {
throw new moodle_exception('couldnotaddcomment', 'oublog');
}
// Log add comment event.
$params = array(
'context' => $context,
'objectid' => $comment->id,
'other' => array(
'oublogid' => $oublog->id,
'postid' => $comment->postid,
)
);
$event = \mod_oublog\event\comment_created::create($params);
$event->trigger();
redirect($viewurl);
}