lib.php 6.75 KB
Newer Older
1
2
3
4
5
<?php
/**
 *
 * @package    mahara
 * @subpackage blocktype-image
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 PluginBlocktypeImage extends MaharaCoreBlocktype {
15

Aaron Wells's avatar
Aaron Wells committed
16
17
18
19
20
21
22
    public static function should_ajaxify() {
        // Most of the load time for an image block is waiting for
        // the image file to get served up, which is already
        // a separate client HTTP request. So no need to ajaxify.
        return false;
    }

23
24
25
26
27
28
29
30
31
    public static function get_title() {
        return get_string('title', 'blocktype.file/image');
    }

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

    public static function get_categories() {
32
        return array('shortcut' => 2000);
33
    }
Penny Leach's avatar
Penny Leach committed
34

Lisa Seeto's avatar
Lisa Seeto committed
35
36
37
38
    public static function single_artefact_per_block() {
        return true;
    }

39
    public static function render_instance(BlockInstance $instance, $editing=false, $versioning=false) {
Penny Leach's avatar
Penny Leach committed
40
        $configdata = $instance->get('configdata'); // this will make sure to unserialize it for us
41

42
43
44
        if (!isset($configdata['artefactid'])) {
            return '';
        }
45

46
47
48
49
        $id = $configdata['artefactid'];
        $image = $instance->get_artefact_instance($id);
        $wwwroot = get_config('wwwroot');
        $viewid = $instance->get('view');
50
        $edittime = '&time=' . time();
51
        if ($image instanceof ArtefactTypeProfileIcon) {
52
            $src = $wwwroot . 'thumb.php?type=profileiconbyid&id=' . $id . '&view=' . $viewid . $edittime;
53
54
55
            $description = $image->get('title');
        }
        else {
56
            $src = $wwwroot . 'artefact/file/download.php?file=' . $id . '&view=' . $viewid . $edittime;
57
            $description = $image->get('description');
58
            $description = empty($description) ? $image->get('title') : $description;
59
        }
60

61
62
        if (!empty($configdata['width'])) {
            $src .= '&maxwidth=' . $configdata['width'];
63
64
        }

65
66
67
        require_once(get_config('docroot') . 'artefact/comment/lib.php');
        require_once(get_config('docroot') . 'lib/view.php');
        $view = new View($viewid);
68
        list($commentcount, $comments) = ArtefactTypeComment::get_artefact_comments_for_view($image, $view, $instance->get('id'), true, $editing, $versioning);
69
        $smarty = smarty_core();
70
        $smarty->assign('editing', $editing);
71
72
        $smarty->assign('commentcount', $commentcount);
        $smarty->assign('comments', $comments);
73
74
75
        $smarty->assign('src', $src);
        $smarty->assign('description', $description);
        $smarty->assign('showdescription', !empty($configdata['showdescription']) && !empty($description));
76
77
        $smarty->assign('blockid', $instance->get('id'));
        $smarty->assign('artefactid', $id);
78
        return $smarty->fetch('blocktype:image:image.tpl');
Penny Leach's avatar
Penny Leach committed
79
80
    }

81
    public static function has_instance_config(BlockInstance $instance) {
82
83
84
        return true;
    }

Son Nguyen's avatar
Son Nguyen committed
85
    public static function instance_config_form(BlockInstance $instance) {
86
        $configdata = $instance->get('configdata');
87
88
        safe_require('artefact', 'file');
        $instance->set('artefactplugin', 'file');
89
90
        $filebrowser = self::filebrowser_element($instance, (isset($configdata['artefactid'])) ? array($configdata['artefactid']) : null);

91
        return array(
92
93
94
95
96
            'artefactfieldset' => array(
                'type'         => 'fieldset',
                'collapsible'  => true,
                'collapsed'    => true,
                'legend'       => get_string('image'),
97
                'class'        => 'last select-file with-formgroup',
98
99
100
101
                'elements'     => array(
                    'artefactid' => $filebrowser
                )
            ),
102
            'showdescription' => array(
103
                'type'  => 'switchbox',
104
                'title' => get_string('showdescription', 'blocktype.file/image'),
105
                'defaultvalue' => !empty($configdata['showdescription']) ? true : false,
106
            ),
107
108
109
110
            'width' => array(
                'type' => 'text',
                'title' => get_string('width', 'blocktype.file/image'),
                'size' => 3,
111
                'description' => get_string('widthdescription1', 'blocktype.file/image'),
112
113
114
115
116
117
                'rules' => array(
                    'minvalue' => 16,
                    'maxvalue' => get_config('imagemaxwidth'),
                ),
                'defaultvalue' => (isset($configdata['width'])) ? $configdata['width'] : '',
            ),
118
119
        );
    }
120

121
122
123
124
125
126
127
128
129
130
131
132
133
    public static function instance_config_save($values, $instance) {
        if (isset($values['artefactid'])) {
            // Pass back a list of any other blocks that need to be rendered
            // due to this change.
            $values['_redrawblocks'] = array_unique(get_column(
                                                               'view_artefact', 'block',
                                                               'artefact', $values['artefactid'],
                                                               'view', $instance->get('view')
                                                               ));
        }
        return $values;
   }

134
135
    public static function filebrowser_element(&$instance, $default=array()) {
        $element = ArtefactTypeFileBase::blockconfig_filebrowser_element($instance, $default);
136
        $element['title'] = get_string('image');
137
        $element['name'] = 'artefactid';
138
        $element['accept'] = 'image/*';
139
        $element['config']['selectone'] = true;
140
        $element['config']['selectmodal'] = true;
141
        $element['filters'] = array(
142
            'artefacttype'    => array('image', 'profileicon'),
143
144
145
146
        );
        return $element;
    }

147
    public static function artefactchooser_element($default=null) {
148
        return array(
149
150
151
152
153
154
            'name'  => 'artefactid',
            'type'  => 'artefactchooser',
            'title' => get_string('image'),
            'defaultvalue' => $default,
            'blocktype' => 'image',
            'limit' => 10,
155
            'selectmodal' => true,
156
157
158
159
160
            'artefacttypes' => array('image', 'profileicon'),
            'template' => 'artefact:file:artefactchooser-element.tpl',
        );
    }

161
    /**
Aaron Wells's avatar
Aaron Wells committed
162
     * Optional method. If specified, allows the blocktype class to munge the
163
164
165
     * artefactchooser element data before it's templated
     */
    public static function artefactchooser_get_element_data($artefact) {
166
        return ArtefactTypeFileBase::artefactchooser_get_file_data($artefact);
167
168
    }

169
    public static function default_copy_type(BlockInstance $instance, View $view) {
170
171
        return 'full';
    }
172
}