MDL-61537 assignfeedback_editpdf: Rotate PDF page
[moodle.git] / mod / assign / feedback / editpdf / classes / renderer.php
CommitLineData
5c386472
DW
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * This file contains the definition for the library class for edit PDF renderer.
19 *
20 * @package assignfeedback_editpdf
21 * @copyright 2012 Davo Smith
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27/**
28 * A custom renderer class that extends the plugin_renderer_base and is used by the editpdf feedback plugin.
29 *
30 * @package assignfeedback_editpdf
31 * @copyright 2013 Davo Smith
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34class assignfeedback_editpdf_renderer extends plugin_renderer_base {
35
36 /**
37 * Return the PDF button shortcut.
38 *
39 * @param string $name the name of a specific button.
40 * @return string the specific shortcut.
41 */
42 private function get_shortcut($name) {
43
44 $shortcuts = array('navigate-previous-button' => 'j',
9432553b
NN
45 'rotateleft' => 'q',
46 'rotateright' => 'w',
5c386472
DW
47 'navigate-page-select' => 'k',
48 'navigate-next-button' => 'l',
49 'searchcomments' => 'h',
23990a0a 50 'expcolcomments' => 'g',
5c386472
DW
51 'comment' => 'z',
52 'commentcolour' => 'x',
53 'select' => 'c',
55907a73 54 'drag' => 'd',
5c386472
DW
55 'pen' => 'y',
56 'line' => 'u',
57 'rectangle' => 'i',
58 'oval' => 'o',
59 'highlight' => 'p',
60 'annotationcolour' => 'r',
61 'stamp' => 'n',
62 'currentstamp' => 'm');
63
64
65 // Return the shortcut.
66 return $shortcuts[$name];
67 }
68
69 /**
70 * Render a single colour button.
71 *
72 * @param string $icon - The key for the icon
73 * @param string $tool - The key for the lang string.
74 * @param string $accesskey Optional - The access key for the button.
75 * @param bool $disabled Optional - Is this button disabled.
76 * @return string
77 */
78 private function render_toolbar_button($icon, $tool, $accesskey = null, $disabled=false) {
79
80 // Build button alt text.
81 $alttext = new stdClass();
c156b7b7 82 $alttext->tool = get_string($tool, 'assignfeedback_editpdf');
5c386472
DW
83 if (!empty($accesskey)) {
84 $alttext->shortcut = '(Alt/Shift-Alt/Ctrl-Option + ' . $accesskey . ')';
85 } else {
86 $alttext->shortcut = '';
87 }
88 $iconalt = get_string('toolbarbutton', 'assignfeedback_editpdf', $alttext);
89
3e6adcd6 90 $iconhtml = $this->image_icon($icon, $iconalt, 'assignfeedback_editpdf');
5c386472
DW
91 $iconparams = array('data-tool'=>$tool, 'class'=>$tool . 'button');
92 if ($disabled) {
93 $iconparams['disabled'] = 'true';
94 }
95 if (!empty($accesskey)) {
96 $iconparams['accesskey'] = $accesskey;
97 }
98
99 return html_writer::tag('button', $iconhtml, $iconparams);
100 }
101
102 /**
103 * Render the editpdf widget in the grading form.
104 *
105 * @param assignfeedback_editpdf_widget $widget - Renderable widget containing assignment, user and attempt number.
106 * @return string
107 */
108 public function render_assignfeedback_editpdf_widget(assignfeedback_editpdf_widget $widget) {
109 global $CFG;
110
111 $html = '';
112
113 $html .= html_writer::div(get_string('jsrequired', 'assignfeedback_editpdf'), 'hiddenifjs');
114 $linkid = html_writer::random_id();
115 if ($widget->readonly) {
116 $launcheditorlink = html_writer::tag('a',
117 get_string('viewfeedbackonline', 'assignfeedback_editpdf'),
118 array('id'=>$linkid, 'class'=>'btn', 'href'=>'#'));
119 } else {
120 $launcheditorlink = html_writer::tag('a',
121 get_string('launcheditor', 'assignfeedback_editpdf'),
122 array('id'=>$linkid, 'class'=>'btn', 'href'=>'#'));
123 }
124 $links = $launcheditorlink;
bb690849 125 $html .= '<input type="hidden" name="assignfeedback_editpdf_haschanges" value="false"/>';
5c386472
DW
126
127 $html .= html_writer::div($links, 'visibleifjs');
128 $header = get_string('pluginname', 'assignfeedback_editpdf');
129 $body = '';
130 // Create the page navigation.
131 $navigation1 = '';
132 $navigation2 = '';
bb690849 133 $navigation3 = '';
5c386472
DW
134
135 // Pick the correct arrow icons for right to left mode.
136 if (right_to_left()) {
137 $nav_prev = 'nav_next';
138 $nav_next = 'nav_prev';
139 } else {
140 $nav_prev = 'nav_prev';
141 $nav_next = 'nav_next';
142 }
143
8e642421
TB
144 $iconshortcut = $this->get_shortcut('navigate-previous-button');
145 $iconalt = get_string('navigateprevious', 'assignfeedback_editpdf', $iconshortcut);
3e6adcd6 146 $iconhtml = $this->image_icon($nav_prev, $iconalt, 'assignfeedback_editpdf');
5c386472
DW
147 $navigation1 .= html_writer::tag('button', $iconhtml, array('disabled'=>'true',
148 'class'=>'navigate-previous-button', 'accesskey' => $this->get_shortcut('navigate-previous-button')));
ea6d8674
JM
149 $navigation1 .= html_writer::tag('select', null, array('disabled'=>'true',
150 'aria-label' => get_string('gotopage', 'assignfeedback_editpdf'), 'class'=>'navigate-page-select',
151 'accesskey' => $this->get_shortcut('navigate-page-select')));
8e642421
TB
152 $iconshortcut = $this->get_shortcut('navigate-next-button');
153 $iconalt = get_string('navigatenext', 'assignfeedback_editpdf', $iconshortcut);
3e6adcd6 154 $iconhtml = $this->image_icon($nav_next, $iconalt, 'assignfeedback_editpdf');
5c386472
DW
155 $navigation1 .= html_writer::tag('button', $iconhtml, array('disabled'=>'true',
156 'class'=>'navigate-next-button', 'accesskey' => $this->get_shortcut('navigate-next-button')));
157
158 $navigation1 = html_writer::div($navigation1, 'navigation', array('role'=>'navigation'));
159
160 $navigation2 .= $this->render_toolbar_button('comment_search', 'searchcomments', $this->get_shortcut('searchcomments'));
161 $navigation2 = html_writer::div($navigation2, 'navigation-search', array('role'=>'navigation'));
162
23990a0a
TB
163 $navigation3 .= $this->render_toolbar_button('comment_expcol', 'expcolcomments', $this->get_shortcut('expcolcomments'));
164 $navigation3 = html_writer::div($navigation3, 'navigation-expcol', array('role' => 'navigation'));
bb690849 165
9432553b
NN
166 $rotationtools = '';
167 if (!$widget->readonly) {
168 $rotationtools .= $this->render_toolbar_button('rotate_left', 'rotateleft', $this->get_shortcut('rotateleft'));
169 $rotationtools .= $this->render_toolbar_button('rotate_right', 'rotateright', $this->get_shortcut('rotateright'));
170 $rotationtools = html_writer::div($rotationtools, 'toolbar', array('role' => 'toolbar'));
171 }
172
bc87f6a2 173 $toolbargroup = '';
5c386472
DW
174 $clearfix = html_writer::div('', 'clearfix');
175 if (!$widget->readonly) {
5c386472 176 // Comments.
bc87f6a2 177 $toolbar1 = '';
5c386472
DW
178 $toolbar1 .= $this->render_toolbar_button('comment', 'comment', $this->get_shortcut('comment'));
179 $toolbar1 .= $this->render_toolbar_button('background_colour_clear', 'commentcolour', $this->get_shortcut('commentcolour'));
bc87f6a2 180 $toolbar1 = html_writer::div($toolbar1, 'toolbar', array('role' => 'toolbar'));
5c386472
DW
181
182 // Select Tool.
bc87f6a2 183 $toolbar2 = '';
55907a73 184 $toolbar2 .= $this->render_toolbar_button('drag', 'drag', $this->get_shortcut('drag'));
5c386472 185 $toolbar2 .= $this->render_toolbar_button('select', 'select', $this->get_shortcut('select'));
bc87f6a2 186 $toolbar2 = html_writer::div($toolbar2, 'toolbar', array('role' => 'toolbar'));
5c386472
DW
187
188 // Other Tools.
bc87f6a2
ZT
189 $toolbar3 = '';
190 $toolbar3 .= $this->render_toolbar_button('pen', 'pen', $this->get_shortcut('pen'));
5c386472
DW
191 $toolbar3 .= $this->render_toolbar_button('line', 'line', $this->get_shortcut('line'));
192 $toolbar3 .= $this->render_toolbar_button('rectangle', 'rectangle', $this->get_shortcut('rectangle'));
193 $toolbar3 .= $this->render_toolbar_button('oval', 'oval', $this->get_shortcut('oval'));
194 $toolbar3 .= $this->render_toolbar_button('highlight', 'highlight', $this->get_shortcut('highlight'));
195 $toolbar3 .= $this->render_toolbar_button('background_colour_clear', 'annotationcolour', $this->get_shortcut('annotationcolour'));
bc87f6a2 196 $toolbar3 = html_writer::div($toolbar3, 'toolbar', array('role' => 'toolbar'));
5c386472
DW
197
198 // Stamps.
bc87f6a2
ZT
199 $toolbar4 = '';
200 $toolbar4 .= $this->render_toolbar_button('stamp', 'stamp', $this->get_shortcut('stamp'));
5c386472
DW
201 $toolbar4 .= $this->render_toolbar_button('background_colour_clear', 'currentstamp', $this->get_shortcut('currentstamp'));
202 $toolbar4 = html_writer::div($toolbar4, 'toolbar', array('role'=>'toolbar'));
bc87f6a2
ZT
203
204 // Add toolbars to toolbar_group in order of display, and float the toolbar_group right.
9432553b 205 $toolbars = $rotationtools . $toolbar1 . $toolbar2 . $toolbar3 . $toolbar4;
bc87f6a2 206 $toolbargroup = html_writer::div($toolbars, 'toolbar_group', array('role' => 'toolbar_group'));
5c386472
DW
207 }
208
5c386472
DW
209 $pageheader = html_writer::div($navigation1 .
210 $navigation2 .
23990a0a 211 $navigation3 .
bc87f6a2 212 $toolbargroup .
5c386472
DW
213 $clearfix,
214 'pageheader');
215 $body = $pageheader;
216
aa3e4bde
JM
217 // Loading progress bar.
218 $progressbar = html_writer::div('', 'bar', array('style' => 'width: 0%'));
219 $progressbar = html_writer::div($progressbar, 'progress progress-info progress-striped active',
220 array('title' => get_string('loadingeditor', 'assignfeedback_editpdf'),
221 'role'=> 'progressbar', 'aria-valuenow' => 0, 'aria-valuemin' => 0,
222 'aria-valuemax' => 100));
223 $progressbarlabel = html_writer::div(get_string('generatingpdf', 'assignfeedback_editpdf'),
224 'progressbarlabel');
225 $loading = html_writer::div($progressbar . $progressbarlabel, 'loading');
226
5c386472 227 $canvas = html_writer::div($loading, 'drawingcanvas');
50c12f01 228 $canvas = html_writer::div($canvas, 'drawingregion');
e3511e79
DW
229 // Place for messages, but no warnings displayed yet.
230 $changesmessage = html_writer::div('', 'warningmessages');
bb690849
DW
231 $canvas .= $changesmessage;
232
e3511e79
DW
233 $infoicon = $this->image_icon('i/info', '');
234 $infomessage = html_writer::div($infoicon, 'infoicon');
235 $canvas .= $infomessage;
236
bb690849 237 $body .= $canvas;
5c386472
DW
238
239 $footer = '';
240
f7a9f1dd
AN
241 $editorparams = array(
242 array(
243 'header' => $header,
244 'body' => $body,
245 'footer' => $footer,
246 'linkid' => $linkid,
247 'assignmentid' => $widget->assignment,
248 'userid' => $widget->userid,
249 'attemptnumber' => $widget->attemptnumber,
250 'stampfiles' => $widget->stampfiles,
251 'readonly' => $widget->readonly,
252 )
253 );
5c386472
DW
254
255 $this->page->requires->yui_module('moodle-assignfeedback_editpdf-editor',
256 'M.assignfeedback_editpdf.editor.init',
257 $editorparams);
258
259 $this->page->requires->strings_for_js(array(
260 'yellow',
261 'white',
262 'red',
263 'blue',
264 'green',
265 'black',
266 'clear',
267 'colourpicker',
268 'loadingeditor',
269 'pagexofy',
270 'deletecomment',
271 'addtoquicklist',
272 'filter',
273 'searchcomments',
274 'commentcontextmenu',
275 'deleteannotation',
276 'stamp',
277 'stamppicker',
ea6d8674 278 'cannotopenpdf',
86c57709 279 'pagenumber',
e3511e79
DW
280 'partialwarning',
281 'draftchangessaved'
5c386472
DW
282 ), 'assignfeedback_editpdf');
283
284 return $html;
285 }
286}