lib.php 10.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');
    }

    /**
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
            $smarty->assign('view', $instance->get('view'));
118 119 120
            $smarty->assign('posts', $posts);

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

        return $result;
    }

    public static function has_instance_config() {
        return true;
    }

130
    public static function instance_config_form(BlockInstance $instance) {
131
        global $USER;
132
        safe_require('artefact', 'blog');
133
        $configdata = $instance->get('configdata');
134

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

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

        $elements = array();

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

189 190 191 192
    /**
     * Returns a list of artefact IDs that are in this blockinstance.
     *
     * {@internal{Because links to artefacts within blogposts don't count
193
     * as making those artefacts 'children' of the blog post, we have to add
194 195
     * them directly to the blog.}}
     *
196 197 198 199
     * @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 -
200 201 202 203 204 205 206 207 208
     *               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'];

209 210 211 212
            // 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
213 214 215 216
            // 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) {
217 218 219 220
                    if ($blogpost->get('published')) {
                        $artefacts[] = $blogpost->get('id');
                        $artefacts = array_merge($artefacts, $blogpost->get_referenced_artefacts_from_postbody());
                    }
221 222 223 224 225 226 227
                }
                $artefacts = array_unique($artefacts);
            }
        }
        return $artefacts;
    }

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

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

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

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

262
}