mirrored from git://git.moodle.org/moodle.git
/
convert_submissions.php
135 lines (115 loc) · 5.19 KB
/
convert_submissions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
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
129
130
131
132
133
134
135
<?php
// 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.
*
* @package assignfeedback_editpdf
* @copyright 2016 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace assignfeedback_editpdf\task;
use core\task\scheduled_task;
use assignfeedback_editpdf\document_services;
use assignfeedback_editpdf\combined_document;
use context_module;
use assign;
/**
* Simple task to convert submissions to pdf in the background.
* @copyright 2016 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
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_editpdf');
}
/**
* 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_editpdf_queue');
$assignmentcache = array();
$conversionattemptlimit = !empty($CFG->conversionattemptlimit) ? $CFG->conversionattemptlimit : 3;
foreach ($records as $record) {
$submissionid = $record->submissionid;
$submission = $DB->get_record('assign_submission', array('id' => $submissionid), '*', IGNORE_MISSING);
if (!$submission || $record->attemptedconversions >= $conversionattemptlimit) {
// Submission no longer exists; or we've exceeded the conversion attempt limit.
$DB->delete_records('assignfeedback_editpdf_queue', array('id' => $record->id));
continue;
}
// Record that we're attempting the conversion ahead of time.
// We can't do this afterwards as its possible for the conversion process to crash the script entirely.
$DB->set_field('assignfeedback_editpdf_queue', 'attemptedconversions',
$record->attemptedconversions + 1, ['id' => $record->id]);
$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);
foreach ($users as $userid) {
try {
$combineddocument = document_services::get_combined_pdf_for_attempt($assignment, $userid, $attemptnumber);
switch ($combineddocument->get_status()) {
case combined_document::STATUS_READY:
case combined_document::STATUS_READY_PARTIAL:
case combined_document::STATUS_PENDING_INPUT:
// The document has not been converted yet or is somehow still ready.
continue 2;
}
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);
}
}
// Remove from queue.
$DB->delete_records('assignfeedback_editpdf_queue', array('id' => $record->id));
}
}
}