lib.php 5.17 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
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 *
 */

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

class PluginBlocktypeRecentposts extends PluginBlocktype {

    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() {
        return array('blog');
    }

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
    public static function render_instance(BlockInstance $instance, $editing=false) {
Penny Leach's avatar
Penny Leach committed
40
41
42
        $configdata = $instance->get('configdata');

        $result = '';
43
44
        $limit = isset($configdata['count']) ? (int) $configdata['count'] : 10;

Penny Leach's avatar
Penny Leach committed
45
        if (!empty($configdata['artefactids'])) {
46
47
48
49
50
51
52
            $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'";
                }
            }
Penny Leach's avatar
Penny Leach committed
53
54
            $artefactids = implode(', ', array_map('db_quote', $configdata['artefactids']));
            if (!$mostrecent = get_records_sql_array(
55
            'SELECT a.title, ' . db_format_tsfield('a.ctime', 'ctime') . ', p.title AS parenttitle, a.id, a.parent
Penny Leach's avatar
Penny Leach committed
56
57
                FROM {artefact} a
                JOIN {artefact} p ON a.parent = p.id
58
                JOIN {artefact_blog_blogpost} ab ON (ab.blogpost = a.id AND ab.published = 1)
Penny Leach's avatar
Penny Leach committed
59
                WHERE a.artefacttype = \'blogpost\'
60
                AND a.parent IN ( ' . $artefactids . ' )
Francois Marier's avatar
Francois Marier committed
61
                AND a.owner = (SELECT "owner" from {view} WHERE id = ?)
62
                AND ' . $before . '
63
                ORDER BY a.ctime DESC, a.id DESC
64
                LIMIT ' . $limit, array($instance->get('view')))) {
Penny Leach's avatar
Penny Leach committed
65
66
67
68
                $mostrecent = array();
            }
            // format the dates
            foreach ($mostrecent as &$data) {
69
                $data->displaydate = format_date($data->ctime);
Penny Leach's avatar
Penny Leach committed
70
71
72
73
            }
            $smarty = smarty_core();
            $smarty->assign('mostrecent', $mostrecent);
            $smarty->assign('view', $instance->get('view'));
74
75
76
77
78
79
80
81
82
83
84
            $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']), '?')) . ')';
                if (!$selectedblogs = get_records_sql_array('SELECT id, title FROM {artefact}'. $wherestm, $recentpostconfigdata['artefactids'])) {
                    $selectedblogs = array();
                }
                $smarty->assign('blogs', $selectedblogs);
            }
Penny Leach's avatar
Penny Leach committed
85
86
87
88
89
90
91
92
93
94
            $result = $smarty->fetch('blocktype:recentposts:recentposts.tpl');
        }

        return $result;
    }

    public static function has_instance_config() {
        return true;
    }

95
    public static function instance_config_form($instance) {
96
        safe_require('artefact', 'blog');
Penny Leach's avatar
Penny Leach committed
97
        $configdata = $instance->get('configdata');
98
99
100
101
        $elements = array(self::artefactchooser_element((isset($configdata['artefactids'])) ? $configdata['artefactids'] : null),
            'count' => array(
                'type' => 'text',
                'title' => get_string('itemstoshow', 'blocktype.blog/recentposts'),
102
                'description'   => get_string('betweenxandy', 'mahara', 1, 100),
103
104
                'defaultvalue' => isset($configdata['count']) ? $configdata['count'] : 10,
                'size' => 3,
105
                'rules' => array('integer' => true, 'minvalue' => 1, 'maxvalue' => 100),
106
107
108
            ),
        );
	
109
        return $elements;
Penny Leach's avatar
Penny Leach committed
110
111
    }

112
    public static function artefactchooser_element($default=null) {
Penny Leach's avatar
Penny Leach committed
113
114
115
116
117
118
        return array(
            'name'  => 'artefactids',
            'type'  => 'artefactchooser',
            'title' => get_string('blogs', 'artefact.blog'),
            'defaultvalue' => $default,
            'blocktype' => 'recentposts',
119
            'limit'     => 10,
Penny Leach's avatar
Penny Leach committed
120
121
122
123
124
125
            'selectone' => false,
            'artefacttypes' => array('blog'),
            'template'  => 'artefact:blog:artefactchooser-element.tpl',
        );
    }

126
127
128
129
    public static function default_copy_type() {
        return 'nocopy';
    }

130
    /**
Aaron Wells's avatar
Aaron Wells committed
131
     * Recentposts blocktype is only allowed in personal views, because
132
133
134
135
136
137
     * currently there's no such thing as group/site blogs
     */
    public static function allowed_in_view(View $view) {
        return $view->get('owner') != null;
    }

Penny Leach's avatar
Penny Leach committed
138
}