Gitlab CSE Unil

renderer.php 15.9 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
38
    const TOOL_SELECT = "select";
    const TOOL_DRAG = "drag";
    const TOOL_ANNOTATIONCOLOR = "annotationcolour";
M. Chardon's avatar
M. Chardon committed
39
40
41
    const HTMLCLASS = "class";
    const HTMLDISABLED = "disabled";
    const TOOL_OBJ_LABEL = "label";
M. Chardon's avatar
init  
M. Chardon committed
42
43

    /**
M. Chardon's avatar
M. Chardon committed
44
45
46
     * 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
47
48
49
     * @param bool $disabled Optional - Is this button disabled.
     * @return string
     */
M. Chardon's avatar
M. Chardon committed
50
    private function render_toolbar_button_tool(assignfeedback_editpdfplus\bdd\tool $fulltool, $disabled = false) {
M. Chardon's avatar
M. Chardon committed
51
        $displayArray = $fulltool->getRendererBoutonHTMLDisplay($disabled);
M. Chardon's avatar
M. Chardon committed
52
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
        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
87
88
    }

M. Chardon's avatar
M. Chardon committed
89
90
91
92
93
94
95
    /**
     * 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
96
        $iconhtml = $axis->label;
M. Chardon's avatar
M. Chardon committed
97
        $iconparams = array('type' => 'checkbox', self::HTMLCLASS => 'axis', 'id' => 'ctaxis' . $axis->id, 'value' => $axis->id);
98
        $inputhtml = html_writer::tag('input', "", $iconparams);
M. Chardon's avatar
M. Chardon committed
99
        return html_writer::label($inputhtml . $iconhtml, "", true, array(self::HTMLCLASS => 'checkbox-inline mt-2 mr-2'));
M. Chardon's avatar
M. Chardon committed
100
101
    }

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

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

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

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

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

126
127
        // Create the page navigation.
        $navigation = '';
M. Chardon's avatar
init  
M. Chardon committed
128
129
130
131
132
133
134
135
        // 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';
        }
136
        $classNav = "btn btn-light ";
M. Chardon's avatar
M. Chardon committed
137
138
139
140
141
142
143
144
145
        $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"));

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

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

M. Chardon's avatar
init  
M. Chardon committed
155
        if (!$widget->readonly) {
156
            /** Toolbar n°0 : basic tools * */
157
158
159
160
161
162
163
164
165
            // 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
166
            // Select Tool.
M. Chardon's avatar
M. Chardon committed
167
            $dragTool = new tool_generic();
M. Chardon's avatar
M. Chardon committed
168
169
            $dragTool->init(array(self::TOOL_OBJ_LABEL => self::TOOL_DRAG));
            $toolbarBase = $this->render_toolbar_button_tool($dragTool);
M. Chardon's avatar
M. Chardon committed
170
            $selectTool = new tool_generic();
M. Chardon's avatar
M. Chardon committed
171
172
173
            $selectTool->init(array(self::TOOL_OBJ_LABEL => self::TOOL_SELECT));
            $toolbarBase .= $this->render_toolbar_button_tool($selectTool);
            $toolbarBaseBlock = $this->render_toolbar($toolbarBase, "mr-3");
M. Chardon's avatar
init  
M. Chardon committed
174

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

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

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

199
            // Toolbar pour lien creation palette et aide
200
201
202
203
204
205
206
            $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() . "';"));
            }
207
            $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
208
            $toolbarAdminBlock = $this->render_toolbar($toolbarAdmin, "mr-3");
M. Chardon's avatar
M. Chardon committed
209
        } else {
M. Chardon's avatar
M. Chardon committed
210
            //readonly view
M. Chardon's avatar
M. Chardon committed
211
            $axis = $widget->axis;
212
            $toolbaraxisContent = "";
M. Chardon's avatar
M. Chardon committed
213
            foreach ($axis as $ax) {
214
                $toolbaraxisContent .= $this->render_toolbar_axis($ax);
M. Chardon's avatar
M. Chardon committed
215
            }
M. Chardon's avatar
M. Chardon committed
216
            $toolbarAxis = $this->render_toolbar($toolbaraxisContent, "mr-2");
217

218
            $questionchoice = html_writer::select(
M. Chardon's avatar
M. Chardon committed
219
                            [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'));
220
            $statuschoice = html_writer::select(
M. Chardon's avatar
M. Chardon committed
221
                            [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'));
222
            $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
223
224
225
            $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
226
227
        }

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

M. Chardon's avatar
M. Chardon committed
243
        $body .= $mainnavigation;
M. Chardon's avatar
init  
M. Chardon committed
244
245

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

253
254
255
        // Place for messages, but no warnings displayed yet.
        $changesmessage = html_writer::div('', 'warningmessages');
        $canvas .= $changesmessage;
256

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

263
264
265
266
        $infoicon = "<i class='fa fa-info-circle p-1'></i>";
        $infomessage = html_writer::div($infoicon, 'infoicon');
        $canvas .= $infomessage;

267
268
        //help message
        $helpmessageTitle = html_writer::div(get_string('help_title', self::PLUGIN_NAME), null, array('id' => 'afppHelpmessageTitle'));
269
        $helpmessagecontent = $this->render_from_template('assignfeedback_editpdfplus/help_workspace', array());
270
271
272
273
        $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
274
275
276
277
        $body .= $canvas;

        $footer = '';

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

287
        $this->page->requires->yui_module('moodle-assignfeedback_editpdfplus-editor', 'M.assignfeedback_editpdfplus.editor.init', $editorparams);
M. Chardon's avatar
init  
M. Chardon committed
288
289
290

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

        return $html;
    }
314

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

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

M. Chardon's avatar
M. Chardon committed
333
334
335
336
337
    /**
     * Display axis form (delete)
     * @param moodleform $form
     * @return String
     */
338
    public function render_assignfeedback_editpdfplus_widget_admin_axisdelform(moodleform $form) {
339
340
        return $this->render_from_template('assignfeedback_editpdfplus/axis_del_form', $form);
    }
341

M. Chardon's avatar
M. Chardon committed
342
343
344
345
346
    /**
     * Display tool form, with preview
     * @param object $data
     * @return String
     */
347
    public function render_assignfeedback_editpdfplus_widget_admin_toolform($data) {
M. Chardon's avatar
M. Chardon committed
348
349
350
        $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
351
352
        return $this->render_from_template('assignfeedback_editpdfplus/tool_form', $data);
    }
M. Chardon's avatar
M. Chardon committed
353

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