Gitlab CSE Unil

renderer.php 15.6 KB
Newer Older
M. Chardon's avatar
init  
M. Chardon committed
1
<?php
2

M. Chardon's avatar
init  
M. Chardon committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
62
63
64
65
66
67
68
69
70
71
72
73
74
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * This file contains the definition for the library class for edit PDF renderer.
 *
 * @package   assignfeedback_editpdfplus
 * @copyright 2012 Davo Smith
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
defined('MOODLE_INTERNAL') || die();

/**
 * A custom renderer class that extends the plugin_renderer_base and is used by the editpdf feedback plugin.
 *
 * @package assignfeedback_editpdfplus
 * @copyright 2013 Davo Smith
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class assignfeedback_editpdfplus_renderer extends plugin_renderer_base {

    /**
     * Return the PDF button shortcut.
     *
     * @param string $name the name of a specific button.
     * @return string the specific shortcut.
     */
    private function get_shortcut($name) {

        $shortcuts = array('navigate-previous-button' => 'j',
            'navigate-page-select' => 'k',
            'navigate-next-button' => 'l',
            'searchcomments' => 'h',
            'comment' => 'z',
            'commentcolour' => 'x',
            'select' => 'c',
            'drag' => 'd',
            'pen' => 'y',
            'line' => 'u',
            'rectangle' => 'i',
            'oval' => 'o',
            'highlight' => 'p',
            'annotationcolour' => 'r',
            'stamp' => 'n',
            'currentstamp' => 'm');


        // Return the shortcut.
        return $shortcuts[$name];
    }

    /**
     * Render a single colour button.
     *
     * @param string $icon - The key for the icon
     * @param string $tool - The key for the lang string.
     * @param string $accesskey Optional - The access key for the button.
     * @param bool $disabled Optional - Is this button disabled.
     * @return string
     */
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    private function render_toolbar_button($icon, $tool, assignfeedback_editpdfplus\tool $fulltool = null, $accesskey = null, $disabled = false) {

        if (!$fulltool) {
            // Build button alt text.
            $alttext = new stdClass();
            $alttext->tool = $tool;
            if (!empty($accesskey)) {
                $alttext->shortcut = '(Alt/Shift-Alt/Ctrl-Option + ' . $accesskey . ')';
            } else {
                $alttext->shortcut = '';
            }
            $iconalt = get_string('toolbarbutton', 'assignfeedback_editpdfplus', $alttext);

            $iconhtml = $this->pix_icon($icon, $iconalt, 'assignfeedback_editpdfplus');
            $iconparams = array('data-tool' => $tool, 'class' => $tool . 'button');
            if ($disabled) {
                $iconparams['disabled'] = 'true';
            }
M. Chardon's avatar
init  
M. Chardon committed
93
        } else {
94
95
            $iconalt = $fulltool->label;
            $iconhtml = $fulltool->label;
M. Chardon's avatar
M. Chardon committed
96
97
98
99
100
101
            if ($fulltool->type == 4) {
                $iconhtml = '| ' . $fulltool->label . ' |';
            }
            if ($fulltool->type == 5) {
                $iconhtml = '| ' . $fulltool->label;
            }
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
            $datatool = '';
            $class = '';
            switch ($fulltool->type) {
                case 1:
                    $datatool = 'highlightplus';
                    $class = 'highlightplus';
                    break;
                case 2:
                    $datatool = 'lineplus';
                    $class = 'lineplus';
                    break;
                case 3:
                    $datatool = 'stampplus';
                    $class = 'stampplus';
                    break;
                case 4:
                    $datatool = 'frame';
                    $class = 'frame';
                    break;
                case 5:
                    $datatool = 'verticalline';
                    $class = 'verticalline';
                    break;
                case 6:
                    $datatool = 'stampcomment';
                    $class = 'stampcomment';
                    break;
                case 7:
                    $datatool = 'commentplus';
                    $class = 'commentplus';
                    break;
                default:
                    break;
            }
136
            $iconparams = array('data-tool' => $datatool, 'class' => $class . 'button costumtoolbarbutton', 'id' => 'ctbutton' . $fulltool->id);
M. Chardon's avatar
init  
M. Chardon committed
137
138
139
140
141
142
143
144
        }

        if (!empty($accesskey)) {
            $iconparams['accesskey'] = $accesskey;
        }
        return html_writer::tag('button', $iconhtml, $iconparams);
    }

M. Chardon's avatar
M. Chardon committed
145
146
147
148
149
150
    private function render_toolbar_axis(assignfeedback_editpdfplus\axis $axis) {
        $iconhtml = $axis->label;
        $iconparams = array('type' => 'checkbox', 'class' => 'axis', 'id' => 'ctaxis' . $axis->id, 'value' => $axis->id);
        return html_writer::tag('input', $iconhtml, $iconparams);
    }

M. Chardon's avatar
init  
M. Chardon committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    /**
     * Render the editpdf widget in the grading form.
     *
     * @param assignfeedback_editpdfplus_widget $widget - Renderable widget containing assignment, user and attempt number.
     * @return string
     */
    public function render_assignfeedback_editpdfplus_widget(assignfeedback_editpdfplus_widget $widget) {
        global $CFG;

        $html = '';

        $html .= html_writer::div(get_string('jsrequired', 'assignfeedback_editpdfplus'), 'hiddenifjs');
        $linkid = html_writer::random_id();
        if ($widget->readonly) {
165
            $launcheditorlink = html_writer::tag('a', get_string('viewfeedbackonline', 'assignfeedback_editpdfplus'), array('id' => $linkid, 'class' => 'btn', 'href' => '#'));
M. Chardon's avatar
init  
M. Chardon committed
166
        } else {
167
            $launcheditorlink = html_writer::tag('a', get_string('launcheditor', 'assignfeedback_editpdfplus'), array('id' => $linkid, 'class' => 'btn', 'href' => '#'));
M. Chardon's avatar
init  
M. Chardon committed
168
169
170
171
172
173
174
175
176
        }
        $links = $launcheditorlink;
        $html .= '<input type="hidden" name="assignfeedback_editpdfplus_haschanges" value="false"/>';

        $html .= html_writer::div($links, 'visibleifjs');
        $header = get_string('pluginname', 'assignfeedback_editpdfplus');
        $body = '';
        // Create the page navigation.
        $navigation1 = '';
M. Chardon's avatar
M. Chardon committed
177
        //$navigation2 = '';
M. Chardon's avatar
init  
M. Chardon committed
178
179
180
181
182
183
184
185
186
187
188
189
        //$divToolbar;
        // Pick the correct arrow icons for right to left mode.
        if (right_to_left()) {
            $nav_prev = 'nav_next';
            $nav_next = 'nav_prev';
        } else {
            $nav_prev = 'nav_prev';
            $nav_next = 'nav_next';
        }

        $iconalt = get_string('navigateprevious', 'assignfeedback_editpdfplus');
        $iconhtml = $this->pix_icon($nav_prev, $iconalt, 'assignfeedback_editpdfplus');
190
191
192
193
194
        $navigation1 .= html_writer::tag('button', $iconhtml, array('disabled' => 'true',
                    'class' => 'navigate-previous-button', 'accesskey' => $this->get_shortcut('navigate-previous-button')));
        $navigation1 .= html_writer::tag('select', null, array('disabled' => 'true',
                    'aria-label' => get_string('gotopage', 'assignfeedback_editpdfplus'), 'class' => 'navigate-page-select',
                    'accesskey' => $this->get_shortcut('navigate-page-select')));
M. Chardon's avatar
init  
M. Chardon committed
195
196
        $iconalt = get_string('navigatenext', 'assignfeedback_editpdfplus');
        $iconhtml = $this->pix_icon($nav_next, $iconalt, 'assignfeedback_editpdfplus');
197
198
        $navigation1 .= html_writer::tag('button', $iconhtml, array('disabled' => 'true',
                    'class' => 'navigate-next-button', 'accesskey' => $this->get_shortcut('navigate-next-button')));
M. Chardon's avatar
init  
M. Chardon committed
199

200
        $divnavigation1 = html_writer::div($navigation1, 'navigation', array('role' => 'navigation'));
M. Chardon's avatar
init  
M. Chardon committed
201

M. Chardon's avatar
M. Chardon committed
202
203
        //$navigation2 .= $this->render_toolbar_button('comment_search', 'searchcomments', null, $this->get_shortcut('searchcomments'));
        //$divnavigation2 = html_writer::div($navigation2, 'navigation-search', array('role' => 'navigation'));
M. Chardon's avatar
init  
M. Chardon committed
204

205
206
        $toolbar001 = '';
        $toolbar002 = '';
M. Chardon's avatar
M. Chardon committed
207
        $toolbarCostumdiv = '';
M. Chardon's avatar
M. Chardon committed
208
        $toolbaraxis = '';
M. Chardon's avatar
init  
M. Chardon committed
209
        $clearfix = html_writer::div('', 'clearfix');
M. Chardon's avatar
M. Chardon committed
210

M. Chardon's avatar
init  
M. Chardon committed
211
212
        if (!$widget->readonly) {

213
            /** Toolbar n°0 : basic tools * */
M. Chardon's avatar
init  
M. Chardon committed
214
            // Select Tool.
215
216
217
            $toolbar001 .= $this->render_toolbar_button('drag', 'drag', null, $this->get_shortcut('drag'));
            $toolbar001 .= $this->render_toolbar_button('select', 'select', null, $this->get_shortcut('select'));
            $toolbar001 = html_writer::div($toolbar001, 'toolbar', array('role' => 'toolbar'));
M. Chardon's avatar
init  
M. Chardon committed
218
219

            // Other Tools.
220
221
222
223
224
225
226
227
228
229
230
231
            $toolbar002 .= $this->render_toolbar_button('pen', 'pen', null, $this->get_shortcut('pen'));
            $toolbar002 .= $this->render_toolbar_button('line', 'line', null, $this->get_shortcut('line'));
            $toolbar002 .= $this->render_toolbar_button('rectangle', 'rectangle', null, $this->get_shortcut('rectangle'));
            $toolbar002 .= $this->render_toolbar_button('oval', 'oval', null, $this->get_shortcut('oval'));
            $toolbar002 .= $this->render_toolbar_button('highlight', 'highlight', null, $this->get_shortcut('highlight'));
            $toolbar002 .= $this->render_toolbar_button('background_colour_clear', 'annotationcolour', null, $this->get_shortcut('annotationcolour'));
            $toolbar002 = html_writer::div($toolbar002, 'toolbar', array('role' => 'toolbar'));

            /** Costum toolbars * */
            $toolbarCostum = array();
            $axis = array();
            foreach ($widget->toolbars as $toolbar) {
M. Chardon's avatar
M. Chardon committed
232
233
234
                $axis[] = $toolbar['label'];
                $toolbartmp = '';
                foreach ($toolbar['tool'] as $tool) {
M. Chardon's avatar
M. Chardon committed
235
236
237
                    if ($tool->enabled) {
                        $toolbartmp .= $this->render_toolbar_button('', '', $tool);
                    }
238
                }
M. Chardon's avatar
M. Chardon committed
239
                $toolbarCostum[] = html_writer::div($toolbartmp, 'toolbar customtoolbar', array('role' => 'toolbar', 'id' => 'toolbaraxis' . $tool->axis, 'style' => 'display:none;'));
240
            }
241
242
243
244
245
246
247
248
            usort($axis, function($a, $b) {
                $al = substr($a, 4, 5);
                $bl = substr($b, 4, 5);
                if ($al == $bl) {
                    return 0;
                }
                return ($al > $bl) ? +1 : -1;
            });
249
250
251
252
253
            $axischoice = html_writer::div(html_writer::select($axis, 'axisselection', 0, FALSE), 'toolbar ', array('role' => 'toolbar'));
            foreach ($toolbarCostum as $toolbarCostumUnit) {
                $toolbarCostumdiv.= $toolbarCostumUnit;
            }
            $toolbarCostumdiv.= $axischoice;
M. Chardon's avatar
M. Chardon committed
254
        } else {
M. Chardon's avatar
M. Chardon committed
255
            $toolbaraxis = "<div class='navigation' style='padding-left:10px;'><div style='display:inline;margin-right:5px;text-align:left;'>";
M. Chardon's avatar
M. Chardon committed
256
257
258
            $axis = $widget->axis;
            foreach ($axis as $ax) {
                $toolbaraxis .= $this->render_toolbar_axis($ax);
M. Chardon's avatar
M. Chardon committed
259
                $toolbaraxis .= "</div><div style='display:inline;margin-left:5px;text-align:left;'>";
M. Chardon's avatar
M. Chardon committed
260
261
            }
            $toolbaraxis .= "</div></div>";
262
263
264
            $questionchoice = html_writer::select([get_string('question_select', 'assignfeedback_editpdfplus'), get_string('question_select_without', 'assignfeedback_editpdfplus'), get_string('question_select_with', 'assignfeedback_editpdfplus')], 'questionselection', 0, FALSE);
            $axischoice = html_writer::select([get_string('statut_select', 'assignfeedback_editpdfplus'), get_string('statut_select_nc', 'assignfeedback_editpdfplus'), get_string('statut_select_ok', 'assignfeedback_editpdfplus'), get_string('statut_select_ko', 'assignfeedback_editpdfplus')], 'statutselection', 0, FALSE);
            $validatebutton = html_writer::tag('button', get_string('send_pdf_update', 'assignfeedback_editpdfplus'), array('class' => 'button', 'id' => 'student_valide_button'));
M. Chardon's avatar
M. Chardon committed
265
266
            $toolbaraxis .= html_writer::div($validatebutton, 'toolbar ', array('role' => 'toolbar'));
            $toolbaraxis .= html_writer::div($axischoice, 'toolbar ', array('role' => 'toolbar'));
267
            $toolbaraxis .= html_writer::div($questionchoice, 'toolbar ', array('role' => 'toolbar'));
M. Chardon's avatar
init  
M. Chardon committed
268
269
270
        }

        // Toobars written in reverse order because they are floated right.
271
272
        $pageheader = html_writer::div($divnavigation1 .
                        $toolbar002 .
M. Chardon's avatar
M. Chardon committed
273
                        $toolbaraxis .
274
275
                        $toolbarCostumdiv .
                        $toolbar001 .
276
                        $clearfix, 'pageheader', array('style' => 'padding:0'));
277

M. Chardon's avatar
init  
M. Chardon committed
278
279
280
281
        $body .= $pageheader;

        // Loading progress bar.
        $progressbar = html_writer::div('', 'bar', array('style' => 'width: 0%'));
282
283
284
285
        $progressbar = html_writer::div($progressbar, 'progress progress-info progress-striped active', array('title' => get_string('loadingeditor', 'assignfeedback_editpdfplus'),
                    'role' => 'progressbar', 'aria-valuenow' => 0, 'aria-valuemin' => 0,
                    'aria-valuemax' => 100));
        $progressbarlabel = html_writer::div(get_string('generatingpdf', 'assignfeedback_editpdfplus'), 'progressbarlabel');
M. Chardon's avatar
init  
M. Chardon committed
286
287
288
289
        $loading = html_writer::div($progressbar . $progressbarlabel, 'loading');

        $canvas = html_writer::div($loading, 'drawingcanvas');
        $canvas = html_writer::div($canvas, 'drawingregion');
290
291
292
        $changesmessage = html_writer::tag('div', get_string('draftchangessaved', 'assignfeedback_editpdfplus'), array(
                    'class' => 'assignfeedback_editpdfplus_unsavedchanges warning label label-info'
        ));
M. Chardon's avatar
init  
M. Chardon committed
293
294
        $changesmessage = html_writer::div($changesmessage, 'unsaved-changes');
        $canvas .= $changesmessage;
M. Chardon's avatar
M. Chardon committed
295

296
297
298
299
300
        $changesmessage2 = html_writer::tag('div', get_string('nodraftchangessaved', 'assignfeedback_editpdfplus'), array(
                    'class' => 'assignfeedback_editpdfplus_unsavedchanges_edit warning label label-info'
        ));
        $changesmessage2 = html_writer::div($changesmessage2, 'unsaved-changes');
        $canvas .= $changesmessage2;
M. Chardon's avatar
init  
M. Chardon committed
301
302
303
304
305

        $body .= $canvas;

        $footer = '';

306
307
308
309
310
311
312
313
314
315
        $editorparams = array(array('header' => $header,
                'body' => $body,
                'footer' => $footer,
                'linkid' => $linkid,
                'assignmentid' => $widget->assignment,
                'userid' => $widget->userid,
                'attemptnumber' => $widget->attemptnumber,
                'stampfiles' => $widget->stampfiles,
                'readonly' => $widget->readonly,
                'pagetotal' => $widget->pagetotal));
M. Chardon's avatar
init  
M. Chardon committed
316

317
        $this->page->requires->yui_module('moodle-assignfeedback_editpdfplus-editor', 'M.assignfeedback_editpdfplus.editor.init', $editorparams);
M. Chardon's avatar
init  
M. Chardon committed
318
319
320

        $this->page->requires->strings_for_js(array(
            'yellow',
321
            'yellowlemon',
M. Chardon's avatar
init  
M. Chardon committed
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
            'white',
            'red',
            'blue',
            'green',
            'black',
            'clear',
            'colourpicker',
            'loadingeditor',
            'pagexofy',
            'deletecomment',
            'addtoquicklist',
            'filter',
            'searchcomments',
            'commentcontextmenu',
            'deleteannotation',
            'stamp',
            'stamppicker',
            'cannotopenpdf',
340
341
342
            'pagenumber',
            'student_statut_nc',
            'student_answer_lib'
343
                ), 'assignfeedback_editpdfplus');
M. Chardon's avatar
init  
M. Chardon committed
344
345
346

        return $html;
    }
347

M. Chardon's avatar
init  
M. Chardon committed
348
}