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:20:40 +08:00
parent ab4afd711a
commit 734fb0f015
3 changed files with 29 additions and 3 deletions
+19
View File
@@ -387,5 +387,24 @@ function xmldb_lesson_upgrade($oldversion) {
// Moodle v3.1.0 release upgrade line.
// Put any upgrade step following this.
if ($oldversion < 2016052301) {
// 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, 2016052301, 'lesson');
}
return true;
}
+9 -2
View File
@@ -2291,6 +2291,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
@@ -2310,8 +2319,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 = 2016052300; // The current module version (Date: YYYYMMDDXX)
$plugin->version = 2016052301; // The current module version (Date: YYYYMMDDXX)
$plugin->requires = 2016051900; // Requires this Moodle version
$plugin->component = 'mod_lesson'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 0;