deletetopic.php 4.98 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
define('MENUITEM', 'groups/forums');
14
15
16
17
define('SECTION_PLUGINTYPE', 'interaction');
define('SECTION_PLUGINNAME', 'forum');
define('SECTION_PAGE', 'deletetopic');

Clare Lenihan's avatar
Clare Lenihan committed
18
19
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
safe_require('interaction' ,'forum');
20
require_once('group.php');
21
22
require_once('pieforms/pieform.php');
require_once(get_config('docroot') . 'interaction/lib.php');
23
require_once('embeddedimage.php');
24

Clare Lenihan's avatar
Clare Lenihan committed
25
$topicid = param_integer('id');
26
$returnto = param_alpha('returnto', 'topic');
Clare Lenihan's avatar
Clare Lenihan committed
27

28
$topic = get_record_sql(
29
    'SELECT f.group, f.id AS forumid, f.title, g.name AS groupname, p.poster, p.subject, p.body, COUNT(p2.id), ' . db_format_tsfield('p.ctime', 'ctime') . ', t.closed, m.user AS moderator
Clare Lenihan's avatar
Clare Lenihan committed
30
    FROM {interaction_forum_topic} t
31
    INNER JOIN {interaction_instance} f ON (f.id = t.forum AND f.deleted != 1)
32
    INNER JOIN {group} g ON (g.id = f.group AND g.deleted = ?)
33
    INNER JOIN {interaction_forum_post} p ON (p.topic = t.id AND p.parent IS NULL)
34
35
36
37
38
    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 = t.forum AND m.user = p.poster)
39
40
41
    INNER JOIN {interaction_forum_post} p2 ON (p.poster = p2.poster AND p2.deleted != 1)
    INNER JOIN {interaction_forum_topic} t2 ON (t2.deleted != 1 AND p2.topic = t2.id)
    INNER JOIN {interaction_instance} f2 ON (t2.forum = f2.id AND f2.deleted != 1 AND f2.group = f.group)
42
    WHERE t.id = ?
Clare Lenihan's avatar
Clare Lenihan committed
43
    AND t.deleted != 1
44
    GROUP BY 1, 2, 3, 4, 5, 6, 7, 9, 10, 11',
45
    array(0, $topicid)
Clare Lenihan's avatar
Clare Lenihan committed
46
47
);

48
if (!$topic) {
49
    throw new NotFoundException(get_string('cantfindtopic', 'interaction.forum', $topicid));
Clare Lenihan's avatar
Clare Lenihan committed
50
51
}

52
53
define('GROUP', $topic->group);

54
$membership = user_can_access_forum((int)$topic->forumid);
Clare Lenihan's avatar
Clare Lenihan committed
55

56
$moderator = (bool)($membership & INTERACTION_FORUM_MOD);
Clare Lenihan's avatar
Clare Lenihan committed
57

58
$topic->ctime = relative_date(get_string('strftimerecentfullrelative', 'interaction.forum'), get_string('strftimerecentfull'), $topic->ctime);
59

60
if (!$moderator || ($topic->group && !group_within_edit_window($topic->group))) {
61
    throw new AccessDeniedException(get_string('cantdeletetopic', 'interaction.forum'));
Clare Lenihan's avatar
Clare Lenihan committed
62
63
}

64
define('TITLE', $topic->title . ' - ' . get_string('deletetopicspecific', 'interaction.forum', $topic->subject));
Clare Lenihan's avatar
Clare Lenihan committed
65
66

$form = pieform(array(
67
    'name'     => 'deletetopic',
68
    'renderer' => 'div',
Clare Lenihan's avatar
Clare Lenihan committed
69
    'autofocus' => false,
Clare Lenihan's avatar
Clare Lenihan committed
70
71
    'elements' => array(
        'title' => array(
Pat Kira's avatar
Pat Kira committed
72
            'type' => 'html',
Clare Lenihan's avatar
Clare Lenihan committed
73
74
75
76
            'value' => get_string('deletetopicsure', 'interaction.forum'),
        ),
        'submit' => array(
            'type'  => 'submitcancel',
77
            'class' => 'btn-default',
Clare Lenihan's avatar
Clare Lenihan committed
78
            'value' => array(get_string('yes'), get_string('no')),
79
            'goto'  => get_config('wwwroot') . ($returnto == 'view' ? 'interaction/forum/view.php?id=' . $topic->forumid : 'interaction/forum/topic.php?id=' . $topicid),
80
81
82
83
        ),
        'forum' => array(
            'type' => 'hidden',
            'value' => $topic->forumid
Clare Lenihan's avatar
Clare Lenihan committed
84
85
86
87
        )
    )
));

88
function deletetopic_submit(Pieform $form, $values) {
89
    global $SESSION, $USER, $topicid;
90
91
92
93
94
95
96
    $objectionable = get_record_sql("SELECT fp.id
            FROM {interaction_forum_post} fp
            JOIN {objectionable} o
            ON (o.objecttype = 'forum' AND o.objectid = fp.id)
            WHERE fp.topic = ?
            AND fp.parent IS NULL
            AND o.resolvedby IS NULL
97
            AND o.resolvedtime IS NULL", array($topicid));
98

99
    if ($objectionable !== false) {
100
101
        // Trigger activity.
        $data = new StdClass;
102
        $data->postid     = $objectionable->id;
103
104
105
106
107
108
        $data->message    = '';
        $data->reporter   = $USER->get('id');
        $data->ctime      = time();
        $data->event      = DELETE_OBJECTIONABLE_TOPIC;
        activity_occurred('reportpost', $data, 'interaction', 'forum');
    }
109
    // mark topic as deleted
Clare Lenihan's avatar
Clare Lenihan committed
110
111
112
113
114
    update_record(
        'interaction_forum_topic',
        array('deleted' => 1),
        array('id' => $topicid)
    );
115
116
    // Delete embedded images in the topic and its posts
    require_once('embeddedimage.php');
117
    EmbeddedImage::delete_embedded_images('topic', $topicid);
118
119
120
121
122
123
    // mark relevant posts as deleted
    update_record(
        'interaction_forum_post',
        array('deleted' => 1),
        array('topic' => $topicid)
    );
Clare Lenihan's avatar
Clare Lenihan committed
124
    $SESSION->add_ok_msg(get_string('deletetopicsuccess', 'interaction.forum'));
125
    redirect('/interaction/forum/view.php?id=' . $values['forum']);
Clare Lenihan's avatar
Clare Lenihan committed
126
127
}

Richard Mansfield's avatar
Richard Mansfield committed
128
$smarty = smarty();
129
$smarty->assign('forum', $topic->title);
130
$smarty->assign('subheading', TITLE);
Clare Lenihan's avatar
Clare Lenihan committed
131
$smarty->assign('topic', $topic);
132
$smarty->assign('groupadmins', group_get_admin_ids($topic->group));
Clare Lenihan's avatar
Clare Lenihan committed
133
134
$smarty->assign('deleteform', $form);
$smarty->display('interaction:forum:deletetopic.tpl');