MDL-60494 mod_lti: Invalid </img>, example context
[moodle.git] / mod / assign / feedback / editpdf / classes / task / convert_submissions.php
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/>.
17 /**
18  * A scheduled task.
19  *
20  * @package    assignfeedback_editpdf
21  * @copyright  2016 Damyon Wiese
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
24 namespace assignfeedback_editpdf\task;
26 use core\task\scheduled_task;
27 use assignfeedback_editpdf\document_services;
28 use assignfeedback_editpdf\combined_document;
29 use context_module;
30 use assign;
32 /**
33  * Simple task to convert submissions to pdf in the background.
34  * @copyright  2016 Damyon Wiese
35  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36  */
37 class convert_submissions extends scheduled_task {
39     /**
40      * Get a descriptive name for this task (shown to admins).
41      *
42      * @return string
43      */
44     public function get_name() {
45         return get_string('preparesubmissionsforannotation', 'assignfeedback_editpdf');
46     }
48     /**
49      * Do the job.
50      * Throw exceptions on errors (the job will be retried).
51      */
52     public function execute() {
53         global $CFG, $DB;
55         require_once($CFG->dirroot . '/mod/assign/locallib.php');
57         $records = $DB->get_records('assignfeedback_editpdf_queue');
59         $assignmentcache = array();
61         $conversionattemptlimit = !empty($CFG->conversionattemptlimit) ? $CFG->conversionattemptlimit : 3;
62         foreach ($records as $record) {
63             $submissionid = $record->submissionid;
64             $submission = $DB->get_record('assign_submission', array('id' => $submissionid), '*', IGNORE_MISSING);
65             if (!$submission || $record->attemptedconversions >= $conversionattemptlimit) {
66                 // Submission no longer exists; or we've exceeded the conversion attempt limit.
67                 $DB->delete_records('assignfeedback_editpdf_queue', array('id' => $record->id));
68                 continue;
69             }
71             // Record that we're attempting the conversion ahead of time.
72             // We can't do this afterwards as its possible for the conversion process to crash the script entirely.
73             $DB->set_field('assignfeedback_editpdf_queue', 'attemptedconversions',
74                     $record->attemptedconversions + 1, ['id' => $record->id]);
76             $assignmentid = $submission->assignment;
77             $attemptnumber = $record->submissionattempt;
79             if (empty($assignmentcache[$assignmentid])) {
80                 $cm = get_coursemodule_from_instance('assign', $assignmentid, 0, false, MUST_EXIST);
81                 $context = context_module::instance($cm->id);
83                 $assignment = new assign($context, null, null);
84                 $assignmentcache[$assignmentid] = $assignment;
85             } else {
86                 $assignment = $assignmentcache[$assignmentid];
87             }
89             $users = array();
90             if ($submission->userid) {
91                 array_push($users, $submission->userid);
92             } else {
93                 $members = $assignment->get_submission_group_members($submission->groupid, true);
95                 foreach ($members as $member) {
96                     array_push($users, $member->id);
97                 }
98             }
100             mtrace('Convert ' . count($users) . ' submission attempt(s) for assignment ' . $assignmentid);
102             foreach ($users as $userid) {
103                 try {
104                     $combineddocument = document_services::get_combined_pdf_for_attempt($assignment, $userid, $attemptnumber);
105                     switch ($combineddocument->get_status()) {
106                         case combined_document::STATUS_READY:
107                         case combined_document::STATUS_READY_PARTIAL:
108                         case combined_document::STATUS_PENDING_INPUT:
109                             // The document has not been converted yet or is somehow still ready.
110                             continue 2;
111                     }
112                     document_services::get_page_images_for_attempt(
113                             $assignment,
114                             $userid,
115                             $attemptnumber,
116                             false
117                         );
118                     document_services::get_page_images_for_attempt(
119                             $assignment,
120                             $userid,
121                             $attemptnumber,
122                             true
123                         );
124                 } catch (\moodle_exception $e) {
125                     mtrace('Conversion failed with error:' . $e->errorcode);
126                 }
127             }
129             // Remove from queue.
130             $DB->delete_records('assignfeedback_editpdf_queue', array('id' => $record->id));
132         }
133     }