lib.php 11.6 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
43
44
45
46
    public static function get_link(BlockInstance $instance) {
        $configdata = $instance->get('configdata');
        if (!empty($configdata['artefactid'])) {
            $data = get_config('wwwroot') . 'artefact/artefact.php?artefact=' . $configdata['artefactid'] . '&view=' . $instance->get('view');
            return sanitize_url($data);
        }
47
    }
48

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

        $result = '';
        if (!empty($configdata['artefactid'])) {
55
            require_once(get_config('docroot') . 'artefact/lib.php');
56
            $blog = $instance->get_artefact_instance($configdata['artefactid']);
57
            $configdata['hidetitle'] = true;
58
            $configdata['countcomments'] = true;
59
            $configdata['versioning'] = $versioning;
60
            $configdata['viewid'] = $instance->get('view');
61
62
63
64
65
66
            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;
                }
            }
67
68

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

            $smarty = smarty_core();
            if (isset($configdata['viewid'])) {
91
                $artefacturl = get_config('wwwroot') . 'artefact/artefact.php?artefact=' . $blog->get('id') . '&view='
92
93
94
95
96
97
                    . $configdata['viewid'];
                $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>';
                }
98
99
100
101
            }
            else {
                $smarty->assign('artefacttitle', hsc($blog->get('title')));
            }
102
103
104
            // Only show the 'New entry' link for blogs that you can add an entry to
            $canaddpost = false;
            $institution = $blog->get('institution');
105
106
            $group = $blog->get('group');
            if (ArtefactTypeBlog::can_edit_blog($blog, $institution, $group)) {
107
108
                $canaddpost = true;
            }
109

110
            $smarty->assign('alldraftposts', (isset($posts['alldraftposts']) ? $posts['alldraftposts'] : null));
111
112
113
114
115
            $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'));
116
            $smarty->assign('editing', $editing);
117
            $smarty->assign('canaddpost', $canaddpost);
118
            $smarty->assign('blogid', $blog->get('id'));
119
            $smarty->assign('view', $instance->get('view'));
120
121
122
            $smarty->assign('posts', $posts);

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

        return $result;
    }

    public static function has_instance_config() {
        return true;
    }

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

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

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

        $elements = array();

Aaron Wells's avatar
Aaron Wells committed
148
149
150
151
        // 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
152
        // choosable.
153
        if (empty($configdata['artefactid'])
154
            || (ArtefactTypeBlog::can_edit_blog($blog, $institution, $group))) {
155
156
157
158
159
160
161
            $where = array('blog');
            $sql = "SELECT a.id FROM {artefact} a
                    WHERE a.artefacttype = ?";
            if ($institution) {
                $sql .= " AND a.institution = ?";
                $where[] = $institution;
            }
162
            else if ($group) {
163
                $sql .= " AND ( a.group = ? OR a.owner = ?)";
164
                $where[] = $group;
165
                $where[] = $USER->get('id');
166
            }
167
168
169
170
171
172
            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);
173
174
175
176
177
178
            $elements['count'] = array(
                'type' => 'text',
                'title' => get_string('postsperpage', 'blocktype.blog/blog'),
                'defaultvalue' => isset($configdata['count']) ? $configdata['count'] : 5,
                'size' => 3,
            );
179
            $elements[] = PluginArtefactBlog::block_advanced_options_element($configdata, 'blog');
180
181
182
183
184
        }
        else {
            $elements[] = array(
                'type' => 'html',
                'name' => 'notice',
185
                'value' => '<div class="metadata">' . get_string('blogcopiedfromanotherview', 'artefact.blog', get_string('Blog', 'artefact.blog')) . '</div>',
186
187
188
            );
        }
        return $elements;
189
190
    }

191
192
193
194
    /**
     * 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
195
     * as making those artefacts 'children' of the blog post, we have to add
196
197
     * them directly to the blog.}}
     *
Aaron Wells's avatar
Aaron Wells committed
198
199
200
201
     * @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 -
202
203
204
205
206
207
208
209
210
     *               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
211
212
213
214
            // 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
215
216
217
218
            // 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) {
219
220
221
222
                    if ($blogpost->get('published')) {
                        $artefacts[] = $blogpost->get('id');
                        $artefacts = array_merge($artefacts, $blogpost->get_referenced_artefacts_from_postbody());
                    }
223
224
225
226
227
228
229
                }
                $artefacts = array_unique($artefacts);
            }
        }
        return $artefacts;
    }

230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
    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;
    }

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

264
265
266
267
    public static function default_copy_type() {
        return 'nocopy';
    }

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

275
276
277
278
279
280
281
282
    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');
        }
    }

283
}