Gitlab CSE Unil

page_editor.php 24.8 KB
Newer Older
M. Chardon's avatar
init  
M. Chardon committed
1
<?php
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
19
20
// 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/>.

/**
 * This file contains the editor class for the assignfeedback_editpdfplus plugin
 *
21
22
23
24
 * This class performs crud operations on comments and annotations from a page of a response.
 *
 * No capability checks are done - they should be done by the calling class.
 *
M. Chardon's avatar
init  
M. Chardon committed
25
 * @package   assignfeedback_editpdfplus
M. Chardon's avatar
GPL3  
M. Chardon committed
26
 * @copyright  2016 Université de Lausanne
27
 * The code is based on mod/assign/feedback/editpdf/classes/page_editor.php by Davo Smith
M. Chardon's avatar
init  
M. Chardon committed
28
29
30
31
32
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

namespace assignfeedback_editpdfplus;

M. Chardon's avatar
M. Chardon committed
33
34
35
36
37
use assignfeedback_editpdfplus\bdd\type_tool;
use assignfeedback_editpdfplus\bdd\tool;
use assignfeedback_editpdfplus\bdd\tool_generic;
use assignfeedback_editpdfplus\bdd\axis;
use assignfeedback_editpdfplus\bdd\annotation;
38

M. Chardon's avatar
init  
M. Chardon committed
39
40
class page_editor {

41
42
43
    const BDDTABLEOOL = "assignfeedback_editpp_tool";
    const BDDTABLETOOLTYPE = "assignfeedback_editpp_typet";
    const BDDTABLEANNOTATION = "assignfeedback_editpp_annot";
M. Chardon's avatar
M. Chardon committed
44
    const BDDTABLEAXIS = "assignfeedback_editpp_axis";
45
46
47
    const CONTEXTID = "contextid";
    const GRADEID = "gradeid";
    const DRAFLIB = "draft";
M. Chardon's avatar
M. Chardon committed
48
    const AXISGENERIC = 0;
49

50
51
52
53
54
55
56
57
    /**
     * Get all tools for a page.
     * @param int $contextid
     * @param int $axis
     * @return tool[]
     */
    public static function get_tools($contextidlist) {
        global $DB;
58

M. Chardon's avatar
M. Chardon committed
59
60
61
62
63
64
        $typeToolsRaw = self::get_typetools(null);
        $typeTools = array();
        foreach ($typeToolsRaw as $typeTool) {
            $typeTools[$typeTool->id] = $typeTool;
        }

65
        $tools = array();
66
        if ($contextidlist) {
67
            $records = $DB->get_records_list(self::BDDTABLEOOL, self::CONTEXTID, $contextidlist);
68
        } else {
69
            $records = $DB->get_records(self::BDDTABLEOOL);
70
        }
71
        foreach ($records as $record) {
M. Chardon's avatar
M. Chardon committed
72
            $tooltmp = null;
M. Chardon's avatar
M. Chardon committed
73
            if ($record->axis == self::AXISGENERIC) {
M. Chardon's avatar
M. Chardon committed
74
75
76
77
78
79
                $tooltmp = new tool_generic($record);
            } else {
                $tooltmp = new tool($record);
            }
            $tooltmp->typeObject = $typeTools[$tooltmp->type];
            array_push($tools, $tooltmp);
80
        }
M. Chardon's avatar
M. Chardon committed
81
        usort($tools, function($a, $b) {
M. Chardon's avatar
M. Chardon committed
82
83
            $al = $a->order_tool;
            $bl = $b->order_tool;
M. Chardon's avatar
M. Chardon committed
84
85
86
87
88
            if ($al == $bl) {
                return 0;
            }
            return ($al > $bl) ? +1 : -1;
        });
89
        return $tools;
M. Chardon's avatar
init  
M. Chardon committed
90
91
    }

M. Chardon's avatar
M. Chardon committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    /**
     * Get all tools for a given axis.
     * @param int $axisid
     * @return tool[]
     */
    public static function get_tools_by_axis($axisid) {
        global $DB;

        $typeToolsRaw = self::get_typetools(null);
        $typeTools = array();
        foreach ($typeToolsRaw as $typeTool) {
            $typeTools[$typeTool->id] = $typeTool;
        }

        $tools = array();
        $records = $DB->get_records(self::BDDTABLEOOL, array('axis' => $axisid));
        foreach ($records as $record) {
            $tooltmp = null;
            if ($record->axis == self::AXISGENERIC) {
                $tooltmp = new tool_generic($record);
            } else {
                $tooltmp = new tool($record);
            }
            $tooltmp->typeObject = $typeTools[$tooltmp->type];
            array_push($tools, $tooltmp);
        }
        usort($tools, function($a, $b) {
            $al = $a->order_tool;
            $bl = $b->order_tool;
            if ($al == $bl) {
                return 0;
            }
            return ($al > $bl) ? +1 : -1;
        });
        return $tools;
    }

M. Chardon's avatar
M. Chardon committed
129
130
131
132
133
134
135
    /**
     * Get a single tool by id.
     * @param int $toolid
     * @return tool or false
     */
    public static function get_tool($toolid) {
        global $DB;
136
        $record = $DB->get_record(self::BDDTABLEOOL, array('id' => $toolid), '*', IGNORE_MISSING);
M. Chardon's avatar
M. Chardon committed
137
        if ($record) {
M. Chardon's avatar
M. Chardon committed
138
139
140
141
            $tool = new tool($record);
            $typetool = self::get_type_tool($tool->type);
            $tool->typeObject = $typetool;
            return $tool;
M. Chardon's avatar
M. Chardon committed
142
143
144
145
146
        }
        return false;
    }

    /**
147
     * Get the tool type with id tooltypeid.
M. Chardon's avatar
M. Chardon committed
148
149
150
151
152
     * @param int $tooltypeid
     * @return type_tool
     */
    public static function get_type_tool($tooltypeid) {
        global $DB;
153
        $record = $DB->get_record(self::BDDTABLETOOLTYPE, array('id' => $tooltypeid));
M. Chardon's avatar
M. Chardon committed
154
        if ($record) {
155
156
            $newTypeTool = new type_tool($record);
            return page_editor::custom_type_tool($newTypeTool);
M. Chardon's avatar
M. Chardon committed
157
158
159
160
        }
        return false;
    }

161
162
163
164
165
    /**
     * Get all the type tools.
     * @param array $contextidlist
     * @return type_tool
     */
M. Chardon's avatar
M. Chardon committed
166
167
168
169
    public static function get_typetools($contextidlist) {
        global $DB;
        $typetools = array();
        if ($contextidlist) {
170
            $records = $DB->get_records_list(self::BDDTABLETOOLTYPE, self::CONTEXTID, $contextidlist);
M. Chardon's avatar
M. Chardon committed
171
        } else {
172
            $records = $DB->get_records(self::BDDTABLETOOLTYPE);
M. Chardon's avatar
M. Chardon committed
173
174
        }
        foreach ($records as $record) {
175
176
            $newToolType = new type_tool($record);
            array_push($typetools, page_editor::custom_type_tool($newToolType));
M. Chardon's avatar
M. Chardon committed
177
178
179
180
        }
        return $typetools;
    }

M. Chardon's avatar
M. Chardon committed
181
    /**
182
183
     * Get all axis for contextid.
     * @param array $contextidlist optional
M. Chardon's avatar
M. Chardon committed
184
     * @return axis[]
M. Chardon's avatar
M. Chardon committed
185
186
187
188
189
     */
    public static function get_axis($contextidlist) {
        global $DB;
        $axis = array();
        if ($contextidlist) {
190
            $records = $DB->get_records_list('assignfeedback_editpp_axis', self::CONTEXTID, $contextidlist);
M. Chardon's avatar
M. Chardon committed
191
192
193
194
        } else {
            $records = $DB->get_records('assignfeedback_editpp_axis');
        }
        foreach ($records as $record) {
M. Chardon's avatar
M. Chardon committed
195
            array_push($axis, new axis($record));
M. Chardon's avatar
M. Chardon committed
196
197
        }
        usort($axis, function($a, $b) {
M. Chardon's avatar
M. Chardon committed
198
199
            $al = $a->order_axis;
            $bl = $b->order_axis;
M. Chardon's avatar
M. Chardon committed
200
201
202
203
204
205
206
207
            if ($al == $bl) {
                return 0;
            }
            return ($al > $bl) ? +1 : -1;
        });
        return $axis;
    }

M. Chardon's avatar
M. Chardon committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
    /**
     * Get an axis given by its id.
     * @param int $axisid
     * @return axis
     */
    public static function get_axis_by_id($axisid) {
        global $DB;
        $record = $DB->get_record(self::BDDTABLEAXIS, array('id' => $axisid), '*', IGNORE_MISSING);
        if ($record) {
            return new axis($record);
        }
        return null;
    }

M. Chardon's avatar
init  
M. Chardon committed
222
223
224
225
226
    /**
     * Get all annotations for a page.
     * @param int $gradeid
     * @param int $pageno
     * @param bool $draft
M. Chardon's avatar
M. Chardon committed
227
     * @return bdd\annotation[]
M. Chardon's avatar
init  
M. Chardon committed
228
229
230
231
     */
    public static function get_annotations($gradeid, $pageno, $draft) {
        global $DB;

232
        $params = array(self::GRADEID => $gradeid, 'pageno' => $pageno, self::DRAFLIB => 1);
M. Chardon's avatar
init  
M. Chardon committed
233
        if (!$draft) {
234
            $params[self::DRAFLIB] = 0;
M. Chardon's avatar
init  
M. Chardon committed
235
236
        }
        $annotations = array();
237
        $records = $DB->get_records(self::BDDTABLEANNOTATION, $params);
M. Chardon's avatar
init  
M. Chardon committed
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
        foreach ($records as $record) {
            array_push($annotations, new annotation($record));
        }

        return $annotations;
    }

    /**
     * Set all annotations for a page.
     * @param int $gradeid
     * @param int $pageno
     * @param annotation[] $annotations
     * @return int - the number of annotations.
     */
    public static function set_annotations($gradeid, $pageno, $annotations) {
253
254
255
        global $CFG, $DB;

        $annotationsRelease = $DB->get_records(self::BDDTABLEANNOTATION, array(self::GRADEID => $gradeid, self::DRAFLIB => 1));
M. Chardon's avatar
init  
M. Chardon committed
256
257

        $added = 0;
258
259
260
261
262
263
264
265
266
267
268
269
        if (!$annotationsRelease || sizeof($annotationsRelease) == 0 || $CFG->preserve_student_on_update == 0) {
            $DB->delete_records(self::BDDTABLEANNOTATION, array(self::GRADEID => $gradeid, 'pageno' => $pageno, self::DRAFLIB => 1));
            $annotationdiv = array();
            foreach ($annotations as $record) {
                $currentdiv = $record->divcartridge;
                $newid = self::create_annotation_for_draft($record, $annotationdiv, $gradeid, $pageno);
                if ($newid) {
                    if ($currentdiv != '') {
                        $annotationdiv[$currentdiv] = $newid;
                    }
                    $added++;
                }
M. Chardon's avatar
init  
M. Chardon committed
270
            }
271
272
273
274
275
276
277
278
279
280
281
282
283
        } else {
            $draftid = [];
            foreach ($annotations as $record) {
                if (!$record->id) {
                    $currentdiv = $record->divcartridge;
                    $newid = self::create_annotation_for_draft($record, $annotationdiv, $gradeid, $pageno);
                    if ($newid) {
                        if ($currentdiv != '') {
                            $annotationdiv[$currentdiv] = $newid;
                        }
                        $added++;
                    }
                    continue;
284
                }
285
286
287
288
289
                $annotationDraft = self::get_annotation($record->id);

                //maj annotation
                $annotationDraft->clone_teacher_annotation($record);
                $DB->update_record(self::BDDTABLEANNOTATION, $annotationDraft);
M. Chardon's avatar
init  
M. Chardon committed
290
                $added++;
291
292
293
294
295
296
297
298
                $draftid[] = $annotationDraft->id;
            }
            foreach ($annotationsRelease as $annotation) {
                if (in_array($annotation->id, $draftid)) {
                    continue;
                }
                //need to be deleted
                self::remove_annotation($annotation->id);
M. Chardon's avatar
init  
M. Chardon committed
299
300
301
302
303
304
            }
        }

        return $added;
    }

305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331

    /**
     * create an annotation for a draft
     * @param annotation|record $annotationRecord annotation to create
     * @param array $annotationdiv array with parent's annotation's id
     * @param type $gradeid grade id
     * @param type $pageno page no
     * @return int new annotation id
     */
    private static function create_annotation_for_draft($annotationRecord, $annotationdiv, $gradeid, $pageno) {
        if ($annotationRecord->parent_annot_div != '') {
            //on est dans le cas d'une annotation liee
            $idparent = $annotationdiv[$annotationRecord->parent_annot_div];
            $annotationRecord->parent_annot = intval($idparent);
        }
        // Force these.
        if (!($annotationRecord instanceof annotation)) {
            $annotation = new annotation($annotationRecord);
        } else {
            $annotation = $annotationRecord;
        }
        $annotation->gradeid = $gradeid;
        $annotation->pageno = $pageno;
        $annotation->draft = 1;
        return self::add_annotation($annotation);
    }

332
333
334
335
336
337
338
    /**
     * Update a set of annotations to database
     * @global $DB
     * @param annotation[] $annotations
     * @return int number of rows updated
     */
    public static function update_annotations_status($annotations) {
339
340
341
342
        global $DB;
        $added = 0;
        foreach ($annotations as $recordtmp) {
            $record = page_editor::get_annotation($recordtmp->id);
343
            //$old = $record->studentstatus;
344
            $record->studentstatus = $recordtmp->studentstatus;
345
            $record->studentanswer = $recordtmp->studentanswer;
346
            //debugging($recordtmp->id . ' - ' . $record->id . ' - ' . $old . ' | ' . $recordtmp->studentstatus . ' - ' . $record->studentstatus . ' | ' . $recordtmp->studentanswer . ' - ' . $record->studentanswer);
347
            $DB->update_record(self::BDDTABLEANNOTATION, $record);
348
349
350
351
352
353
            $added++;
        }

        return $added;
    }

M. Chardon's avatar
init  
M. Chardon committed
354
355
356
357
358
359
360
361
    /**
     * Get a single annotation by id.
     * @param int $annotationid
     * @return annotation or false
     */
    public static function get_annotation($annotationid) {
        global $DB;

362
        $record = $DB->get_record(self::BDDTABLEANNOTATION, array('id' => $annotationid), '*', IGNORE_MISSING);
M. Chardon's avatar
init  
M. Chardon committed
363
364
365
366
367
368
369
370
371
372
373
374
375
376
        if ($record) {
            return new annotation($record);
        }
        return false;
    }

    /**
     * Unrelease drafts
     * @param int $gradeid
     * @return bool
     */
    public static function unrelease_drafts($gradeid) {
        global $DB;

377
        // Delete the non-draft annotations.
M. Chardon's avatar
M. Chardon committed
378
        return $DB->delete_records(self::BDDTABLEANNOTATION, array(self::GRADEID => $gradeid, self::DRAFLIB => 0));
M. Chardon's avatar
init  
M. Chardon committed
379
380
381
382
383
384
385
386
    }

    /**
     * Release the draft comments and annotations to students.
     * @param int $gradeid
     * @return bool
     */
    public static function release_drafts($gradeid) {
387
        global $CFG, $DB;
M. Chardon's avatar
init  
M. Chardon committed
388

389
        $annotationsRelease = $DB->get_records(self::BDDTABLEANNOTATION, array(self::GRADEID => $gradeid, self::DRAFLIB => 0));
390
        $parentlink = [];
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430

        if (!$annotationsRelease || sizeof($annotationsRelease) == 0 || $CFG->preserve_student_on_update == 0) {
            // Delete the previous non-draft annotations.
            $DB->delete_records(self::BDDTABLEANNOTATION, array(self::GRADEID => $gradeid, self::DRAFLIB => 0));

            // Copy all the draft annotations to non-drafts.
            $records = $DB->get_records(self::BDDTABLEANNOTATION, array(self::GRADEID => $gradeid, self::DRAFLIB => 1));
            foreach ($records as $record) {
                $newid = $this->create_annotation_for_release($record, $parentlink);
                $parentlink[$oldid] = $newid;
            }
        } else {
            $records = $DB->get_records(self::BDDTABLEANNOTATION, array(self::GRADEID => $gradeid, self::DRAFLIB => 1));
            $draftid = [];
            // update existing annotations
            foreach ($annotationsRelease as $annotation) {
                $annotationDraft = self::get_annotation($annotation->draft_id);

                //if no result, annotation has been deleted
                if (!$annotationDraft) {
                    self::remove_annotation($annotation->id);
                    continue;
                }

                //maj annotation
                $annotation = new annotation($annotation);
                $annotation->clone_teacher_annotation($annotationDraft);
                $DB->update_record(self::BDDTABLEANNOTATION, $annotation);

                $parentlink[$annotation->draft_id] = $annotation->id;
                $draftid[] = $annotation->draft_id;
            }
            //create only new annotations
            foreach ($records as $record) {
                if (in_array($record->id, $draftid)) {
                    continue;
                }
                //need to be created
                $newid = self::create_annotation_for_release($record, $parentlink);
                $parentlink[$oldid] = $newid;
431
            }
M. Chardon's avatar
init  
M. Chardon committed
432
433
434
435
436
        }

        return true;
    }

437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
    /**
     * create an annotation for a release draft
     * @param annotation|record $annotationRecord annotation to create
     * @param array $parentlink array with parent's annotation's id
     * @return int new annotation id
     */
    private static function create_annotation_for_release($annotationRecord, $parentlink) {
        $oldid = $annotationRecord->id;
        unset($annotationRecord->id);
        $annotationRecord->draft = 0;
        $annotationRecord->draft_id = $oldid;
        if ($annotationRecord->parent_annot > 0) {
            $annotationRecord->parent_annot = $parentlink[$annotationRecord->parent_annot];
        }
        // Force these.
        if (!($annotationRecord instanceof annotation)) {
            $annotation = new annotation($annotationRecord);
        } else {
            $annotation = $annotationRecord;
        }
        return self::add_annotation($annotation);
    }

M. Chardon's avatar
init  
M. Chardon committed
460
461
462
463
464
465
466
    /**
     * Has annotations or comments.
     * @param int $gradeid
     * @return bool
     */
    public static function has_annotations_or_comments($gradeid, $includedraft) {
        global $DB;
467
        $params = array(self::GRADEID => $gradeid);
M. Chardon's avatar
init  
M. Chardon committed
468
        if (!$includedraft) {
469
            $params[self::DRAFLIB] = 0;
M. Chardon's avatar
init  
M. Chardon committed
470
        }
471
        if ($DB->count_records(self::BDDTABLEANNOTATION, $params)) {
M. Chardon's avatar
init  
M. Chardon committed
472
473
474
475
476
477
478
479
480
481
482
483
484
485
            return true;
        }
        return false;
    }

    /**
     * Aborts all draft annotations and reverts to the last version released to students.
     * @param int $gradeid
     * @return bool
     */
    public static function revert_drafts($gradeid) {
        global $DB;

        // Delete the previous non-draft annotations and comments.
486
        $DB->delete_records(self::BDDTABLEANNOTATION, array(self::GRADEID => $gradeid, self::DRAFLIB => 1));
M. Chardon's avatar
init  
M. Chardon committed
487
488

        // Copy all the draft annotations and comments to non-drafts.
489
        $records = $DB->get_records(self::BDDTABLEANNOTATION, array(self::GRADEID => $gradeid, self::DRAFLIB => 0));
M. Chardon's avatar
init  
M. Chardon committed
490
491
492
        foreach ($records as $record) {
            unset($record->id);
            $record->draft = 0;
493
            $DB->insert_record(self::BDDTABLEANNOTATION, $record);
M. Chardon's avatar
init  
M. Chardon committed
494
495
496
497
498
499
500
501
502
503
504
505
506
507
        }

        return true;
    }

    /**
     * Add a annotation to a page.
     * @param annotation $annotation
     * @return bool
     */
    public static function add_annotation(annotation $annotation) {
        global $DB;

        $annotation->id = null;
508
509
510
        if ($annotation->parent_annot == 0) {
            $annotation->parent_annot = null;
        }
511
        return $DB->insert_record(self::BDDTABLEANNOTATION, $annotation);
M. Chardon's avatar
init  
M. Chardon committed
512
513
514
515
516
517
518
519
520
521
    }

    /**
     * Remove a annotation from a page.
     * @param int $annotationid
     * @return bool
     */
    public static function remove_annotation($annotationid) {
        global $DB;

522
        return $DB->delete_records(self::BDDTABLEANNOTATION, array('id' => $annotationid));
M. Chardon's avatar
init  
M. Chardon committed
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
    }

    /**
     * Copy annotations, comments, pages, and other required content from the source user to the current group member
     * being procssed when using applytoall.
     *
     * @param int|\assign $assignment
     * @param stdClass $grade
     * @param int $sourceuserid
     * @return bool
     */
    public static function copy_drafts_from_to($assignment, $grade, $sourceuserid) {
        global $DB;

        // Delete any existing annotations and comments from current user.
538
        $DB->delete_records(self::BDDTABLEANNOTATION, array(self::GRADEID => $grade->id));
M. Chardon's avatar
init  
M. Chardon committed
539
540
        // Get gradeid, annotations and comments from sourceuserid.
        $sourceusergrade = $assignment->get_user_grade($sourceuserid, true, $grade->attemptnumber);
541
        $annotations = $DB->get_records(self::BDDTABLEANNOTATION, array(self::GRADEID => $sourceusergrade->id, self::DRAFLIB => 1));
M. Chardon's avatar
init  
M. Chardon committed
542
543
544
545
546
547
        $contextid = $assignment->get_context()->id;
        $sourceitemid = $sourceusergrade->id;

        // Add annotations and comments to current user to generate feedback file.
        foreach ($annotations as $annotation) {
            $annotation->gradeid = $grade->id;
548
            $DB->insert_record(self::BDDTABLEANNOTATION, $annotation);
M. Chardon's avatar
init  
M. Chardon committed
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
        }

        $fs = get_file_storage();

        // Copy the stamp files.
        self::replace_files_from_to($fs, $contextid, $sourceitemid, $grade->id, document_services::STAMPS_FILEAREA, true);

        // Copy the PAGE_IMAGE_FILEAREA files.
        self::replace_files_from_to($fs, $contextid, $sourceitemid, $grade->id, document_services::PAGE_IMAGE_FILEAREA);

        return true;
    }

    /**
     * Replace the area files in the specified area with those in the source item id.
     *
     * @param \file_storage $fs The file storage class
     * @param int $contextid The ID of the context for the assignment.
     * @param int $sourceitemid The itemid to copy from - typically the source grade id.
     * @param int $itemid The itemid to copy to - typically the target grade id.
     * @param string $area The file storage area.
     * @param bool $includesubdirs Whether to copy the content of sub-directories too.
     */
    public static function replace_files_from_to($fs, $contextid, $sourceitemid, $itemid, $area, $includesubdirs = false) {
        $component = 'assignfeedback_editpdfplus';
        // Remove the existing files within this area.
        $fs->delete_area_files($contextid, $component, $area, $itemid);

        // Copy the files from the source area.
578
        if ($files = $fs->get_area_files($contextid, $component, $area, $sourceitemid, "filename", $includesubdirs)) {
M. Chardon's avatar
init  
M. Chardon committed
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
            foreach ($files as $file) {
                $newrecord = new \stdClass();
                $newrecord->contextid = $contextid;
                $newrecord->itemid = $itemid;
                $fs->create_file_from_storedfile($newrecord, $file);
            }
        }
    }

    /**
     * Delete the draft annotations and comments.
     *
     * This is intended to be used when the version of the PDF has changed and the annotations
     * might not be relevant any more, therefore we should delete them.
     *
     * @param int $gradeid The grade ID.
     * @return bool
     */
    public static function delete_draft_content($gradeid) {
        global $DB;
599
600
        $conditions = array(self::GRADEID => $gradeid, self::DRAFLIB => 1);
        $result = $DB->delete_records(self::BDDTABLEANNOTATION, $conditions);
M. Chardon's avatar
init  
M. Chardon committed
601
602
        return $result;
    }
603

604
605
606
607
608
609
610
611
612
    /**
     * Get the feedback comment from the database.
     *
     * @param int $gradeid
     * @return stdClass|false The feedback comments for the given grade if it exists.
     *                        False if it doesn't.
     */
    public function get_feedback_comments($gradeid) {
        global $DB;
613
        return $DB->get_record('assignfeedback_comments', array('grade' => $gradeid));
614
615
    }

616
617
618
619
    public static function custom_type_tool(type_tool $newToolType) {
        global $CFG;
        switch ($newToolType->label) {
            case 'highlightplus':
M. Chardon's avatar
M. Chardon committed
620
621
622
623
                $newToolType->set_color($CFG->highlightplus_color);
                $newToolType->set_cartridge_color($CFG->highlightplus_cartridge_color);
                $newToolType->set_cartridge_x($CFG->highlightplus_cartridge_x);
                $newToolType->set_cartridge_y($CFG->highlightplus_cartridge_y);
624
625
626
                break;

            case 'stampplus':
M. Chardon's avatar
M. Chardon committed
627
                $newToolType->set_color($CFG->stampplus_color);
628
629
630
                break;

            case 'frame':
M. Chardon's avatar
M. Chardon committed
631
632
                $newToolType->set_cartridge_x($CFG->frame_cartridge_x);
                $newToolType->set_cartridge_y($CFG->frame_cartridge_y);
633
634
635
                break;

            case 'verticalline':
M. Chardon's avatar
M. Chardon committed
636
637
638
639
                $newToolType->set_color($CFG->verticalline_color);
                $newToolType->set_cartridge_color($CFG->verticalline_cartridge_color);
                $newToolType->set_cartridge_x($CFG->verticalline_cartridge_x);
                $newToolType->set_cartridge_y($CFG->verticalline_cartridge_y);
640
641
642
                break;

            case 'stampcomment':
M. Chardon's avatar
M. Chardon committed
643
644
645
                $newToolType->set_cartridge_color($CFG->stampcomment_cartridge_color);
                $newToolType->set_cartridge_x($CFG->stampcomment_cartridge_x);
                $newToolType->set_cartridge_y($CFG->stampcomment_cartridge_y);
646
647
648
                break;

            case 'commentplus':
M. Chardon's avatar
M. Chardon committed
649
650
651
                $newToolType->set_cartridge_color($CFG->commentplus_cartridge_color);
                $newToolType->set_cartridge_x($CFG->commentplus_cartridge_x);
                $newToolType->set_cartridge_y($CFG->commentplus_cartridge_y);
652
653
654
655
656
657
658
659
                break;

            default:
                break;
        }
        return $newToolType;
    }

660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
    /**
     * Set page rotation value.
     * @param int $gradeid grade id.
     * @param int $pageno page number.
     * @param bool $isrotated whether the page is rotated or not.
     * @param string $pathnamehash path name hash
     * @param int $degree rotation degree.
     * @throws \dml_exception
     */
    public static function set_page_rotation($gradeid, $pageno, $isrotated, $pathnamehash, $degree = 0) {
        global $DB;
        $oldrecord = self::get_page_rotation($gradeid, $pageno);
        if ($oldrecord == null) {
            $record = new \stdClass();
            $record->gradeid = $gradeid;
            $record->pageno = $pageno;
            $record->isrotated = $isrotated;
            $record->pathnamehash = $pathnamehash;
            $record->degree = $degree;
            $DB->insert_record('assignfeedback_editpp_rot', $record, false);
        } else {
            $oldrecord->isrotated = $isrotated;
            $oldrecord->pathnamehash = $pathnamehash;
            $oldrecord->degree = $degree;
            $DB->update_record('assignfeedback_editpp_rot', $oldrecord, false);
        }
    }

    /**
     * Get Page Rotation Value.
     * @param int $gradeid grade id.
     * @param int $pageno page number.
     * @return mixed
     * @throws \dml_exception
     */
    public static function get_page_rotation($gradeid, $pageno) {
        global $DB;
        $result = $DB->get_record('assignfeedback_editpp_rot', array('gradeid' => $gradeid, 'pageno' => $pageno));
        return $result;
    }

M. Chardon's avatar
init  
M. Chardon committed
701
}