lib.php 9.48 KB
Newer Older
Penny Leach's avatar
Penny Leach committed
1
2
3
4
5
<?php
/**
 *
 * @package    mahara
 * @subpackage blocktype-recentposts
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.
Penny Leach's avatar
Penny Leach committed
9
10
11
12
13
 *
 */

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

14
class PluginBlocktypeRecentposts extends MaharaCoreBlocktype {
Penny Leach's avatar
Penny Leach committed
15
16
17
18
19
20
21
22
23
24
25

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


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

    public static function get_categories() {
26
        return array('blog' => 12000);
Penny Leach's avatar
Penny Leach committed
27
28
    }

29
30
31
32
33
34
35
36
37
38
    public static function get_instance_javascript(BlockInstance $bi) {
        $blockid = $bi->get('id');
        return array(
            array(
                'file'   => 'js/recentposts.js',
                'initjs' => "addNewPostShortcut($blockid);",
            )
        );
    }

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    /**
     * Returns a list of artefact IDs that are "in" this blockinstance.
     *
     * {@internal{Because links to artefacts within blogposts don't count
     * as making those artefacts 'children' of the blog post, we have to add
     * them directly to the blog.}}
     *
     * @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 -
     *               the artefact parent cache is used for them
     * @see PluginBlocktypeBlogPost::get_artefacts()
     */
    public static function get_artefacts(BlockInstance $instance) {
Penny Leach's avatar
Penny Leach committed
54
        $configdata = $instance->get('configdata');
55
56
57
58
59
60
61
62
63
64
65
66
67
68
        $artefacts = array();
        if (isset($configdata['artefactids'])) {
            if (isset($configdata['artefactids']) && is_array($configdata['artefactids'])) {
                $artefacts = array_merge($artefacts, $configdata['artefactids']);
            }
            $blogposts = self::get_blog_posts_in_block($instance);
            foreach ($blogposts as $blogpost) {
                $artefacts[] = $blogpost->id;
                $blogpostobj = $instance->get_artefact_instance($blogpost->id);
                $artefacts = array_merge($artefacts, $blogpostobj->get_referenced_artefacts_from_postbody());
            }
        }
        return $artefacts;
    }
Penny Leach's avatar
Penny Leach committed
69

70
71
72
73
74
75
76
77
78
    /**
     * Get the blog entries that will be displayed by this block.
     * (This list will change depending when new blog entries are created, published, etc
     *
     * @param BlockInstance $instance
     * @return array of objects
     */
    public static function get_blog_posts_in_block(BlockInstance $instance) {
        $configdata = $instance->get('configdata');
79
        $limit = isset($configdata['count']) ? (int) $configdata['count'] : 10;
80
        $mostrecent = array();
Penny Leach's avatar
Penny Leach committed
81
        if (!empty($configdata['artefactids'])) {
82
83
84
85
86
87
88
            $before = 'TRUE';
            if ($instance->get_view()->is_submitted()) {
                if ($submittedtime = $instance->get_view()->get('submittedtime')) {
                    // Don't display posts added after the submitted date.
                    $before = "a.ctime < '$submittedtime'";
                }
            }
89
90
91
92

            $blogids = $configdata['artefactids'];
            $artefactids = implode(', ', array_map('db_quote', $blogids));
            $mostrecent = get_records_sql_array(
Lisa Seeto's avatar
Lisa Seeto committed
93
                'SELECT a.title, a.description, a.allowcomments, ' . db_format_tsfield('a.ctime', 'ctime') . ', p.title AS parenttitle, a.id, a.parent, ' . db_format_tsfield('a.mtime', 'mtime') . '
94
95
96
97
98
99
100
                    FROM {artefact} a
                    JOIN {artefact} p ON a.parent = p.id
                    JOIN {artefact_blog_blogpost} ab ON (ab.blogpost = a.id AND ab.published = 1)
                    WHERE a.artefacttype = \'blogpost\'
                    AND a.parent IN ( ' . $artefactids . ' )
                    AND ' . $before . '
                    ORDER BY a.ctime DESC, a.id DESC
101
102
                    LIMIT ' . $limit
            );
103
            if (!$mostrecent) {
Penny Leach's avatar
Penny Leach committed
104
105
                $mostrecent = array();
            }
106
107
108
109
        }
        return $mostrecent;
    }

110
    public static function render_instance(BlockInstance $instance, $editing=false, $versioning=false) {
111
        global $USER;
112
113
114
115

        $result = '';
        $mostrecent = self::get_blog_posts_in_block($instance);
        if ($mostrecent) {
116
117
            // format the dates and blog title
            safe_require('artefact', 'blog');
Penny Leach's avatar
Penny Leach committed
118
            foreach ($mostrecent as &$data) {
119
                $data->displaydate = format_date($data->ctime);
120
121
122
                if ($data->ctime != $data->mtime) {
                    $data->updateddate = format_date($data->mtime);
                }
123
124
                $blog = new ArtefactTypeBlog($data->parent);
                $data->parenttitle = $blog->display_title();
Lisa Seeto's avatar
Lisa Seeto committed
125
126
127
128
129
                $data->commentcount = count_records_select('artefact_comment_comment', "onartefact = ? AND private = ? AND deletedby IS NULL AND hidden= ?", array($data->id, 0, 0));
                if ($tags = ArtefactType::artefact_get_tags($data->id)) {
                    $data->tags = $tags;
                }
                $data->owner = $blog->get('owner');
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
                $sql = 'SELECT a.title, a.id, a.artefacttype, af.size, a.description
                        FROM {artefact} a
                        JOIN {artefact_file_files} af ON af.artefact = a.id
                        JOIN {artefact_attachment} at ON at.attachment = a.id
                        WHERE at.artefact = ?
                        ORDER BY a.title';
                if ($attachments = get_records_sql_array($sql, array($data->id))) {
                    foreach ($attachments as &$attachment) {
                        $f = artefact_instance_from_id($attachment->id);
                        $attachment->iconpath = $f->get_icon(array('id' => $attachment->id, 'viewid' => isset($options['viewid']) ? $options['viewid'] : 0));
                        $attachment->downloadpath = get_config('wwwroot') . 'artefact/file/download.php?file=' . $attachment->id;
                        if (isset($options['viewid'])) {
                            $attachment->downloadpath .= '&view=' . $options['viewid'];
                        }
                    }
                    $data->attachments = $attachments;
                }
Penny Leach's avatar
Penny Leach committed
147
            }
148

Penny Leach's avatar
Penny Leach committed
149
150
151
            $smarty = smarty_core();
            $smarty->assign('mostrecent', $mostrecent);
            $smarty->assign('view', $instance->get('view'));
152
153
            $smarty->assign('blockid', $instance->get('id'));
            $smarty->assign('editing', $editing);
Lisa Seeto's avatar
Lisa Seeto committed
154
            $smarty->assign('licensemetadata', get_config('licensemetadata') ? true : false);
155
            $smarty->assign('canviewblog', $USER->can_view_artefact($blog));
Lisa Seeto's avatar
Lisa Seeto committed
156

157
158
159
160
            if ($editing) {
                // Get id and title of configued blogs
                $recentpostconfigdata = $instance->get('configdata');
                $wherestm = ' WHERE id IN (' . join(',', array_fill(0, count($recentpostconfigdata['artefactids']), '?')) . ')';
161
162
163
164
165
166
167
168
169

                $blogs = array();
                if ($selectedblogs = get_records_sql_array('SELECT id, title FROM {artefact}'. $wherestm, $recentpostconfigdata['artefactids'])) {
                    foreach ($selectedblogs as $selectedblog) {
                        $blog = new ArtefactTypeBlog($selectedblog->id);
                        if (ArtefactTypeBlog::can_edit_blog($blog, $blog->get('institution'), $blog->get('group'))) {
                          $blogs[] = $selectedblog;
                        }
                    }
170
                }
171
                $smarty->assign('blogs', $blogs);
172
            }
Penny Leach's avatar
Penny Leach committed
173
174
175
176
177
178
179
180
181
182
            $result = $smarty->fetch('blocktype:recentposts:recentposts.tpl');
        }

        return $result;
    }

    public static function has_instance_config() {
        return true;
    }

Son Nguyen's avatar
Son Nguyen committed
183
    public static function instance_config_form(BlockInstance $instance) {
184
        safe_require('artefact', 'blog');
Penny Leach's avatar
Penny Leach committed
185
        $configdata = $instance->get('configdata');
186
187
188
189
        $elements = array(self::artefactchooser_element((isset($configdata['artefactids'])) ? $configdata['artefactids'] : null),
            'count' => array(
                'type' => 'text',
                'title' => get_string('itemstoshow', 'blocktype.blog/recentposts'),
190
                'description'   => get_string('betweenxandy', 'mahara', 1, 100),
191
192
                'defaultvalue' => isset($configdata['count']) ? $configdata['count'] : 10,
                'size' => 3,
193
                'rules' => array('integer' => true, 'minvalue' => 1, 'maxvalue' => 100),
194
195
            ),
        );
196

197
        return $elements;
Penny Leach's avatar
Penny Leach committed
198
199
    }

200
    public static function artefactchooser_element($default=null) {
Penny Leach's avatar
Penny Leach committed
201
202
203
        return array(
            'name'  => 'artefactids',
            'type'  => 'artefactchooser',
204
            'title' => get_string('Blogs', 'artefact.blog'),
Penny Leach's avatar
Penny Leach committed
205
206
            'defaultvalue' => $default,
            'blocktype' => 'recentposts',
207
            'limit'     => 10,
Penny Leach's avatar
Penny Leach committed
208
209
210
211
212
213
            'selectone' => false,
            'artefacttypes' => array('blog'),
            'template'  => 'artefact:blog:artefactchooser-element.tpl',
        );
    }

214
215
216
217
    public static function default_copy_type() {
        return 'nocopy';
    }

218
    /**
219
     * Blog blocktype is only allowed in personal / institution / group views
220
221
     */
    public static function allowed_in_view(View $view) {
222
        return true;
223
224
    }

Penny Leach's avatar
Penny Leach committed
225
}