From 4a44c660e521bfa76dde2fd3f3911d532aefacbd Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20Mudr=C3=A1k?= Date: Wed, 17 Sep 2014 23:04:42 +0200 Subject: [PATCH] MDL-31936 workshop: Delete attachments on record removal The methods workshop::delete_submission() and workshop::delete_assessment() did not delete files (embedded and attachments) associated with the given submission or assessment. This is fixed now. Additionally, the delete_assessment() method now cleans-up records from the table workshop_grades, too. This internal workshop API still does not give workshop subplugins a chance to clean up their data, should they store them in their own tables instead of the workshop_grades one. This should be improved in the future yet. --- mod/workshop/locallib.php | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/mod/workshop/locallib.php b/mod/workshop/locallib.php index 9c314ddfb5f..b8e100c8388 100644 --- a/mod/workshop/locallib.php +++ b/mod/workshop/locallib.php @@ -1059,6 +1059,11 @@ class workshop { global $DB; $assessments = $DB->get_records('workshop_assessments', array('submissionid' => $submission->id), '', 'id'); $this->delete_assessment(array_keys($assessments)); + + $fs = get_file_storage(); + $fs->delete_area_files($this->context->id, 'mod_workshop', 'submission_content', $submission->id); + $fs->delete_area_files($this->context->id, 'mod_workshop', 'submission_attachment', $submission->id); + $DB->delete_records('workshop_submissions', array('id' => $submission->id)); } @@ -1262,21 +1267,39 @@ class workshop { } /** - * Delete assessment record or records + * Delete assessment record or records. * - * @param mixed $id int|array assessment id or array of assessments ids - * @return bool false if $id not a valid parameter, true otherwise + * Removes associated records from the workshop_grades table, too. + * + * @param int|array $id assessment id or array of assessments ids + * @todo Give grading strategy plugins a chance to clean up their data, too. + * @return bool true */ public function delete_assessment($id) { global $DB; - // todo remove all given grades from workshop_grades; + if (empty($id)) { + return true; + } + + $fs = get_file_storage(); if (is_array($id)) { - return $DB->delete_records_list('workshop_assessments', 'id', $id); + $DB->delete_records_list('workshop_grades', 'assessmentid', $id); + foreach ($id as $itemid) { + $fs->delete_area_files($this->context->id, 'mod_workshop', 'overallfeedback_content', $itemid); + $fs->delete_area_files($this->context->id, 'mod_workshop', 'overallfeedback_attachment', $itemid); + } + $DB->delete_records_list('workshop_assessments', 'id', $id); + } else { - return $DB->delete_records('workshop_assessments', array('id' => $id)); + $DB->delete_records('workshop_grades', array('assessmentid' => $id)); + $fs->delete_area_files($this->context->id, 'mod_workshop', 'overallfeedback_content', $id); + $fs->delete_area_files($this->context->id, 'mod_workshop', 'overallfeedback_attachment', $id); + $DB->delete_records('workshop_assessments', array('id' => $id)); } + + return true; } /** -- 2.43.0