Gitlab CSE Unil

renderer.php 18.4 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
// 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
M. Chardon's avatar
GPL3    
M. Chardon committed
22
 * @copyright  2016 Université de Lausanne
M. Chardon's avatar
init  
M. Chardon committed
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
 * @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',
            '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
     */
72
73
74
75
76
77
78
79
80
81
82
83
84
    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);

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
            $class = "";
            switch ($tool) {
                case "drag":
                    $class = "fa-hand-paper-o";
                    break;
                case "select":
                    $class = "fa-mouse-pointer";
                    break;
                case "pen":
                    $class = "fa-pencil";
                    break;
                case "line":
                    $class = "fa-minus";
                    break;
                case "rectangle":
                    $class = "fa-square-o";
                    break;
                case "oval":
                    $class = "fa-circle-o";
                    break;
                case "highlight":
                    $class = "fa-paint-brush";
                    break;
                case "annotationcolour":
                    $class = "fa-tint"; //,'style'=>'color:red;'
                    break;
                default:
                    break;
            }
            $iconhtml = html_writer::tag("i", "", array('class' => 'fa ' . $class,
                        'aria-hidden' => 'true'));
            $iconparams = array('data-tool' => $tool, 'class' => $tool . 'button btn btn-default', 'type' => 'button');
117
118
119
            if ($disabled) {
                $iconparams['disabled'] = 'true';
            }
M. Chardon's avatar
init  
M. Chardon committed
120
        } else {
121
122
            $iconalt = $fulltool->label;
            $iconhtml = $fulltool->label;
M. Chardon's avatar
M. Chardon committed
123
124
125
126
127
128
            if ($fulltool->type == 4) {
                $iconhtml = '| ' . $fulltool->label . ' |';
            }
            if ($fulltool->type == 5) {
                $iconhtml = '| ' . $fulltool->label;
            }
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
            $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;
            }
163
164
165
166
            $iconparams = array('data-tool' => $datatool,
                'class' => $class . 'button btn btn-default costumtoolbarbutton',
                'id' => 'ctbutton' . $fulltool->id,
                'type' => 'button');
M. Chardon's avatar
init  
M. Chardon committed
167
168
169
170
171
172
173
174
        }

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

M. Chardon's avatar
M. Chardon committed
175
176
177
178
179
180
    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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
    /**
     * 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) {
195
            $launcheditorlink = html_writer::tag('a', get_string('viewfeedbackonline', 'assignfeedback_editpdfplus'), array('id' => $linkid, 'class' => 'btn', 'href' => '#'));
M. Chardon's avatar
init  
M. Chardon committed
196
        } else {
197
            $launcheditorlink = html_writer::tag('a', get_string('launcheditor', 'assignfeedback_editpdfplus'), array('id' => $linkid, 'class' => 'btn', 'href' => '#'));
M. Chardon's avatar
init  
M. Chardon committed
198
199
200
201
202
203
204
205
206
        }
        $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 = '';
207

M. Chardon's avatar
init  
M. Chardon committed
208
209
210
211
212
213
214
215
216
        // 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';
        }

M. Chardon's avatar
M. Chardon committed
217
       $iconhtmlP = html_writer::tag("i", "", array('class' => 'fa fa-caret-left fa-2x',
218
219
220
221
222
223
                    'aria-hidden' => 'true'));
        $navigation1 .= html_writer::tag('button', $iconhtmlP, array('disabled' => 'true',
                    'class' => 'btn btn-default navigate-previous-button',
                    'type' => 'button',
                    'accesskey' => $this->get_shortcut('navigate-previous-button')));

224
225
226
        $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')));
227
228
229
230
231
232
        $iconhtmlN = html_writer::tag("i", "", array('class' => 'fa fa-caret-right fa-2x',
                    'aria-hidden' => 'true'));
        $navigation1 .= html_writer::tag('button', $iconhtmlN, array('disabled' => 'true',
                    'class' => 'btn btn-default navigate-next-button',
                    'type' => 'button',
                    'accesskey' => $this->get_shortcut('navigate-next-button')));
M. Chardon's avatar
init  
M. Chardon committed
233

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

236
237
        $toolbar001 = '';
        $toolbar002 = '';
238
        $toolbar003 = '';
M. Chardon's avatar
M. Chardon committed
239
        $toolbarCostumdiv = '';
M. Chardon's avatar
M. Chardon committed
240
        $toolbaraxis = '';
M. Chardon's avatar
init  
M. Chardon committed
241
        $clearfix = html_writer::div('', 'clearfix');
M. Chardon's avatar
M. Chardon committed
242

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

245
            /** Toolbar n°0 : basic tools * */
M. Chardon's avatar
init  
M. Chardon committed
246
            // Select Tool.
247
248
249
            $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
250
251

            // Other Tools.
252
253
254
255
256
257
258
259
260
261
262
263
            $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) {
264
                $axis[$toolbar['axeid']] = $toolbar['label'];
M. Chardon's avatar
M. Chardon committed
265
266
                $toolbartmp = '';
                foreach ($toolbar['tool'] as $tool) {
M. Chardon's avatar
M. Chardon committed
267
268
269
                    if ($tool->enabled) {
                        $toolbartmp .= $this->render_toolbar_button('', '', $tool);
                    }
270
                }
271
                $toolbarCostum[] = html_writer::div($toolbartmp, 'toolbar customtoolbar', array('role' => 'toolbar', 'id' => 'toolbaraxis' . $toolbar['axeid'], 'style' => 'display:none;'));
272
273
274
            }
            $axischoice = html_writer::div(html_writer::select($axis, 'axisselection', 0, FALSE), 'toolbar ', array('role' => 'toolbar'));
            foreach ($toolbarCostum as $toolbarCostumUnit) {
275
                $toolbarCostumdiv .= $toolbarCostumUnit;
276
            }
277
            $toolbarCostumdiv .= $axischoice;
278
279
280
281
282
283
284
285
286
287
            
            // Toolbar pour lien creation palette
            $courseid = $this->page->course->id;
            $lienAdmin = new moodle_url('/mod/assign/feedback/editpdfplus/view_admin.php', array('id' => $courseid));
            $toolbar003 .= html_writer::tag('button',
                    html_writer::tag("i", "", array('class' => 'fa fa-wrench','aria-hidden' => 'true')),
                    array('class' => 'button btn btn-default', 'type' => 'button'));
            $toolbar003 = html_writer::link($lienAdmin, $toolbar003);
            $toolbar003 = html_writer::div($toolbar003, 'toolbar', array('role' => 'toolbar'));
            //$url = new moodle_url('/mod/assign/feedback/editpdfplus/view_admin.php', array('id' => $course->id));
M. Chardon's avatar
M. Chardon committed
288
        } else {
M. Chardon's avatar
M. Chardon committed
289
            $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
290
291
292
            $axis = $widget->axis;
            foreach ($axis as $ax) {
                $toolbaraxis .= $this->render_toolbar_axis($ax);
M. Chardon's avatar
M. Chardon committed
293
                $toolbaraxis .= "</div><div style='display:inline;margin-left:5px;text-align:left;'>";
M. Chardon's avatar
M. Chardon committed
294
295
            }
            $toolbaraxis .= "</div></div>";
296
            $questionchoice = html_writer::select(
M. Chardon's avatar
M. Chardon committed
297
                            [get_string('question_select', 'assignfeedback_editpdfplus'), get_string('question_select_without', 'assignfeedback_editpdfplus'), get_string('question_select_with', 'assignfeedback_editpdfplus')], 'questionselection', 0, FALSE, array('class' => 'form-control'));
298
            $axischoice = html_writer::select(
M. Chardon's avatar
M. Chardon committed
299
                            [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, array('class' => 'form-control'));
300
            $validatebutton = html_writer::tag('button', get_string('send_pdf_update', 'assignfeedback_editpdfplus'), array('class' => 'button btn btn-default', 'id' => 'student_valide_button'));
M. Chardon's avatar
M. Chardon committed
301
302
            $toolbaraxis .= html_writer::div($validatebutton, 'toolbar ', array('role' => 'toolbar'));
            $toolbaraxis .= html_writer::div($axischoice, 'toolbar ', array('role' => 'toolbar'));
303
            $toolbaraxis .= html_writer::div($questionchoice, 'toolbar ', array('role' => 'toolbar'));
M. Chardon's avatar
init  
M. Chardon committed
304
305
306
        }

        // Toobars written in reverse order because they are floated right.
307
308
        $pageheader = html_writer::div($divnavigation1 .
                        $toolbar002 .
M. Chardon's avatar
M. Chardon committed
309
                        $toolbaraxis .
310
311
                        $toolbarCostumdiv .
                        $toolbar001 .
312
                        $toolbar003 .
313
                        $clearfix, 'pageheader', array('style' => 'padding:0'));
314

M. Chardon's avatar
init  
M. Chardon committed
315
316
317
318
        $body .= $pageheader;

        // Loading progress bar.
        $progressbar = html_writer::div('', 'bar', array('style' => 'width: 0%'));
319
320
321
322
        $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
323
324
325
326
        $loading = html_writer::div($progressbar . $progressbarlabel, 'loading');

        $canvas = html_writer::div($loading, 'drawingcanvas');
        $canvas = html_writer::div($canvas, 'drawingregion');
327
328
329
        $changesmessage = html_writer::tag('div', get_string('draftchangessaved', 'assignfeedback_editpdfplus'), array(
                    'class' => 'assignfeedback_editpdfplus_unsavedchanges warning label label-info'
        ));
330
331
        $changesmessageDiv = html_writer::div($changesmessage, 'unsaved-changes');
        $canvas .= $changesmessageDiv;
332

333
334
335
        $changesmessage2 = html_writer::tag('div', get_string('nodraftchangessaved', 'assignfeedback_editpdfplus'), array(
                    'class' => 'assignfeedback_editpdfplus_unsavedchanges_edit warning label label-info'
        ));
336
337
        $changesmessage2Div = html_writer::div($changesmessage2, 'unsaved-changes');
        $canvas .= $changesmessage2Div;
M. Chardon's avatar
init  
M. Chardon committed
338
339
340
341
342

        $body .= $canvas;

        $footer = '';

343
344
345
346
347
348
349
350
        $editorparams = array(array('header' => $header,
                'body' => $body,
                'footer' => $footer,
                'linkid' => $linkid,
                'assignmentid' => $widget->assignment,
                'userid' => $widget->userid,
                'attemptnumber' => $widget->attemptnumber,
                'stampfiles' => $widget->stampfiles,
351
                'readonly' => $widget->readonly));
M. Chardon's avatar
init  
M. Chardon committed
352

353
        $this->page->requires->yui_module('moodle-assignfeedback_editpdfplus-editor', 'M.assignfeedback_editpdfplus.editor.init', $editorparams);
M. Chardon's avatar
init  
M. Chardon committed
354
355
356

        $this->page->requires->strings_for_js(array(
            'yellow',
357
            'yellowlemon',
M. Chardon's avatar
init  
M. Chardon committed
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
            'white',
            'red',
            'blue',
            'green',
            'black',
            'clear',
            'colourpicker',
            'loadingeditor',
            'pagexofy',
            'addtoquicklist',
            'filter',
            'deleteannotation',
            'stamp',
            'stamppicker',
            'cannotopenpdf',
373
374
375
            'pagenumber',
            'student_statut_nc',
            'student_answer_lib'
376
                ), 'assignfeedback_editpdfplus');
M. Chardon's avatar
init  
M. Chardon committed
377
378
379

        return $html;
    }
380

M. Chardon's avatar
M. Chardon committed
381
382
383
384
385
    /**
     * Display admin view
     * @param assignfeedback_editpdfplus\widget_admin $widget
     * @return String
     */
M. Chardon's avatar
M. Chardon committed
386
387
388
    public function render_assignfeedback_editpdfplus_widget_admin(assignfeedback_editpdfplus\widget_admin $widget) {
        return $this->render_from_template('assignfeedback_editpdfplus/admin', $widget);
    }
389

M. Chardon's avatar
M. Chardon committed
390
391
392
393
394
    /**
     * Display axis form (add and edit)
     * @param moodleform $form
     * @return String
     */
395
    public function render_assignfeedback_editpdfplus_widget_admin_axisform(moodleform $form) {
M. Chardon's avatar
M. Chardon committed
396
397
        return $this->render_from_template('assignfeedback_editpdfplus/axis_form', $form);
    }
398

M. Chardon's avatar
M. Chardon committed
399
400
401
402
403
    /**
     * Display axis form (delete)
     * @param moodleform $form
     * @return String
     */
404
    public function render_assignfeedback_editpdfplus_widget_admin_axisdelform(moodleform $form) {
405
406
        return $this->render_from_template('assignfeedback_editpdfplus/axis_del_form', $form);
    }
407

M. Chardon's avatar
M. Chardon committed
408
409
410
411
412
    /**
     * Display tool form, with preview
     * @param object $data
     * @return String
     */
413
414
415
416
    public function render_assignfeedback_editpdfplus_widget_admin_toolform($data) {
        $data->map01 = $this->pix_url('map01', 'assignfeedback_editpdfplus');
        $data->map02 = $this->pix_url('map02', 'assignfeedback_editpdfplus');
        $data->map03 = $this->pix_url('map03', 'assignfeedback_editpdfplus');
M. Chardon's avatar
M. Chardon committed
417
418
        return $this->render_from_template('assignfeedback_editpdfplus/tool_form', $data);
    }
M. Chardon's avatar
M. Chardon committed
419

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