lib.php 11.8 KB
Newer Older
1
2
3
4
5
<?php
/**
 *
 * @package    mahara
 * @subpackage blocktype-blog
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.
9
10
11
12
13
 *
 */

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

14
class PluginBlocktypeBlog extends MaharaCoreBlocktype {
15
16
17
18
19
20

    public static function get_title() {
        return get_string('title', 'blocktype.blog/blog');
    }

    /**
Aaron Wells's avatar
Aaron Wells committed
21
     * Optional method. If exists, allows this class to decide the title for
22
23
24
25
26
27
     * all blockinstances of this type
     */
    public static function get_instance_title(BlockInstance $bi) {
        $configdata = $bi->get('configdata');

        if (!empty($configdata['artefactid'])) {
28
            return $bi->get_artefact_instance($configdata['artefactid'])->get('title');
29
30
31
32
33
34
35
36
37
        }
        return '';
    }

    public static function get_description() {
        return get_string('description', 'blocktype.blog/blog');
    }

    public static function get_categories() {
38
        return array('blog' => 10000);
39
    }
40
41
42
    public static function get_viewtypes() {
        return array('dashboard', 'portfolio', 'profile', 'grouphomepage');
    }
43

44
45
46
    public static function get_link(BlockInstance $instance) {
        $configdata = $instance->get('configdata');
        if (!empty($configdata['artefactid'])) {
47
            $data = get_config('wwwroot') . 'view/view.php?id=' . $instance->get('view') . '&modal=1&block=' . $instance->get('id') . '&artefact=' . $configdata['artefactid'];
48
49
            return sanitize_url($data);
        }
50
    }
51

52
    public static function render_instance(BlockInstance $instance, $editing=false, $versioning=false) {
53
        global $exporter;
54
55
56
57
        $configdata = $instance->get('configdata');

        $result = '';
        if (!empty($configdata['artefactid'])) {
58
            require_once(get_config('docroot') . 'artefact/lib.php');
59
            $blog = $instance->get_artefact_instance($configdata['artefactid']);
60
            $configdata['hidetitle'] = true;
61
            $configdata['countcomments'] = true;
62
            $configdata['versioning'] = $versioning;
63
            $configdata['viewid'] = $instance->get('view');
64
65
66
67
68
69
            if ($instance->get_view()->is_submitted()) {
                // Don't display posts added after the submitted date.
                if ($submittedtime = $instance->get_view()->get('submittedtime')) {
                    $configdata['before'] = $submittedtime;
                }
            }
70
71

            $limit = isset($configdata['count']) ? intval($configdata['count']) : 5;
72
            $limit = ($exporter || $versioning) ? 0 : $limit;
73
74
            $posts = ArtefactTypeBlogpost::get_posts($blog->get('id'), $limit, 0, $configdata);
            $template = 'artefact:blog:viewposts.tpl';
75
            if ($exporter || $versioning) {
76
77
78
                $pagination = false;
            }
            else {
79
80
                $baseurl = $instance->get_view()->get_url();
                $baseurl .= (strpos($baseurl, '?') === false ? '?' : '&') . 'block=' . $instance->get('id');
81
                $pagination = array(
82
                    'baseurl' => $baseurl,
83
84
85
86
87
                    'id' => 'blogpost_pagination_' . $instance->get('id'),
                    'datatable' => 'postlist_' . $instance->get('id'),
                    'jsonscript' => 'artefact/blog/posts.json.php',
                );
            }
88
            $configdata['blockid'] = $instance->get('id');
89
            $configdata['editing'] = $editing;
90
91
92
93
            ArtefactTypeBlogpost::render_posts($posts, $template, $configdata, $pagination);

            $smarty = smarty_core();
            if (isset($configdata['viewid'])) {
Lisa Seeto's avatar
Lisa Seeto committed
94
                $artefacturl = get_config('wwwroot') . 'view/view.php?id=' . $configdata['viewid'] . '&modal=1&artefact= ' . $blog->get('id');
95
96
97
98
99
                $smarty->assign('artefacttitle', '<a href="' . $artefacturl . '">' . hsc($blog->get('title')) . '</a>');
                if ($exporter && $posts['count'] > $limit) {
                    $posts['pagination'] = '<a href="' . $artefacturl . '">'
                        . get_string('allposts', 'artefact.blog') . '</a>';
                }
100
101
102
103
            }
            else {
                $smarty->assign('artefacttitle', hsc($blog->get('title')));
            }
104
105
106
            // Only show the 'New entry' link for blogs that you can add an entry to
            $canaddpost = false;
            $institution = $blog->get('institution');
107
108
            $group = $blog->get('group');
            if (ArtefactTypeBlog::can_edit_blog($blog, $institution, $group)) {
109
110
                $canaddpost = true;
            }
111

112
            $smarty->assign('alldraftposts', (isset($posts['alldraftposts']) ? $posts['alldraftposts'] : null));
113
114
115
116
117
            $smarty->assign('options', $configdata);
            $smarty->assign('description', $blog->get('description'));
            $smarty->assign('owner', $blog->get('owner'));
            $smarty->assign('tags', $blog->get('tags'));
            $smarty->assign('blockid', $instance->get('id'));
118
            $smarty->assign('editing', $editing);
119
            $smarty->assign('canaddpost', $canaddpost);
120
            $smarty->assign('blogid', $blog->get('id'));
121
            $smarty->assign('view', $instance->get('view'));
122
123
124
            $smarty->assign('posts', $posts);

            $result = $smarty->fetch('artefact:blog:blog.tpl');
125
126
127
128
129
        }

        return $result;
    }

130
    public static function has_instance_config(BlockInstance $instance) {
131
132
133
        return true;
    }

Son Nguyen's avatar
Son Nguyen committed
134
    public static function instance_config_form(BlockInstance $instance) {
135
        global $USER;
136
        safe_require('artefact', 'blog');
137
        $configdata = $instance->get('configdata');
138

139
140
141
        require_once(get_config('libroot') . 'view.php');
        $view = new View($instance->get('view'));
        $institution = $view->get('institution');
142
        $group = $view->get('group');
143

144
145
146
147
148
149
        if (!empty($configdata['artefactid'])) {
            $blog = $instance->get_artefact_instance($configdata['artefactid']);
        }

        $elements = array();

Aaron Wells's avatar
Aaron Wells committed
150
151
152
153
        // If the blog in this block is owned by the owner of the View, then
        // the block can be configured. Otherwise, the blog was copied in from
        // another View. We won't confuse users by asking them to choose a blog
        // to put in this block, when the one that is currently in it isn't
154
        // choosable.
155
        if (empty($configdata['artefactid'])
156
            || (ArtefactTypeBlog::can_edit_blog($blog, $institution, $group))) {
157
158
159
160
161
162
163
            $where = array('blog');
            $sql = "SELECT a.id FROM {artefact} a
                    WHERE a.artefacttype = ?";
            if ($institution) {
                $sql .= " AND a.institution = ?";
                $where[] = $institution;
            }
164
            else if ($group) {
165
                $sql .= " AND ( a.group = ? OR a.owner = ?)";
166
                $where[] = $group;
167
                $where[] = $USER->get('id');
168
            }
169
170
171
172
173
174
            else {
                $sql .= " AND a.owner = ?";
                $where[] = $USER->get('id');
            }
            $blogids = get_column_sql($sql, $where);
            $elements[] = self::artefactchooser_element((isset($configdata['artefactid'])) ? $configdata['artefactid'] : null, $blogids);
175
176
177
178
179
180
            $elements['count'] = array(
                'type' => 'text',
                'title' => get_string('postsperpage', 'blocktype.blog/blog'),
                'defaultvalue' => isset($configdata['count']) ? $configdata['count'] : 5,
                'size' => 3,
            );
181
            $elements[] = PluginArtefactBlog::block_advanced_options_element($configdata, 'blog');
182
183
184
185
186
        }
        else {
            $elements[] = array(
                'type' => 'html',
                'name' => 'notice',
187
                'value' => '<div class="metadata">' . get_string('blogcopiedfromanotherview', 'artefact.blog', get_string('Blog', 'artefact.blog')) . '</div>',
188
189
190
            );
        }
        return $elements;
191
192
    }

193
194
195
196
    /**
     * Returns a list of artefact IDs that are in this blockinstance.
     *
     * {@internal{Because links to artefacts within blogposts don't count
Aaron Wells's avatar
Aaron Wells committed
197
     * as making those artefacts 'children' of the blog post, we have to add
198
199
     * them directly to the blog.}}
     *
Aaron Wells's avatar
Aaron Wells committed
200
201
202
203
     * @return array List of artefact IDs that are 'in' this blog - all
     *               blogposts in it plus all links to other artefacts that are
     *               part of the blogpost text. Note that proper artefact
     *               children, such as blog post attachments, aren't included -
204
205
206
207
208
209
210
211
212
     *               the artefact parent cache is used for them
     * @see PluginBlocktypeBlogPost::get_artefacts()
     */
    public static function get_artefacts(BlockInstance $instance) {
        $configdata = $instance->get('configdata');
        $artefacts = array();
        if (isset($configdata['artefactid'])) {
            $artefacts[] = $configdata['artefactid'];

Aaron Wells's avatar
Aaron Wells committed
213
214
215
216
            // Artefacts that are linked to directly in blog post text aren't
            // strictly children of blog posts, which means that
            // artefact_in_view won't understand that they are "within the
            // blog". We have to help it here directly by working out what
217
218
219
220
            // artefacts are linked to in all of this blog's blog posts.
            $blog = $instance->get_artefact_instance($configdata['artefactid']);
            if ($blogposts = $blog->get_children_instances()) {
                foreach ($blogposts as $blogpost) {
221
222
223
224
                    if ($blogpost->get('published')) {
                        $artefacts[] = $blogpost->get('id');
                        $artefacts = array_merge($artefacts, $blogpost->get_referenced_artefacts_from_postbody());
                    }
225
226
227
228
229
230
231
                }
                $artefacts = array_unique($artefacts);
            }
        }
        return $artefacts;
    }

232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
    public static function get_current_artefacts(BlockInstance $instance) {

        $configdata = $instance->get('configdata');
        $artefacts = array();

        if (isset($configdata['artefactid'])) {
            $blog = $instance->get_artefact_instance($configdata['artefactid']);
            if ($blogposts = $blog->get_children_instances()) {
                foreach ($blogposts as $blogpost) {
                    if ($blogpost->get('published')) {
                        $artefacts[] = $blogpost->get('id');
                    }
                }
                $artefacts = array_unique($artefacts);
            }
        }
        return $artefacts;
    }

251
    public static function artefactchooser_element($default=null, $blogids=array()) {
252
        return array(
253
254
            'name'  => 'artefactid',
            'type'  => 'artefactchooser',
255
            'title' => get_string('Blog', 'artefact.blog'),
256
257
            'defaultvalue' => $default,
            'blocktype' => 'blog',
258
            'limit'     => 10,
259
260
261
            'selectone' => true,
            'artefacttypes' => array('blog'),
            'template'  => 'artefact:blog:artefactchooser-element.tpl',
262
            'extraselect' => !empty($blogids) ? array(array('fieldname' => 'id', 'type' => 'int', 'values' => $blogids)) : null,
263
264
265
        );
    }

266
    public static function default_copy_type(BlockInstance $instance, View $view) {
267
268
269
        return 'nocopy';
    }

270
    /**
271
     * Blog blocktype is only allowed in personal / institution / group views
272
273
     */
    public static function allowed_in_view(View $view) {
274
        return true;
275
276
    }

277
278
279
280
281
282
283
    public static function feed_url(BlockInstance $instance) {
        $configdata = $instance->get('configdata');
        if (!empty($configdata['artefactid']) && $instance->get_view()->is_public()) {
            return get_config('wwwroot') . 'artefact/blog/atom.php?artefact='
                . $configdata['artefactid'] . '&view=' . $instance->get('view');
        }
    }
284
}