lib.php 4.81 KB
Newer Older
Penny Leach's avatar
Penny Leach committed
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4
5
 * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
Penny Leach's avatar
Penny Leach committed
6
 *
Francois Marier's avatar
Francois Marier committed
7
8
9
10
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
Penny Leach's avatar
Penny Leach committed
11
 *
Francois Marier's avatar
Francois Marier committed
12
13
14
15
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
Penny Leach's avatar
Penny Leach committed
16
 *
Francois Marier's avatar
Francois Marier committed
17
18
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Penny Leach's avatar
Penny Leach committed
19
20
21
 *
 * @package    mahara
 * @subpackage blocktype-recentposts
22
 * @author     Catalyst IT Ltd
Penny Leach's avatar
Penny Leach committed
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
24
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
Penny Leach's avatar
Penny Leach committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 *
 */

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');
    }

45
    public static function render_instance(BlockInstance $instance, $editing=false) {
Penny Leach's avatar
Penny Leach committed
46
47
48
        $configdata = $instance->get('configdata');

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

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

        return $result;
    }

    public static function has_instance_config() {
        return true;
    }

90
    public static function instance_config_form($instance) {
91
        safe_require('artefact', 'blog');
Penny Leach's avatar
Penny Leach committed
92
        $configdata = $instance->get('configdata');
93
94
95
96
97
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'),
                'defaultvalue' => isset($configdata['count']) ? $configdata['count'] : 10,
                'size' => 3,
            ),
        );
	
102
        return $elements;
Penny Leach's avatar
Penny Leach committed
103
104
    }

105
    public static function artefactchooser_element($default=null) {
Penny Leach's avatar
Penny Leach committed
106
107
108
109
110
111
        return array(
            'name'  => 'artefactids',
            'type'  => 'artefactchooser',
            'title' => get_string('blogs', 'artefact.blog'),
            'defaultvalue' => $default,
            'blocktype' => 'recentposts',
112
            'limit'     => 10,
Penny Leach's avatar
Penny Leach committed
113
114
115
116
117
118
            'selectone' => false,
            'artefacttypes' => array('blog'),
            'template'  => 'artefact:blog:artefactchooser-element.tpl',
        );
    }

119
120
121
122
    public static function default_copy_type() {
        return 'nocopy';
    }

123
124
125
126
127
128
129
130
    /**
     * Recentposts blocktype is only allowed in personal views, because 
     * 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
131
}