lib.php 8.87 KB
Newer Older
Evan Goldenberg's avatar
Evan Goldenberg committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/**
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
 * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
 *
 * 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.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package    mahara
 * @subpackage blocktype-slideshow
 * @author     Catalyst IT Ltd
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

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

class PluginBlocktypeGallery extends PluginBlocktype {

    public static function get_title() {
        return get_string('title', 'blocktype.file/gallery');
    }

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

    public static function get_categories() {
        return array('fileimagevideo');
    }

    public static function get_instance_javascript(BlockInstance $instance) {
        $configdata = $instance->get('configdata');
        switch ($configdata['style']) {
            case 0: // thumbnails
                return array();
            case 1: // slideshow
                return array('js/slideshow.js');
        }
    }

    public static function get_instance_config_javascript() {
        return array('js/configform.js');
    }

    public static function render_instance(BlockInstance $instance, $editing=false) {
        $configdata = $instance->get('configdata'); // this will make sure to unserialize it for us
        $configdata['viewid'] = $instance->get('view');
Richard Mansfield's avatar
Richard Mansfield committed
61
62
        $style = isset($configdata['style']) ? intval($configdata['style']) : 0;
        switch ($style) {
Evan Goldenberg's avatar
Evan Goldenberg committed
63
64
            case 0: // thumbnails
                $template = 'thumbnails';
65
                $width = isset($configdata['width']) ? $configdata['width'] : 75;
Evan Goldenberg's avatar
Evan Goldenberg committed
66
67
68
                break;
            case 1: // slideshow
                $template = 'slideshow';
69
                $width = isset($configdata['width']) ? $configdata['width'] : 400;
Evan Goldenberg's avatar
Evan Goldenberg committed
70
71
72
                break;
        }

73
74
75
76
77
78
79
80
81
        $artefactids = array();
        if (isset($configdata['select']) && $configdata['select'] == 1 && is_array($configdata['artefactids'])) {
            $artefactids = $configdata['artefactids'];
        }
        else if (!empty($configdata['artefactid'])) {
            // Get descendents of this folder.
            $artefactids = artefact_get_descendants(array(intval($configdata['artefactid'])));
        }

Evan Goldenberg's avatar
Evan Goldenberg committed
82
83
84
        // This can be either an image or profileicon. They both implement
        // render_self
        $images = array();
85
86
87
88
89
90
91
92
93
94
95
96
97
98
        foreach ($artefactids as $artefactid) {
            $image = $instance->get_artefact_instance($artefactid);

            if ($image instanceof ArtefactTypeProfileIcon) {
                $src = get_config('wwwroot') . 'thumb.php?type=profileiconbyid&id=' . $artefactid;
                $description = $image->get('title');
            }
            else if ($image instanceof ArtefactTypeImage) {
                $src = get_config('wwwroot') . 'artefact/file/download.php?file=' . $artefactid;
                $src .= '&view=' . $instance->get('view');
                $description = $image->get('description');
            }
            else {
                continue;
Evan Goldenberg's avatar
Evan Goldenberg committed
99
            }
100

101
            $src .= '&maxwidth=' . $width;
102
103
104
105
106
107
108

            $images[] = array(
                'link' => get_config('wwwroot') . 'view/artefact.php?artefact=' .
                    $artefactid . '&view=' . $instance->get('view'),
                'source' => $src,
                'title' => $image->get('title'),
            );
Evan Goldenberg's avatar
Evan Goldenberg committed
109
110
111
112
113
114
        }

        $smarty = smarty_core();
        $smarty->assign('instanceid', $instance->get('id'));
        $smarty->assign('count', count($images));
        $smarty->assign('images', $images);
115
        $smarty->assign('width', $width);
Evan Goldenberg's avatar
Evan Goldenberg committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129

        return $smarty->fetch('blocktype:gallery:' . $template . '.tpl');
    }

    public static function has_instance_config() {
        return true;
    }

    public static function instance_config_form($instance) {
        $configdata = $instance->get('configdata');
        safe_require('artefact', 'file');
        $instance->set('artefactplugin', 'file');
        $user = $instance->get('view_obj')->get('owner');
        $select_options = array(
130
131
            0 => get_string('selectfolder', 'blocktype.file/gallery'),
            1 => get_string('selectimages', 'blocktype.file/gallery'),
Evan Goldenberg's avatar
Evan Goldenberg committed
132
133
134
135
136
        );
        $style_options = array(
            0 => get_string('stylethumbs', 'blocktype.file/gallery'),
            1 => get_string('styleslideshow', 'blocktype.file/gallery'),
        );
137
138
139
140
141
142
143
144
145
146
        if (isset($configdata['select']) && $configdata['select'] == 1) {
            $imageids = isset($configdata['artefactids']) ? $configdata['artefactids'] : array();
            $imageselector = self::imageselector($instance, $imageids);
            $folderselector = self::folderselector($instance, null, 'hidden');
        }
        else {
            $imageselector = self::imageselector($instance, null, 'hidden');
            $folderid = !empty($configdata['artefactid']) ? array(intval($configdata['artefactid'])) : null;
            $folderselector = self::folderselector($instance, $folderid);
        }
Evan Goldenberg's avatar
Evan Goldenberg committed
147
148
149
150
151
152
153
154
155
156
157
158
        return array(
            'user' => array(
                'type' => 'hidden',
                'value' => $user,
            ),
            'select' => array(
                'type' => 'radio',
                'title' => get_string('select', 'blocktype.file/gallery'),
                'options' => $select_options,
                'defaultvalue' => (isset($configdata['select'])) ? $configdata['select'] : 0,
                'separator' => '<br>',
            ),
159
160
            'images' => $imageselector,
            'folder' => $folderselector,
Evan Goldenberg's avatar
Evan Goldenberg committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
            'style' => array(
                'type' => 'radio',
                'title' => get_string('style', 'blocktype.file/gallery'),
                'options' => $style_options,
                'defaultvalue' => (isset($configdata['style'])) ? $configdata['style'] : 0,
                'separator' => '<br>',
            ),
            'width' => array(
                'type' => 'text',
                'title' => get_string('width', 'blocktype.file/gallery'),
                'size' => 3,
                'description' => get_string('widthdescription', 'blocktype.file/gallery'),
                'rules' => array(
                    'minvalue' => 16,
                    'maxvalue' => get_config('imagemaxwidth'),
                ),
                'defaultvalue' => (isset($configdata['width'])) ? $configdata['width'] : '75',
            ),
        );
    }

    public static function instance_config_save($values) {
        if ($values['select'] == 0) {
184
185
186
187
188
189
            $values['artefactid'] = $values['folder'];
            unset($values['artefactids']);
        }
        else if ($values['select'] == 1) {
            $values['artefactids'] = $values['images'];
            unset($values['artefactid']);
Evan Goldenberg's avatar
Evan Goldenberg committed
190
        }
191
192
        unset($values['folder']);
        unset($values['images']);
Evan Goldenberg's avatar
Evan Goldenberg committed
193
194
195
        return $values;
    }

196
    public static function imageselector(&$instance, $default=array(), $class=null) {
Evan Goldenberg's avatar
Evan Goldenberg committed
197
        $element = ArtefactTypeFileBase::blockconfig_filebrowser_element($instance, $default);
198
199
200
201
202
        $element['title'] = get_string('Images', 'artefact.file');
        $element['name'] = 'images';
        if ($class) {
            $element['class'] = $class;
        }
Evan Goldenberg's avatar
Evan Goldenberg committed
203
204
205
206
207
208
209
        $element['config']['selectone'] = false;
        $element['filters'] = array(
            'artefacttype'    => array('image'),
        );
        return $element;
    }

210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
    public static function folderselector(&$instance, $default=array(), $class=null) {
        $element = ArtefactTypeFileBase::blockconfig_filebrowser_element($instance, $default);
        $element['title'] = get_string('folder', 'artefact.file');
        $element['name'] = 'folder';
        if ($class) {
            $element['class'] = $class;
        }
        $element['config']['upload'] = false;
        $element['config']['selectone'] = true;
        $element['config']['selectfolders'] = true;
        $element['filters'] = array(
            'artefacttype'    => array('folder'),
        );
        return $element;
    }

Evan Goldenberg's avatar
Evan Goldenberg committed
226
227
228
229
230
231
232
    public static function artefactchooser_element($default=null) {
    }

    public static function default_copy_type() {
        return 'full';
    }
}