MDL-61537 assignfeedback_editpdf: Rotate PDF page
[moodle.git] / mod / assign / feedback / editpdf / classes / renderer.php
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/>.
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  */
25 defined('MOODLE_INTERNAL') || die();
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  */
34 class assignfeedback_editpdf_renderer extends plugin_renderer_base {
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) {
44         $shortcuts = array('navigate-previous-button' => 'j',
45             'rotateleft' => 'q',
46             'rotateright' => 'w',
47             'navigate-page-select' => 'k',
48             'navigate-next-button' => 'l',
49             'searchcomments' => 'h',
50             'expcolcomments' => 'g',
51             'comment' => 'z',
52             'commentcolour' => 'x',
53             'select' => 'c',
54             'drag' => 'd',
55             'pen' => 'y',
56             'line' => 'u',
57             'rectangle' => 'i',
58             'oval' => 'o',
59             'highlight' => 'p',
60             'annotationcolour' => 'r',
61             'stamp' => 'n',
62             'currentstamp' => 'm');
65         // Return the shortcut.
66         return $shortcuts[$name];
67     }
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) {
80         // Build button alt text.
81         $alttext = new stdClass();
82         $alttext->tool = get_string($tool, 'assignfeedback_editpdf');
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);
90         $iconhtml = $this->image_icon($icon, $iconalt, 'assignfeedback_editpdf');
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         }
99         return html_writer::tag('button', $iconhtml, $iconparams);
100     }
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;
111         $html = '';
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;
125         $html .= '<input type="hidden" name="assignfeedback_editpdf_haschanges" value="false"/>';
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 = '';
133         $navigation3 = '';
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         }
144         $iconshortcut = $this->get_shortcut('navigate-previous-button');
145         $iconalt = get_string('navigateprevious', 'assignfeedback_editpdf', $iconshortcut);
146         $iconhtml = $this->image_icon($nav_prev, $iconalt, 'assignfeedback_editpdf');
147         $navigation1 .= html_writer::tag('button', $iconhtml, array('disabled'=>'true',
148             'class'=>'navigate-previous-button', 'accesskey' => $this->get_shortcut('navigate-previous-button')));
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')));
152         $iconshortcut = $this->get_shortcut('navigate-next-button');
153         $iconalt = get_string('navigatenext', 'assignfeedback_editpdf', $iconshortcut);
154         $iconhtml = $this->image_icon($nav_next, $iconalt, 'assignfeedback_editpdf');
155         $navigation1 .= html_writer::tag('button', $iconhtml, array('disabled'=>'true',
156             'class'=>'navigate-next-button', 'accesskey' => $this->get_shortcut('navigate-next-button')));
158         $navigation1 = html_writer::div($navigation1, 'navigation', array('role'=>'navigation'));
160         $navigation2 .= $this->render_toolbar_button('comment_search', 'searchcomments', $this->get_shortcut('searchcomments'));
161         $navigation2 = html_writer::div($navigation2, 'navigation-search', array('role'=>'navigation'));
163         $navigation3 .= $this->render_toolbar_button('comment_expcol', 'expcolcomments', $this->get_shortcut('expcolcomments'));
164         $navigation3 = html_writer::div($navigation3, 'navigation-expcol', array('role' => 'navigation'));
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         }
173         $toolbargroup = '';
174         $clearfix = html_writer::div('', 'clearfix');
175         if (!$widget->readonly) {
176             // Comments.
177             $toolbar1 = '';
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'));
180             $toolbar1 = html_writer::div($toolbar1, 'toolbar', array('role' => 'toolbar'));
182             // Select Tool.
183             $toolbar2 = '';
184             $toolbar2 .= $this->render_toolbar_button('drag', 'drag', $this->get_shortcut('drag'));
185             $toolbar2 .= $this->render_toolbar_button('select', 'select', $this->get_shortcut('select'));
186             $toolbar2 = html_writer::div($toolbar2, 'toolbar', array('role' => 'toolbar'));
188             // Other Tools.
189             $toolbar3 = '';
190             $toolbar3 .= $this->render_toolbar_button('pen', 'pen', $this->get_shortcut('pen'));
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'));
196             $toolbar3 = html_writer::div($toolbar3, 'toolbar', array('role' => 'toolbar'));
198             // Stamps.
199             $toolbar4 = '';
200             $toolbar4 .= $this->render_toolbar_button('stamp', 'stamp', $this->get_shortcut('stamp'));
201             $toolbar4 .= $this->render_toolbar_button('background_colour_clear', 'currentstamp', $this->get_shortcut('currentstamp'));
202             $toolbar4 = html_writer::div($toolbar4, 'toolbar', array('role'=>'toolbar'));
204             // Add toolbars to toolbar_group in order of display, and float the toolbar_group right.
205             $toolbars = $rotationtools . $toolbar1 . $toolbar2 . $toolbar3 . $toolbar4;
206             $toolbargroup = html_writer::div($toolbars, 'toolbar_group', array('role' => 'toolbar_group'));
207         }
209         $pageheader = html_writer::div($navigation1 .
210                                        $navigation2 .
211                                        $navigation3 .
212                                        $toolbargroup .
213                                        $clearfix,
214                                        'pageheader');
215         $body = $pageheader;
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');
227         $canvas = html_writer::div($loading, 'drawingcanvas');
228         $canvas = html_writer::div($canvas, 'drawingregion');
229         // Place for messages, but no warnings displayed yet.
230         $changesmessage = html_writer::div('', 'warningmessages');
231         $canvas .= $changesmessage;
233         $infoicon = $this->image_icon('i/info', '');
234         $infomessage = html_writer::div($infoicon, 'infoicon');
235         $canvas .= $infomessage;
237         $body .= $canvas;
239         $footer = '';
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         );
255         $this->page->requires->yui_module('moodle-assignfeedback_editpdf-editor',
256                                           'M.assignfeedback_editpdf.editor.init',
257                                           $editorparams);
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',
278             'cannotopenpdf',
279             'pagenumber',
280             'partialwarning',
281             'draftchangessaved'
282         ), 'assignfeedback_editpdf');
284         return $html;
285     }