editpost.php 9.03 KB
Newer Older
Clare Lenihan's avatar
Clare Lenihan committed
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
<?php
/**
 * This program is part of Mahara
 *
 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 * @package    mahara
 * @subpackage interaction-forum
 * @author     Clare Lenihan <clare@catalyst.net.nz>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

define('INTERNAL', 1);
define('MENUITEM', 'groups');
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
safe_require('interaction', 'forum');
require('group.php');


$userid = $USER->get('id');

$postid = param_integer('id',0);
$topicid = 0;
if ($postid==0) {
    unset($postid);
Clare Lenihan's avatar
Clare Lenihan committed
40
    define('TITLE', get_string('postreply','interaction.forum'));
Clare Lenihan's avatar
Clare Lenihan committed
41
    $parentid = param_integer('parent');
Clare Lenihan's avatar
Clare Lenihan committed
42
    $topic = get_record_sql(
43
        'SELECT p.topic AS id, p2.subject, t.closed, f.id AS forum, f.title AS forumtitle, f.group
Clare Lenihan's avatar
Clare Lenihan committed
44
45
        FROM {interaction_forum_post} p
        INNER JOIN {interaction_forum_topic} t
Clare Lenihan's avatar
Clare Lenihan committed
46
        ON p.topic = t.id
47
        AND t.deleted != 1
Clare Lenihan's avatar
Clare Lenihan committed
48
49
50
        INNER JOIN {interaction_forum_post} p2
        ON p2.topic = t.id
        AND p2.parent IS NULL
Clare Lenihan's avatar
Clare Lenihan committed
51
        INNER JOIN {interaction_instance} f
Clare Lenihan's avatar
Clare Lenihan committed
52
        ON t.forum = f.id
53
54
55
        AND f.deleted != 1
        WHERE p.id = ?
        AND p.deleted != 1',
Clare Lenihan's avatar
Clare Lenihan committed
56
57
        array($parentid)
    );
Clare Lenihan's avatar
Clare Lenihan committed
58

59
    if (!$topic) {
60
        throw new NotFoundException(get_string('cantfindpost', 'interaction.forum', $parentid));
61
62
    }

Clare Lenihan's avatar
Clare Lenihan committed
63
64
65
66
67
68
69
    $membership = user_can_access_group((int)$topic->group);

    $admin = (bool)($membership & GROUP_MEMBERSHIP_OWNER);

    $moderator = $admin || is_forum_moderator((int)$topic->forum);

    if (!$membership || (!$moderator && $topic->closed)) {
70
        throw new AccessDeniedException(get_string('cantaddpost', 'interaction.forum'));
Clare Lenihan's avatar
Clare Lenihan committed
71
72
    }

Clare Lenihan's avatar
Clare Lenihan committed
73
    $topicid = $topic->id;
Clare Lenihan's avatar
Clare Lenihan committed
74
    $topicsubject = $topic->subject;
75
76
77
78
79
80
81

    $breadcrumbs = array(
        array(
            get_config('wwwroot') . 'interaction/forum/index.php?group=' . $topic->group,
            get_string('nameplural', 'interaction.forum')
        ),
        array(
82
83
84
85
86
87
88
89
90
91
92
93
            array(
                get_config('wwwroot') . 'interaction/forum/view.php?id=' . $topic->forum,
                $topic->forumtitle
            ),
            array(
                get_config('wwwroot') . 'interaction/forum/topic.php?id=' . $topicid,
                $topic->subject
            ),
            array(
                get_config('wwwroot') . 'interaction/forum/editpost.php?parent=' . $parentid,
                get_string('postreply', 'interaction.forum')
            )
94
95
        )
    );
Clare Lenihan's avatar
Clare Lenihan committed
96
97
98
}

if (isset($postid)) {
Clare Lenihan's avatar
Clare Lenihan committed
99
    define('TITLE', get_string('editpost','interaction.forum'));
Clare Lenihan's avatar
Clare Lenihan committed
100
    $post = get_record_sql(
101
        'SELECT p.subject, p.body, p.parent, p.topic, p.poster, p.ctime, t.forum, p2.subject AS topicsubject, f.title AS forumtitle, f.group
Clare Lenihan's avatar
Clare Lenihan committed
102
103
        FROM {interaction_forum_post} p
        INNER JOIN {interaction_forum_topic} t
Clare Lenihan's avatar
Clare Lenihan committed
104
        ON p.topic = t.id
105
        AND t.deleted != 1
Clare Lenihan's avatar
Clare Lenihan committed
106
107
108
        INNER JOIN {interaction_forum_post} p2
        ON p2.topic = t.id
        AND p2.parent IS NULL
Clare Lenihan's avatar
Clare Lenihan committed
109
        INNER JOIN {interaction_instance} f
Clare Lenihan's avatar
Clare Lenihan committed
110
        ON t.forum = f.id
111
112
113
        AND f.deleted != 1
        WHERE p.id = ?
        AND p.deleted != 1',
Clare Lenihan's avatar
Clare Lenihan committed
114
115
        array($postid)
    );
Clare Lenihan's avatar
Clare Lenihan committed
116
117

    if (!$post) {
118
        throw new NotFoundException(get_string('cantfindpost', 'interaction.forum', $postid));
Clare Lenihan's avatar
Clare Lenihan committed
119
    }
120

121
    $topicid = $post->topic;
Clare Lenihan's avatar
Clare Lenihan committed
122
    $topicsubject = $post->topicsubject;
Clare Lenihan's avatar
Clare Lenihan committed
123

124
    $membership = user_can_access_group((int)$post->group);
Clare Lenihan's avatar
Clare Lenihan committed
125
126
127

    $admin = (bool)($membership & GROUP_MEMBERSHIP_OWNER);

128
    $moderator = $admin || is_forum_moderator((int)$post->forum);
Clare Lenihan's avatar
Clare Lenihan committed
129

Clare Lenihan's avatar
Clare Lenihan committed
130
    if (!$moderator &&
131
132
        ($post->poster != $userid
        || (time() - strtotime($post->ctime)) > (30 * 60))) {
133
        throw new AccessDeniedException(get_string('canteditpost', 'interaction.forum'));
Clare Lenihan's avatar
Clare Lenihan committed
134
    }
135
136
137
138
139
140
141

    $breadcrumbs = array(
        array(
            get_config('wwwroot') . 'interaction/forum/index.php?group=' . $post->group,
            get_string('nameplural', 'interaction.forum')
        ),
        array(
142
143
144
145
146
147
148
149
150
151
152
153
            array(
                get_config('wwwroot') . 'interaction/forum/view.php?id=' . $post->forum,
                $post->forumtitle
            ),
            array(
                get_config('wwwroot') . 'interaction/forum/topic.php?id=' . $topicid,
                $topicsubject
            ),
            array(
                get_config('wwwroot') . 'interaction/forum/editpost.php?id=' . $postid,
                get_string('editpost', 'interaction.forum')
            )
154
155
        )
    );
Clare Lenihan's avatar
Clare Lenihan committed
156
157
}

158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Javascript to hide the subject box if it has nothing in it, with a link you 
// click to expand it.
$clicksetsubject = json_encode(get_string('clicksetsubject', 'interaction.forum'));
$inlinejs = <<<EOF
addLoadEvent(function() {
    var subjectInput = $('editpost_subject');
    if (subjectInput.value == '') {
        hideElement(subjectInput);
        var expandLink = A({'href': ''}, {$clicksetsubject});
        connect(expandLink, 'onclick', function(e) {
            showElement(subjectInput);
            subjectInput.focus();
            e.stop();
            removeElement(expandLink);
        });
        insertSiblingNodesBefore(subjectInput, expandLink);
    }
    tinyMCE.execCommand('mceFocus',false,'mce_editor_0');
});
EOF;

Clare Lenihan's avatar
Clare Lenihan committed
179
180
181
182
183
184
185
186
187
require_once('pieforms/pieform.php');

$editform = pieform(array(
    'name'     => 'editpost',
    'method'   => 'post',
    'elements' => array(
        'subject' => array(
            'type'         => 'text',
            'title'        => get_string('subject', 'interaction.forum'),
188
            'defaultvalue' => isset($post) ? $post->subject : null,
Clare Lenihan's avatar
Clare Lenihan committed
189
            'rules'        => array(
190
191
                'maxlength' => 255,
                'required'  => isset($post) && !$post->parent ? true : false
Clare Lenihan's avatar
Clare Lenihan committed
192
193
194
195
196
197
198
            )
        ),
        'body' => array(
            'type'         => 'wysiwyg',
            'title'        => get_string('body', 'interaction.forum'),
            'rows'         => 10,
            'cols'         => 70,
199
            'defaultvalue' => isset($post) ? $post->body : null,
Clare Lenihan's avatar
Clare Lenihan committed
200
201
202
203
204
            'rules'        => array( 'required' => true )
        ),
        'submit'   => array(
            'type'  => 'submitcancel',
            'value'       => array(
205
206
                isset($post) ? get_string('edit') : get_string('post','interaction.forum'),
                get_string('cancel')
Clare Lenihan's avatar
Clare Lenihan committed
207
208
209
210
211
212
213
214
215
216
217
218
            ),
        'goto'      => get_config('wwwroot') . 'interaction/forum/topic.php?id='.$topicid
        ),
    ),
));

function editpost_submit(Pieform $form, $values) {
    global $USER;
    $postid = param_integer('id',0);
    if ($postid==0) {
        $parentid = param_integer('parent');
        $topic = get_record_sql(
Clare Lenihan's avatar
Clare Lenihan committed
219
            'SELECT topic AS id
Clare Lenihan's avatar
Clare Lenihan committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
            FROM {interaction_forum_post}
            WHERE id = ?',
            array($parentid)
        );
        insert_record(
            'interaction_forum_post',
            (object)array(
                'topic' => $topic->id,
                'poster' => $USER->get('id'),
                'parent' => $parentid,
                'subject' => $values['subject'],
                'body' => $values['body'],
                'ctime' =>  db_format_timestamp(time())
            ),
            'id'
        );
    }
    else {
        $topic = get_record_sql(
Clare Lenihan's avatar
Clare Lenihan committed
239
            'SELECT topic AS id, poster, ctime AS posttime
Clare Lenihan's avatar
Clare Lenihan committed
240
241
242
243
244
245
246
247
248
249
250
251
252
            FROM {interaction_forum_post}
            WHERE id = ?',
            array($postid)
        );
        update_record(
            'interaction_forum_post',
            array(
                'subject' => $values['subject'],
                'body' => $values['body']
            ),
            array('id' => $postid)
        );
        if ($topic->poster != $USER->get('id') ||
253
           (time() - strtotime($topic->posttime)) > (30 * 60)) {
Clare Lenihan's avatar
Clare Lenihan committed
254
255
256
257
258
259
260
261
            insert_record(
                'interaction_forum_edit',
                (object)array(
                    'user' => $USER->get('id'),
                    'post' => $postid,
                    'ctime' => db_format_timestamp(time())
                )
            );
Clare Lenihan's avatar
Clare Lenihan committed
262
263
264
265
266
267
        }
    }
    redirect('/interaction/forum/topic.php?id='.$topic->id);
}

$smarty = smarty();
268
$smarty->assign('breadcrumbs', $breadcrumbs);
Clare Lenihan's avatar
Clare Lenihan committed
269
270
271
$smarty->assign('topicsubject', $topicsubject);
$smarty->assign('heading', TITLE);
$smarty->assign('topic', $topicsubject);
Clare Lenihan's avatar
Clare Lenihan committed
272
$smarty->assign('editform', $editform);
273
274
275
if (isset($inlinejs)) {
    $smarty->assign('INLINEJAVASCRIPT', $inlinejs);
}
Clare Lenihan's avatar
Clare Lenihan committed
276
277
278
$smarty->display('interaction:forum:editpost.tpl');

?>