lib.php 6.44 KB
Newer Older
1
2
3
4
5
6
<?php
/**
 *
 * @package    mahara
 * @subpackage blocktype-pdf
 * @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 PluginBlocktypePdf extends MaharaCoreBlocktype {
15
16
17
18
19
20
21
22
23
24
25
26
27
28

    public static function single_only() {
        return false;
    }

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

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

    public static function get_categories() {
29
        return array('fileimagevideo' => 8000);
30
31
32
    }

    public static function render_instance(BlockInstance $instance, $editing=false) {
33
        global $USER;
34
        require_once(get_config('docroot') . 'lib/view.php');
35
36
        $configdata = $instance->get('configdata'); // this will make sure to unserialize it for us
        $configdata['viewid'] = $instance->get('view');
37
38
        $view = new View($configdata['viewid']);
        $group = $view->get('group');
39

40
41
42
43
        $result = '';
        $artefactid = isset($configdata['artefactid']) ? $configdata['artefactid'] : null;
        if ($artefactid) {
            $artefact = $instance->get_artefact_instance($configdata['artefactid']);
44

45
            if (!file_exists($artefact->get_path())) {
46
47
48
                return '';
            }

49
50
51
            $urlbase = get_config('wwwroot');
            // edit view doesn't use subdomains, neither do groups
            if (get_config('cleanurls') && get_config('cleanurlusersubdomains') && !$editing && empty($group)) {
52
53
54
55
56
                $viewauthor = new User();
                $viewauthor->find_by_id($view->get('owner'));
                $viewauthorurlid = $viewauthor->get('urlid');
                if ($urlallowed = !is_null($viewauthorurlid) && strlen($viewauthorurlid)) {
                    $urlbase = profile_url($viewauthor) . '/';
57
58
                }
            }
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
            // Send the current language to the pdf viewer
            $language = current_language();
            $language = str_replace('_', '-', substr($language, 0, ((substr_count($language, '_') > 0) ? 5 : 2)));
            if ($language != 'en' && !file_exists(get_config('docroot') . 'artefact/file/blocktype/pdf/js/pdfjs/web/locale/' . $language . '/viewer.properties')) {
                // In case the language file exists as a string with both lower and upper case, eg fr_FR we test for this
                $language = substr($language, 0, 2) . '-' . strtoupper(substr($language, 0, 2));
                if (!file_exists(get_config('docroot') . 'artefact/file/blocktype/pdf/js/pdfjs/web/locale/' . $language . '/viewer.properties')) {
                    // In case we fail to find a language of 5 chars, eg pt_BR (Portugese, Brazil) we try the 'parent' pt (Portugese)
                    $language = substr($language, 0, 2);
                    if ($language != 'en' && !file_exists(get_config('docroot') . 'artefact/file/blocktype/pdf/js/pdfjs/web/locale/' . $language . '/viewer.properties')) {
                        $language = 'en-GB';
                    }
                }
            }
            $result = '<iframe src="' . $urlbase . 'artefact/file/blocktype/pdf/viewer.php?editing=' . $editing . '&ingroup=' . !empty($group) . '&file=' . $artefactid . '&lang=' . $language . '&view=' . $instance->get('view')
74
75
                 . '" width="100%" height="500" frameborder="0"></iframe>';

76
            require_once(get_config('docroot') . 'artefact/comment/lib.php');
77
78
79
            require_once(get_config('docroot') . 'lib/view.php');
            $view = new View($configdata['viewid']);
            list($commentcount, $comments) = ArtefactTypeComment::get_artefact_comments_for_view($artefact, $view, $instance->get('id'), true, $editing);
80
81
82
83
84
85
86
87
        }
        $smarty = smarty_core();
        if ($artefactid) {
            $smarty->assign('commentcount', $commentcount);
            $smarty->assign('comments', $comments);
        }
        $smarty->assign('html', $result);
        return $smarty->fetch('blocktype:pdf:pdfrender.tpl');
88
89
90
91
92
93
    }

    public static function has_instance_config() {
        return true;
    }

Son Nguyen's avatar
Son Nguyen committed
94
    public static function instance_config_form(BlockInstance $instance) {
95
96
97
        $configdata = $instance->get('configdata');
        safe_require('artefact', 'file');
        $instance->set('artefactplugin', 'file');
98
        $filebrowser = self::filebrowser_element($instance, (isset($configdata['artefactid'])) ? array($configdata['artefactid']) : null);
99
        return array(
100
101
102
103
104
            'artefactfieldset' => array(
                'type'         => 'fieldset',
                'collapsible'  => true,
                'collapsed'    => true,
                'legend'       => get_string('file', 'artefact.file'),
105
                'class'        => 'last select-file with-formgroup',
106
107
108
109
                'elements'     => array(
                    'artefactid' => $filebrowser
                )
            ),
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
        );
    }

    private static function get_allowed_mimetypes() {
        static $mimetypes = array();
        if (!$mimetypes) {
            $mimetypes = get_column('artefact_file_mime_types', 'mimetype', 'description', 'pdf');
        }
        return $mimetypes;
    }

    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';
125
        $element['accept'] = 'application/pdf';
126
        $element['config']['selectone'] = true;
127
        $element['config']['selectmodal'] = true;
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
        $element['filters'] = array(
            'artefacttype'    => array('file'),
            'filetype'        => self::get_allowed_mimetypes(),
        );
        return $element;
    }

    public static function artefactchooser_element($default=null) {
        return array(
            'name'  => 'artefactid',
            'type'  => 'artefactchooser',
            'title' => get_string('file', 'artefact.file'),
            'defaultvalue' => $default,
            'blocktype' => 'html',
            'limit' => 10,
            'artefacttypes' => array('file'),
            'template' => 'artefact:file:artefactchooser-element.tpl',
        );
    }

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

}