Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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   * Cleans up orphaned feedback pdf files and table entries.
  19   *
  20   * @package    assignfeedback_editpdf
  21   * @copyright  2022 Adrian Greeve <adrian@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace assignfeedback_editpdf\task;
  26  
  27  use core\task\adhoc_task;
  28  
  29  /**
  30   * Cleans up orphaned feedback pdf files and table entries.
  31   *
  32   * @package    assignfeedback_editpdf
  33   * @copyright  2022 Adrian Greeve <adrian@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class remove_orphaned_editpdf_files extends adhoc_task {
  37  
  38      /**
  39       * Run the task.
  40       */
  41      public function execute() {
  42          $this->remove_files_and_entries();
  43          $this->remove_rotated_table_entries();
  44      }
  45  
  46      /**
  47       * Removes edit pdf feedback files and table entries that have been orphaned.
  48       */
  49      private function remove_files_and_entries(): void {
  50          global $DB;
  51  
  52          // Patiently remove all orphaned temporary pdf files.
  53          $sql = "SELECT DISTINCT f.contextid, f.component, f.filearea, f.itemid
  54                    FROM {files} f
  55               LEFT JOIN {assign_grades} g ON g.id = f.itemid
  56                   WHERE f.component = :assigneditpdf
  57                 AND NOT (filearea = :stamps AND f.itemid = 0)
  58                     AND g.id IS NULL";
  59          $params = ['assigneditpdf' => 'assignfeedback_editpdf', 'stamps' => 'stamps'];
  60  
  61          $results = $DB->get_recordset_sql($sql, $params);
  62          foreach ($results as $record) {
  63              $fs = get_file_storage();
  64              $fs->delete_area_files($record->contextid, $record->component, $record->filearea, $record->itemid);
  65          }
  66          $results->close();
  67      }
  68  
  69      /**
  70       * Removes orphaned entries in the feedback edit pdf rotation table.
  71       */
  72      private function remove_rotated_table_entries(): void {
  73          global $DB;
  74          $rotatesql = "SELECT er.id AS erid
  75                          FROM {assignfeedback_editpdf_rot} er
  76                     LEFT JOIN {assign_grades} g ON g.id = er.gradeid
  77                         WHERE g.id IS NULL";
  78          $DB->delete_records_subquery('assignfeedback_editpdf_rot', 'id', 'erid', $rotatesql);
  79      }
  80  }