MDL-30619 mod_data: fixed deletion of files on reset

This commit is contained in:
Mark Nelson
2014-11-16 21:42:59 -08:00
parent c106341098
commit 4e4572963a
+14 -14
View File
@@ -2567,6 +2567,9 @@ function data_reset_userdata($data) {
$ratingdeloptions->component = 'mod_data';
$ratingdeloptions->ratingarea = 'entry';
// Set the file storage - may need it to remove files later.
$fs = get_file_storage();
// delete entries if requested
if (!empty($data->reset_data)) {
$DB->delete_records_select('comments', "itemid IN ($allrecordssql) AND commentarea='database_entry'", array($data->courseid));
@@ -2575,13 +2578,14 @@ function data_reset_userdata($data) {
if ($datas = $DB->get_records_sql($alldatassql, array($data->courseid))) {
foreach ($datas as $dataid=>$unused) {
fulldelete("$CFG->dataroot/$data->courseid/moddata/data/$dataid");
if (!$cm = get_coursemodule_from_instance('data', $dataid)) {
continue;
}
$datacontext = context_module::instance($cm->id);
// Delete any files that may exist.
$fs->delete_area_files($datacontext->id, 'mod_data', 'content');
$ratingdeloptions->contextid = $datacontext->id;
$rm->delete_ratings($ratingdeloptions);
}
@@ -2618,21 +2622,17 @@ function data_reset_userdata($data) {
$ratingdeloptions->itemid = $record->id;
$rm->delete_ratings($ratingdeloptions);
$DB->delete_records('comments', array('itemid'=>$record->id, 'commentarea'=>'database_entry'));
$DB->delete_records('data_content', array('recordid'=>$record->id));
$DB->delete_records('data_records', array('id'=>$record->id));
// HACK: this is ugly - the recordid should be before the fieldid!
if (!array_key_exists($record->dataid, $fields)) {
if ($fs = $DB->get_records('data_fields', array('dataid'=>$record->dataid))) {
$fields[$record->dataid] = array_keys($fs);
} else {
$fields[$record->dataid] = array();
// Delete any files that may exist.
if ($contents = $DB->get_records('data_content', array('recordid' => $record->id), '', 'id')) {
foreach ($contents as $content) {
$fs->delete_area_files($datacontext->id, 'mod_data', 'content', $content->id);
}
}
foreach($fields[$record->dataid] as $fieldid) {
fulldelete("$CFG->dataroot/$data->courseid/moddata/data/$record->dataid/$fieldid/$record->id");
}
$notenrolled[$record->userid] = true;
$DB->delete_records('comments', array('itemid' => $record->id, 'commentarea' => 'database_entry'));
$DB->delete_records('data_content', array('recordid' => $record->id));
$DB->delete_records('data_records', array('id' => $record->id));
}
}
$rs->close();