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  namespace core\task;
  18  
  19  /**
  20   * A schedule task to clean orphaned h5p records (for example for deleted activity).
  21   *
  22   * @package    core_h5p
  23   * @copyright  2021 Ilya Tregubov <ilya@moodle.com>
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  class h5p_clean_orphaned_records_task extends scheduled_task {
  27  
  28      /**
  29       * Get a descriptive name for this task (shown to admins).
  30       *
  31       * @return string
  32       */
  33      public function get_name() {
  34          return get_string('taskh5pcleanup', 'admin');
  35      }
  36  
  37      /**
  38       * Execute the task.
  39       */
  40      public function execute() {
  41          global $DB;
  42  
  43          $sql = "SELECT h5p.id
  44                    FROM {h5p} h5p
  45               LEFT JOIN {files} f
  46                      ON f.pathnamehash = h5p.pathnamehash
  47                   WHERE f.pathnamehash IS NULL";
  48  
  49          $orphanedrecords = $DB->get_recordset_sql($sql);
  50  
  51          foreach ($orphanedrecords as $orphanedrecord) {
  52  
  53              $sql = "SELECT f.id, f.pathnamehash
  54                        FROM {files} f
  55                       WHERE f.itemid = :itemid
  56                         AND f.filearea = :filearea
  57                         AND f.component = :component";
  58              $params = ['itemid' => $orphanedrecord->id, 'filearea' => 'content', 'component' => 'core_h5p'];
  59              $filerecords = $DB->get_recordset_sql($sql, $params);
  60  
  61              foreach ($filerecords as $filerecord) {
  62                  $fs = get_file_storage();
  63                  $file = $fs->get_file_by_hash($filerecord->pathnamehash);
  64                  if ($file) {
  65                      $file->delete();
  66                  }
  67              }
  68  
  69              $DB->delete_records('h5p', ['id' => $orphanedrecord->id]);
  70              $DB->delete_records('h5p_contents_libraries', ['h5pid' => $orphanedrecord->id]);
  71          }
  72  
  73      }
  74  }