lib.php 10.6 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 PluginBlocktypeFolder extends MaharaCoreBlocktype {
15
16
17
18
19

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

20
    /**
Aaron Wells's avatar
Aaron Wells committed
21
     * Optional method. If exists, allows this class to decide the title for
22
23
24
25
26
27
     * all blockinstances of this type
     */
    public static function get_instance_title(BlockInstance $bi) {
        $configdata = $bi->get('configdata');

        if (!empty($configdata['artefactid'])) {
28
            return $bi->get_artefact_instance($configdata['artefactid'])->get('title');
29
30
31
32
        }
        return '';
    }

33
34
35
36
37
    public static function get_description() {
        return get_string('description', 'blocktype.file/folder');
    }

    public static function get_categories() {
38
        return array('fileimagevideo' => 4000);
39
40
    }

41
42
43
44
    public static function get_viewtypes() {
        return array('dashboard', 'portfolio', 'profile');
    }

45
46
47
48
49
50
51
52
53
54
    public static function render_instance_export(BlockInstance $instance, $editing=false, $versioning=false, $exporting=null) {
        if ($exporting != 'pdf') {
            return self::render_instance($instance, $editing, $versioning);
        }
        // The exporting for pdf
        require_once(get_config('docroot') . 'artefact/lib.php');
        $configdata = $instance->get('configdata');
        $configdata['viewid'] = $instance->get('view');
        $configdata['simpledisplay'] = true;
        $configdata['pdfexport'] = true; // pass down flag that we are in pdf export
55
        $configdata['pdfexportfiledir'] = 'export_info/files/';
56
57
58
59
60
61
62
63
64
65
66
67
        list($result, $commentcount, $comments) = self::render_instance_data($instance, $editing, $versioning, $configdata);

        $smarty = smarty_core();
        if (isset($configdata['artefactid'])) {
            $smarty->assign('commentcount', $commentcount);
            $smarty->assign('comments', $comments);
        }
        $smarty = smarty_core();
        $smarty->assign('html', $result);
        return $smarty->fetch('blocktype:folder:folder.tpl');
    }

68
    public static function render_instance(BlockInstance $instance, $editing=false, $versioning=false) {
69
70
71
        require_once(get_config('docroot') . 'artefact/lib.php');
        $configdata = $instance->get('configdata');
        $configdata['viewid'] = $instance->get('view');
72
        $configdata['simpledisplay'] = true;
73
        $configdata['blockid'] = $instance->get('id');
Lisa Seeto's avatar
Lisa Seeto committed
74
        $configdata['editing'] = $editing;
75
        list($result, $commentcount, $comments) = self::render_instance_data($instance, $editing, $versioning, $configdata);
76

77
78
79
80
81
82
83
84
85
86
        $smarty = smarty_core();
        if (isset($configdata['artefactid'])) {
            $smarty->assign('commentcount', $commentcount);
            $smarty->assign('comments', $comments);
        }
        $smarty->assign('html', $result);
        return $smarty->fetch('blocktype:folder:folder.tpl');
    }

    private static function render_instance_data(BlockInstance $instance, $editing=false, $versioning=false, $configdata=array()) {
Aaron Wells's avatar
Aaron Wells committed
87
        // This can be either an image or profileicon. They both implement
88
89
        // render_self
        $result = '';
90
91
        $commentcount = 0;
        $comments = null;
92
93
94
95
        $artefactid = isset($configdata['artefactid']) ? $configdata['artefactid'] : null;
        if ($artefactid) {
            $artefact = $instance->get_artefact_instance($artefactid);
            $result = $artefact->render_self($configdata);
96
97
            $result = $result['html'];

98
99
100
            require_once(get_config('docroot') . 'artefact/comment/lib.php');
            require_once(get_config('docroot') . 'lib/view.php');
            $view = new View($configdata['viewid']);
101
            list($commentcount, $comments) = ArtefactTypeComment::get_artefact_comments_for_view($artefact, $view, $instance->get('id'), true, $editing, $versioning);
102
        }
103
        return array($result, $commentcount, $comments);
104
105
    }

106
107
108
109
110
111
112
113
114
115
116
117
    public static function has_config() {
        return true;
    }

    public static function get_config_options() {
        $elements = array();
        $elements['foldersettings'] = array(
            'type' => 'fieldset',
            'legend' => get_string('foldersettings', 'blocktype.file/folder'),
            'elements' => array(
                'sortorder' => array(
                    'type'         => 'select',
118
                    'title'    => get_string('defaultsortorder', 'blocktype.file/folder'),
119
120
121
122
123
124
125
126
                    'defaultvalue' => get_config_plugin('blocktype', 'folder', 'sortorder'),
                    'options'      => array(
                        'asc' => get_string('ascending'),
                        'desc' => get_string('descending'),
                    )
                )
            ),
        );
127
128
129
130
131
        $elements['downloadfolderzip'] = array(
            'type' => 'fieldset',
            'legend' => get_string('zipdownloadheading', 'artefact.file'),
            'elements' => array(
                'folderdownloadzip' => array(
132
                    'type' => 'switchbox',
133
                    'title' => get_string('downloadfolderzip', 'artefact.file'),
134
                    'description' => get_string('downloadfolderzipdescription3', 'artefact.file'),
135
136
137
138
                    'defaultvalue' => get_config_plugin('blocktype', 'folder', 'folderdownloadzip'),
                ),
            ),
        );
139
140
141
142
143
        return array(
            'elements' => $elements,
        );
    }

Son Nguyen's avatar
Son Nguyen committed
144
    public static function save_config_options(Pieform $form, $values) {
145
        set_config_plugin('blocktype', 'folder', 'sortorder', $values['sortorder']);
146
        set_config_plugin('blocktype', 'folder', 'folderdownloadzip', $values['folderdownloadzip']);
147
148
149
150
151
152
153
154
    }

    public static function postinst($prevversion) {
        if ($prevversion < 2013120900) {
            set_config_plugin('blocktype', 'folder', 'sortorder', 'asc');
        }
    }

155
    public static function has_instance_config(BlockInstance $instance) {
156
157
158
        return true;
    }

Son Nguyen's avatar
Son Nguyen committed
159
    public static function instance_config_form(BlockInstance $instance) {
160
        $configdata = $instance->get('configdata');
161
162
        safe_require('artefact', 'file');
        $instance->set('artefactplugin', 'file');
163
        $elements = array(
164
165
166
167
168
            'artefactfieldset' => array(
                'type'         => 'fieldset',
                'collapsible'  => true,
                'collapsed'    => true,
                'legend'       => get_string('Folders', 'artefact.file'),
169
                'class'        => 'last select-file with-formgroup',
170
171
172
173
                'elements'     => array(
                    'artefactid' => self::filebrowser_element($instance, (isset($configdata['artefactid'])) ? array($configdata['artefactid']) : null),
                )
            ),
174
175
            'sortorder' => array(
                'type' => 'select',
176
                'title' => get_string('sortorder'),
177
                'defaultvalue' => isset($configdata['sortorder']) ? $configdata['sortorder'] : get_config_plugin('blocktype', 'folder', 'sortorder'),
178
179
                'options' => array('asc' => get_string('ascending'), 'desc' => get_string('descending')),
            ),
180
        );
181
        if (get_config_plugin('blocktype', 'folder', 'folderdownloadzip')) {
182
            $elements['folderdownloadzip'] = array(
183
184
                'type' => 'switchbox',
                'title' => get_string('downloadfolderzipblock', 'artefact.file'),
185
                'description' => get_string('downloadfolderzipdescriptionblock', 'artefact.file'),
186
                'defaultvalue' => (get_config_plugin('blocktype', 'folder', 'folderdownloadzip') ? (isset($configdata['folderdownloadzip']) ? $configdata['folderdownloadzip'] : 0) : 0),
187
188
            );
        }
189
        return $elements;
190
191
    }

192
    public static function artefactchooser_element($default=null) {
193
        return array(
194
195
            'name'  => 'artefactid',
            'type'  => 'artefactchooser',
196
            'title' => get_string('folder', 'artefact.file'),
197
198
199
200
201
202
203
204
205
            'defaultvalue' => $default,
            'blocktype' => 'folder',
            'limit' => 10,
            'artefacttypes' => array('folder'),
            'template' => 'artefact:file:artefactchooser-element.tpl',
        );
    }

    /**
Aaron Wells's avatar
Aaron Wells committed
206
     * Optional method. If specified, allows the blocktype class to munge the
207
208
209
     * artefactchooser element data before it's templated
     */
    public static function artefactchooser_get_element_data($artefact) {
210
        $folderdata = ArtefactTypeFileBase::artefactchooser_folder_data($artefact);
211
212
213

        $artefact->icon = call_static_method(generate_artefact_class_name($artefact->artefacttype), 'get_icon', array('id' => $artefact->id));
        $artefact->hovertitle = $artefact->description;
214
215

        $path = $artefact->parent ? ArtefactTypeFileBase::get_full_path($artefact->parent, $folderdata->data) : '';
216
        $artefact->description = str_shorten_text($folderdata->ownername . $path . $artefact->title, 30);
217
218
219
220
221

        return $artefact;
    }

    public static function artefactchooser_get_sort_order() {
222
        return array(array('fieldname' => 'parent'), array('fieldname' => 'title'));
223
224
    }

225
226
227
228
    public static function filebrowser_element(&$instance, $default=array()) {
        $element = ArtefactTypeFileBase::blockconfig_filebrowser_element($instance, $default);
        $element['title'] = get_string('file', 'artefact.file');
        $element['name'] = 'artefactid';
229
        $element['config']['upload'] = false;
230
        $element['config']['selectone'] = true;
231
        $element['config']['selectmodal'] = true;
232
233
234
235
236
237
238
        $element['config']['selectfolders'] = true;
        $element['filters'] = array(
            'artefacttype'    => array('folder'),
        );
        return $element;
    }

239
    public static function default_copy_type(BlockInstance $instance, View $view) {
240
241
242
        return 'full';
    }

243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
    public static function get_current_artefacts(BlockInstance $instance) {

        $configdata = $instance->get('configdata');
        $artefacts = array();
        if (isset($configdata['artefactid'])) {
            $folder = $instance->get_artefact_instance($configdata['artefactid']);
            if ($files = $folder->get_children_instances()) {
                foreach ($files as $file) {
                    $artefacts[] = $file->get('id');
                }
            }
            $artefacts = array_unique($artefacts);
        }
        return $artefacts;
    }
258
}