lib.php 5.26 KB
Newer Older
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
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.
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.
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/>.
19
20
21
 *
 * @package    mahara
 * @subpackage blocktype-image
22
 * @author     Catalyst IT Ltd
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
24
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 *
 */

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

class PluginBlocktypeImage extends PluginBlocktype {

    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() {
41
        return array('fileimagevideo');
42
    }
Penny Leach's avatar
Penny Leach committed
43

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

47
48
49
        if (!isset($configdata['artefactid'])) {
            return '';
        }
50

51
52
53
54
        $id = $configdata['artefactid'];
        $image = $instance->get_artefact_instance($id);
        $wwwroot = get_config('wwwroot');
        $viewid = $instance->get('view');
55

56
57
58
59
60
61
62
63
        if ($image instanceof ArtefactTypeProfileIcon) {
            $src = $wwwroot . 'thumb.php?type=profileiconbyid&id=' . $id;
            $description = $image->get('title');
        }
        else {
            $src = $wwwroot . 'artefact/file/download.php?file=' . $id . '&view=' . $viewid;
            $description = $image->get('description');
        }
64

65
66
        if (!empty($configdata['width'])) {
            $src .= '&maxwidth=' . $configdata['width'];
67
68
        }

69
70
71
72
73
74
        $smarty = smarty_core();
        $smarty->assign('url', $wwwroot . 'view/artefact.php?artefact=' . $id . '&view=' . $viewid);
        $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
75
76
    }

77
78
79
80
    public static function has_instance_config() {
        return true;
    }

81
    public static function instance_config_form($instance) {
82
        $configdata = $instance->get('configdata');
83
84
        safe_require('artefact', 'file');
        $instance->set('artefactplugin', 'file');
85
        return array(
86
            'artefactid' => self::filebrowser_element($instance, (isset($configdata['artefactid'])) ? array($configdata['artefactid']) : null),
87
88
            'showdescription' => array(
                'type'  => 'checkbox',
89
                'title' => get_string('showdescription', 'blocktype.file/image'),
90
                'defaultvalue' => !empty($configdata['showdescription']) ? true : false,
91
            ),
92
93
94
95
96
97
98
99
100
101
102
            'width' => array(
                'type' => 'text',
                'title' => get_string('width', 'blocktype.file/image'),
                'size' => 3,
                'description' => get_string('widthdescription', 'blocktype.file/image'),
                'rules' => array(
                    'minvalue' => 16,
                    'maxvalue' => get_config('imagemaxwidth'),
                ),
                'defaultvalue' => (isset($configdata['width'])) ? $configdata['width'] : '',
            ),
103
104
        );
    }
105

106
107
    public static function filebrowser_element(&$instance, $default=array()) {
        $element = ArtefactTypeFileBase::blockconfig_filebrowser_element($instance, $default);
108
        $element['title'] = get_string('image');
109
        $element['name'] = 'artefactid';
110
111
        $element['config']['selectone'] = true;
        $element['filters'] = array(
112
            'artefacttype'    => array('image', 'profileicon'),
113
114
115
116
        );
        return $element;
    }

117
    public static function artefactchooser_element($default=null) {
118
        return array(
119
120
121
122
123
124
125
126
127
128
129
            'name'  => 'artefactid',
            'type'  => 'artefactchooser',
            'title' => get_string('image'),
            'defaultvalue' => $default,
            'blocktype' => 'image',
            'limit' => 10,
            'artefacttypes' => array('image', 'profileicon'),
            'template' => 'artefact:file:artefactchooser-element.tpl',
        );
    }

130
131
132
133
134
    /**
     * Optional method. If specified, allows the blocktype class to munge the 
     * artefactchooser element data before it's templated
     */
    public static function artefactchooser_get_element_data($artefact) {
135
        return ArtefactTypeFileBase::artefactchooser_get_file_data($artefact);
136
137
    }

138
139
140
141
    public static function default_copy_type() {
        return 'full';
    }

142
}