lib.php 9.05 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

    public static function single_only() {
        return false;
    }

Lisa Seeto's avatar
Lisa Seeto committed
20
21
22
23
    public static function single_artefact_per_block() {
        return true;
    }

24
25
26
27
28
29
30
31
32
    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() {
33
        return array('fileimagevideo' => 8000);
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
61
    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') . 'lib/view.php');
        $configdata = $instance->get('configdata'); // this will make sure to unserialize it for us
        $configdata['viewid'] = $instance->get('view');
        $view = new View($configdata['viewid']);
        $artefactid = isset($configdata['artefactid']) ? $configdata['artefactid'] : null;
        $html = '';
        if ($artefactid) {
            $artefact = $instance->get_artefact_instance($configdata['artefactid']);
            if (!file_exists($artefact->get_path())) {
                return '';
            }
            $urlbase = get_config('wwwroot');
            $url = $urlbase . 'artefact/file/download.php?file=' . $artefactid . '&view=' . $view->get('id');
            $description = $artefact->get('description');
            if ($description) {
                $html .= '<div class="card-body">' . $description . '</div>';
            }
            $html .= '<div class="text-midtone">' . get_string('notrendertopdf', 'artefact.file');
            $html .= '<br>' . get_string('notrendertopdffiles', 'artefact.file', 1);
            // We need to add an <a> link so that the HTML export() sub-task makes a copy of the artefct for the export 'files/' directory
            // We then override the link in the PDF pdf_view_export_data() function.
62
            $html .= '<a href="' . $url . '">export_info/files/' . $artefact->get('id') . '_' . $artefact->get('title') . '</a>';
63
64
65
66
67
            $html .= '</div>';
        }
        return $html;
    }

68
    public static function render_instance(BlockInstance $instance, $editing=false, $versioning=false) {
69
        global $USER;
70
        require_once(get_config('docroot') . 'lib/view.php');
71
72
        $configdata = $instance->get('configdata'); // this will make sure to unserialize it for us
        $configdata['viewid'] = $instance->get('view');
73
74
        $view = new View($configdata['viewid']);
        $group = $view->get('group');
75

76
77
78
79
        $result = '';
        $artefactid = isset($configdata['artefactid']) ? $configdata['artefactid'] : null;
        if ($artefactid) {
            $artefact = $instance->get_artefact_instance($configdata['artefactid']);
80

81
            if (!file_exists($artefact->get_path())) {
82
83
84
                return '';
            }

85
86
87
            $urlbase = get_config('wwwroot');
            // edit view doesn't use subdomains, neither do groups
            if (get_config('cleanurls') && get_config('cleanurlusersubdomains') && !$editing && empty($group)) {
88
89
90
91
92
                $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) . '/';
93
94
                }
            }
95
96
97
98
99
100
101
102
103
104
105
106
107
108
            // 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';
                    }
                }
            }
109
110
111
            else if ($language == 'en') {
                $language = 'en-GB';
            }
112
            $result = '<iframe allowfullscreen src="' . $urlbase . 'artefact/file/blocktype/pdf/viewer.php?editing=' . $editing . '&ingroup=' . !empty($group) . '&file=' . $artefactid . '&lang=' . $language . '&view=' . $instance->get('view')
113
                 . ($versioning ? '&versioning=true' : '')
114
                 . '" class="pdfiframe"></iframe>';
115

116
            require_once(get_config('docroot') . 'artefact/comment/lib.php');
117
118
            require_once(get_config('docroot') . 'lib/view.php');
            $view = new View($configdata['viewid']);
119
            list($commentcount, $comments) = ArtefactTypeComment::get_artefact_comments_for_view($artefact, $view, $instance->get('id'), true, $editing, $versioning);
Lisa Seeto's avatar
Lisa Seeto committed
120

121
122
123
        }
        $smarty = smarty_core();
        if ($artefactid) {
Lisa Seeto's avatar
Lisa Seeto committed
124
125
126
            $smarty->assign('artefactid', $artefactid);
            $artefact = $instance->get_artefact_instance($configdata['artefactid']);
            $smarty->assign('allowcomments', $artefact->get('allowcomments'));
Lisa Seeto's avatar
Lisa Seeto committed
127
128
129
            if ($commentcount) {
                $smarty->assign('commentcount', $commentcount);
            }
130
131
        }
        $smarty->assign('html', $result);
Lisa Seeto's avatar
Lisa Seeto committed
132
133
        $smarty->assign('editing', $editing);
        $smarty->assign('blockid', $instance->get('id'));
134
        return $smarty->fetch('blocktype:pdf:pdfrender.tpl');
135
136
    }

137
    public static function has_instance_config(BlockInstance $instance) {
138
139
140
        return true;
    }

Son Nguyen's avatar
Son Nguyen committed
141
    public static function instance_config_form(BlockInstance $instance) {
142
143
144
        $configdata = $instance->get('configdata');
        safe_require('artefact', 'file');
        $instance->set('artefactplugin', 'file');
145
        $filebrowser = self::filebrowser_element($instance, (isset($configdata['artefactid'])) ? array($configdata['artefactid']) : null);
146
        return array(
147
148
149
150
151
            'artefactfieldset' => array(
                'type'         => 'fieldset',
                'collapsible'  => true,
                'collapsed'    => true,
                'legend'       => get_string('file', 'artefact.file'),
152
                'class'        => 'last select-file with-formgroup',
153
                'elements'     => array(
154
155
156
157
158
                    'pdfwarning' => array(
                        'type' => 'html',
                        'value' => get_string('pdfwarning', 'blocktype.file/pdf'),
                        'class' => 'alert alert-info',
                    ),
159
160
161
                    'artefactid' => $filebrowser
                )
            ),
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
        );
    }

    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';
177
        $element['accept'] = 'application/pdf';
178
        $element['config']['selectone'] = true;
179
        $element['config']['selectmodal'] = true;
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
        $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';
    }

}