Skip to content

Commit

Permalink
MDL-60972 core_course: delete section files, when section is deleted.
Browse files Browse the repository at this point in the history
  • Loading branch information
awagner committed Jan 17, 2019
1 parent 4c6ae31 commit 9ebaea2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
5 changes: 5 additions & 0 deletions course/format/lib.php
Expand Up @@ -1074,6 +1074,11 @@ public function delete_section($section, $forcedeleteifnotempty = false) {
$DB->delete_records('course_sections', array('id' => $section->id));
rebuild_course_cache($course->id, true);

// Delete section summary files.
$context = \context_course::instance($course->id);
$fs = get_file_storage();
$fs->delete_area_files($context->id, 'course', 'section', $section->id);

// Descrease 'numsections' if needed.
if ($decreasenumsections) {
$this->update_course_format_options(array('numsections' => $course->numsections - 1));
Expand Down
24 changes: 24 additions & 0 deletions lib/db/upgrade.php
Expand Up @@ -2337,5 +2337,29 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2018051703.09);
}

if ($oldversion < 2018051704.01) {

// Delete all files that have been used in sections, which are already deleted.
$sql = "SELECT DISTINCT f.itemid as sectionid, f.contextid
FROM {files} f
LEFT JOIN {course_sections} s ON f.itemid = s.id
WHERE f.component = :component AND f.filearea = :filearea AND s.id IS NULL ";

$params = [
'component' => 'course',
'filearea' => 'section'
];

$stalefiles = $DB->get_recordset_sql($sql, $params);

$fs = get_file_storage();
foreach ($stalefiles as $stalefile) {
$fs->delete_area_files($stalefile->contextid, 'course', 'section', $stalefile->sectionid);
}
$stalefiles->close();

upgrade_main_savepoint(true, 2018051704.01);
}

return true;
}
2 changes: 1 addition & 1 deletion version.php
Expand Up @@ -29,7 +29,7 @@

defined('MOODLE_INTERNAL') || die();

$version = 2018051704.00; // 20180517 = branching date YYYYMMDD - do not modify!
$version = 2018051704.01; // 20180517 = branching date YYYYMMDD - do not modify!
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.

Expand Down

0 comments on commit 9ebaea2

Please sign in to comment.