MDL-57649 mod_lesson: Delete answer files correctly.

The files associated with answers and responses were
not being deleted due to the fact that the wrong itemid
was being used.
This commit is contained in:
Adrian Greeve
2017-07-17 09:15:21 +08:00
parent 16a68a2f76
commit 63cf03265b
3 changed files with 29 additions and 3 deletions
+19
View File
@@ -141,5 +141,24 @@ function xmldb_lesson_upgrade($oldversion) {
// Automatically generated Moodle v3.3.0 release upgrade line.
// Put any upgrade step following this.
if ($oldversion < 2017051501) {
// Delete orphaned lesson answer and response files.
$sql = "SELECT DISTINCT f.*
FROM {files} f
LEFT JOIN {lesson_answers} la ON f.itemid = la.id
WHERE component = :component
AND la.id IS NULL";
$orphanedfiles = $DB->get_recordset_sql($sql, array('component' => 'mod_lesson'));
$fs = get_file_storage();
foreach ($orphanedfiles as $file) {
$fs->delete_area_files($file->contextid, $file->component, $file->filearea, $file->itemid);
}
$orphanedfiles->close();
upgrade_mod_savepoint(true, 2017051501, 'lesson');
}
return true;
}
+9 -2
View File
@@ -3831,6 +3831,15 @@ abstract class lesson_page extends lesson_base {
$DB->delete_records("lesson_attempts", array("pageid" => $this->properties->id));
$DB->delete_records("lesson_branch", array("pageid" => $this->properties->id));
// Delete files related to answers and responses.
if ($answers = $DB->get_records("lesson_answers", array("pageid" => $this->properties->id))) {
foreach ($answers as $answer) {
$fs->delete_area_files($context->id, 'mod_lesson', 'page_answers', $answer->id);
$fs->delete_area_files($context->id, 'mod_lesson', 'page_responses', $answer->id);
}
}
// ...now delete the answers...
$DB->delete_records("lesson_answers", array("pageid" => $this->properties->id));
// ..and the page itself
@@ -3850,8 +3859,6 @@ abstract class lesson_page extends lesson_base {
// Delete files associated with this page.
$fs->delete_area_files($context->id, 'mod_lesson', 'page_contents', $this->properties->id);
$fs->delete_area_files($context->id, 'mod_lesson', 'page_answers', $this->properties->id);
$fs->delete_area_files($context->id, 'mod_lesson', 'page_responses', $this->properties->id);
// repair the hole in the linkage
if (!$this->properties->prevpageid && !$this->properties->nextpageid) {
+1 -1
View File
@@ -24,7 +24,7 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2017051500; // The current module version (Date: YYYYMMDDXX)
$plugin->version = 2017051501; // The current module version (Date: YYYYMMDDXX)
$plugin->requires = 2017050500; // Requires this Moodle version
$plugin->component = 'mod_lesson'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 0;