Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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/>.
16
 
17
/**
18
 * Bump submission timemodified for conversions that are stale.
19
 *
20
 * @package    assignfeedback_editpdf
21
 * @copyright  2022 Catalyst IT Australia Pty Ltd
22
 * @author     Cameron Ball <cameronball@catalyst-au.net>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace assignfeedback_editpdf\task;
27
 
28
use core\task\adhoc_task;
29
 
30
/**
31
 * Adhoc task to bump the submission timemodified associated with a stale conversion.
32
 *
33
 * @package    assignfeedback_editpdf
34
 * @copyright  2022 Catalyst IT Australia Pty Ltd
35
 * @author     Cameron Ball <cameronball@catalyst-au.net>
36
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class bump_submission_for_stale_conversions extends adhoc_task {
39
 
40
    /**
41
     * Run the task.
42
     */
43
    public function execute() {
44
        global $DB;
45
 
46
        // Used to only get records after whenever document conversion was enabled for this site.
47
        $earliestconversion = $DB->get_record_sql("SELECT MIN(timecreated) AS min
48
                                                     FROM {files}
49
                                                    WHERE filearea = 'documentconversion'");
50
 
51
        if (isset($earliestconversion->min)) {
52
            ['sql' => $extensionsql, 'params' => $extensionparams] = array_reduce(
53
                ['doc', 'docx', 'rtf', 'xls', 'xlsx', 'ppt', 'pptx', 'html', 'odt', 'ods', 'png', 'jpg', 'txt', 'gif'],
54
                function(array $c, string $ext) use ($DB): array {
55
                    return [
56
                        'sql' => $c['sql'] . ($c['sql'] ? ' OR ' : '') . $DB->sql_like('f1.filename', ':' . $ext),
57
                        'params' => $c['params'] + [$ext => '%.' . $ext]
58
                    ];
59
                },
60
                ['sql' => '', 'params' => []]
61
            );
62
 
63
            // A converted file has its filename set to the contenthash of the file it converted.
64
            // Find all files in the relevant file areas for which there is no corresponding
65
            // file with the contenthash as the file name.
66
            //
67
            // Also check if the file has a greater modified time than the submission, if it does
68
            // that means it is both stale (as per the above) and will never be reconverted.
69
            $sql = "SELECT f3.id, f3.timemodified as fmodified, asu.id as submissionid
70
                      FROM {files} f1
71
                 LEFT JOIN {files} f2 ON f1.contenthash = f2.filename
72
                           AND f2.component = 'core' AND f2.filearea = 'documentconversion'
73
                      JOIN {assign_submission} asu ON asu.id = f1.itemid
74
                      JOIN {assign_grades} asg ON asg.userid = asu.userid AND asg.assignment = asu.assignment
75
                      JOIN {files} f3 ON f3.itemid = asg.id
76
                     WHERE f1.filearea = 'submission_files'
77
                           AND f3.timecreated >= :earliest
78
                           AND ($extensionsql)
79
                           AND f2.filename IS NULL
80
                           AND f3.component = 'assignfeedback_editpdf'
81
                           AND f3.filearea = 'combined'
82
                           AND f3.filename = 'combined.pdf'
83
                           AND f3.timemodified >= asu.timemodified";
84
 
85
            $submissionstobump = $DB->get_records_sql($sql, ['earliest' => $earliestconversion->min] + $extensionparams);
86
            foreach ($submissionstobump as $submission) {
87
 
88
                // Set the submission modified time to one second later than the
89
                // converted files modified time, this will cause assign to reconvert
90
                // everything and delete the old files when the assignment grader is
91
                // viewed. See get_page_images_for_attempt in document_services.php.
92
                $newmodified = $submission->fmodified + 1;
93
                $record = (object)[
94
                    'id' => $submission->submissionid,
95
                    'timemodified' => $newmodified
96
                ];
97
 
98
                mtrace('Set submission ' . $submission->submissionid . ' timemodified to ' . $newmodified);
99
                $DB->update_record('assign_submission', $record);
100
            }
101
        }
102
    }
103
}