lib.php 8.31 KB
Newer Older
1
2
3
4
5
<?php
/**
 *
 * @package    mahara
 * @subpackage blocktype-blogpost
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 PluginBlocktypeBlogpost extends MaharaCoreBlocktype {
15
16
17
18
19

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

Lisa Seeto's avatar
Lisa Seeto committed
20
21
22
23
24
    //The block only allows one artefact
    public static function single_artefact_per_block() {
        return true;
    }

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

        if (!empty($configdata['artefactid'])) {
33
            return $bi->get_artefact_instance($configdata['artefactid'])->get('title');
34
35
36
37
38
39
40
41
42
        }
        return '';
    }

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

    public static function get_categories() {
43
        return array('blog' => 11000);
44
45
    }

46
47
48
49
    public static function get_viewtypes() {
        return array('dashboard', 'portfolio', 'profile', 'grouphomepage');
    }

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

        $result = '';
54
        $artefactid = isset($configdata['artefactid']) ? $configdata['artefactid'] : null;
Lisa Seeto's avatar
Lisa Seeto committed
55
        $smarty = smarty_core();
56
        if ($artefactid) {
57
            require_once(get_config('docroot') . 'artefact/lib.php');
58
            $artefact = $instance->get_artefact_instance($artefactid);
59
            $configdata['hidetitle'] = true;
60
            $configdata['countcomments'] = true;
61
            $configdata['viewid'] = $instance->get('view');
62
            $configdata['blockid'] = $instance->get('id');
Lisa Seeto's avatar
Lisa Seeto committed
63
            $configdata['editing'] = $editing;
64
            $result = $artefact->render_self($configdata);
65
            $result = $result['html'];
66
67
68
69
70
            require_once(get_config('docroot') . 'artefact/comment/lib.php');
            require_once(get_config('docroot') . 'lib/view.php');
        }
        $smarty->assign('html', $result);
        return $smarty->fetch('blocktype:blogpost:blogpost.tpl');
71
72
    }

73
74
75
    /**
     * Returns a list of artefact IDs that are in this blockinstance.
     *
Aaron Wells's avatar
Aaron Wells committed
76
77
78
79
80
     * Normally this would just include the blogpost ID itself (children such
     * as attachments don't need to be included here, they're handled by the
     * artefact parent cache). But people might just link to artefacts without
     * using the attachment facility. There's nothing wrong with them doing
     * that, so if they do we should scrape the post looking for such links and
81
82
     * include those artefacts as being part of this blockinstance.
     *
Aaron Wells's avatar
Aaron Wells committed
83
84
85
86
     * @return array List of artefact IDs that are 'in' this blogpost - all
     *               the blogpost ID plus links to other artefacts that are
     *               part of the blogpost text. Note that proper artefact
     *               children, such as blog post attachments, aren't included -
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
     *               the artefact parent cache is used for them
     * @see PluginBlocktypeBlog::get_artefacts()
     */
    public static function get_artefacts(BlockInstance $instance) {
        $configdata = $instance->get('configdata');
        $artefacts = array();
        if (isset($configdata['artefactid'])) {
            $artefacts[] = $configdata['artefactid'];

            // Add all artefacts found in the blogpost text
            $blogpost = $instance->get_artefact_instance($configdata['artefactid']);
            $artefacts = array_unique(array_merge($artefacts, $blogpost->get_referenced_artefacts_from_postbody()));
        }
        return $artefacts;
    }

103
    public static function has_instance_config(BlockInstance $instance) {
104
105
106
        return true;
    }

Son Nguyen's avatar
Son Nguyen committed
107
    public static function instance_config_form(BlockInstance $instance) {
108
        global $USER;
109
        safe_require('artefact', 'blog');
110
        $configdata = $instance->get('configdata');
111
112
113
114
115
116
117

        if (!empty($configdata['artefactid'])) {
            $blog = $instance->get_artefact_instance($configdata['artefactid']);
        }

        $elements = array();

Aaron Wells's avatar
Aaron Wells committed
118
119
120
121
        // If the blog post in this block is owned by the owner of the View,
        // then the block can be configured. Otherwise, the blog post was
        // copied in from another View. We won't confuse users by asking them to
        // choose a blog post to put in this block, when the one that is
122
        // currently in it isn't choosable.
123
        $institution = $instance->get('view_obj')->get('institution');
124
125
        $group = $instance->get('view_obj')->get('group');
        if (empty($configdata['artefactid']) || ArtefactTypeBlog::can_edit_blog($blog, $institution, $group)) {
126
            $sql = "SELECT a.id FROM {artefact} a
127
                    INNER JOIN {artefact_blog_blogpost} p ON p.blogpost = a.id
128
129
130
131
132
                    WHERE p.published = 1";
            if ($institution) {
                $sql .= " AND a.institution = ?";
                $where = array($institution);
            }
133
            else if ($group) {
134
135
                $sql .= " AND ( a.group = ? OR a.owner = ? )";
                $where = array($group, $USER->get('id'));
136
            }
137
138
139
140
141
            else {
                $sql .= " AND a.owner = ?";
                $where = array($USER->get('id'));
            }
            $publishedposts = get_column_sql($sql, $where);
142
            $elements[] = self::artefactchooser_element((isset($configdata['artefactid'])) ? $configdata['artefactid'] : null, $publishedposts);
143
            $elements[] = PluginArtefactBlog::block_advanced_options_element($configdata, 'blogpost');
144
145
146
147
148
        }
        else {
            $elements[] = array(
                'type' => 'html',
                'name' => 'notice',
Naomi Guyer's avatar
Naomi Guyer committed
149
                'value' => '<div class="metadata">' . get_string('blogcopiedfromanotherview', 'artefact.blog', get_string('blogpost', 'artefact.blog')) . '</div>',
150
151
152
            );
        }
        return $elements;
153
154
    }

155
    public static function artefactchooser_element($default=null, $publishedposts=array()) {
156
        $element = array(
157
158
159
            'name'  => 'artefactid',
            'type'  => 'artefactchooser',
            'title' => get_string('blogpost', 'artefact.blog'),
160
            'description' => get_string('choosepublishedblogpostsdescription', 'blocktype.blog/blogpost'),
161
162
            'defaultvalue' => $default,
            'blocktype' => 'blogpost',
163
            'limit'     => 10,
164
165
166
            'selectone' => true,
            'artefacttypes' => array('blogpost'),
            'template'  => 'artefact:blog:artefactchooser-element.tpl',
167
            'extraselect' => !empty($publishedposts) ? array(array('fieldname' => 'id', 'type' => 'int', 'values' => $publishedposts)) : null,
168
        );
169
        return $element;
170
    }
171
172

    /**
Aaron Wells's avatar
Aaron Wells committed
173
     * Optional method. If specified, allows the blocktype class to munge the
174
175
176
177
178
179
180
181
182
     * artefactchooser element data before it's templated
     */
    public static function artefactchooser_get_element_data($artefact) {
        static $blognames = array();

        if (!isset($blognames[$artefact->parent])) {
            $blognames[$artefact->parent] = get_field('artefact', 'title', 'id', $artefact->parent);
        }
        $artefact->blog = $blognames[$artefact->parent];
183
        $artefact->description = str_shorten_html($artefact->description, 50, true);
184
185
186
187
188
189
190
191
192
193

        return $artefact;
    }

    /**
     * Optional method. If specified, changes the order in which the artefacts are sorted in the artefact chooser.
     *
     * This is a valid SQL string for the ORDER BY clause. Fields you can sort on are as per the artefact table
     */
    public static function artefactchooser_get_sort_order() {
194
195
        return array(array('fieldname' => 'parent'),
                     array('fieldname' => 'ctime', 'order' => 'DESC'));
196
    }
Richard Mansfield's avatar
Richard Mansfield committed
197

198
    public static function default_copy_type(BlockInstance $instance, View $view) {
199
200
201
        return 'nocopy';
    }

202
    /**
203
     * Blogpost blocktype is allowed in personal / institution / group views
204
205
     */
    public static function allowed_in_view(View $view) {
206
        return true;
207
208
    }

209
}