lib.php 7.33 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
/**
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
 * Copyright (C) 2006-2011 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
 *
 * 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.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 * @package    mahara
21
 * @subpackage blocktype.blog/taggedposts
22
23
24
25
26
27
28
29
30
31
32
 * @author     Catalyst IT Ltd
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2011 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

defined('INTERNAL') || die();

class PluginBlocktypeTaggedposts extends SystemBlocktype {

    public static function get_title() {
33
        return get_string('title', 'blocktype.blog/taggedposts');
34
35
36
    }

    public static function get_description() {
37
        return get_string('description', 'blocktype.blog/taggedposts');
38
39
40
41
42
43
    }

    public static function get_categories() {
        return array('blog');
    }

44
45
46
47
48
49
50
51
52
53
    public static function get_instance_javascript(BlockInstance $bi) {
        $blockid = $bi->get('id');
        return array(
            array(
                'file'   => 'js/taggedposts.js',
                'initjs' => "addNewTaggedPostShortcut($blockid);",
            )
        );
    }

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
    public static function render_instance(BlockInstance $instance, $editing=false) {
        global $USER;

        $configdata = $instance->get('configdata');
        $view = $instance->get('view');
        $limit = isset($configdata['count']) ? (int) $configdata['count'] : 10;
        $results = '';

        $smarty = smarty_core();
        $smarty->assign('view', $view);

        // Display all posts, from all blogs, owned by this user
        if (!empty($configdata['tagselect'])) {
            $tagselect = $configdata['tagselect'];

            $sql =
                'SELECT a.title, p.title AS parenttitle, a.id, a.parent, a.owner, at.tag
                FROM {artefact} a
                JOIN {artefact} p ON a.parent = p.id
                JOIN {artefact_blog_blogpost} ab ON (ab.blogpost = a.id AND ab.published = 1)
                JOIN {artefact_tag} at ON (at.artefact = a.id)
                WHERE a.artefacttype = \'blogpost\'
                AND a.owner = (SELECT "owner" from {view} WHERE id = ?)
                AND at.tag = ?
78
                ORDER BY a.ctime DESC, a.id DESC
79
80
81
82
                LIMIT ?';

            $results = get_records_sql_array($sql, array($view, $tagselect, $limit));

83
84
85
86
87
88
89
90
91
92
93
            $smarty->assign('blockid', $instance->get('id'));
            $smarty->assign('editing', $editing);
            if ($editing) {
                // Get list of blogs owned by this user to create the "Add new post" shortcut while editing
                $viewowner = $instance->get_view()->get('owner');
                if (!$viewowner || !$blogs = get_records_select_array('artefact', 'artefacttype = \'blog\' AND owner = ?', array($viewowner), 'title ASC', 'id, title')) {
                    $blogs = array();
                }
                $smarty->assign('tagselect', $tagselect);
                $smarty->assign('blogs', $blogs);
            }
94

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
            // if posts are not found with the selected tag, notify the user
            if (!$results) {
                $smarty->assign('badtag', $tagselect);
                return $smarty->fetch('blocktype:taggedposts:taggedposts.tpl');
            }

            // update the view_artefact table so journal entries are accessible when this is the only block on the page
            // referencing this journal
            $dataobject = array(
                'view'      => $view,
                'block'     => $instance->get('id'),
            );

            foreach ($results as $result) {
                $dataobject["artefact"] = $result->parent;
                ensure_record_exists('view_artefact', $dataobject, $dataobject);
            }

            // check if the user viewing the page is the owner of the selected tag
            $owner = $results[0]->owner;
            if ($USER->id != $owner) {
116
                $viewowner = get_user_for_display($owner);
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
                $smarty->assign('viewowner', $viewowner);
            }

            $smarty->assign('tag', $tagselect);
        }
        else {
            // error if block configuration fails
            $smarty->assign('configerror', true);
            return $smarty->fetch('blocktype:taggedposts:taggedposts.tpl');
        }

        $smarty->assign('results', $results);
        return $smarty->fetch('blocktype:taggedposts:taggedposts.tpl');
    }

    public static function has_instance_config() {
        return true;
    }

    public static function instance_config_form($instance) {
137
        global $USER;
138
139
        $configdata = $instance->get('configdata');

140
141
142
143
144
145
146
147
148
149
        $tags = get_records_sql_array("
            SELECT at.tag
            FROM {artefact_tag} at
            JOIN {artefact} a
            ON a.id = at.artefact
            WHERE a.owner = ?
            AND a.artefacttype = 'blogpost'
            GROUP BY at.tag
            ORDER BY at.tag ASC
            ", array($USER->id));
150
151
152
153
154
155
156
157

        $options = array();
        if (!empty($tags)) {
            foreach ($tags as $tag) {
                $options[$tag->tag] = $tag->tag;
            }
            return array(
                'tagselect' => array(
158
159
                    'type'          => 'select',
                    'title'         => get_string('taglist','blocktype.blog/taggedposts'),
160
161
162
163
164
165
                    'options'       => $options,
                    'defaultvalue'  => !empty($configdata['tagselect']) ? $configdata['tagselect'] : $tags[0]->tag,
                    'required'      => true,
                ),
                'count'     => array(
                    'type'          => 'text',
166
                    'title'         => get_string('itemstoshow', 'blocktype.blog/taggedposts'),
167
                    'description'   => get_string('betweenxandy', 'mahara', 1, 100),
168
169
                    'defaultvalue'  => isset($configdata['count']) ? $configdata['count'] : 10,
                    'size'          => 3,
170
                    'rules'         => array('integer' => true, 'minvalue' => 1, 'maxvalue' => 100),
171
172
173
174
175
176
177
                ),
            );
        }
        else {
            return array(
                'notags'    => array(
                    'type'          => 'html',
178
179
                    'title'         => get_string('taglist', 'blocktype.blog/taggedposts'),
                    'value'         => get_string('notagsavailable', 'blocktype.blog/taggedposts'),
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
                ),
            );
        }

    }

    public static function default_copy_type() {
        return 'nocopy';
    }

    /**
     * Taggedposts blocktype is only allowed in personal views, because currently
     * there's no such thing as group/site blogs
     */
    public static function allowed_in_view(View $view) {
        return $view->get('owner') != null;
    }

}