Gitlab CSE Unil

renderer.php 14.8 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
117
        $labelLaunchedEditor = ($widget->readonly) ? get_string('viewfeedbackonline', self::PLUGIN_NAME) : get_string('launcheditor', self::PLUGIN_NAME);
        $links = html_writer::tag('a', $labelLaunchedEditor, array('id' => $linkid, self::HTMLCLASS => 'btn btn-secondary', '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';
        }
M. Chardon's avatar
M. Chardon committed
136
137
138
139
140
141
142
143
144
145
146
        $classNav = "btn btn-secondary ";
        $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"));

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

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

M. Chardon's avatar
init  
M. Chardon committed
154
        if (!$widget->readonly) {
155
            /** Toolbar n°0 : basic tools * */
M. Chardon's avatar
init  
M. Chardon committed
156
            // Select Tool.
M. Chardon's avatar
M. Chardon committed
157
            $dragTool = new tool_generic();
M. Chardon's avatar
M. Chardon committed
158
159
            $dragTool->init(array(self::TOOL_OBJ_LABEL => self::TOOL_DRAG));
            $toolbarBase = $this->render_toolbar_button_tool($dragTool);
M. Chardon's avatar
M. Chardon committed
160
            $selectTool = new tool_generic();
M. Chardon's avatar
M. Chardon committed
161
162
163
            $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
164

M. Chardon's avatar
M. Chardon committed
165
166
167
            // Generic Tools.
            $toolbarDraw = '';
            foreach ($widget->genericToolbar as $tool) {
M. Chardon's avatar
M. Chardon committed
168
                $toolbarDraw .= $this->render_toolbar_button_tool($tool);
M. Chardon's avatar
M. Chardon committed
169
170
            }
            $colorTool = new tool_generic();
M. Chardon's avatar
M. Chardon committed
171
172
173
            $colorTool->init(array(self::TOOL_OBJ_LABEL => self::TOOL_ANNOTATIONCOLOR));
            $toolbarDraw .= $this->render_toolbar_button_tool($colorTool);
            $toolbarDrawBlock = $this->render_toolbar($toolbarDraw);
174
175
176

            /** Costum toolbars * */
            $axis = array();
M. Chardon's avatar
M. Chardon committed
177
            foreach ($widget->customToolbars as $toolbar) {
M. Chardon's avatar
M. Chardon committed
178
                $axis[$toolbar['axeid']] = $toolbar[self::TOOL_OBJ_LABEL];
M. Chardon's avatar
M. Chardon committed
179
180
                $toolbartmp = '';
                foreach ($toolbar['tool'] as $tool) {
M. Chardon's avatar
M. Chardon committed
181
                    $toolbartmp .= $this->render_toolbar_button_tool($tool);
182
                }
M. Chardon's avatar
M. Chardon committed
183
                $toolbarCostumdiv .= $this->render_toolbar($toolbartmp, "mr-3 customtoolbar", array('id' => 'toolbaraxis' . $toolbar['axeid']));
184
            }
185

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

189
            // Toolbar pour lien creation palette et aide
190
191
            $courseid = $this->page->course->id;
            $lienAdmin = new moodle_url('/mod/assign/feedback/editpdfplus/view_admin.php', array('id' => $courseid));
M. Chardon's avatar
M. Chardon committed
192
            $toolbarAdmin = $this->render_toolbar_button_html($this->render_toolbar_button_icon("fa-wrench"), array(
193
                self::HTMLCLASS => 'btn btn-outline-info',
M. Chardon's avatar
M. Chardon committed
194
                'onclick' => "document.location='" . $lienAdmin->out() . "';"));
195
            $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
196
            $toolbarAdminBlock = $this->render_toolbar($toolbarAdmin, "mr-3");
M. Chardon's avatar
M. Chardon committed
197
        } else {
M. Chardon's avatar
M. Chardon committed
198
            //readonly view
M. Chardon's avatar
M. Chardon committed
199
            $axis = $widget->axis;
200
            $toolbaraxisContent = "";
M. Chardon's avatar
M. Chardon committed
201
            foreach ($axis as $ax) {
202
                $toolbaraxisContent .= $this->render_toolbar_axis($ax);
M. Chardon's avatar
M. Chardon committed
203
            }
M. Chardon's avatar
M. Chardon committed
204
            $toolbarAxis = $this->render_toolbar($toolbaraxisContent, "mr-2");
205

206
            $questionchoice = html_writer::select(
M. Chardon's avatar
M. Chardon committed
207
                            [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'));
208
            $statuschoice = html_writer::select(
M. Chardon's avatar
M. Chardon committed
209
210
211
212
213
                            [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'));
            $validatebutton = $this->render_toolbar_button_html(get_string('send_pdf_update', self::PLUGIN_NAME), array(self::HTMLCLASS => 'btn btn-secondary', 'id' => 'student_valide_button'));
            $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
214
215
        }

216
217
218
219
220
221
        $pageheadercontent = $navigationBlock
                . $toolbarAdminBlock
                . $toolbarBaseBlock
                . $toolbarAxis
                . $toolbarCostumdiv
                . $toolbarDrawBlock;
M. Chardon's avatar
M. Chardon committed
222
        $mainnavigation = html_writer::div($pageheadercontent, "btn-toolbar btn-group-sm bg-light p-1", array('role' => 'toolbar'));
223

M. Chardon's avatar
M. Chardon committed
224
        $body .= $mainnavigation;
M. Chardon's avatar
init  
M. Chardon committed
225
226

        // Loading progress bar.
M. Chardon's avatar
M. Chardon committed
227
        $progressbar = html_writer::div(html_writer::div('', 'bar'), 'progress progress-info progress-striped active', array('title' => get_string('loadingeditor', self::PLUGIN_NAME),
228
229
                    'role' => 'progressbar', 'aria-valuenow' => 0, 'aria-valuemin' => 0,
                    'aria-valuemax' => 100));
M. Chardon's avatar
M. Chardon committed
230
        $progressbarlabel = html_writer::div(get_string('generatingpdf', self::PLUGIN_NAME), 'progressbarlabel');
M. Chardon's avatar
init  
M. Chardon committed
231
        $loading = html_writer::div($progressbar . $progressbarlabel, 'loading');
M. Chardon's avatar
M. Chardon committed
232
        $canvas = html_writer::div(html_writer::div($loading, 'drawingcanvas'), 'drawingregion');
233

M. Chardon's avatar
M. Chardon committed
234
235
        $changesmessage = html_writer::tag('div', get_string('draftchangessaved', self::PLUGIN_NAME), array(
                    self::HTMLCLASS => 'assignfeedback_editpdfplus_unsavedchanges warning label label-info'
236
        ));
237
238
        $changesmessageDiv = html_writer::div($changesmessage, 'unsaved-changes');
        $canvas .= $changesmessageDiv;
239

M. Chardon's avatar
M. Chardon committed
240
241
        $changesmessage2 = html_writer::tag('div', get_string('nodraftchangessaved', self::PLUGIN_NAME), array(
                    self::HTMLCLASS => 'assignfeedback_editpdfplus_unsavedchanges_edit warning label label-info'
242
        ));
243
244
        $changesmessage2Div = html_writer::div($changesmessage2, 'unsaved-changes');
        $canvas .= $changesmessage2Div;
M. Chardon's avatar
init  
M. Chardon committed
245

246
247
248
249
250
251
252
        //help message
        $helpmessageTitle = html_writer::div(get_string('help_title', self::PLUGIN_NAME), null, array('id' => 'afppHelpmessageTitle'));
        $helpmessagecontent=$this->render_from_template('assignfeedback_editpdfplus/help_workspace', array());
        $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
253
254
255
256
        $body .= $canvas;

        $footer = '';

257
258
259
260
261
262
263
        $editorparams = array(array('header' => $header,
                'body' => $body,
                'footer' => $footer,
                'linkid' => $linkid,
                'assignmentid' => $widget->assignment,
                'userid' => $widget->userid,
                'attemptnumber' => $widget->attemptnumber,
264
                'readonly' => $widget->readonly));
M. Chardon's avatar
init  
M. Chardon committed
265

266
        $this->page->requires->yui_module('moodle-assignfeedback_editpdfplus-editor', 'M.assignfeedback_editpdfplus.editor.init', $editorparams);
M. Chardon's avatar
init  
M. Chardon committed
267
268
269

        $this->page->requires->strings_for_js(array(
            'yellow',
270
            'yellowlemon',
M. Chardon's avatar
init  
M. Chardon committed
271
272
273
274
275
276
277
278
279
280
281
282
283
            'white',
            'red',
            'blue',
            'green',
            'black',
            'clear',
            'colourpicker',
            'loadingeditor',
            'pagexofy',
            'addtoquicklist',
            'filter',
            'deleteannotation',
            'cannotopenpdf',
284
285
286
            'pagenumber',
            'student_statut_nc',
            'student_answer_lib'
M. Chardon's avatar
M. Chardon committed
287
                ), self::PLUGIN_NAME);
M. Chardon's avatar
init  
M. Chardon committed
288
289
290

        return $html;
    }
291

M. Chardon's avatar
M. Chardon committed
292
293
294
295
296
    /**
     * Display admin view
     * @param assignfeedback_editpdfplus\widget_admin $widget
     * @return String
     */
M. Chardon's avatar
M. Chardon committed
297
298
299
    public function render_assignfeedback_editpdfplus_widget_admin(assignfeedback_editpdfplus\widget_admin $widget) {
        return $this->render_from_template('assignfeedback_editpdfplus/admin', $widget);
    }
300

M. Chardon's avatar
M. Chardon committed
301
302
303
304
305
    /**
     * Display axis form (add and edit)
     * @param moodleform $form
     * @return String
     */
306
    public function render_assignfeedback_editpdfplus_widget_admin_axisform(moodleform $form) {
M. Chardon's avatar
M. Chardon committed
307
308
        return $this->render_from_template('assignfeedback_editpdfplus/axis_form', $form);
    }
309

M. Chardon's avatar
M. Chardon committed
310
311
312
313
314
    /**
     * Display axis form (delete)
     * @param moodleform $form
     * @return String
     */
315
    public function render_assignfeedback_editpdfplus_widget_admin_axisdelform(moodleform $form) {
316
317
        return $this->render_from_template('assignfeedback_editpdfplus/axis_del_form', $form);
    }
318

M. Chardon's avatar
M. Chardon committed
319
320
321
322
323
    /**
     * Display tool form, with preview
     * @param object $data
     * @return String
     */
324
    public function render_assignfeedback_editpdfplus_widget_admin_toolform($data) {
M. Chardon's avatar
M. Chardon committed
325
326
327
        $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
328
329
        return $this->render_from_template('assignfeedback_editpdfplus/tool_form', $data);
    }
M. Chardon's avatar
M. Chardon committed
330

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