lib.php 8.26 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
    public static function render_instance(BlockInstance $instance, $editing=false) {
42
43
44
        require_once(get_config('docroot') . 'artefact/lib.php');
        $configdata = $instance->get('configdata');
        $configdata['viewid'] = $instance->get('view');
45
        $configdata['simpledisplay'] = true;
46

Aaron Wells's avatar
Aaron Wells committed
47
        // This can be either an image or profileicon. They both implement
48
49
        // render_self
        $result = '';
50
51
52
53
        $artefactid = isset($configdata['artefactid']) ? $configdata['artefactid'] : null;
        if ($artefactid) {
            $artefact = $instance->get_artefact_instance($artefactid);
            $result = $artefact->render_self($configdata);
54
55
            $result = $result['html'];

56
57
58
            require_once(get_config('docroot') . 'artefact/comment/lib.php');
            require_once(get_config('docroot') . 'lib/view.php');
            $view = new View($configdata['viewid']);
59
            list($commentcount, $comments) = ArtefactTypeComment::get_artefact_comments_for_view($artefact, $view, $instance->get('id'), true, $editing);
60
61
62
63
64
65
66
67
        }
        $smarty = smarty_core();
        if ($artefactid) {
            $smarty->assign('commentcount', $commentcount);
            $smarty->assign('comments', $comments);
        }
        $smarty->assign('html', $result);
        return $smarty->fetch('blocktype:folder:folder.tpl');
68
69
    }

70
71
72
73
74
75
76
77
78
79
80
81
    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',
82
                    'title'    => get_string('defaultsortorder', 'blocktype.file/folder'),
83
84
85
86
87
88
89
90
                    'defaultvalue' => get_config_plugin('blocktype', 'folder', 'sortorder'),
                    'options'      => array(
                        'asc' => get_string('ascending'),
                        'desc' => get_string('descending'),
                    )
                )
            ),
        );
91
92
93
94
95
        $elements['downloadfolderzip'] = array(
            'type' => 'fieldset',
            'legend' => get_string('zipdownloadheading', 'artefact.file'),
            'elements' => array(
                'folderdownloadzip' => array(
96
                    'type' => 'switchbox',
97
                    'title' => get_string('downloadfolderzip', 'artefact.file'),
98
                    'description' => get_string('downloadfolderzipdescription2', 'artefact.file'),
99
100
101
102
                    'defaultvalue' => get_config_plugin('blocktype', 'folder', 'folderdownloadzip'),
                ),
            ),
        );
103
        return array(
104
            'class' => 'panel panel-body',
105
106
107
108
            'elements' => $elements,
        );
    }

Son Nguyen's avatar
Son Nguyen committed
109
    public static function save_config_options(Pieform $form, $values) {
110
        set_config_plugin('blocktype', 'folder', 'sortorder', $values['sortorder']);
111
        set_config_plugin('blocktype', 'folder', 'folderdownloadzip', $values['folderdownloadzip']);
112
113
114
115
116
117
118
119
    }

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

120
121
122
123
    public static function has_instance_config() {
        return true;
    }

Son Nguyen's avatar
Son Nguyen committed
124
    public static function instance_config_form(BlockInstance $instance) {
125
        $configdata = $instance->get('configdata');
126
127
        safe_require('artefact', 'file');
        $instance->set('artefactplugin', 'file');
128
        $elements = array(
129
130
131
132
133
            'artefactfieldset' => array(
                'type'         => 'fieldset',
                'collapsible'  => true,
                'collapsed'    => true,
                'legend'       => get_string('Folders', 'artefact.file'),
134
                'class'        => 'last select-file with-formgroup',
135
136
137
138
                'elements'     => array(
                    'artefactid' => self::filebrowser_element($instance, (isset($configdata['artefactid'])) ? array($configdata['artefactid']) : null),
                )
            ),
139
140
            'sortorder' => array(
                'type' => 'select',
141
                'title' => get_string('sortorder'),
142
                'defaultvalue' => isset($configdata['sortorder']) ? $configdata['sortorder'] : get_config_plugin('blocktype', 'folder', 'sortorder'),
143
144
                'options' => array('asc' => get_string('ascending'), 'desc' => get_string('descending')),
            ),
145
        );
146
        if (get_config_plugin('blocktype', 'folder', 'folderdownloadzip')) {
147
            $elements['folderdownloadzip'] = array(
148
149
                'type' => 'switchbox',
                'title' => get_string('downloadfolderzipblock', 'artefact.file'),
150
                'description' => get_string('downloadfolderzipdescriptionblock', 'artefact.file'),
151
                'defaultvalue' => (get_config_plugin('blocktype', 'folder', 'folderdownloadzip') ? (isset($configdata['folderdownloadzip']) ? $configdata['folderdownloadzip'] : 0) : 0),
152
153
            );
        }
154
        return $elements;
155
156
    }

157
    public static function artefactchooser_element($default=null) {
158
        return array(
159
160
            'name'  => 'artefactid',
            'type'  => 'artefactchooser',
161
            'title' => get_string('folder', 'artefact.file'),
162
163
164
165
166
167
168
169
170
            'defaultvalue' => $default,
            'blocktype' => 'folder',
            'limit' => 10,
            'artefacttypes' => array('folder'),
            'template' => 'artefact:file:artefactchooser-element.tpl',
        );
    }

    /**
Aaron Wells's avatar
Aaron Wells committed
171
     * Optional method. If specified, allows the blocktype class to munge the
172
173
174
     * artefactchooser element data before it's templated
     */
    public static function artefactchooser_get_element_data($artefact) {
175
        $folderdata = ArtefactTypeFileBase::artefactchooser_folder_data($artefact);
176
177
178

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

        $path = $artefact->parent ? ArtefactTypeFileBase::get_full_path($artefact->parent, $folderdata->data) : '';
181
        $artefact->description = str_shorten_text($folderdata->ownername . $path . $artefact->title, 30);
182
183
184
185
186

        return $artefact;
    }

    public static function artefactchooser_get_sort_order() {
187
        return array(array('fieldname' => 'parent'), array('fieldname' => 'title'));
188
189
    }

190
191
192
193
    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';
194
        $element['config']['upload'] = false;
195
        $element['config']['selectone'] = true;
196
        $element['config']['selectmodal'] = true;
197
198
199
200
201
202
203
        $element['config']['selectfolders'] = true;
        $element['filters'] = array(
            'artefacttype'    => array('folder'),
        );
        return $element;
    }

204
205
206
207
    public static function default_copy_type() {
        return 'full';
    }

208
}