edittopic.php 9.18 KB
Newer Older
Clare Lenihan's avatar
Clare Lenihan committed
1 2
<?php
/**
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4 5
 * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
Clare Lenihan's avatar
Clare Lenihan committed
6
 *
7 8 9 10
 * 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 3 of the License, or
 * (at your option) any later version.
Clare Lenihan's avatar
Clare Lenihan committed
11
 *
12 13 14 15
 * 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.
Clare Lenihan's avatar
Clare Lenihan committed
16
 *
17 18
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Clare Lenihan's avatar
Clare Lenihan committed
19 20 21
 *
 * @package    mahara
 * @subpackage interaction-forum
22
 * @author     Catalyst IT Ltd
Clare Lenihan's avatar
Clare Lenihan committed
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
24
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
Clare Lenihan's avatar
Clare Lenihan committed
25 26 27 28
 *
 */

define('INTERNAL', 1);
29
define('MENUITEM', 'groups/forums');
30 31 32 33
define('SECTION_PLUGINTYPE', 'interaction');
define('SECTION_PLUGINNAME', 'forum');
define('SECTION_PAGE', 'edittopic');

Clare Lenihan's avatar
Clare Lenihan committed
34 35 36
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
safe_require('interaction', 'forum');
require_once('group.php');
37 38
require_once(get_config('docroot') . 'interaction/lib.php');
require_once('pieforms/pieform.php');
Clare Lenihan's avatar
Clare Lenihan committed
39 40

$userid = $USER->get('id');
41
$topicid = param_integer('id', 0);
42 43
$returnto = param_alpha('returnto', 'topic');

44
if ($topicid == 0) { // new topic
Clare Lenihan's avatar
Clare Lenihan committed
45 46 47
    unset($topicid);
    $forumid = param_integer('forum');
}
48
else { // edit topic
49
    $topic = get_record_sql(
50
        'SELECT p.subject, p.id AS postid, p.body, p.poster, p.topic AS id, ' . db_format_tsfield('p.ctime', 'ctime') . ', t.sticky, t.closed, f.id AS forum
Clare Lenihan's avatar
Clare Lenihan committed
51
        FROM {interaction_forum_post} p
52 53
        INNER JOIN {interaction_forum_topic} t ON (p.topic = t.id AND t.deleted != 1)
        INNER JOIN {interaction_instance} f ON (f.id = t.forum AND f.deleted != 1)
54 55
        WHERE p.parent IS NULL
        AND p.topic = ?',
Clare Lenihan's avatar
Clare Lenihan committed
56 57
        array($topicid)
    );
58
    $forumid = $topic->forum;
59

60
    if (!$topic) {
61
        throw new NotFoundException(get_string('cantfindtopic', 'interaction.forum', $topicid));
Clare Lenihan's avatar
Clare Lenihan committed
62
    }
63
}
Clare Lenihan's avatar
Clare Lenihan committed
64

65
$forum = get_record_sql(
66
    'SELECT f.group AS groupid, f.title, g.name AS groupname, g.grouptype
67
    FROM {interaction_instance} f
68
    INNER JOIN {group} g ON (g.id = f.group AND g.deleted = 0)
69
    WHERE f.id = ?
70 71
    AND f.deleted != 1',
    array($forumid)
72
);
73

74 75 76
if (!$forum) {
    throw new NotFoundException(get_string('cantfindforum', 'interaction.forum', $forumid));
}
Clare Lenihan's avatar
Clare Lenihan committed
77

78 79
$forumconfig = get_records_assoc('interaction_forum_instance_config', 'forum', $forumid, '', 'field,value');

80
define('GROUP', $forum->groupid);
81 82 83
$membership = user_can_access_forum((int)$forumid);
$moderator = (bool)($membership & INTERACTION_FORUM_MOD);

84
if (!$membership || ($forumconfig['createtopicusers']->value == 'moderators' && !$moderator)) {
85 86 87 88 89 90 91 92 93
    throw new AccessDeniedException(get_string('cantaddtopic', 'interaction.forum'));
}

if (!isset($topicid)) { // new topic
    define('TITLE', $forum->title . ' - ' . get_string('addtopic','interaction.forum'));
}

else { // edit topic
    define('TITLE', $forum->title . ' - ' . get_string('edittopic','interaction.forum'));
Clare Lenihan's avatar
Clare Lenihan committed
94

95 96 97 98 99 100 101 102
    // no record for edits to own posts with 30 minutes
    if (user_can_edit_post($topic->poster, $topic->ctime)) {
        $topic->editrecord = false;
    }
    else if ($moderator) {
        $topic->editrecord = true;
    }
    else {
103
        throw new AccessDeniedException(get_string('cantedittopic', 'interaction.forum'));
Clare Lenihan's avatar
Clare Lenihan committed
104 105 106
    }
}

Clare Lenihan's avatar
Clare Lenihan committed
107
$editform = array(
Clare Lenihan's avatar
Clare Lenihan committed
108
    'name'     => isset($topic) ? 'edittopic' : 'addtopic',
Clare Lenihan's avatar
Clare Lenihan committed
109
    'method'   => 'post',
Clare Lenihan's avatar
Clare Lenihan committed
110
    'autofocus' => false,
Clare Lenihan's avatar
Clare Lenihan committed
111 112 113
    'elements' => array(
        'subject' => array(
            'type'         => 'text',
114
            'title'        => get_string('Subject', 'interaction.forum'),
115
            'defaultvalue' => isset($topic) ? $topic->subject : null,
Clare Lenihan's avatar
Clare Lenihan committed
116 117 118 119 120 121 122
            'rules'        => array(
                'required' => true,
                'maxlength' => 255
            )
        ),
        'body' => array(
            'type'         => 'wysiwyg',
123
            'title'        => get_string('Body', 'interaction.forum'),
124
            'rows'         => 18,
Clare Lenihan's avatar
Clare Lenihan committed
125
            'cols'         => 70,
126
            'defaultvalue' => isset($topic) ? $topic->body : null,
Clare Lenihan's avatar
Clare Lenihan committed
127 128 129 130
            'rules'        => array( 'required' => true )
        ),
        'sticky' => array(
            'type'         => 'checkbox',
131
            'title'        => get_string('Sticky', 'interaction.forum'),
Clare Lenihan's avatar
Clare Lenihan committed
132
            'description'  => get_string('stickydescription', 'interaction.forum'),
133
            'defaultvalue' => isset($topic) && $topic->sticky == 1 ? 'checked' : null
Clare Lenihan's avatar
Clare Lenihan committed
134
        ),
Clare Lenihan's avatar
Clare Lenihan committed
135 136
        'closed' => array(
            'type'         => 'checkbox',
137
            'title'        => get_string('Closed', 'interaction.forum'),
Clare Lenihan's avatar
Clare Lenihan committed
138
            'description'  => get_string('closeddescription', 'interaction.forum'),
139
            'defaultvalue' => isset($topic) ? $topic->closed : !empty($forumconfig['closetopics']->value),
Clare Lenihan's avatar
Clare Lenihan committed
140 141 142 143
        ),
        'submit'   => array(
            'type'  => 'submitcancel',
            'value'       => array(
144
                isset($topic) ? get_string('edit') : get_string('Post','interaction.forum'),
145
                get_string('cancel')
Clare Lenihan's avatar
Clare Lenihan committed
146
            ),
147
            'goto'      => get_config('wwwroot') . 'interaction/forum/' . (isset($topic) && $returnto != 'view'  ? 'topic.php?id='.$topicid : 'view.php?id='.$forumid)
Clare Lenihan's avatar
Clare Lenihan committed
148
        ),
149 150 151
        'post' => array(
            'type' => 'hidden',
            'value' => isset($topic) ? $topic->postid : false
152 153 154 155
        ),
        'editrecord' => array(
            'type' => 'hidden',
            'value' => isset($topic) ? $topic->editrecord : false
156
        )
Clare Lenihan's avatar
Clare Lenihan committed
157 158 159 160
    ),
);

if(!$moderator){
Clare Lenihan's avatar
Clare Lenihan committed
161 162
    unset($editform['elements']['sticky']);
    unset($editform['elements']['closed']);
Clare Lenihan's avatar
Clare Lenihan committed
163 164 165 166
}

$editform = pieform($editform);

Clare Lenihan's avatar
Clare Lenihan committed
167 168 169 170 171 172 173 174 175 176
function addtopic_submit(Pieform $form, $values) {
    global $USER, $SESSION;
    $forumid = param_integer('forum');
    db_begin();
    $topicid = insert_record(
        'interaction_forum_topic',
        (object)array(
            'forum' => $forumid,
            'sticky' => isset($values['sticky']) && $values['sticky'] ? 1 : 0,
            'closed' => isset($values['closed']) && $values['closed'] ? 1 : 0
177
        ), 'id', true
Clare Lenihan's avatar
Clare Lenihan committed
178
    );
179
    $postid = insert_record(
Clare Lenihan's avatar
Clare Lenihan committed
180 181 182 183 184 185 186
        'interaction_forum_post',
        (object)array(
            'topic' => $topicid,
            'poster' => $USER->get('id'),
            'subject' => $values['subject'],
            'body' => $values['body'],
            'ctime' =>  db_format_timestamp(time())
187 188
        ),
        'id', true
Clare Lenihan's avatar
Clare Lenihan committed
189
    );
190 191 192 193
    if (!record_exists('interaction_forum_subscription_forum', 'user', $USER->get('id'), 'forum', $forumid)) {
        insert_record('interaction_forum_subscription_topic', (object)array(
            'user'  => $USER->get('id'),
            'topic' => $topicid,
194
            'key'   => PluginInteractionForum::generate_unsubscribe_key(),
195 196
        ));
    }
Clare Lenihan's avatar
Clare Lenihan committed
197
    db_commit();
198 199 200 201
    $delay = get_config_plugin('interaction', 'forum', 'postdelay');
    if (!is_null($delay) && $delay == 0) {
        PluginInteractionForum::interaction_forum_new_post(array($postid));
    }
Clare Lenihan's avatar
Clare Lenihan committed
202 203 204 205
    $SESSION->add_ok_msg(get_string('addtopicsuccess', 'interaction.forum'));
    redirect('/interaction/forum/topic.php?id='.$topicid);
}

Clare Lenihan's avatar
Clare Lenihan committed
206
function edittopic_submit(Pieform $form, $values) {
207
    global $SESSION, $USER, $topic;
Clare Lenihan's avatar
Clare Lenihan committed
208
    $topicid = param_integer('id');
209
    $returnto = param_alpha('returnto', 'topic');
Clare Lenihan's avatar
Clare Lenihan committed
210
    db_begin();
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    // check the post content actually changed
    // otherwise topic could have been set as sticky/closed
    $postchanged = $values['subject'] != $topic->subject || $values['body'] != $topic->body;
    if ($postchanged) {
        update_record(
            'interaction_forum_post',
            array(
                'subject' => $values['subject'],
                'body' => $values['body']
            ),
            array('id' => $values['post'])
        );
    }
    if ($values['editrecord'] && $postchanged) {
        insert_record(
            'interaction_forum_edit',
            (object)array(
                'user' => $USER->get('id'),
                'post' => $values['post'],
                'ctime' => db_format_timestamp(time())
            )
        );
    }
Clare Lenihan's avatar
Clare Lenihan committed
234
    if(isset($values['sticky'])){
Clare Lenihan's avatar
Clare Lenihan committed
235
        update_record(
Clare Lenihan's avatar
Clare Lenihan committed
236
            'interaction_forum_topic',
Clare Lenihan's avatar
Clare Lenihan committed
237
            array(
Clare Lenihan's avatar
Clare Lenihan committed
238 239
                'sticky' => isset($values['sticky']) && $values['sticky'] == 1 ? 1 : 0,
                'closed' => isset($values['closed']) && $values['closed'] == 1 ? 1 : 0
Clare Lenihan's avatar
Clare Lenihan committed
240
            ),
Clare Lenihan's avatar
Clare Lenihan committed
241
            array('id' => $topicid)
Clare Lenihan's avatar
Clare Lenihan committed
242 243
        );
    }
Clare Lenihan's avatar
Clare Lenihan committed
244 245
    db_commit();
    $SESSION->add_ok_msg(get_string('edittopicsuccess', 'interaction.forum'));
246
    if ($returnto == 'view') {
247
        redirect('/interaction/forum/view.php?id=' . $topic->forum);
248 249 250 251
    }
    else {
        redirect('/interaction/forum/topic.php?id=' . $topicid);
    }
Clare Lenihan's avatar
Clare Lenihan committed
252 253
}

Richard Mansfield's avatar
Richard Mansfield committed
254
$smarty = smarty();
255 256
$smarty->assign('heading', $forum->groupname);
$smarty->assign('subheading', TITLE);
257
$smarty->assign('editform', $editform);
Clare Lenihan's avatar
Clare Lenihan committed
258 259 260
$smarty->display('interaction:forum:edittopic.tpl');

?>