Gitlab CSE Unil

convert_submissions.php 4.48 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
// 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/>.

/**
 * A scheduled task.
20
21
 * 
 * Simple task to convert submissions to pdf in the background.
M. Chardon's avatar
init  
M. Chardon committed
22
23
 *
 * @package    assignfeedback_editpdfplus
24
25
 * @copyright  2016 Université de Lausanne
 * The code is based on mod/assign/feedback/editpdf/classes/task/convert_submissions.php by Damyon Wiese.
M. Chardon's avatar
init  
M. Chardon committed
26
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
28

M. Chardon's avatar
init  
M. Chardon committed
29
30
31
32
namespace assignfeedback_editpdfplus\task;

use core\task\scheduled_task;
use assignfeedback_editpdfplus\document_services;
33
use assignfeedback_editpdfplus\combined_document;
M. Chardon's avatar
init  
M. Chardon committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use context_module;
use assign;

class convert_submissions extends scheduled_task {

    /**
     * Get a descriptive name for this task (shown to admins).
     *
     * @return string
     */
    public function get_name() {
        return get_string('preparesubmissionsforannotation', 'assignfeedback_editpdfplus');
    }

    /**
     * Do the job.
     * Throw exceptions on errors (the job will be retried).
     */
    public function execute() {
        global $CFG, $DB;

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

        $records = $DB->get_records('assignfeedback_editpp_queue');

        $assignmentcache = array();

        foreach ($records as $record) {
            $submissionid = $record->submissionid;
            $submission = $DB->get_record('assign_submission', array('id' => $submissionid), '*', IGNORE_MISSING);
            if (!$submission) {
                // Submission no longer exists.
                $DB->delete_records('assignfeedback_editpp_queue', array('id' => $record->id));
                continue;
            }

            $assignmentid = $submission->assignment;
            $attemptnumber = $record->submissionattempt;

            if (empty($assignmentcache[$assignmentid])) {
                $cm = get_coursemodule_from_instance('assign', $assignmentid, 0, false, MUST_EXIST);
                $context = context_module::instance($cm->id);

                $assignment = new assign($context, null, null);
                $assignmentcache[$assignmentid] = $assignment;
            } else {
                $assignment = $assignmentcache[$assignmentid];
            }

            $users = array();
            if ($submission->userid) {
                array_push($users, $submission->userid);
            } else {
                $members = $assignment->get_submission_group_members($submission->groupid, true);

                foreach ($members as $member) {
                    array_push($users, $member->id);
                }
            }

            mtrace('Convert ' . count($users) . ' submission attempt(s) for assignment ' . $assignmentid);
95

M. Chardon's avatar
M. Chardon committed
96
            foreach ($users as $userid) {
97
                try {
M. Chardon's avatar
M. Chardon committed
98
99
100
101
102
103
104
105
106
                    $combineddocument = document_services::get_combined_pdf_for_attempt($assignment, $userid, $attemptnumber);
                    $status = $combineddocument->get_status();

                    switch ($combineddocument->get_status()) {
                        case combined_document::STATUS_READY:
                        case combined_document::STATUS_PENDING_INPUT:
                            // The document has not been converted yet or is somehow still ready.
                            continue;
                    }
107
108
109
110
111
112
113
114
115
                    document_services::get_page_images_for_attempt(
                            $assignment, $userid, $attemptnumber, false
                    );
                    document_services::get_page_images_for_attempt(
                            $assignment, $userid, $attemptnumber, true
                    );
                } catch (\moodle_exception $e) {
                    mtrace('Conversion failed with error:' . $e->errorcode);
                }
M. Chardon's avatar
init  
M. Chardon committed
116
117
            }

M. Chardon's avatar
M. Chardon committed
118
119
            // Remove from queue.
            $DB->delete_records('assignfeedback_editpp_queue', array('id' => $record->id));
M. Chardon's avatar
init  
M. Chardon committed
120
121
122
123
        }
    }

}