~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to mod/assign/feedback/editpdf/classes/renderer.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
25
defined('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
 */
 
34
class 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',
 
45
            'navigate-page-select' => 'k',
 
46
            'navigate-next-button' => 'l',
 
47
            'searchcomments' => 'h',
 
48
            'comment' => 'z',
 
49
            'commentcolour' => 'x',
 
50
            'select' => 'c',
 
51
            'pen' => 'y',
 
52
            'line' => 'u',
 
53
            'rectangle' => 'i',
 
54
            'oval' => 'o',
 
55
            'highlight' => 'p',
 
56
            'annotationcolour' => 'r',
 
57
            'stamp' => 'n',
 
58
            'currentstamp' => 'm');
 
59
 
 
60
 
 
61
        // Return the shortcut.
 
62
        return $shortcuts[$name];
 
63
    }
 
64
 
 
65
    /**
 
66
     * Render a single colour button.
 
67
     *
 
68
     * @param string $icon - The key for the icon
 
69
     * @param string $tool - The key for the lang string.
 
70
     * @param string $accesskey Optional - The access key for the button.
 
71
     * @param bool $disabled Optional - Is this button disabled.
 
72
     * @return string
 
73
     */
 
74
    private function render_toolbar_button($icon, $tool, $accesskey = null, $disabled=false) {
 
75
 
 
76
        // Build button alt text.
 
77
        $alttext = new stdClass();
 
78
        $alttext->tool = $tool;
 
79
        if (!empty($accesskey)) {
 
80
            $alttext->shortcut = '(Alt/Shift-Alt/Ctrl-Option + ' . $accesskey . ')';
 
81
        } else {
 
82
            $alttext->shortcut = '';
 
83
        }
 
84
        $iconalt = get_string('toolbarbutton', 'assignfeedback_editpdf', $alttext);
 
85
 
 
86
        $iconhtml = $this->pix_icon($icon, $iconalt, 'assignfeedback_editpdf');
 
87
        $iconparams = array('data-tool'=>$tool, 'class'=>$tool . 'button');
 
88
        if ($disabled) {
 
89
            $iconparams['disabled'] = 'true';
 
90
        }
 
91
        if (!empty($accesskey)) {
 
92
            $iconparams['accesskey'] = $accesskey;
 
93
        }
 
94
 
 
95
        return html_writer::tag('button', $iconhtml, $iconparams);
 
96
    }
 
97
 
 
98
    /**
 
99
     * Render the editpdf widget in the grading form.
 
100
     *
 
101
     * @param assignfeedback_editpdf_widget $widget - Renderable widget containing assignment, user and attempt number.
 
102
     * @return string
 
103
     */
 
104
    public function render_assignfeedback_editpdf_widget(assignfeedback_editpdf_widget $widget) {
 
105
        global $CFG;
 
106
 
 
107
        $html = '';
 
108
 
 
109
        $html .= html_writer::div(get_string('jsrequired', 'assignfeedback_editpdf'), 'hiddenifjs');
 
110
        $linkid = html_writer::random_id();
 
111
        if ($widget->readonly) {
 
112
            $launcheditorlink = html_writer::tag('a',
 
113
                                              get_string('viewfeedbackonline', 'assignfeedback_editpdf'),
 
114
                                              array('id'=>$linkid, 'class'=>'btn', 'href'=>'#'));
 
115
        } else {
 
116
            $launcheditorlink = html_writer::tag('a',
 
117
                                              get_string('launcheditor', 'assignfeedback_editpdf'),
 
118
                                              array('id'=>$linkid, 'class'=>'btn', 'href'=>'#'));
 
119
        }
 
120
        $links = $launcheditorlink;
 
121
 
 
122
        $links .= html_writer::tag('div',
 
123
                                   get_string('unsavedchanges', 'assignfeedback_editpdf'),
 
124
                                   array('class'=>'assignfeedback_editpdf_unsavedchanges warning'));
 
125
 
 
126
        $html .= html_writer::div($links, 'visibleifjs');
 
127
        $header = get_string('pluginname', 'assignfeedback_editpdf');
 
128
        $body = '';
 
129
        // Create the page navigation.
 
130
        $navigation1 = '';
 
131
        $navigation2 = '';
 
132
 
 
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
        }
 
141
 
 
142
        $iconalt = get_string('navigateprevious', 'assignfeedback_editpdf');
 
143
        $iconhtml = $this->pix_icon($nav_prev, $iconalt, 'assignfeedback_editpdf');
 
144
        $navigation1 .= html_writer::tag('button', $iconhtml, array('disabled'=>'true',
 
145
            'class'=>'navigate-previous-button', 'accesskey' => $this->get_shortcut('navigate-previous-button')));
 
146
        $navigation1 .= html_writer::tag('select', null, array('disabled'=>'true',
 
147
            'aria-label' => get_string('gotopage', 'assignfeedback_editpdf'), 'class'=>'navigate-page-select',
 
148
            'accesskey' => $this->get_shortcut('navigate-page-select')));
 
149
        $iconalt = get_string('navigatenext', 'assignfeedback_editpdf');
 
150
        $iconhtml = $this->pix_icon($nav_next, $iconalt, 'assignfeedback_editpdf');
 
151
        $navigation1 .= html_writer::tag('button', $iconhtml, array('disabled'=>'true',
 
152
            'class'=>'navigate-next-button', 'accesskey' => $this->get_shortcut('navigate-next-button')));
 
153
 
 
154
        $navigation1 = html_writer::div($navigation1, 'navigation', array('role'=>'navigation'));
 
155
 
 
156
        $navigation2 .= $this->render_toolbar_button('comment_search', 'searchcomments', $this->get_shortcut('searchcomments'));
 
157
        $navigation2 = html_writer::div($navigation2, 'navigation-search', array('role'=>'navigation'));
 
158
 
 
159
        $toolbar1 = '';
 
160
        $toolbar2 = '';
 
161
        $toolbar3 = '';
 
162
        $toolbar4 = '';
 
163
        $clearfix = html_writer::div('', 'clearfix');
 
164
        if (!$widget->readonly) {
 
165
 
 
166
            // Comments.
 
167
            $toolbar1 .= $this->render_toolbar_button('comment', 'comment', $this->get_shortcut('comment'));
 
168
            $toolbar1 .= $this->render_toolbar_button('background_colour_clear', 'commentcolour', $this->get_shortcut('commentcolour'));
 
169
            $toolbar1 = html_writer::div($toolbar1, 'toolbar', array('role'=>'toolbar'));
 
170
 
 
171
            // Select Tool.
 
172
            $toolbar2 .= $this->render_toolbar_button('select', 'select', $this->get_shortcut('select'));
 
173
            $toolbar2 = html_writer::div($toolbar2, 'toolbar', array('role'=>'toolbar'));
 
174
 
 
175
            // Other Tools.
 
176
            $toolbar3 = $this->render_toolbar_button('pen', 'pen', $this->get_shortcut('pen'));
 
177
            $toolbar3 .= $this->render_toolbar_button('line', 'line', $this->get_shortcut('line'));
 
178
            $toolbar3 .= $this->render_toolbar_button('rectangle', 'rectangle', $this->get_shortcut('rectangle'));
 
179
            $toolbar3 .= $this->render_toolbar_button('oval', 'oval', $this->get_shortcut('oval'));
 
180
            $toolbar3 .= $this->render_toolbar_button('highlight', 'highlight', $this->get_shortcut('highlight'));
 
181
            $toolbar3 .= $this->render_toolbar_button('background_colour_clear', 'annotationcolour', $this->get_shortcut('annotationcolour'));
 
182
            $toolbar3 = html_writer::div($toolbar3, 'toolbar', array('role'=>'toolbar'));
 
183
 
 
184
            // Stamps.
 
185
            $toolbar4 .= $this->render_toolbar_button('stamp', 'stamp', 'n');
 
186
            $toolbar4 .= $this->render_toolbar_button('background_colour_clear', 'currentstamp', $this->get_shortcut('currentstamp'));
 
187
            $toolbar4 = html_writer::div($toolbar4, 'toolbar', array('role'=>'toolbar'));
 
188
        }
 
189
 
 
190
        // Toobars written in reverse order because they are floated right.
 
191
        $pageheader = html_writer::div($navigation1 .
 
192
                                       $navigation2 .
 
193
                                       $toolbar4 .
 
194
                                       $toolbar3 .
 
195
                                       $toolbar2 .
 
196
                                       $toolbar1 .
 
197
                                       $clearfix,
 
198
                                       'pageheader');
 
199
        $body = $pageheader;
 
200
 
 
201
        // Loading progress bar.
 
202
        $progressbar = html_writer::div('', 'bar', array('style' => 'width: 0%'));
 
203
        $progressbar = html_writer::div($progressbar, 'progress progress-info progress-striped active',
 
204
            array('title' => get_string('loadingeditor', 'assignfeedback_editpdf'),
 
205
                  'role'=> 'progressbar', 'aria-valuenow' => 0, 'aria-valuemin' => 0,
 
206
                  'aria-valuemax' => 100));
 
207
        $progressbarlabel = html_writer::div(get_string('generatingpdf', 'assignfeedback_editpdf'),
 
208
            'progressbarlabel');
 
209
        $loading = html_writer::div($progressbar . $progressbarlabel, 'loading');
 
210
 
 
211
        $canvas = html_writer::div($loading, 'drawingcanvas');
 
212
        $body .= html_writer::div($canvas, 'drawingregion');
 
213
 
 
214
        $body .= '<hr/>';
 
215
 
 
216
        $footer = '';
 
217
 
 
218
        $editorparams = array(array('header'=>$header,
 
219
                                    'body'=>$body,
 
220
                                    'footer'=>$footer,
 
221
                                    'linkid'=>$linkid,
 
222
                                    'assignmentid'=>$widget->assignment,
 
223
                                    'userid'=>$widget->userid,
 
224
                                    'attemptnumber'=>$widget->attemptnumber,
 
225
                                    'stampfiles'=>$widget->stampfiles,
 
226
                                    'readonly'=>$widget->readonly,
 
227
                                    'pagetotal'=>$widget->pagetotal));
 
228
 
 
229
        $this->page->requires->yui_module('moodle-assignfeedback_editpdf-editor',
 
230
                                          'M.assignfeedback_editpdf.editor.init',
 
231
                                          $editorparams);
 
232
 
 
233
        $this->page->requires->strings_for_js(array(
 
234
            'yellow',
 
235
            'white',
 
236
            'red',
 
237
            'blue',
 
238
            'green',
 
239
            'black',
 
240
            'clear',
 
241
            'colourpicker',
 
242
            'loadingeditor',
 
243
            'pagexofy',
 
244
            'deletecomment',
 
245
            'addtoquicklist',
 
246
            'filter',
 
247
            'searchcomments',
 
248
            'commentcontextmenu',
 
249
            'deleteannotation',
 
250
            'stamp',
 
251
            'stamppicker',
 
252
            'cannotopenpdf',
 
253
            'pagenumber'
 
254
        ), 'assignfeedback_editpdf');
 
255
 
 
256
        return $html;
 
257
    }
 
258
}