atom.php 6.15 KB
Newer Older
Evan Goldenberg's avatar
Evan Goldenberg committed
1
2
3
4
5
6
<?php
/**
 *
 * @package    mahara
 * @subpackage interaction-forum
 * @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.
Evan Goldenberg's avatar
Evan Goldenberg committed
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
 *
 */

define('INTERNAL', 1);
define('PUBLIC', 1);

require(dirname(dirname(dirname(__FILE__))) . '/init.php');

function atom_date($date) {
    $date = str_replace(' ', 'T', $date);
    $date .= date('P');
    return $date;
}

function generate_feed($feed, $posts) {
    $smarty = smarty();
    $smarty->assign('feed', $feed);
    $smarty->assign('posts', $posts);

    header("Content-Type: application/atom+xml");
    $smarty->display('interaction:forum:atom.xml.tpl');
}

function error_feed() {
    return array(
        'title' => get_string('accessdenied', 'error'),
        'link' => '',
        'selflink' => '',
        'id' => '',
        'description' => '',
        'ownername' => '',
        'updated' => '',
        'logo' => '',
    );
}

function error_post($message) {
    return array(
        0 => array(
            'title' => get_string('accessdenied', 'error'),
            'link' => '',
            'id' => '',
            'description' => $message,
            'mtime' => '',
        ));
}

$feedtype = param_alpha('type'); //g = group, f = forum, t = topic
$id = param_integer('id');

if ($feedtype == 'g') {
60
    if (!$group = get_record('group', 'id', $id, 'deleted', 0)) {
Evan Goldenberg's avatar
Evan Goldenberg committed
61
62
63
64
65
66
67
        generate_feed(error_feed(), error_post(get_string('groupnotfound', 'group', $id)));
        exit();
    }

    $sql = "
        SELECT u.firstname, u.lastname, p.id, p.parent, p.topic, p.subject, p.body, p.ctime
        FROM {interaction_forum_post} p
Evan Goldenberg's avatar
Evan Goldenberg committed
68
69
        INNER JOIN {interaction_forum_topic} t ON p.topic = t.id
        INNER JOIN {interaction_instance} f ON t.forum = f.id
Evan Goldenberg's avatar
Evan Goldenberg committed
70
        INNER JOIN {usr} u ON p.poster = u.id
Evan Goldenberg's avatar
Evan Goldenberg committed
71
72
        WHERE f.group = ?
        AND p.deleted = 0";
Evan Goldenberg's avatar
Evan Goldenberg committed
73
74
75
76
77
78

    $link = get_config('wwwroot') . 'interaction/forum/index.php?group=' . $id;
    $title = implode(' - ', array(get_field('group', 'name', 'id', $id),
        get_string('allposts', 'interaction.forum')));
}
elseif ($feedtype == 'f') {
79
80
81
82
83
84
85
    $group = get_record_sql('
        SELECT g.*
        FROM {interaction_instance} i JOIN {group} g ON i.group = g.id
        WHERE i.id = ? AND i.deleted = 0 AND g.deleted = 0',
        array($id)
    );
    if (!$group) {
Evan Goldenberg's avatar
Evan Goldenberg committed
86
87
88
89
90
91
92
93
94
95
        generate_feed(error_feed(), error_post(get_string('cantfindforum', 'interaction.forum', $id)));
        exit();
    }

    $sql = "
        SELECT u.firstname, u.lastname, p.id, p.parent, p.topic, p.subject, p.body, p.ctime
        FROM {interaction_forum_post} p
        INNER JOIN {interaction_forum_topic} t ON p.topic = t.id
        INNER JOIN {usr} u ON p.poster = u.id
        WHERE t.forum = ?
96
97
        AND p.deleted = 0
        AND t.deleted = 0";
Evan Goldenberg's avatar
Evan Goldenberg committed
98
99

    $link = get_config('wwwroot') . 'interaction/forum/view.php?id=' . $id;
100
    $title = implode(' - ', array(get_field('group', 'name', 'id', $group->id),
Evan Goldenberg's avatar
Evan Goldenberg committed
101
102
103
104
        get_field('interaction_instance', 'title', 'id', $id),
        get_string('allposts', 'interaction.forum')));
}
elseif ($feedtype == 't') {
105
106
107
108
109
110
111
112
113
    $group = get_record_sql('
        SELECT g.*, f.id AS forumid
        FROM {interaction_forum_topic} t
            INNER JOIN {interaction_instance} f ON t.forum = f.id
            INNER JOIN {group} g ON f.group = g.id
        WHERE t.id = ? AND t.deleted = 0 AND g.deleted = 0',
        array($id)
    );
    if (!$group) {
Evan Goldenberg's avatar
Evan Goldenberg committed
114
115
116
117
        generate_feed(error_feed(), error_post(get_string('cantfindtopic', 'interaction.forum', $id)));
        exit();
    }

118
    $forumid = $group->forumid;
Evan Goldenberg's avatar
Evan Goldenberg committed
119
120
121

    $sql = "
        SELECT u.firstname, u.lastname, p.id, p.parent, p.topic, p.subject, p.body, p.ctime
Evan Goldenberg's avatar
Evan Goldenberg committed
122
123
        FROM {interaction_forum_post} p
        INNER JOIN {usr} u ON p.poster = u.id
Evan Goldenberg's avatar
Evan Goldenberg committed
124
        WHERE p.deleted = 0
Evan Goldenberg's avatar
Evan Goldenberg committed
125
        AND p.topic = ?";
Evan Goldenberg's avatar
Evan Goldenberg committed
126
127

    $link = get_config('wwwroot') . 'interaction/forum/topic.php?id=' . $id;
128
    $title = implode(' - ', array(get_field('group', 'name', 'id', $group->id),
Evan Goldenberg's avatar
Evan Goldenberg committed
129
130
131
132
133
134
135
136
        get_field('interaction_instance', 'title', 'id', $forumid),
        get_field_sql("
            SELECT p.subject
            FROM {interaction_forum_post} p
            WHERE p.topic = ?
            AND p.parent IS NULL", array($id)),
        get_string('allposts', 'interaction.forum')));
}
137
if (!$group->public) {
Evan Goldenberg's avatar
Evan Goldenberg committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
    generate_feed(error_feed(), error_post(get_string('notpublic', 'group')));
    exit();
}

$sql .= "
    ORDER BY p.ctime DESC
    LIMIT ?;";

$selflink = get_config('wwwroot') . 'interaction/forum/atom.php?type=' . $feedtype . '&id=' . $id;

$postcount = 20;
$postrecords = get_records_sql_array($sql, array($id, $postcount));

$image = get_config('wwwroot') . 'theme/raw/static/images/site-logo.png';
152
$updated = ($postrecords) ? atom_date($postrecords[0]->ctime) : '';
Evan Goldenberg's avatar
Evan Goldenberg committed
153
154

$generator = array(
155
156
    'uri' => 'https://mahara.org',
    'version' => get_config('series'),
Evan Goldenberg's avatar
Evan Goldenberg committed
157
158
159
160
161
162
163
164
    'text' => 'Mahara',
);

$feed = array(
    'title' => $title,
    'link' => $link,
    'selflink' => $selflink,
    'id' => implode(',', array(get_config('wwwroot'), $feedtype, $id)),
165
    'updated' => $updated,
Evan Goldenberg's avatar
Evan Goldenberg committed
166
167
168
169
170
171
    'logo' => $image,
    'icon' => get_config('wwwroot') . 'favicon.ico',
    'generator' => $generator,
);

$posts = array();
Evan Goldenberg's avatar
Evan Goldenberg committed
172
173
if ($postrecords) {
    foreach ($postrecords as &$post) {
Evan Goldenberg's avatar
Evan Goldenberg committed
174
175
176
177
178
179
180
181
182
183
184
        $parent = $post->parent;
        while(!$post->subject) {
            $post->subject = get_field('interaction_forum_post', 'subject', 'id', $parent);
            $parent = get_field('interaction_forum_post', 'parent', 'id', $parent);
        }
        if ($parent != $post->parent) {
            $post->subject = get_string('re', 'interaction.forum', $post->subject);
        }
        $posts[] = array(
            'title' => $post->subject,
            'link' => get_config('wwwroot') . 'interaction/forum/topic.php?id=' .
185
                $post->topic . '&post=' . $post->id,
Evan Goldenberg's avatar
Evan Goldenberg committed
186
187
188
189
190
191
192
193
194
            'id' => implode(',', array(get_config('wwwroot'), $post->topic, $post->id)),
            'description' => $post->body,
            'mtime' => atom_date($post->ctime),
            'author' => implode(' ', array($post->firstname, $post->lastname)),
        );
    }
}

generate_feed($feed, $posts);