lib.php 7.79 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(
93
                'SELECT a.title, ' . 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
110
        }
        return $mostrecent;
    }

    public static function render_instance(BlockInstance $instance, $editing=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();
Penny Leach's avatar
Penny Leach committed
125
            }
126

Penny Leach's avatar
Penny Leach committed
127
128
129
            $smarty = smarty_core();
            $smarty->assign('mostrecent', $mostrecent);
            $smarty->assign('view', $instance->get('view'));
130
131
132
133
134
135
            $smarty->assign('blockid', $instance->get('id'));
            $smarty->assign('editing', $editing);
            if ($editing) {
                // Get id and title of configued blogs
                $recentpostconfigdata = $instance->get('configdata');
                $wherestm = ' WHERE id IN (' . join(',', array_fill(0, count($recentpostconfigdata['artefactids']), '?')) . ')';
136
137
138
139
140
141
142
143
144

                $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;
                        }
                    }
145
                }
146
                $smarty->assign('blogs', $blogs);
147
            }
Penny Leach's avatar
Penny Leach committed
148
149
150
151
152
153
154
155
156
157
            $result = $smarty->fetch('blocktype:recentposts:recentposts.tpl');
        }

        return $result;
    }

    public static function has_instance_config() {
        return true;
    }

Son Nguyen's avatar
Son Nguyen committed
158
    public static function instance_config_form(BlockInstance $instance) {
159
        safe_require('artefact', 'blog');
Penny Leach's avatar
Penny Leach committed
160
        $configdata = $instance->get('configdata');
161
162
163
164
        $elements = array(self::artefactchooser_element((isset($configdata['artefactids'])) ? $configdata['artefactids'] : null),
            'count' => array(
                'type' => 'text',
                'title' => get_string('itemstoshow', 'blocktype.blog/recentposts'),
165
                'description'   => get_string('betweenxandy', 'mahara', 1, 100),
166
167
                'defaultvalue' => isset($configdata['count']) ? $configdata['count'] : 10,
                'size' => 3,
168
                'rules' => array('integer' => true, 'minvalue' => 1, 'maxvalue' => 100),
169
170
            ),
        );
171

172
        return $elements;
Penny Leach's avatar
Penny Leach committed
173
174
    }

175
    public static function artefactchooser_element($default=null) {
Penny Leach's avatar
Penny Leach committed
176
177
178
        return array(
            'name'  => 'artefactids',
            'type'  => 'artefactchooser',
179
            'title' => get_string('Blogs', 'artefact.blog'),
Penny Leach's avatar
Penny Leach committed
180
181
            'defaultvalue' => $default,
            'blocktype' => 'recentposts',
182
            'limit'     => 10,
Penny Leach's avatar
Penny Leach committed
183
184
185
186
187
188
            'selectone' => false,
            'artefacttypes' => array('blog'),
            'template'  => 'artefact:blog:artefactchooser-element.tpl',
        );
    }

189
190
191
192
    public static function default_copy_type() {
        return 'nocopy';
    }

193
    /**
194
     * Blog blocktype is only allowed in personal / institution / group views
195
196
     */
    public static function allowed_in_view(View $view) {
197
        return true;
198
199
    }

Penny Leach's avatar
Penny Leach committed
200
}