editpost.php 11.8 KB
Newer Older
Clare Lenihan's avatar
Clare Lenihan committed
1
2
3
4
5
<?php
/**
 *
 * @package    mahara
 * @subpackage interaction-forum
6
 * @author     Catalyst IT Ltd
7
8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
Clare Lenihan's avatar
Clare Lenihan committed
9
10
11
12
 *
 */

define('INTERNAL', 1);
13
14
define('MENUITEM', 'engage/mygroups');
define('MENUITEM_SUBPAGE', 'forums');
15
16
17
18
define('SECTION_PLUGINTYPE', 'interaction');
define('SECTION_PLUGINNAME', 'forum');
define('SECTION_PAGE', 'editpost');

Clare Lenihan's avatar
Clare Lenihan committed
19
20
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
safe_require('interaction', 'forum');
21
require_once('group.php');
22
require_once(get_config('docroot') . 'interaction/lib.php');
23
require_once('antispam.php');
24
define('SUBSECTIONHEADING', get_string('nameplural', 'interaction.forum'));
Clare Lenihan's avatar
Clare Lenihan committed
25

26
$postid = param_integer('id', 0);
27

28
if ($postid == 0) { // post reply
Clare Lenihan's avatar
Clare Lenihan committed
29
30
    unset($postid);
    $parentid = param_integer('parent');
31
32
33
34
}
else { // edit post
    $post = get_record_sql(
        'SELECT p.subject, p.body, p.parent, p.topic, p.poster, ' . db_format_tsfield('p.ctime', 'ctime') . '
Clare Lenihan's avatar
Clare Lenihan committed
35
        FROM {interaction_forum_post} p
36
        WHERE p.id = ?
Clare Lenihan's avatar
Clare Lenihan committed
37
        AND p.deleted != 1
38
39
        AND p.parent IS NOT NULL',
        array($postid)
Clare Lenihan's avatar
Clare Lenihan committed
40
    );
41
    if (!$post) {
42
43
        throw new NotFoundException(get_string('cantfindpost', 'interaction.forum', $postid));
    }
44
45
    $parentid = $post->parent;
}
Clare Lenihan's avatar
Clare Lenihan committed
46

47
48
49
50
if (!$parentid) {
    throw new NotFoundException(get_string('cantfindpost', 'interaction.forum', $parentid));
}

51
$parent = get_record_sql(
52
    'SELECT p.subject, p.body, p.topic, p.parent, p.poster, p.deleted, ' . db_format_tsfield('p.ctime', 'ctime') . ', m.user AS moderator, t.id AS topic, t.forum, t.closed AS topicclosed, p2.subject AS topicsubject, f.group AS "group", f.title AS forumtitle, g.name AS groupname, COUNT(p3.id)
53
54
55
56
    FROM {interaction_forum_post} p
    INNER JOIN {interaction_forum_topic} t ON (p.topic = t.id AND t.deleted != 1)
    INNER JOIN {interaction_forum_post} p2 ON (p2.topic = t.id AND p2.parent IS NULL)
    INNER JOIN {interaction_instance} f ON (t.forum = f.id AND f.deleted != 1)
57
58
59
60
61
    LEFT JOIN (
        SELECT m.forum, m.user
        FROM {interaction_forum_moderator} m
        INNER JOIN {usr} u ON (m.user = u.id AND u.deleted = 0)
    ) m ON (m.forum = f.id AND m.user = p.poster)
62
    INNER JOIN {group} g ON (g.id = f.group AND g.deleted = ?)
63
    INNER JOIN {interaction_forum_post} p3 ON (p.poster = p3.poster)
64
65
66
    INNER JOIN {interaction_forum_topic} t2 ON (t2.deleted != 1 AND p3.topic = t2.id)
    INNER JOIN {interaction_instance} f2 ON (t2.forum = f2.id AND f2.deleted != 1 AND f2.group = f.group)
    WHERE p.id = ?
67
    GROUP BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15',
68
    array(0, $parentid)
69
70
71
);


72
define('GROUP', $parent->group);
73

74
75
$membership = user_can_access_forum((int)$parent->forum);
$moderator = (bool)($membership & INTERACTION_FORUM_MOD);
76
$admintutor = (bool) group_get_user_admintutor_groups();
77
$poster = new User();
Clare Lenihan's avatar
Clare Lenihan committed
78

79
if (!isset($postid)) { // post reply
80
81
82
    if ($parent->deleted) {
        throw new NotFoundException(get_string('cantfindpost', 'interaction.forum', $parentid));
    }
83
84
85
    if (!group_within_edit_window($parent->group)) {
        throw new AccessDeniedException(get_string('cantaddposttoforum', 'interaction.forum'));
    }
86
87
88
89
90
    if (!$membership) {
        throw new AccessDeniedException(get_string('cantaddposttoforum', 'interaction.forum'));
    }
    if (!$moderator && $parent->topicclosed) {
        throw new AccessDeniedException(get_string('cantaddposttotopic', 'interaction.forum'));
Clare Lenihan's avatar
Clare Lenihan committed
91
    }
92
93
    $action = get_string('postreply', 'interaction.forum');
    define('TITLE', $parent->topicsubject . ' - ' . $action);
94
    $poster->find_by_id($parent->poster);
Clare Lenihan's avatar
Clare Lenihan committed
95
}
96
else { // edit post
97
98
99
    if (!group_within_edit_window($parent->group)) {
        throw new AccessDeniedException(get_string('canteditpost', 'interaction.forum'));
    }
100
    // no record for edits to own posts with 30 minutes
101
    if (user_can_edit_post($post->poster, $post->ctime)) {
102
        $post->editrecord = false;
103
        $timeleft = ceil(get_config_plugin('interaction', 'forum', 'postdelay') - (time() - $post->ctime) / 60);
104
105
106
107
    }
    else if ($moderator) {
        $post->editrecord = true;
    }
108
109
110
111
    else if (user_can_edit_post($post->poster, $post->ctime, $USER->get('id'), false)) {
        $SESSION->add_error_msg(get_string('postaftertimeout', 'interaction.forum', get_config_plugin('interaction', 'forum', 'postdelay')));
        redirect('/interaction/forum/topic.php?id=' . $parent->topic);
    }
112
    else {
113
        throw new AccessDeniedException(get_string('canteditpost', 'interaction.forum'));
Clare Lenihan's avatar
Clare Lenihan committed
114
    }
115
116
    $action = get_string('editpost', 'interaction.forum');
    define('TITLE', $parent->topicsubject . ' - ' . $action);
117
    $poster->find_by_id($post->poster);
Clare Lenihan's avatar
Clare Lenihan committed
118
119
}

120
$parent->ctime = relative_date(get_string('strftimerecentfullrelative', 'interaction.forum'), get_string('strftimerecentfull'), $parent->ctime);
Clare Lenihan's avatar
Clare Lenihan committed
121

122
$editform = array(
123
124
    'name'     => 'editpost',
    'successcallback' => isset($post) ? 'editpost_submit' : 'addpost_submit',
125
    'autofocus' => 'body',
Clare Lenihan's avatar
Clare Lenihan committed
126
127
128
    'elements' => array(
        'subject' => array(
            'type'         => 'text',
129
            'title'        => get_string('Subject', 'interaction.forum'),
130
            'defaultvalue' => isset($post) ? $post->subject : null,
Clare Lenihan's avatar
Clare Lenihan committed
131
            'rules'        => array(
132
                'maxlength' => 255
133
134
135
            ),
            'hidewhenempty' => true,
            'expandtext'    => get_string('clicksetsubject', 'interaction.forum'),
Clare Lenihan's avatar
Clare Lenihan committed
136
137
138
        ),
        'body' => array(
            'type'         => 'wysiwyg',
139
            'title'        => get_string('Body', 'interaction.forum'),
140
            'rows'         => 18,
Clare Lenihan's avatar
Clare Lenihan committed
141
            'cols'         => 70,
142
            'defaultvalue' => isset($post) ? $post->body : null,
143
144
145
146
            'rules'        => array(
                'required'  => true,
                'maxlength' => 65536,
            ),
Clare Lenihan's avatar
Clare Lenihan committed
147
        ),
148
        'sendnow' => array(
149
            'type'         => 'switchbox',
150
151
152
153
            'title'        => get_string('sendnow', 'interaction.forum'),
            'description'  => get_string('sendnowdescription', 'interaction.forum', get_config_plugin('interaction', 'forum', 'postdelay')),
            'defaultvalue' => false,
        ),
Clare Lenihan's avatar
Clare Lenihan committed
154
155
        'submit'   => array(
            'type'  => 'submitcancel',
156
            'class' => 'btn-primary',
Clare Lenihan's avatar
Clare Lenihan committed
157
            'value'       => array(
158
                isset($post) ? get_string('save') : get_string('Post','interaction.forum'),
159
                get_string('cancel')
Clare Lenihan's avatar
Clare Lenihan committed
160
            ),
161
            'goto'      => get_config('wwwroot') . 'interaction/forum/topic.php?id=' . $parent->topic . '&post=' . (isset($postid) ? $postid : $parentid)
Clare Lenihan's avatar
Clare Lenihan committed
162
        ),
163
164
        'topic' => array(
            'type' => 'hidden',
165
            'value' => $parent->topic
166
167
168
169
170
        ),
        'editrecord' => array(
            'type' => 'hidden',
            'value' => isset($post) ? $post->editrecord : false
        )
Clare Lenihan's avatar
Clare Lenihan committed
171
    ),
172
173
174
175
176
177
178
);

if ((!$moderator && !$admintutor && !group_sendnow($parent->group)) || get_config_plugin('interaction', 'forum', 'postdelay') <= 0) {
    unset($editform['elements']['sendnow']);
}

$editform = pieform($editform);
Clare Lenihan's avatar
Clare Lenihan committed
179

180
181
182
183
function editpost_validate(Pieform $form, $values) {
    if ($baddomain = get_first_blacklisted_domain($values['body'])) {
        $form->set_error('body', get_string('blacklisteddomaininurl', 'mahara', $baddomain));
    }
184
185
    $result = probation_validate_content($values['body']);
    if ($result !== true) {
186
        $form->set_error('body', get_string('newuserscantpostlinksorimages1'));
187
    }
188
189
}

190
191
192
193
194
195
196
197
function get_groupid_from_postid($postid) {
    $groupid = get_field_sql("SELECT i.group FROM {interaction_instance} i
                              INNER JOIN {interaction_forum_topic} t ON i.id = t.forum
                              INNER JOIN {interaction_forum_post} p on p.topic = t.id
                              WHERE p.id =?", array($postid));
    return $groupid;
}

Clare Lenihan's avatar
Clare Lenihan committed
198
function editpost_submit(Pieform $form, $values) {
Clare Lenihan's avatar
Clare Lenihan committed
199
    global $USER, $SESSION;
200
    require_once('embeddedimage.php');
Clare Lenihan's avatar
Clare Lenihan committed
201
    $postid = param_integer('id');
202
203
    $groupid = get_groupid_from_postid($postid);
    $newbody = EmbeddedImage::prepare_embedded_images($values['body'], 'post', $postid, $groupid);
Clare Lenihan's avatar
Clare Lenihan committed
204
205
206
207
208
    db_begin();
    update_record(
        'interaction_forum_post',
        array(
            'subject' => $values['subject'],
209
            'body' => PluginInteractionForum::prepare_post_body($newbody, $postid),
Clare Lenihan's avatar
Clare Lenihan committed
210
211
212
        ),
        array('id' => $postid)
    );
213
    if ($values['editrecord']) {
Clare Lenihan's avatar
Clare Lenihan committed
214
        insert_record(
Clare Lenihan's avatar
Clare Lenihan committed
215
            'interaction_forum_edit',
Clare Lenihan's avatar
Clare Lenihan committed
216
            (object)array(
Clare Lenihan's avatar
Clare Lenihan committed
217
218
219
220
                'user' => $USER->get('id'),
                'post' => $postid,
                'ctime' => db_format_timestamp(time())
            )
Clare Lenihan's avatar
Clare Lenihan committed
221
222
        );
    }
223
    db_commit();
Clare Lenihan's avatar
Clare Lenihan committed
224
    $SESSION->add_ok_msg(get_string('editpostsuccess', 'interaction.forum'));
225
    redirect(get_config('wwwroot') . 'interaction/forum/topic.php?id=' . $values['topic'] . '&post=' . $postid);
Clare Lenihan's avatar
Clare Lenihan committed
226
227
228
229
}

function addpost_submit(Pieform $form, $values) {
    global $USER, $SESSION;
230
    require_once('embeddedimage.php');
Clare Lenihan's avatar
Clare Lenihan committed
231
    $parentid = param_integer('parent');
232
233
234
235
236
237
238
    $post = (object)array(
        'topic'   => $values['topic'],
        'poster'  => $USER->get('id'),
        'parent'  => $parentid,
        'subject' => $values['subject'],
        'body'    => $values['body'],
        'ctime'   =>  db_format_timestamp(time())
Clare Lenihan's avatar
Clare Lenihan committed
239
    );
240
    $sendnow = isset($values['sendnow']) && $values['sendnow'] ? 1 : 0;
241
242
243
244
245
    // See if the same content has been submitted in the last 5 seconds. If so, don't add this post.
    $oldpost = get_record_select('interaction_forum_post', 'topic = ? AND poster = ? AND parent = ? AND subject = ? AND body = ? AND ctime > ?',
        array($post->topic, $post->poster, $post->parent, $post->subject, $post->body, db_format_timestamp(time() - 5)),
        'id');
    if ($oldpost) {
246
        redirect(get_config('wwwroot') . 'interaction/forum/topic.php?id=' . $values['topic'] . '&post=' . $oldpost->id);
247
    }
248
249
250
251
    $postrec = new stdClass();
    $postid = $postrec->id = insert_record('interaction_forum_post', $post, 'id', true);
    $postrec->path = get_field('interaction_forum_post', 'path', 'id', $parentid) . '/' . sprintf('%010d', $postrec->id);
    update_record('interaction_forum_post', $postrec);
252
253

    // Rewrite the post id into links in the body
254
255
256
    $groupid = get_groupid_from_postid($postid);
    $newbody = EmbeddedImage::prepare_embedded_images($post->body, 'post', $postid, $groupid);
    $newbody = PluginInteractionForum::prepare_post_body($newbody, $postid);
257
258
259
    if (!empty($newbody) && $newbody != $post->body) {
        set_field('interaction_forum_post', 'body', $newbody, 'id', $postid);
    }
260
261
262
263
264
265
    if ($sendnow == 0) {
      $delay = get_config_plugin('interaction', 'forum', 'postdelay');
    }
    else {
      $delay = 0;
    }
266
267
268
    if (!is_null($delay) && $delay == 0) {
        PluginInteractionForum::interaction_forum_new_post(array($postid));
    }
Clare Lenihan's avatar
Clare Lenihan committed
269
    $SESSION->add_ok_msg(get_string('addpostsuccess', 'interaction.forum'));
270
271
272
273
274
275

    if (is_using_probation() && $post->parent) {
        $parentposter = get_field('interaction_forum_post', 'poster', 'id', $post->parent);
        vouch_for_probationary_user($parentposter);
    }

276
    redirect(get_config('wwwroot') . 'interaction/forum/topic.php?id=' . $values['topic'] . '&post=' . $postid);
Clare Lenihan's avatar
Clare Lenihan committed
277
278
}

Richard Mansfield's avatar
Richard Mansfield committed
279
$smarty = smarty();
280
281
$smarty->assign('deleteduser', $poster->get('deleted'));
$smarty->assign('poster', $poster);
Clare Lenihan's avatar
Clare Lenihan committed
282
$smarty->assign('editform', $editform);
283
$smarty->assign('moderator', $moderator);
284
$smarty->assign('parent', $parent);
285
$smarty->assign('action', $action);
286
287
$smarty->assign('groupadmins', group_get_admin_ids($parent->group));

288
289
290
if (isset($inlinejs)) {
    $smarty->assign('INLINEJAVASCRIPT', $inlinejs);
}
291
292
293
294

if (isset($timeleft)) {
    $smarty->assign('timeleft', $timeleft);
}
Clare Lenihan's avatar
Clare Lenihan committed
295
$smarty->display('interaction:forum:editpost.tpl');