lib.php 6.47 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 PluginBlocktypeFiledownload extends MaharaCoreBlocktype {
15 16 17 18 19 20 21 22 23 24

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

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

    public static function get_categories() {
25
        return array('fileimagevideo' => 3000);
26 27
    }

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    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
        $files = self::render_instance_data($instance, $editing, $versioning);

        $smarty = smarty_core();
        $smarty->assign('viewid', $instance->get('view'));
        $smarty->assign('blockid', $instance->get('id'));
        $smarty->assign('files', $files);
        $smarty->assign('editing', $editing);
        return $smarty->fetch('blocktype:filedownload:filedownload_pdfexport.tpl');
    }

43
    public static function render_instance(BlockInstance $instance, $editing=false, $versioning=false) {
44 45 46 47 48 49 50 51 52 53 54
        $files = self::render_instance_data($instance, $editing, $versioning);

        $smarty = smarty_core();
        $smarty->assign('viewid', $instance->get('view'));
        $smarty->assign('blockid', $instance->get('id'));
        $smarty->assign('files', $files);
        $smarty->assign('editing', $editing);
        return $smarty->fetch('blocktype:filedownload:filedownload.tpl');
    }

    private static function render_instance_data(BlockInstance $instance, $editing=false, $versioning=false) {
55
        require_once(get_config('docroot') . 'artefact/lib.php');
56 57
        require_once(get_config('docroot') . 'artefact/comment/lib.php');

58 59
        $configdata = $instance->get('configdata');

60 61 62 63
        $viewid = $instance->get('view');
        $wwwroot = get_config('wwwroot');
        $files = array();

64 65
        if (isset($configdata['artefactids']) && is_array($configdata['artefactids'])) {
            foreach ($configdata['artefactids'] as $artefactid) {
66 67 68 69 70 71
                try {
                    $artefact = $instance->get_artefact_instance($artefactid);
                }
                catch (ArtefactNotFoundException $e) {
                    continue;
                }
72 73
                $view = new View($viewid);
                list($commentcount, $comments) = ArtefactTypeComment::get_artefact_comments_for_view($artefact, $view, $instance->get('id'), true, $editing, $versioning);
74

75 76 77 78 79 80
                $file = array(
                    'id' => $artefactid,
                    'title' => $artefact->get('title'),
                    'description' => $artefact->get('description'),
                    'size' => $artefact->get('size'),
                    'ctime' => $artefact->get('ctime'),
81
                    'artefacttype' => $artefact->get('artefacttype'),
82 83 84 85 86 87
                    'iconsrc' => call_static_method(
                        generate_artefact_class_name($artefact->get('artefacttype')),
                        'get_icon',
                        array('id' => $artefactid, 'viewid' => $viewid)
                    ),
                    'downloadurl' => $wwwroot,
88
                    'commentcount' => $commentcount,
Lisa Seeto's avatar
Lisa Seeto committed
89
                    'allowcomments' => $artefact->get('allowcomments'),
90 91
                );

92
                if ($artefact instanceof ArtefactTypeProfileIcon) {
93
                    $file['downloadurl'] .= 'thumb.php?type=profileiconbyid&id=' . $artefactid . '&view=' . $viewid;
94 95
                }
                else if ($artefact instanceof ArtefactTypeFile) {
96
                    $file['downloadurl'] .= 'artefact/file/download.php?file=' . $artefactid . '&view=' . $viewid;
97
                }
98
                $file['is_image'] = ($artefact instanceof ArtefactTypeImage) ? true : false;
99
                $files[] = $file;
100 101
            }
        }
102
        return $files;
103 104 105 106 107 108
    }

    public static function has_instance_config() {
        return true;
    }

Son Nguyen's avatar
Son Nguyen committed
109
    public static function instance_config_form(BlockInstance $instance) {
110
        $configdata = $instance->get('configdata');
111 112
        safe_require('artefact', 'file');
        $instance->set('artefactplugin', 'file');
113
        return array(
114 115 116 117 118
            'artefactfieldset' => array(
                'type'         => 'fieldset',
                'collapsible'  => true,
                'collapsed'    => true,
                'legend'       => get_string('Files', 'blocktype.file/filedownload'),
119
                'class'        => 'last with-formgroup',
120 121 122 123
                'elements'     => array(
                    'artefactid' => self::filebrowser_element($instance, (isset($configdata['artefactids'])) ? $configdata['artefactids'] : null)
                )
            )
124 125 126
        );
    }

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

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

150 151
    public static function filebrowser_element(&$instance, $default=array()) {
        $element = ArtefactTypeFileBase::blockconfig_filebrowser_element($instance, $default);
152
        $element['title'] = get_string('Files', 'blocktype.file/filedownload');
153
        $element['name'] = 'artefactids';
154
        $element['config']['selectone'] = false;
155
        $element['config']['selectmodal'] = true;
156 157 158
        return $element;
    }

159 160 161 162
    public static function default_copy_type() {
        return 'full';
    }

163
}