Gitlab CSE Unil

document_services.php 33.7 KB
Newer Older
M. Chardon's avatar
init  
M. Chardon committed
1
<?php
M. Chardon's avatar
M. Chardon committed
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
// 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/>.

/**
M. Chardon's avatar
GPL3    
M. Chardon committed
19
 * This file contains the annotation class for the assignfeedback_editpdfplus plugin
M. Chardon's avatar
init  
M. Chardon committed
20
21
 *
 * @package   assignfeedback_editpdfplus
M. Chardon's avatar
GPL3    
M. Chardon committed
22
 * @copyright  2016 Université de Lausanne
M. Chardon's avatar
init  
M. Chardon committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

namespace assignfeedback_editpdfplus;

use DOMDocument;

/**
 * Functions for generating the annotated pdf.
 *
 * This class controls the ingest of student submission files to a normalised
 * PDF 1.4 document with all submission files concatinated together. It also
 * provides the functions to generate a downloadable pdf with all comments and
 * annotations embedded.
 * @copyright 2012 Davo Smith
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class document_services {

    /** File area for generated pdf */
    const FINAL_PDF_FILEAREA = 'download';
M. Chardon's avatar
M. Chardon committed
44

M. Chardon's avatar
init  
M. Chardon committed
45
46
    /** File area for combined pdf */
    const COMBINED_PDF_FILEAREA = 'combined';
M. Chardon's avatar
M. Chardon committed
47

M. Chardon's avatar
init  
M. Chardon committed
48
49
    /** File area for importing html */
    const IMPORT_HTML_FILEAREA = 'importhtml';
M. Chardon's avatar
M. Chardon committed
50

M. Chardon's avatar
init  
M. Chardon committed
51
52
    /** File area for page images */
    const PAGE_IMAGE_FILEAREA = 'pages';
M. Chardon's avatar
M. Chardon committed
53

M. Chardon's avatar
init  
M. Chardon committed
54
55
    /** File area for readonly page images */
    const PAGE_IMAGE_READONLY_FILEAREA = 'readonlypages';
M. Chardon's avatar
M. Chardon committed
56

M. Chardon's avatar
init  
M. Chardon committed
57
58
    /** File area for the stamps */
    const STAMPS_FILEAREA = 'stamps';
M. Chardon's avatar
M. Chardon committed
59

M. Chardon's avatar
init  
M. Chardon committed
60
61
62
    /** Filename for combined pdf */
    const COMBINED_PDF_FILENAME = 'combined.pdf';

63
64
65
    /** Hash of blank pdf */
    const BLANK_PDF_HASH = '4c803c92c71f21b423d13de570c8a09e0a31c718';

M. Chardon's avatar
init  
M. Chardon committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    /** Base64 encoded blank pdf. This is the most reliable/fastest way to generate a blank pdf. */
    const BLANK_PDF_BASE64 = <<<EOD
JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURl
Y29kZT4+CnN0cmVhbQp4nDPQM1Qo5ypUMFAwALJMLU31jBQsTAz1LBSKUrnCtRTyuAIVAIcdB3IK
ZW5kc3RyZWFtCmVuZG9iagoKMyAwIG9iago0MgplbmRvYmoKCjUgMCBvYmoKPDwKPj4KZW5kb2Jq
Cgo2IDAgb2JqCjw8L0ZvbnQgNSAwIFIKL1Byb2NTZXRbL1BERi9UZXh0XQo+PgplbmRvYmoKCjEg
MCBvYmoKPDwvVHlwZS9QYWdlL1BhcmVudCA0IDAgUi9SZXNvdXJjZXMgNiAwIFIvTWVkaWFCb3hb
MCAwIDU5NSA4NDJdL0dyb3VwPDwvUy9UcmFuc3BhcmVuY3kvQ1MvRGV2aWNlUkdCL0kgdHJ1ZT4+
L0NvbnRlbnRzIDIgMCBSPj4KZW5kb2JqCgo0IDAgb2JqCjw8L1R5cGUvUGFnZXMKL1Jlc291cmNl
cyA2IDAgUgovTWVkaWFCb3hbIDAgMCA1OTUgODQyIF0KL0tpZHNbIDEgMCBSIF0KL0NvdW50IDE+
PgplbmRvYmoKCjcgMCBvYmoKPDwvVHlwZS9DYXRhbG9nL1BhZ2VzIDQgMCBSCi9PcGVuQWN0aW9u
WzEgMCBSIC9YWVogbnVsbCBudWxsIDBdCi9MYW5nKGVuLUFVKQo+PgplbmRvYmoKCjggMCBvYmoK
PDwvQ3JlYXRvcjxGRUZGMDA1NzAwNzIwMDY5MDA3NDAwNjUwMDcyPgovUHJvZHVjZXI8RkVGRjAw
NEMwMDY5MDA2MjAwNzIwMDY1MDA0RjAwNjYwMDY2MDA2OTAwNjMwMDY1MDAyMDAwMzQwMDJFMDAz
ND4KL0NyZWF0aW9uRGF0ZShEOjIwMTYwMjI2MTMyMzE0KzA4JzAwJyk+PgplbmRvYmoKCnhyZWYK
MCA5CjAwMDAwMDAwMDAgNjU1MzUgZiAKMDAwMDAwMDIyNiAwMDAwMCBuIAowMDAwMDAwMDE5IDAw
MDAwIG4gCjAwMDAwMDAxMzIgMDAwMDAgbiAKMDAwMDAwMDM2OCAwMDAwMCBuIAowMDAwMDAwMTUx
IDAwMDAwIG4gCjAwMDAwMDAxNzMgMDAwMDAgbiAKMDAwMDAwMDQ2NiAwMDAwMCBuIAowMDAwMDAw
NTYyIDAwMDAwIG4gCnRyYWlsZXIKPDwvU2l6ZSA5L1Jvb3QgNyAwIFIKL0luZm8gOCAwIFIKL0lE
IFsgPEJDN0REQUQwRDQyOTQ1OTQ2OUU4NzJCMjI1ODUyNkU4Pgo8QkM3RERBRDBENDI5NDU5NDY5
RTg3MkIyMjU4NTI2RTg+IF0KL0RvY0NoZWNrc3VtIC9BNTYwMEZCMDAzRURCRTg0MTNBNTk3RTZF
MURDQzJBRgo+PgpzdGFydHhyZWYKNzM2CiUlRU9GCg==
EOD;
89
90
    const NOPERMISSIONMESSAGE = "nopermission";
    const PLUGIN_NAME = "assignfeedback_editpdfplus";
M. Chardon's avatar
init  
M. Chardon committed
91
92
93
94
95
96
97
98
99
100
101
102
103

    /**
     * This function will take an int or an assignment instance and
     * return an assignment instance. It is just for convenience.
     * @param int|\assign $assignment
     * @return assign
     */
    private static function get_assignment_from_param($assignment) {
        global $CFG;

        require_once($CFG->dirroot . '/mod/assign/locallib.php');

        if (!is_object($assignment)) {
104
            $cm = get_coursemodule_from_instance('assign', $assignment, 0, false, MUST_EXIST);
M. Chardon's avatar
init  
M. Chardon committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
            $context = \context_module::instance($cm->id);

            $assignment = new \assign($context, null, null);
        }
        return $assignment;
    }

    /**
     * Get a hash that will be unique and can be used in a path name.
     * @param int|\assign $assignment
     * @param int $userid
     * @param int $attemptnumber (-1 means latest attempt)
     */
    private static function hash($assignment, $userid, $attemptnumber) {
        if (is_object($assignment)) {
            $assignmentid = $assignment->get_instance()->id;
        } else {
            $assignmentid = $assignment;
        }
        return sha1($assignmentid . '_' . $userid . '_' . $attemptnumber);
    }

    /**
     * Use a DOM parser to accurately replace images with their alt text.
     * @param string $html
     * @return string New html with no image tags.
     */
    protected static function strip_images($html) {
        $dom = new DOMDocument();
134
        $dom->loadHTML("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" . $html);
M. Chardon's avatar
init  
M. Chardon committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
        $images = $dom->getElementsByTagName('img');
        $i = 0;

        for ($i = ($images->length - 1); $i >= 0; $i--) {
            $node = $images->item($i);

            if ($node->hasAttribute('alt')) {
                $replacement = ' [ ' . $node->getAttribute('alt') . ' ] ';
            } else {
                $replacement = ' ';
            }

            $text = $dom->createTextNode($replacement);
            $node->parentNode->replaceChild($text, $node);
        }
150
151
        $count = 1;
        return str_replace("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>", "", $dom->saveHTML(), $count);
M. Chardon's avatar
init  
M. Chardon committed
152
153
154
155
156
157
    }

    /**
     * This function will search for all files that can be converted
     * and concatinated into a PDF (1.4) - for any submission plugin
     * for this students attempt.
158
     * 
M. Chardon's avatar
init  
M. Chardon committed
159
160
161
     * @param int|\assign $assignment
     * @param int $userid
     * @param int $attemptnumber (-1 means latest attempt)
162
     * @return combined_document
M. Chardon's avatar
init  
M. Chardon committed
163
     */
164
    protected static function list_compatible_submission_files_for_attempt($assignment, $userid, $attemptnumber) {
165
        global $DB;
M. Chardon's avatar
init  
M. Chardon committed
166
167
168
169
170

        $assignment = self::get_assignment_from_param($assignment);

        // Capability checks.
        if (!$assignment->can_view_submission($userid)) {
171
            print_error(self::NOPERMISSIONMESSAGE);
M. Chardon's avatar
init  
M. Chardon committed
172
173
174
175
176
        }

        $files = array();

        if ($assignment->get_instance()->teamsubmission) {
177
            $submission = $assignment->get_group_submission($userid, 0, false, $attemptnumber);
M. Chardon's avatar
init  
M. Chardon committed
178
        } else {
179
            $submission = $assignment->get_user_submission($userid, false, $attemptnumber);
M. Chardon's avatar
init  
M. Chardon committed
180
181
182
183
184
        }
        $user = $DB->get_record('user', array('id' => $userid));

        // User has not submitted anything yet.
        if (!$submission) {
185
            return new combined_document();
M. Chardon's avatar
init  
M. Chardon committed
186
187
        }

188
        $converter = new \core_files\converter();
M. Chardon's avatar
init  
M. Chardon committed
189
190
        // Ask each plugin for it's list of files.
        foreach ($assignment->get_submission_plugins() as $plugin) {
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
            if (!$plugin->is_enabled() || !$plugin->is_visible()) {
                continue;
            }
            $pluginfiles = $plugin->get_files($submission, $user);
            foreach ($pluginfiles as $filename => $file) {
                if ($file instanceof \stored_file) {
                    if ($file->get_mimetype() === 'application/pdf') {
                        $files[$filename] = $file;
                    } else if ($convertedfile = $converter->start_conversion($file, 'pdf')) {
                        $files[$filename] = $convertedfile;
                    }
                } else if ($converter->can_convert_format_to('html', 'pdf')) {
                    $record = new \stdClass();
                    $record->contextid = $assignment->get_context()->id;
                    $record->component = self::PLUGIN_NAME;
                    $record->filearea = self::IMPORT_HTML_FILEAREA;
                    $record->itemid = $submission->id;
                    $record->filepath = '/';
                    $record->filename = $plugin->get_type() . '-' . $filename;

                    $convertedfile = self::convertDocHtmlToPdf($converter, $file, $record);

                    if ($convertedfile) {
                        $files[$filename] = $convertedfile;
M. Chardon's avatar
init  
M. Chardon committed
215
216
217
218
                    }
                }
            }
        }
219
220
221
222
        $combineddocument = new combined_document();
        $combineddocument->set_source_files($files);

        return $combineddocument;
M. Chardon's avatar
init  
M. Chardon committed
223
224
    }

225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
    /**
     * Create a PDF file from a HTML file
     * @param \core_files\converter $converter
     * @param string $file Html file
     * @param \stdClass $fileProperties
     * @return conversion PDF file
     */
    protected static function convertDocHtmlToPdf(\core_files\converter $converter, $file, \stdClass $fileProperties) {
        // Create a tmp stored_file from this html string.
        $file = reset($file);
        // Strip image tags, because they will not be resolvable.
        $file = self::strip_images($file);

        $fs = get_file_storage();
        $htmlfile = $fs->get_file($fileProperties->contextid, $fileProperties->component, $fileProperties->filearea, $fileProperties->itemid, $fileProperties->filepath, $fileProperties->filename);

        $newhash = sha1($file);

        // If the file exists, and the content hash doesn't match, remove it.
        if ($htmlfile && $newhash !== $htmlfile->get_contenthash()) {
            $htmlfile->delete();
            $htmlfile = false;
        }

        // If the file doesn't exist, or if it was removed above, create a new one.
        if (!$htmlfile) {
            $htmlfile = $fs->create_file_from_string($fileProperties, $file);
        }

M. Chardon's avatar
M. Chardon committed
254
        return $converter->start_conversion($htmlfile, 'pdf');
255
256
    }

M. Chardon's avatar
init  
M. Chardon committed
257
    /**
258
259
     * Fetch the current combined document ready for state checking.
     *
M. Chardon's avatar
init  
M. Chardon committed
260
261
262
     * @param int|\assign $assignment
     * @param int $userid
     * @param int $attemptnumber (-1 means latest attempt)
263
     * @return combined_document
M. Chardon's avatar
init  
M. Chardon committed
264
     */
265
    public static function get_combined_document_for_attempt($assignment, $userid, $attemptnumber) {
M. Chardon's avatar
init  
M. Chardon committed
266
267
268
269
270

        $assignment = self::get_assignment_from_param($assignment);

        // Capability checks.
        if (!$assignment->can_view_submission($userid)) {
271
            print_error(self::NOPERMISSIONMESSAGE);
M. Chardon's avatar
init  
M. Chardon committed
272
273
274
275
        }

        $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
        if ($assignment->get_instance()->teamsubmission) {
276
            $submission = $assignment->get_group_submission($userid, 0, false, $attemptnumber);
M. Chardon's avatar
init  
M. Chardon committed
277
        } else {
278
            $submission = $assignment->get_user_submission($userid, false, $attemptnumber);
M. Chardon's avatar
init  
M. Chardon committed
279
280
281
        }

        $contextid = $assignment->get_context()->id;
282
        $component = self::PLUGIN_NAME;
M. Chardon's avatar
init  
M. Chardon committed
283
284
285
286
        $filearea = self::COMBINED_PDF_FILEAREA;
        $itemid = $grade->id;
        $filepath = '/';
        $filename = self::COMBINED_PDF_FILENAME;
287
        $fs = get_file_storage();
M. Chardon's avatar
init  
M. Chardon committed
288
289

        $combinedpdf = $fs->get_file($contextid, $component, $filearea, $itemid, $filepath, $filename);
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
        if ($combinedpdf && $submission) {
            if ($combinedpdf->get_timemodified() < $submission->timemodified) {
                // The submission has been updated since the PDF was generated.
                $combinedpdf = false;
            } else if ($combinedpdf->get_contenthash() == self::BLANK_PDF_HASH) {
                // The PDF is for a blank page.
                $combinedpdf = false;
            }
        }

        if (empty($combinedpdf)) {
            // The combined PDF does not exist yet. Return the list of files to be combined.
            return self::list_compatible_submission_files_for_attempt($assignment, $userid, $attemptnumber);
        } else {
            // The combined PDF aleady exists. Return it in a new combined_document object.
            $combineddocument = new combined_document();
            return $combineddocument->set_combined_file($combinedpdf);
M. Chardon's avatar
init  
M. Chardon committed
307
308
309
310
        }
    }

    /**
311
312
     * This function return the combined pdf for all valid submission files.
     * 
M. Chardon's avatar
init  
M. Chardon committed
313
314
315
     * @param int|\assign $assignment
     * @param int $userid
     * @param int $attemptnumber (-1 means latest attempt)
316
     * @return combined_document
M. Chardon's avatar
init  
M. Chardon committed
317
     */
318
319
    public static function get_combined_pdf_for_attempt($assignment, $userid, $attemptnumber) {
        $document = self::get_combined_document_for_attempt($assignment, $userid, $attemptnumber);
M. Chardon's avatar
init  
M. Chardon committed
320

321
322
323
        if ($document->get_status() === combined_document::STATUS_COMPLETE) {
            // The combined document is already ready.
            return $document;
M. Chardon's avatar
init  
M. Chardon committed
324
        } else {
325
326
327
328
329
            // Attempt to combined the files in the document.
            $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
            $document->combine_files($assignment->get_context()->id, $grade->id);
            return $document;
        }
M. Chardon's avatar
init  
M. Chardon committed
330
331
332
333
    }

    /**
     * This function will return the number of pages of a pdf.
334
     * 
M. Chardon's avatar
init  
M. Chardon committed
335
336
337
338
339
340
341
342
343
344
345
346
347
348
     * @param int|\assign $assignment
     * @param int $userid
     * @param int $attemptnumber (-1 means latest attempt)
     * @param bool $readonly When true we get the number of pages for the readonly version.
     * @return int number of pages
     */
    public static function page_number_for_attempt($assignment, $userid, $attemptnumber, $readonly = false) {
        global $CFG;

        require_once($CFG->libdir . '/pdflib.php');

        $assignment = self::get_assignment_from_param($assignment);

        if (!$assignment->can_view_submission($userid)) {
349
            print_error(self::NOPERMISSIONMESSAGE);
M. Chardon's avatar
init  
M. Chardon committed
350
351
352
353
354
355
356
        }

        // When in readonly we can return the number of images in the DB because they should already exist,
        // if for some reason they do not, then we proceed as for the normal version.
        if ($readonly) {
            $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
            $fs = get_file_storage();
357
            $files = $fs->get_directory_files($assignment->get_context()->id, self::PLUGIN_NAME, self::PAGE_IMAGE_READONLY_FILEAREA, $grade->id, '/');
M. Chardon's avatar
init  
M. Chardon committed
358
359
360
361
362
363
364
            $pagecount = count($files);
            if ($pagecount > 0) {
                return $pagecount;
            }
        }

        // Get a combined pdf file from all submitted pdf files.
365
366
        $document = self::get_combined_pdf_for_attempt($assignment, $userid, $attemptnumber);
        return $document->get_page_count();
M. Chardon's avatar
init  
M. Chardon committed
367
368
369
370
371
372
373
374
375
    }

    /**
     * This function will generate and return a list of the page images from a pdf.
     * @param int|\assign $assignment
     * @param int $userid
     * @param int $attemptnumber (-1 means latest attempt)
     * @return array(stored_file)
     */
376
    protected static function generate_page_images_for_attempt($assignment, $userid, $attemptnumber) {
M. Chardon's avatar
init  
M. Chardon committed
377
378
379
380
381
382
383
        global $CFG;

        require_once($CFG->libdir . '/pdflib.php');

        $assignment = self::get_assignment_from_param($assignment);

        if (!$assignment->can_view_submission($userid)) {
384
            print_error(self::NOPERMISSIONMESSAGE);
M. Chardon's avatar
init  
M. Chardon committed
385
386
387
        }

        // Need to generate the page images - first get a combined pdf.
388
389
390
391
392
393
394
395
        $document = self::get_combined_pdf_for_attempt($assignment, $userid, $attemptnumber);

        $status = $document->get_status();
        if ($status === combined_document::STATUS_FAILED) {
            print_error('Could not generate combined pdf.');
        } else if ($status === combined_document::STATUS_PENDING_INPUT) {
            // The conversion is still in progress.
            return [];
M. Chardon's avatar
init  
M. Chardon committed
396
397
398
399
        }

        $tmpdir = \make_temp_directory('assignfeedback_editpdfplus/pageimages/' . self::hash($assignment, $userid, $attemptnumber));
        $combined = $tmpdir . '/' . self::COMBINED_PDF_FILENAME;
400
        $document->get_combined_file()->copy_content_to($combined); // Copy the file.
M. Chardon's avatar
init  
M. Chardon committed
401
402
403
404
405
406
407
408
409
410

        $pdf = new pdf();

        $pdf->set_image_folder($tmpdir);
        $pagecount = $pdf->set_pdf($combined);

        $grade = $assignment->get_user_grade($userid, true, $attemptnumber);

        $record = new \stdClass();
        $record->contextid = $assignment->get_context()->id;
411
        $record->component = self::PLUGIN_NAME;
M. Chardon's avatar
init  
M. Chardon committed
412
413
414
        $record->filearea = self::PAGE_IMAGE_FILEAREA;
        $record->itemid = $grade->id;
        $record->filepath = '/';
415
        $fs = get_file_storage();
M. Chardon's avatar
init  
M. Chardon committed
416
417
418
419
420
421

        // Remove the existing content of the filearea.
        $fs->delete_area_files($record->contextid, $record->component, $record->filearea, $record->itemid);

        $files = array();
        for ($i = 0; $i < $pagecount; $i++) {
422
423
424
425
426
427
            try {
                $image = $pdf->get_image($i);
            } catch (\moodle_exception $e) {
                // We catch only moodle_exception here as other exceptions indicate issue with setup not the pdf.
                $image = pdf::get_error_image($tmpdir, $i);
            }
M. Chardon's avatar
init  
M. Chardon committed
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
            $record->filename = basename($image);
            $files[$i] = $fs->create_file_from_pathname($record, $tmpdir . '/' . $image);
            @unlink($tmpdir . '/' . $image);
        }
        $pdf->Close(); // PDF loaded and never saved/outputted needs to be closed.

        @unlink($combined);
        @rmdir($tmpdir);

        return $files;
    }

    /**
     * This function returns a list of the page images from a pdf.
     *
     * The readonly version is different than the normal one. The readonly version contains a copy
     * of the pages in the state they were when the PDF was annotated, by doing so we prevent the
     * the pages that are displayed to change as soon as the submission changes.
     *
     * Though there is an edge case, if the PDF was annotated before MDL-45580, then it is possible
     * that we do not find any readonly version of the pages. In that case, we will get the normal
     * pages and copy them to the readonly area. This ensures that the pages will remain in that
     * state until the submission is updated. When the normal files do not exist, we throw an exception
     * because the readonly pages should only ever be displayed after a teacher has annotated the PDF,
     * they would not exist until they do.
     *
     * @param int|\assign $assignment
     * @param int $userid
     * @param int $attemptnumber (-1 means latest attempt)
     * @param bool $readonly If true, then we are requesting the readonly version.
     * @return array(stored_file)
     */
    public static function get_page_images_for_attempt($assignment, $userid, $attemptnumber, $readonly = false) {

        $assignment = self::get_assignment_from_param($assignment);

        if (!$assignment->can_view_submission($userid)) {
465
            print_error(self::NOPERMISSIONMESSAGE);
M. Chardon's avatar
init  
M. Chardon committed
466
467
468
        }

        if ($assignment->get_instance()->teamsubmission) {
469
            $submission = $assignment->get_group_submission($userid, 0, false, $attemptnumber);
M. Chardon's avatar
init  
M. Chardon committed
470
        } else {
471
            $submission = $assignment->get_user_submission($userid, false, $attemptnumber);
M. Chardon's avatar
init  
M. Chardon committed
472
473
474
475
        }
        $grade = $assignment->get_user_grade($userid, true, $attemptnumber);

        $contextid = $assignment->get_context()->id;
476
        $component = self::PLUGIN_NAME;
M. Chardon's avatar
init  
M. Chardon committed
477
478
479
480
        $itemid = $grade->id;
        $filepath = '/';
        $filearea = self::PAGE_IMAGE_FILEAREA;

481
        $fs = get_file_storage();
M. Chardon's avatar
init  
M. Chardon committed
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498

        // If we are after the readonly pages...
        if ($readonly) {
            $filearea = self::PAGE_IMAGE_READONLY_FILEAREA;
            if ($fs->is_area_empty($contextid, $component, $filearea, $itemid)) {
                // We have a problem here, we were supposed to find the files.
                // Attempt to re-generate the pages from the combined images.
                self::generate_page_images_for_attempt($assignment, $userid, $attemptnumber);
                self::copy_pages_to_readonly_area($assignment, $grade);
            }
        }

        $files = $fs->get_directory_files($contextid, $component, $filearea, $itemid, $filepath);

        $pages = array();
        if (!empty($files)) {
            $first = reset($files);
499
500
501
502
503
504
505
            $pagemodified = $first->get_timemodified();
            // Check that we don't just have a single blank page. The hash of a blank page image can vary with
            // the version of ghostscript used, so we need to examine the combined pdf it was generated from.
            $blankpage = false;
            if (!$readonly && count($files) == 1) {
                $pdfarea = self::COMBINED_PDF_FILEAREA;
                $pdfname = self::COMBINED_PDF_FILENAME;
506
507
508
509
                $pdf = $fs->get_file($contextid, $component, $pdfarea, $itemid, $filepath, $pdfname);
                // The combined pdf may have a different hash if it has been regenerated since the page
                // image was created. However if this is the case the page image will be stale anyway.
                $blankpage = $pdf && ($pdf->get_contenthash() == self::BLANK_PDF_HASH || $pagemodified < $pdf->get_timemodified());
510
511
            }
            if (!$readonly && ($pagemodified < $submission->timemodified || $blankpage)) {
M. Chardon's avatar
init  
M. Chardon committed
512
513
514
515
516
517
518
519
520
                // Image files are stale, we need to regenerate them, except in readonly mode.
                // We also need to remove the draft annotations and comments associated with this attempt.
                $fs->delete_area_files($contextid, $component, $filearea, $itemid);
                page_editor::delete_draft_content($itemid);
                $files = array();
            } else {

                // Need to reorder the files following their name.
                // because get_directory_files() return a different order than generate_page_images_for_attempt().
M. Chardon's avatar
M. Chardon committed
521
                foreach ($files as $file) {
M. Chardon's avatar
init  
M. Chardon committed
522
523
                    // Extract the page number from the file name image_pageXXXX.png.
                    preg_match('/page([\d]+)\./', $file->get_filename(), $matches);
M. Chardon's avatar
M. Chardon committed
524
                    if (empty($matches) or ! is_numeric($matches[1])) {
M. Chardon's avatar
init  
M. Chardon committed
525
                        throw new \coding_exception("'" . $file->get_filename()
M. Chardon's avatar
M. Chardon committed
526
                        . "' file hasn't the expected format filename: image_pageXXXX.png.");
M. Chardon's avatar
init  
M. Chardon committed
527
                    }
M. Chardon's avatar
M. Chardon committed
528
                    $pagenumber = (int) $matches[1];
M. Chardon's avatar
init  
M. Chardon committed
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564

                    // Save the page in the ordered array.
                    $pages[$pagenumber] = $file;
                }
                ksort($pages);
            }
        }

        if (empty($pages)) {
            if ($readonly) {
                // This should never happen, there should be a version of the pages available
                // whenever we are requesting the readonly version.
                throw new \moodle_exception('Could not find readonly pages for grade ' . $grade->id);
            }
            $pages = self::generate_page_images_for_attempt($assignment, $userid, $attemptnumber);
        }

        return $pages;
    }

    /**
     * This function returns sensible filename for a feedback file.
     * @param int|\assign $assignment
     * @param int $userid
     * @param int $attemptnumber (-1 means latest attempt)
     * @return string
     */
    protected static function get_downloadable_feedback_filename($assignment, $userid, $attemptnumber) {
        global $DB;

        $assignment = self::get_assignment_from_param($assignment);

        $groupmode = groups_get_activity_groupmode($assignment->get_course_module());
        $groupname = '';
        if ($groupmode) {
            $groupid = groups_get_activity_group($assignment->get_course_module(), true);
M. Chardon's avatar
M. Chardon committed
565
            $groupname = groups_get_group_name($groupid) . '-';
M. Chardon's avatar
init  
M. Chardon committed
566
567
568
569
570
        }
        if ($groupname == '-') {
            $groupname = '';
        }
        $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
M. Chardon's avatar
M. Chardon committed
571
        $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
M. Chardon's avatar
init  
M. Chardon committed
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598

        if ($assignment->is_blind_marking()) {
            $prefix = $groupname . get_string('participant', 'assign');
            $prefix = str_replace('_', ' ', $prefix);
            $prefix = clean_filename($prefix . '_' . $assignment->get_uniqueid_for_user($userid) . '_');
        } else {
            $prefix = $groupname . fullname($user);
            $prefix = str_replace('_', ' ', $prefix);
            $prefix = clean_filename($prefix . '_' . $assignment->get_uniqueid_for_user($userid) . '_');
        }
        $prefix .= $grade->attemptnumber;

        return $prefix . '.pdf';
    }

    /**
     * This function takes the combined pdf and embeds all the comments and annotations.
     *
     * This also moves the annotations and comments from drafts to not drafts. And it will
     * copy all the images stored to the readonly area, so that they can be viewed online, and
     * not be overwritten when a new submission is sent.
     *
     * @param int|\assign $assignment
     * @param int $userid
     * @param int $attemptnumber (-1 means latest attempt)
     * @return stored_file
     */
M. Chardon's avatar
M. Chardon committed
599
    public static function generate_feedback_document($assignment, $userid, $attemptnumber, $refresh = false) {
M. Chardon's avatar
init  
M. Chardon committed
600
601
602

        $assignment = self::get_assignment_from_param($assignment);

603
        if (!$refresh && !$assignment->can_view_submission($userid)) {
604
            print_error(self::NOPERMISSIONMESSAGE);
M. Chardon's avatar
init  
M. Chardon committed
605
        }
606
        if (!$refresh && !$assignment->can_grade()) {
607
            print_error(self::NOPERMISSIONMESSAGE);
M. Chardon's avatar
init  
M. Chardon committed
608
609
610
        }

        // Need to generate the page images - first get a combined pdf.
611
612
613
614
615
616
617
618
        $document = self::get_combined_pdf_for_attempt($assignment, $userid, $attemptnumber);

        $status = $document->get_status();
        if ($status === combined_document::STATUS_FAILED) {
            print_error('Could not generate combined pdf.');
        } else if ($status === combined_document::STATUS_PENDING_INPUT) {
            // The conversion is still in progress.
            return false;
M. Chardon's avatar
init  
M. Chardon committed
619
620
        }

621
622
        $file = $document->get_combined_file();
        $tmpdir = make_temp_directory('assignfeedback_editpdfplus/final/' . self::hash($assignment, $userid, $attemptnumber));
M. Chardon's avatar
init  
M. Chardon committed
623
624
625
626
        $combined = $tmpdir . '/' . self::COMBINED_PDF_FILENAME;
        $file->copy_content_to($combined); // Copy the file.

        $pdf = new pdf();
M. Chardon's avatar
M. Chardon committed
627

M. Chardon's avatar
init  
M. Chardon committed
628
629
        $pagecount = $pdf->set_pdf($combined);
        $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
M. Chardon's avatar
M. Chardon committed
630
631
632
        if (!$refresh) {
            page_editor::release_drafts($grade->id);
        }
M. Chardon's avatar
init  
M. Chardon committed
633

634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
        $pdf = self::addAnnotationstoPDF($pdf, $pagecount, $grade->id);

        $filenameRaw = self::get_downloadable_feedback_filename($assignment, $userid, $attemptnumber);
        $filename = clean_param($filenameRaw, PARAM_FILE);

        $generatedpdf = $tmpdir . '/' . $filename;
        $pdf->save_pdf($generatedpdf);

        $record = new \stdClass();

        $record->contextid = $assignment->get_context()->id;
        $record->component = self::PLUGIN_NAME;
        $record->filearea = self::FINAL_PDF_FILEAREA;
        $record->itemid = $grade->id;
        $record->filepath = '/';
        $record->filename = $filename;

        // Only keep one current version of the generated pdf.
        $fs = get_file_storage();
        $fs->delete_area_files($record->contextid, $record->component, $record->filearea, $record->itemid);

        $file = $fs->create_file_from_pathname($record, $generatedpdf);

        // Cleanup.
        @unlink($generatedpdf);
        @unlink($combined);
        @rmdir($tmpdir);

        self::copy_pages_to_readonly_area($assignment, $grade);

        return $file;
    }

    /**
     * Write annotations and feedback into the combined pdf file.
     * @param pdf $pdf init pdf
     * @param int $pagecount number of pages of the pdf
     * @param int $gradeid grade id
     * @return pdf init pdf with annotations and feedback
     */
    protected static function addAnnotationstoPDF($pdf, $pagecount, $gradeid) {
675
676
677
        $annotation_index = [];
        $compteur = 1;

M. Chardon's avatar
init  
M. Chardon committed
678
679
        for ($i = 0; $i < $pagecount; $i++) {
            $pdf->copy_page();
680
            $annotations = page_editor::get_annotations($gradeid, $i, false);
M. Chardon's avatar
init  
M. Chardon committed
681
682

            foreach ($annotations as $annotation) {
M. Chardon's avatar
M. Chardon committed
683
                $pdf->add_annotation($annotation, $annotation->path, $compteur);
684
685
                if (!$annotation->hasReadableComment()) {
                    continue;
686
                }
687
688
                $annotation_index[$annotation->id] = $compteur;
                $compteur++;
689
690
            }
        }
691

692
        //add feedback by annotation
M. Chardon's avatar
M. Chardon committed
693
        $pdf->SetAutoPageBreak(true);
694
695
696
697
        $pdf->AddPage();
        $pdf->SetTextColor(0, 0, 0);
        $pdf->Write(10, "", '', false, 'L', true);
        $pdf->SetFontSize(14);
M. Chardon's avatar
M. Chardon committed
698
699
        $pdf->SetLeftMargin(40);
        $pdf->SetTopMargin(20);
700
        $pdf->Write(5, "Références des annotations", '', false, 'L', true);
M. Chardon's avatar
M. Chardon committed
701
        $pdf->SetFontSize(8);
702
        $pdf->Write(10, "", '', false, 'L', true);
M. Chardon's avatar
M. Chardon committed
703
704
705
706
        $pdf->SetTextColor(255, 0, 0);
        $pdf->Write(5, '*', '', false, 'L', false);
        $pdf->SetTextColor(0, 0, 0);
        $pdf->Write(5, " : annotation marquée comme \"non comprise\"", '', false, 'L', true);
707
        $pdf->SetFontSize(10);
M. Chardon's avatar
M. Chardon committed
708
        $pdf->Write(10, "", '', false, 'L', true);
709
710
        foreach ($annotation_index as $id => $index) {
            $annot = page_editor::get_annotation($id);
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
            if (!$annot->hasReadableComment()) {
                continue;
            }
            $pdf->Write(5, $index, '', false, 'L', false);
            if ($annot->studentstatus === "2") {
                $pdf->SetTextColor(255, 0, 0);
                $pdf->Write(5, '*', '', false, 'L', false);
                $pdf->SetTextColor(0, 0, 0);
            }
            $pdf->Write(5, " : ", '', false, 'L', false);
            if ($annot->answerrequested) {
                $pdf->SetTextColor(255, 0, 0);
                $pdf->Write(5, "[question] ", '', false, 'L', true);
                $pdf->SetTextColor(0, 0, 0);
            }
            $pdf->Write(5, $annot->textannot, '', false, 'L', true);
            if ($annot->studentanswer) {
                $pdf->SetTextColor(0, 0, 255);
                $pdf->Write(5, "[réponse]", '', false, 'L', true);
                $pdf->Write(5, $annot->studentanswer, '', false, 'L', true);
                $pdf->SetTextColor(0, 0, 0);
M. Chardon's avatar
init  
M. Chardon committed
732
            }
733
            $pdf->Write(0, "", '', false, 'L', true);
M. Chardon's avatar
init  
M. Chardon committed
734
        }
735
        //add general feedback in last page
736
737
738
739
        $pdf->AddPage();
        $pdf->SetTextColor(0, 0, 0);
        $pdf->Write(10, "", '', false, 'L', true);
        $pdf->SetFontSize(14);
M. Chardon's avatar
M. Chardon committed
740
741
        $pdf->SetLeftMargin(40);
        $pdf->SetTopMargin(20);
742
743
744
        $pdf->Write(5, "Feedback global", '', false, 'L', true);
        $pdf->Write(10, "", '', false, 'L', true);
        $pdf->SetFontSize(10);
745
        $finalcomment = page_editor::get_feedback_comments($gradeid);
M. Chardon's avatar
M. Chardon committed
746
        $pdf->writeHTMLCell(0, 0, 40, 70, $finalcomment->commenttext);
M. Chardon's avatar
init  
M. Chardon committed
747

748
        return $pdf;
M. Chardon's avatar
init  
M. Chardon committed
749
750
751
752
753
754
755
756
757
758
759
760
761
    }

    /**
     * Copy the pages image to the readonly area.
     *
     * @param int|\assign $assignment The assignment.
     * @param \stdClass $grade The grade record.
     * @return void
     */
    public static function copy_pages_to_readonly_area($assignment, $grade) {
        $fs = get_file_storage();
        $assignment = self::get_assignment_from_param($assignment);
        $contextid = $assignment->get_context()->id;
762
        $component = self::PLUGIN_NAME;
M. Chardon's avatar
init  
M. Chardon committed
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
        $itemid = $grade->id;

        // Get all the pages.
        $originalfiles = $fs->get_area_files($contextid, $component, self::PAGE_IMAGE_FILEAREA, $itemid);
        if (empty($originalfiles)) {
            // Nothing to do here...
            return;
        }

        // Delete the old readonly files.
        $fs->delete_area_files($contextid, $component, self::PAGE_IMAGE_READONLY_FILEAREA, $itemid);

        // Do the copying.
        foreach ($originalfiles as $originalfile) {
            $fs->create_file_from_storedfile(array('filearea' => self::PAGE_IMAGE_READONLY_FILEAREA), $originalfile);
        }
    }

    /**
     * This function returns the generated pdf (if it exists).
     * @param int|\assign $assignment
     * @param int $userid
     * @param int $attemptnumber (-1 means latest attempt)
     * @return stored_file
     */
    public static function get_feedback_document($assignment, $userid, $attemptnumber) {

        $assignment = self::get_assignment_from_param($assignment);

        if (!$assignment->can_view_submission($userid)) {
793
            print_error(self::NOPERMISSIONMESSAGE);
M. Chardon's avatar
init  
M. Chardon committed
794
795
796
797
798
        }

        $grade = $assignment->get_user_grade($userid, true, $attemptnumber);

        $contextid = $assignment->get_context()->id;
799
        $component = self::PLUGIN_NAME;
M. Chardon's avatar
init  
M. Chardon committed
800
801
802
803
        $filearea = self::FINAL_PDF_FILEAREA;
        $itemid = $grade->id;
        $filepath = '/';

804
        $fs = get_file_storage();
M. Chardon's avatar
M. Chardon committed
805
        $files = $fs->get_area_files($contextid, $component, $filearea, $itemid, "itemid, filepath, filename", false);
M. Chardon's avatar
init  
M. Chardon committed
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
        if ($files) {
            return reset($files);
        }
        return false;
    }

    /**
     * This function deletes the generated pdf for a student.
     * @param int|\assign $assignment
     * @param int $userid
     * @param int $attemptnumber (-1 means latest attempt)
     * @return bool
     */
    public static function delete_feedback_document($assignment, $userid, $attemptnumber) {

        $assignment = self::get_assignment_from_param($assignment);

        if (!$assignment->can_view_submission($userid)) {
824
            print_error(self::NOPERMISSIONMESSAGE);
M. Chardon's avatar
init  
M. Chardon committed
825
826
        }
        if (!$assignment->can_grade()) {
827
            print_error(self::NOPERMISSIONMESSAGE);
M. Chardon's avatar
init  
M. Chardon committed
828
829
830
831
832
        }

        $grade = $assignment->get_user_grade($userid, true, $attemptnumber);

        $contextid = $assignment->get_context()->id;
833
        $component = self::PLUGIN_NAME;
M. Chardon's avatar
init  
M. Chardon committed
834
835
836
        $filearea = self::FINAL_PDF_FILEAREA;
        $itemid = $grade->id;

837
        $fs = get_file_storage();
M. Chardon's avatar
init  
M. Chardon committed
838
839
840
841
        return $fs->delete_area_files($contextid, $component, $filearea, $itemid);
    }

}