7e1ddb55d90b04f0b939374ae50aefa36f58e503
[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             'navigate-page-select' => 'k',
46             'navigate-next-button' => 'l',
47             'searchcomments' => 'h',
48             'expcolcomments' => 'g',
49             'comment' => 'z',
50             'commentcolour' => 'x',
51             'select' => 'c',
52             'drag' => 'd',
53             'pen' => 'y',
54             'line' => 'u',
55             'rectangle' => 'i',
56             'oval' => 'o',
57             'highlight' => 'p',
58             'annotationcolour' => 'r',
59             'stamp' => 'n',
60             'currentstamp' => 'm');
63         // Return the shortcut.
64         return $shortcuts[$name];
65     }
67     /**
68      * Render a single colour button.
69      *
70      * @param string $icon - The key for the icon
71      * @param string $tool - The key for the lang string.
72      * @param string $accesskey Optional - The access key for the button.
73      * @param bool $disabled Optional - Is this button disabled.
74      * @return string
75      */
76     private function render_toolbar_button($icon, $tool, $accesskey = null, $disabled=false) {
78         // Build button alt text.
79         $alttext = new stdClass();
80         $alttext->tool = get_string($tool, 'assignfeedback_editpdf');
81         if (!empty($accesskey)) {
82             $alttext->shortcut = '(Alt/Shift-Alt/Ctrl-Option + ' . $accesskey . ')';
83         } else {
84             $alttext->shortcut = '';
85         }
86         $iconalt = get_string('toolbarbutton', 'assignfeedback_editpdf', $alttext);
88         $iconhtml = $this->image_icon($icon, $iconalt, 'assignfeedback_editpdf');
89         $iconparams = array('data-tool'=>$tool, 'class'=>$tool . 'button');
90         if ($disabled) {
91             $iconparams['disabled'] = 'true';
92         }
93         if (!empty($accesskey)) {
94             $iconparams['accesskey'] = $accesskey;
95         }
97         return html_writer::tag('button', $iconhtml, $iconparams);
98     }
100     /**
101      * Render the editpdf widget in the grading form.
102      *
103      * @param assignfeedback_editpdf_widget $widget - Renderable widget containing assignment, user and attempt number.
104      * @return string
105      */
106     public function render_assignfeedback_editpdf_widget(assignfeedback_editpdf_widget $widget) {
107         global $CFG;
109         $html = '';
111         $html .= html_writer::div(get_string('jsrequired', 'assignfeedback_editpdf'), 'hiddenifjs');
112         $linkid = html_writer::random_id();
113         if ($widget->readonly) {
114             $launcheditorlink = html_writer::tag('a',
115                                               get_string('viewfeedbackonline', 'assignfeedback_editpdf'),
116                                               array('id'=>$linkid, 'class'=>'btn', 'href'=>'#'));
117         } else {
118             $launcheditorlink = html_writer::tag('a',
119                                               get_string('launcheditor', 'assignfeedback_editpdf'),
120                                               array('id'=>$linkid, 'class'=>'btn', 'href'=>'#'));
121         }
122         $links = $launcheditorlink;
123         $html .= '<input type="hidden" name="assignfeedback_editpdf_haschanges" value="false"/>';
125         $html .= html_writer::div($links, 'visibleifjs');
126         $header = get_string('pluginname', 'assignfeedback_editpdf');
127         $body = '';
128         // Create the page navigation.
129         $navigation1 = '';
130         $navigation2 = '';
131         $navigation3 = '';
133         // Pick the correct arrow icons for right to left mode.
134         if (right_to_left()) {
135             $nav_prev = 'nav_next';
136             $nav_next = 'nav_prev';
137         } else {
138             $nav_prev = 'nav_prev';
139             $nav_next = 'nav_next';
140         }
142         $iconshortcut = $this->get_shortcut('navigate-previous-button');
143         $iconalt = get_string('navigateprevious', 'assignfeedback_editpdf', $iconshortcut);
144         $iconhtml = $this->image_icon($nav_prev, $iconalt, 'assignfeedback_editpdf');
145         $navigation1 .= html_writer::tag('button', $iconhtml, array('disabled'=>'true',
146             'class'=>'navigate-previous-button', 'accesskey' => $this->get_shortcut('navigate-previous-button')));
147         $navigation1 .= html_writer::tag('select', null, array('disabled'=>'true',
148             'aria-label' => get_string('gotopage', 'assignfeedback_editpdf'), 'class'=>'navigate-page-select',
149             'accesskey' => $this->get_shortcut('navigate-page-select')));
150         $iconshortcut = $this->get_shortcut('navigate-next-button');
151         $iconalt = get_string('navigatenext', 'assignfeedback_editpdf', $iconshortcut);
152         $iconhtml = $this->image_icon($nav_next, $iconalt, 'assignfeedback_editpdf');
153         $navigation1 .= html_writer::tag('button', $iconhtml, array('disabled'=>'true',
154             'class'=>'navigate-next-button', 'accesskey' => $this->get_shortcut('navigate-next-button')));
156         $navigation1 = html_writer::div($navigation1, 'navigation', array('role'=>'navigation'));
158         $navigation2 .= $this->render_toolbar_button('comment_search', 'searchcomments', $this->get_shortcut('searchcomments'));
159         $navigation2 = html_writer::div($navigation2, 'navigation-search', array('role'=>'navigation'));
161         $navigation3 .= $this->render_toolbar_button('comment_expcol', 'expcolcomments', $this->get_shortcut('expcolcomments'));
162         $navigation3 = html_writer::div($navigation3, 'navigation-expcol', array('role' => 'navigation'));
164         $toolbargroup = '';
165         $clearfix = html_writer::div('', 'clearfix');
166         if (!$widget->readonly) {
167             // Comments.
168             $toolbar1 = '';
169             $toolbar1 .= $this->render_toolbar_button('comment', 'comment', $this->get_shortcut('comment'));
170             $toolbar1 .= $this->render_toolbar_button('background_colour_clear', 'commentcolour', $this->get_shortcut('commentcolour'));
171             $toolbar1 = html_writer::div($toolbar1, 'toolbar', array('role' => 'toolbar'));
173             // Select Tool.
174             $toolbar2 = '';
175             $toolbar2 .= $this->render_toolbar_button('drag', 'drag', $this->get_shortcut('drag'));
176             $toolbar2 .= $this->render_toolbar_button('select', 'select', $this->get_shortcut('select'));
177             $toolbar2 = html_writer::div($toolbar2, 'toolbar', array('role' => 'toolbar'));
179             // Other Tools.
180             $toolbar3 = '';
181             $toolbar3 .= $this->render_toolbar_button('pen', 'pen', $this->get_shortcut('pen'));
182             $toolbar3 .= $this->render_toolbar_button('line', 'line', $this->get_shortcut('line'));
183             $toolbar3 .= $this->render_toolbar_button('rectangle', 'rectangle', $this->get_shortcut('rectangle'));
184             $toolbar3 .= $this->render_toolbar_button('oval', 'oval', $this->get_shortcut('oval'));
185             $toolbar3 .= $this->render_toolbar_button('highlight', 'highlight', $this->get_shortcut('highlight'));
186             $toolbar3 .= $this->render_toolbar_button('background_colour_clear', 'annotationcolour', $this->get_shortcut('annotationcolour'));
187             $toolbar3 = html_writer::div($toolbar3, 'toolbar', array('role' => 'toolbar'));
189             // Stamps.
190             $toolbar4 = '';
191             $toolbar4 .= $this->render_toolbar_button('stamp', 'stamp', $this->get_shortcut('stamp'));
192             $toolbar4 .= $this->render_toolbar_button('background_colour_clear', 'currentstamp', $this->get_shortcut('currentstamp'));
193             $toolbar4 = html_writer::div($toolbar4, 'toolbar', array('role'=>'toolbar'));
195             // Add toolbars to toolbar_group in order of display, and float the toolbar_group right.
196             $toolbars = $toolbar1 . $toolbar2 . $toolbar3 . $toolbar4;
197             $toolbargroup = html_writer::div($toolbars, 'toolbar_group', array('role' => 'toolbar_group'));
198         }
200         $pageheader = html_writer::div($navigation1 .
201                                        $navigation2 .
202                                        $navigation3 .
203                                        $toolbargroup .
204                                        $clearfix,
205                                        'pageheader');
206         $body = $pageheader;
208         // Loading progress bar.
209         $progressbar = html_writer::div('', 'bar', array('style' => 'width: 0%'));
210         $progressbar = html_writer::div($progressbar, 'progress progress-info progress-striped active',
211             array('title' => get_string('loadingeditor', 'assignfeedback_editpdf'),
212                   'role'=> 'progressbar', 'aria-valuenow' => 0, 'aria-valuemin' => 0,
213                   'aria-valuemax' => 100));
214         $progressbarlabel = html_writer::div(get_string('generatingpdf', 'assignfeedback_editpdf'),
215             'progressbarlabel');
216         $loading = html_writer::div($progressbar . $progressbarlabel, 'loading');
218         $canvas = html_writer::div($loading, 'drawingcanvas');
219         $canvas = html_writer::div($canvas, 'drawingregion');
220         // Place for messages, but no warnings displayed yet.
221         $changesmessage = html_writer::div('', 'warningmessages');
222         $canvas .= $changesmessage;
224         $infoicon = $this->image_icon('i/info', '');
225         $infomessage = html_writer::div($infoicon, 'infoicon');
226         $canvas .= $infomessage;
228         $body .= $canvas;
230         $footer = '';
232         $editorparams = array(
233             array(
234                 'header' => $header,
235                 'body' => $body,
236                 'footer' => $footer,
237                 'linkid' => $linkid,
238                 'assignmentid' => $widget->assignment,
239                 'userid' => $widget->userid,
240                 'attemptnumber' => $widget->attemptnumber,
241                 'stampfiles' => $widget->stampfiles,
242                 'readonly' => $widget->readonly,
243             )
244         );
246         $this->page->requires->yui_module('moodle-assignfeedback_editpdf-editor',
247                                           'M.assignfeedback_editpdf.editor.init',
248                                           $editorparams);
250         $this->page->requires->strings_for_js(array(
251             'yellow',
252             'white',
253             'red',
254             'blue',
255             'green',
256             'black',
257             'clear',
258             'colourpicker',
259             'loadingeditor',
260             'pagexofy',
261             'deletecomment',
262             'addtoquicklist',
263             'filter',
264             'searchcomments',
265             'commentcontextmenu',
266             'deleteannotation',
267             'stamp',
268             'stamppicker',
269             'cannotopenpdf',
270             'pagenumber',
271             'partialwarning',
272             'draftchangessaved'
273         ), 'assignfeedback_editpdf');
275         return $html;
276     }