lib.php 5.73 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

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

38
39
40
        if (!isset($configdata['artefactid'])) {
            return '';
        }
41

42
43
44
45
        $id = $configdata['artefactid'];
        $image = $instance->get_artefact_instance($id);
        $wwwroot = get_config('wwwroot');
        $viewid = $instance->get('view');
46

47
        if ($image instanceof ArtefactTypeProfileIcon) {
48
            $src = $wwwroot . 'thumb.php?type=profileiconbyid&id=' . $id . '&view=' . $viewid;
49
50
51
52
53
54
            $description = $image->get('title');
        }
        else {
            $src = $wwwroot . 'artefact/file/download.php?file=' . $id . '&view=' . $viewid;
            $description = $image->get('description');
        }
55

56
57
        if (!empty($configdata['width'])) {
            $src .= '&maxwidth=' . $configdata['width'];
58
59
        }

60
61
62
        require_once(get_config('docroot') . 'artefact/comment/lib.php');
        require_once(get_config('docroot') . 'lib/view.php');
        $view = new View($viewid);
63
        list($commentcount, $comments) = ArtefactTypeComment::get_artefact_comments_for_view($image, $view, $instance->get('id'), true, $editing);
64
        $smarty = smarty_core();
65
66
        $smarty->assign('commentcount', $commentcount);
        $smarty->assign('comments', $comments);
67
        $smarty->assign('url', $wwwroot . 'artefact/artefact.php?artefact=' . $id . '&view=' . $viewid);
68
69
70
71
        $smarty->assign('src', $src);
        $smarty->assign('description', $description);
        $smarty->assign('showdescription', !empty($configdata['showdescription']) && !empty($description));
        return $smarty->fetch('blocktype:image:image.tpl');
Penny Leach's avatar
Penny Leach committed
72
73
    }

74
75
76
77
    public static function has_instance_config() {
        return true;
    }

Son Nguyen's avatar
Son Nguyen committed
78
    public static function instance_config_form(BlockInstance $instance) {
79
        $configdata = $instance->get('configdata');
80
81
        safe_require('artefact', 'file');
        $instance->set('artefactplugin', 'file');
82
83
        $filebrowser = self::filebrowser_element($instance, (isset($configdata['artefactid'])) ? array($configdata['artefactid']) : null);

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

114
115
    public static function filebrowser_element(&$instance, $default=array()) {
        $element = ArtefactTypeFileBase::blockconfig_filebrowser_element($instance, $default);
116
        $element['title'] = get_string('image');
117
        $element['name'] = 'artefactid';
118
        $element['accept'] = 'image/*';
119
        $element['config']['selectone'] = true;
120
        $element['config']['selectmodal'] = true;
121
        $element['filters'] = array(
122
            'artefacttype'    => array('image', 'profileicon'),
123
124
125
126
        );
        return $element;
    }

127
    public static function artefactchooser_element($default=null) {
128
        return array(
129
130
131
132
133
134
            'name'  => 'artefactid',
            'type'  => 'artefactchooser',
            'title' => get_string('image'),
            'defaultvalue' => $default,
            'blocktype' => 'image',
            'limit' => 10,
135
            'selectmodal' => true,
136
137
138
139
140
            'artefacttypes' => array('image', 'profileicon'),
            'template' => 'artefact:file:artefactchooser-element.tpl',
        );
    }

141
    /**
Aaron Wells's avatar
Aaron Wells committed
142
     * Optional method. If specified, allows the blocktype class to munge the
143
144
145
     * artefactchooser element data before it's templated
     */
    public static function artefactchooser_get_element_data($artefact) {
146
        return ArtefactTypeFileBase::artefactchooser_get_file_data($artefact);
147
148
    }

149
150
151
    public static function default_copy_type() {
        return 'full';
    }
152
}