lib.php 10.7 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) {
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['viewid'] = $instance->get('view');
60
61
62
63
64
65
            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;
                }
            }
66
67
68
69

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

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

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

            $result = $smarty->fetch('artefact:blog:blog.tpl');
120
121
122
123
124
125
126
127
128
        }

        return $result;
    }

    public static function has_instance_config() {
        return true;
    }

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

134
135
136
        require_once(get_config('libroot') . 'view.php');
        $view = new View($instance->get('view'));
        $institution = $view->get('institution');
137
        $group = $view->get('group');
138

139
140
141
142
143
144
        if (!empty($configdata['artefactid'])) {
            $blog = $instance->get_artefact_instance($configdata['artefactid']);
        }

        $elements = array();

Aaron Wells's avatar
Aaron Wells committed
145
146
147
148
        // 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
149
150
        // choosable.
        //
Aaron Wells's avatar
Aaron Wells committed
151
        // Note: the owner check will have to change when we do group/site
152
        // blogs
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
163
164
165
            else if ($group) {
                $sql .= " AND a.group = ?";
                $where[] = $group;
            }
166
167
168
169
170
171
            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);
172
173
174
175
176
177
            $elements['count'] = array(
                'type' => 'text',
                'title' => get_string('postsperpage', 'blocktype.blog/blog'),
                'defaultvalue' => isset($configdata['count']) ? $configdata['count'] : 5,
                'size' => 3,
            );
178
            $elements[] = PluginArtefactBlog::block_advanced_options_element($configdata, 'blog');
179
180
181
182
183
        }
        else {
            $elements[] = array(
                'type' => 'html',
                'name' => 'notice',
184
                'value' => '<div class="metadata">' . get_string('blogcopiedfromanotherview', 'artefact.blog', get_string('Blog', 'artefact.blog')) . '</div>',
185
186
187
            );
        }
        return $elements;
188
189
    }

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

229
    public static function artefactchooser_element($default=null, $blogids=array()) {
230
        return array(
231
232
            'name'  => 'artefactid',
            'type'  => 'artefactchooser',
233
            'title' => get_string('Blog', 'artefact.blog'),
234
235
            'defaultvalue' => $default,
            'blocktype' => 'blog',
236
            'limit'     => 10,
237
238
239
            'selectone' => true,
            'artefacttypes' => array('blog'),
            'template'  => 'artefact:blog:artefactchooser-element.tpl',
240
            'extraselect' => !empty($blogids) ? array(array('fieldname' => 'id', 'type' => 'int', 'values' => $blogids)) : null,
241
242
243
        );
    }

244
245
246
247
    public static function default_copy_type() {
        return 'nocopy';
    }

248
    /**
249
     * Blog blocktype is only allowed in personal / institution / group views
250
251
     */
    public static function allowed_in_view(View $view) {
252
        return true;
253
254
    }

255
256
257
258
259
260
261
262
    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');
        }
    }

263
}