Gitlab CSE Unil

renderer.php 16.1 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
// 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.
 *
21
22
 * A custom renderer class that extends the plugin_renderer_base and is used by the editpdfplus feedback plugin.
 * 
M. Chardon's avatar
init  
M. Chardon committed
23
 * @package   assignfeedback_editpdfplus
M. Chardon's avatar
GPL3    
M. Chardon committed
24
 * @copyright  2016 Université de Lausanne
25
 * The code is based on mod/assign/feedback/editpdf/classes/renderer.php by Davo Smith.
M. Chardon's avatar
init  
M. Chardon committed
26
27
28
29
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
defined('MOODLE_INTERNAL') || die();

M. Chardon's avatar
M. Chardon committed
30
use \assignfeedback_editpdfplus\bdd\tool_generic;
M. Chardon's avatar
M. Chardon committed
31
use \assignfeedback_editpdfplus\bdd\axis;
M. Chardon's avatar
M. Chardon committed
32

M. Chardon's avatar
init  
M. Chardon committed
33
34
class assignfeedback_editpdfplus_renderer extends plugin_renderer_base {

M. Chardon's avatar
M. Chardon committed
35
    const PLUGIN_NAME = "assignfeedback_editpdfplus";
M. Chardon's avatar
M. Chardon committed
36
37
    const TOOL_SELECT = "select";
    const TOOL_DRAG = "drag";
38
    const TOOL_RESIZE = "resize";
M. Chardon's avatar
M. Chardon committed
39
    const TOOL_ANNOTATIONCOLOR = "annotationcolour";
M. Chardon's avatar
M. Chardon committed
40
41
42
    const HTMLCLASS = "class";
    const HTMLDISABLED = "disabled";
    const TOOL_OBJ_LABEL = "label";
M. Chardon's avatar
init  
M. Chardon committed
43
44

    /**
M. Chardon's avatar
M. Chardon committed
45
46
47
     * Render a HTML button for a Tool.
     * 
     * @param assignfeedback_editpdfplus\bdd\tool $fulltool Object tool to reprensant
M. Chardon's avatar
init  
M. Chardon committed
48
49
50
     * @param bool $disabled Optional - Is this button disabled.
     * @return string
     */
M. Chardon's avatar
M. Chardon committed
51
    private function render_toolbar_button_tool(assignfeedback_editpdfplus\bdd\tool $fulltool, $disabled = false) {
M. Chardon's avatar
M. Chardon committed
52
        $displayArray = $fulltool->getRendererBoutonHTMLDisplay($disabled);
M. Chardon's avatar
M. Chardon committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
        return $this->render_toolbar_button_html($displayArray["content"], $displayArray["parameters"]);
    }

    /**
     * Render a simple HTML button
     * 
     * @param string $content Button's content
     * @param array $parameters Button's parameters. Optional
     * @return string
     */
    private function render_toolbar_button_html($content, $parameters = array()) {
        return html_writer::tag("button", $content, array_merge(array('type' => 'button'), $parameters));
    }

    /**
     * Render an icon from FontAwesome class
     * 
     * @param string $faClass
     * @return string
     */
    private function render_toolbar_button_icon($faClass = "") {
        return html_writer::tag("i", "", array(self::HTMLCLASS => 'fa ' . $faClass,
                    "aria-hidden"));
    }

    /**
     * Render a complete toolbar
     * 
     * @param string $content buttons or whatever inside the toolbar
     * @param string $class Optionals classes
     * @param string $param Optionals parameters
     * @return string
     */
    private function render_toolbar($content, $class = "", $param = array()) {
        return html_writer::div($content, "btn-group btn-group-sm " . $class, array_merge(array('role' => 'group'), $param));
M. Chardon's avatar
init  
M. Chardon committed
88
89
    }

M. Chardon's avatar
M. Chardon committed
90
91
92
93
94
95
96
    /**
     * Render a graphic reprensation for an axis in readonly mode (checkbox)
     * 
     * @param axis $axis Axis to display
     * @return string
     */
    private function render_toolbar_axis(axis $axis) {
M. Chardon's avatar
M. Chardon committed
97
        $iconhtml = $axis->label;
M. Chardon's avatar
M. Chardon committed
98
        $iconparams = array('type' => 'checkbox', self::HTMLCLASS => 'axis', 'id' => 'ctaxis' . $axis->id, 'value' => $axis->id);
99
        $inputhtml = html_writer::tag('input', "", $iconparams);
M. Chardon's avatar
M. Chardon committed
100
        return html_writer::label($inputhtml . $iconhtml, "", true, array(self::HTMLCLASS => 'checkbox-inline mt-2 mr-2'));
M. Chardon's avatar
M. Chardon committed
101
102
    }

M. Chardon's avatar
init  
M. Chardon committed
103
    /**
104
     * Render the editpdfplus widget in the grading form.
M. Chardon's avatar
init  
M. Chardon committed
105
106
107
108
109
110
111
     *
     * @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) {
        $html = '';

112
        //JS declaration
M. Chardon's avatar
M. Chardon committed
113
        $html .= html_writer::div(get_string('jsrequired', self::PLUGIN_NAME), 'hiddenifjs');
114
115

        //Random id for plugin identification
M. Chardon's avatar
init  
M. Chardon committed
116
        $linkid = html_writer::random_id();
M. Chardon's avatar
M. Chardon committed
117
        $labelLaunchedEditor = ($widget->readonly) ? get_string('viewfeedbackonline', self::PLUGIN_NAME) : get_string('launcheditor', self::PLUGIN_NAME);
118
        $links = html_writer::tag('a', $labelLaunchedEditor, array('id' => $linkid, self::HTMLCLASS => 'btn btn-light', 'href' => '#'));
M. Chardon's avatar
init  
M. Chardon committed
119
120
        $html .= '<input type="hidden" name="assignfeedback_editpdfplus_haschanges" value="false"/>';
        $html .= html_writer::div($links, 'visibleifjs');
121
122

        //html header
M. Chardon's avatar
M. Chardon committed
123
        $header = get_string('pluginname', self::PLUGIN_NAME);
124

M. Chardon's avatar
init  
M. Chardon committed
125
        $body = '';
126

127
128
        // Create the page navigation.
        $navigation = '';
M. Chardon's avatar
init  
M. Chardon committed
129
130
131
132
133
134
135
136
        // 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';
        }
137
        $classNav = "btn btn-light ";
M. Chardon's avatar
M. Chardon committed
138
139
140
141
142
143
144
145
146
        $iconhtmlP = $this->render_toolbar_button_icon("fa-caret-left fa-2x");
        $navigation .= $this->render_toolbar_button_html($iconhtmlP, array(self::HTMLDISABLED => 'true',
            self::HTMLCLASS => $classNav . 'navigate-previous-button'));
        $navigation .= html_writer::tag('select', null, array(self::HTMLDISABLED => 'true',
                    'aria-label' => get_string('gotopage', self::PLUGIN_NAME), self::HTMLCLASS => "navigate-page-select"));
        $iconhtmlN = $this->render_toolbar_button_icon("fa-caret-right fa-2x");
        $navigation .= $this->render_toolbar_button_html($iconhtmlN, array(self::HTMLDISABLED => 'true',
            self::HTMLCLASS => $classNav . "navigate-next-button"));

147
        $navigationBlock = $this->render_toolbar($navigation, "mr-3");
M. Chardon's avatar
init  
M. Chardon committed
148

149
        $toolbarRotationBlock = '';
150
151
152
        $toolbarBaseBlock = '';
        $toolbarDrawBlock = '';
        $toolbarAdminBlock = '';
M. Chardon's avatar
M. Chardon committed
153
        $toolbarCostumdiv = '';
154
        $toolbarAxis = '';
M. Chardon's avatar
M. Chardon committed
155

M. Chardon's avatar
init  
M. Chardon committed
156
        if (!$widget->readonly) {
157
            /** Toolbar n°0 : basic tools * */
158
159
160
161
162
163
164
165
166
            // Rotate Tool.
            $rotateToolLeft = new tool_generic();
            $rotateToolLeft->init(array(self::TOOL_OBJ_LABEL => "rotateleft"));
            $toolbarRotation = $this->render_toolbar_button_tool($rotateToolLeft);
            $rotateToolRight = new tool_generic();
            $rotateToolRight->init(array(self::TOOL_OBJ_LABEL => 'rotateright'));
            $toolbarRotation .= $this->render_toolbar_button_tool($rotateToolRight);
            $toolbarRotationBlock = $this->render_toolbar($toolbarRotation, "mr-3");

M. Chardon's avatar
init  
M. Chardon committed
167
            // Select Tool.
M. Chardon's avatar
M. Chardon committed
168
            $dragTool = new tool_generic();
M. Chardon's avatar
M. Chardon committed
169
170
            $dragTool->init(array(self::TOOL_OBJ_LABEL => self::TOOL_DRAG));
            $toolbarBase = $this->render_toolbar_button_tool($dragTool);
M. Chardon's avatar
M. Chardon committed
171
            $selectTool = new tool_generic();
M. Chardon's avatar
M. Chardon committed
172
173
            $selectTool->init(array(self::TOOL_OBJ_LABEL => self::TOOL_SELECT));
            $toolbarBase .= $this->render_toolbar_button_tool($selectTool);
174
175
176
            $resizeTool = new tool_generic();
            $resizeTool->init(array(self::TOOL_OBJ_LABEL => self::TOOL_RESIZE));
            $toolbarBase .= $this->render_toolbar_button_tool($resizeTool);
M. Chardon's avatar
M. Chardon committed
177
            $toolbarBaseBlock = $this->render_toolbar($toolbarBase, "mr-3");
M. Chardon's avatar
init  
M. Chardon committed
178

M. Chardon's avatar
M. Chardon committed
179
180
181
            // Generic Tools.
            $toolbarDraw = '';
            foreach ($widget->genericToolbar as $tool) {
M. Chardon's avatar
M. Chardon committed
182
                $toolbarDraw .= $this->render_toolbar_button_tool($tool);
M. Chardon's avatar
M. Chardon committed
183
184
            }
            $colorTool = new tool_generic();
M. Chardon's avatar
M. Chardon committed
185
186
187
            $colorTool->init(array(self::TOOL_OBJ_LABEL => self::TOOL_ANNOTATIONCOLOR));
            $toolbarDraw .= $this->render_toolbar_button_tool($colorTool);
            $toolbarDrawBlock = $this->render_toolbar($toolbarDraw);
188
189
190

            /** Costum toolbars * */
            $axis = array();
M. Chardon's avatar
M. Chardon committed
191
            foreach ($widget->customToolbars as $toolbar) {
M. Chardon's avatar
M. Chardon committed
192
                $axis[$toolbar['axeid']] = $toolbar[self::TOOL_OBJ_LABEL];
M. Chardon's avatar
M. Chardon committed
193
194
                $toolbartmp = '';
                foreach ($toolbar['tool'] as $tool) {
M. Chardon's avatar
M. Chardon committed
195
                    $toolbartmp .= $this->render_toolbar_button_tool($tool);
196
                }
M. Chardon's avatar
M. Chardon committed
197
                $toolbarCostumdiv .= $this->render_toolbar($toolbartmp, "mr-3 customtoolbar", array('id' => 'toolbaraxis' . $toolbar['axeid']));
198
            }
199

M. Chardon's avatar
M. Chardon committed
200
            $statuschoice = $this->render_toolbar(html_writer::select($axis, 'axisselection', 0, FALSE), "mr-0");
201
            $toolbarAxis = $statuschoice;
202

203
            // Toolbar pour lien creation palette et aide
204
205
206
207
208
209
210
            $parentContext = $this->page->context->get_parent_context();
            if ($parentContext->contextlevel == CONTEXT_COURSE) {
                $lienAdmin = new moodle_url('/mod/assign/feedback/editpdfplus/view_admin.php', array('id' => $parentContext->id));
                $toolbarAdmin = $this->render_toolbar_button_html($this->render_toolbar_button_icon("fa-wrench"), array(
                    self::HTMLCLASS => 'btn btn-outline-info',
                    'onclick' => "document.location='" . $lienAdmin->out() . "';"));
            }
211
            $toolbarAdmin .= $this->render_toolbar_button_html($this->render_toolbar_button_icon("fa-question-circle"), array(self::HTMLCLASS => 'btn btn-outline-info helpmessage'));
M. Chardon's avatar
M. Chardon committed
212
            $toolbarAdminBlock = $this->render_toolbar($toolbarAdmin, "mr-3");
M. Chardon's avatar
M. Chardon committed
213
        } else {
M. Chardon's avatar
M. Chardon committed
214
            //readonly view
M. Chardon's avatar
M. Chardon committed
215
            $axis = $widget->axis;
216
            $toolbaraxisContent = "";
M. Chardon's avatar
M. Chardon committed
217
            foreach ($axis as $ax) {
218
                $toolbaraxisContent .= $this->render_toolbar_axis($ax);
M. Chardon's avatar
M. Chardon committed
219
            }
M. Chardon's avatar
M. Chardon committed
220
            $toolbarAxis = $this->render_toolbar($toolbaraxisContent, "mr-2");
221

222
            $questionchoice = html_writer::select(
M. Chardon's avatar
M. Chardon committed
223
                            [get_string('question_select', self::PLUGIN_NAME), get_string('question_select_without', self::PLUGIN_NAME), get_string('question_select_with', self::PLUGIN_NAME)], 'questionselection', 0, FALSE, array(self::HTMLCLASS => 'form-control'));
224
            $statuschoice = html_writer::select(
M. Chardon's avatar
M. Chardon committed
225
                            [get_string('statut_select', self::PLUGIN_NAME), get_string('statut_select_nc', self::PLUGIN_NAME), get_string('statut_select_ok', self::PLUGIN_NAME), get_string('statut_select_ko', self::PLUGIN_NAME)], 'statutselection', 0, FALSE, array(self::HTMLCLASS => 'form-control'));
226
            $validatebutton = $this->render_toolbar_button_html(get_string('send_pdf_update', self::PLUGIN_NAME), array(self::HTMLCLASS => 'btn btn-light', 'id' => 'student_valide_button'));
M. Chardon's avatar
M. Chardon committed
227
228
229
            $toolbarAxis .= $this->render_toolbar($statuschoice);
            $toolbarAxis .= $this->render_toolbar($questionchoice, 'mr-3');
            $toolbarAxis .= $this->render_toolbar($validatebutton, 'mr-0');
M. Chardon's avatar
init  
M. Chardon committed
230
231
        }

232
233
        $pageheadercontent = "<div class='d-flex align-content-center flex-nowrap align-items-center'>"
                . $navigationBlock
234
                . $toolbarAdminBlock
235
                . "<div class='d-fex flex-wrap align-content-center align-items-center'>"
236
                . $toolbarRotationBlock
237
                . $toolbarBaseBlock
238
                . "<div class='btn-group btn-group-sm p-1'>"
239
240
                . $toolbarAxis
                . $toolbarCostumdiv
241
242
243
244
245
                . '</div>'
                . $toolbarDrawBlock
                . '</div>'
                . '</div>';
        $mainnavigation = html_writer::div($pageheadercontent, "drawingtoolbar btn-toolbar btn-group-sm bg-light p-1", array('role' => 'toolbar'));
246

M. Chardon's avatar
M. Chardon committed
247
        $body .= $mainnavigation;
M. Chardon's avatar
init  
M. Chardon committed
248
249

        // Loading progress bar.
M. Chardon's avatar
M. Chardon committed
250
        $progressbar = html_writer::div(html_writer::div('', 'bar'), 'progress progress-info progress-striped active', array('title' => get_string('loadingeditor', self::PLUGIN_NAME),
251
252
                    'role' => 'progressbar', 'aria-valuenow' => 0, 'aria-valuemin' => 0,
                    'aria-valuemax' => 100));
M. Chardon's avatar
M. Chardon committed
253
        $progressbarlabel = html_writer::div(get_string('generatingpdf', self::PLUGIN_NAME), 'progressbarlabel');
M. Chardon's avatar
init  
M. Chardon committed
254
        $loading = html_writer::div($progressbar . $progressbarlabel, 'loading');
M. Chardon's avatar
M. Chardon committed
255
        $canvas = html_writer::div(html_writer::div($loading, 'drawingcanvas'), 'drawingregion');
256

257
258
259
        // Place for messages, but no warnings displayed yet.
        $changesmessage = html_writer::div('', 'warningmessages');
        $canvas .= $changesmessage;
260

M. Chardon's avatar
M. Chardon committed
261
262
        $changesmessage2 = html_writer::tag('div', get_string('nodraftchangessaved', self::PLUGIN_NAME), array(
                    self::HTMLCLASS => 'assignfeedback_editpdfplus_unsavedchanges_edit warning label label-info'
263
        ));
264
265
        $changesmessage2Div = html_writer::div($changesmessage2, 'unsaved-changes');
        $canvas .= $changesmessage2Div;
M. Chardon's avatar
init  
M. Chardon committed
266

267
268
269
270
        $infoicon = "<i class='fa fa-info-circle p-1'></i>";
        $infomessage = html_writer::div($infoicon, 'infoicon');
        $canvas .= $infomessage;

271
272
        //help message
        $helpmessageTitle = html_writer::div(get_string('help_title', self::PLUGIN_NAME), null, array('id' => 'afppHelpmessageTitle'));
273
        $helpmessagecontent = $this->render_from_template('assignfeedback_editpdfplus/help_workspace', array());
274
275
276
277
        $helpmessageBody = html_writer::div($helpmessagecontent, null, array('id' => 'afppHelpmessageBody'));
        $helpmessageDiv = html_writer::div($helpmessageTitle . $helpmessageBody, 'helpmessage');
        $canvas .= $helpmessageDiv;

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

        $footer = '';

282
283
284
285
286
287
288
        $editorparams = array(array('header' => $header,
                'body' => $body,
                'footer' => $footer,
                'linkid' => $linkid,
                'assignmentid' => $widget->assignment,
                'userid' => $widget->userid,
                'attemptnumber' => $widget->attemptnumber,
289
                'readonly' => $widget->readonly));
M. Chardon's avatar
init  
M. Chardon committed
290

291
        $this->page->requires->yui_module('moodle-assignfeedback_editpdfplus-editor', 'M.assignfeedback_editpdfplus.editor.init', $editorparams);
M. Chardon's avatar
init  
M. Chardon committed
292
293
294

        $this->page->requires->strings_for_js(array(
            'yellow',
295
            'yellowlemon',
M. Chardon's avatar
init  
M. Chardon committed
296
297
298
299
300
301
302
303
304
305
306
307
308
            'white',
            'red',
            'blue',
            'green',
            'black',
            'clear',
            'colourpicker',
            'loadingeditor',
            'pagexofy',
            'addtoquicklist',
            'filter',
            'deleteannotation',
            'cannotopenpdf',
309
            'pagenumber',
310
311
            'partialwarning',
            'draftchangessaved',
312
313
            'student_statut_nc',
            'student_answer_lib'
M. Chardon's avatar
M. Chardon committed
314
                ), self::PLUGIN_NAME);
M. Chardon's avatar
init  
M. Chardon committed
315
316
317

        return $html;
    }
318

M. Chardon's avatar
M. Chardon committed
319
320
321
322
323
    /**
     * Display admin view
     * @param assignfeedback_editpdfplus\widget_admin $widget
     * @return String
     */
M. Chardon's avatar
M. Chardon committed
324
325
326
    public function render_assignfeedback_editpdfplus_widget_admin(assignfeedback_editpdfplus\widget_admin $widget) {
        return $this->render_from_template('assignfeedback_editpdfplus/admin', $widget);
    }
327

M. Chardon's avatar
M. Chardon committed
328
329
330
331
332
    /**
     * Display axis form (add and edit)
     * @param moodleform $form
     * @return String
     */
333
    public function render_assignfeedback_editpdfplus_widget_admin_axisform(moodleform $form) {
M. Chardon's avatar
M. Chardon committed
334
335
        return $this->render_from_template('assignfeedback_editpdfplus/axis_form', $form);
    }
336

M. Chardon's avatar
M. Chardon committed
337
    /**
M. Chardon's avatar
M. Chardon committed
338
339
     * Display axis form (export)
     * @param stdClass $widget
M. Chardon's avatar
M. Chardon committed
340
341
     * @return String
     */
M. Chardon's avatar
M. Chardon committed
342
343
    public function render_assignfeedback_editpdfplus_widget_admin_axisexportform($widget) {
        return $this->render_from_template('assignfeedback_editpdfplus/axis_export_form', $widget);
344
    }
345

M. Chardon's avatar
M. Chardon committed
346
347
348
349
350
    /**
     * Display tool form, with preview
     * @param object $data
     * @return String
     */
351
    public function render_assignfeedback_editpdfplus_widget_admin_toolform($data) {
M. Chardon's avatar
M. Chardon committed
352
353
354
        $data->map01 = $this->pix_url('map01', self::PLUGIN_NAME);
        $data->map02 = $this->pix_url('map02', self::PLUGIN_NAME);
        $data->map03 = $this->pix_url('map03', self::PLUGIN_NAME);
M. Chardon's avatar
M. Chardon committed
355
356
        return $this->render_from_template('assignfeedback_editpdfplus/tool_form', $data);
    }
M. Chardon's avatar
M. Chardon committed
357

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