MDL-63591 core_grades: delete files when grade object is deleted

This commit is contained in:
Mark Nelson 2018-11-16 11:23:59 +08:00
parent 4f95f92edc
commit ac81c98648
5 changed files with 136 additions and 3 deletions

View File

@ -1067,7 +1067,7 @@ class grade_grade extends grade_object {
*
* @param int|null $historyid
*/
protected function update_feedback_files(int $historyid = null){
protected function update_feedback_files(int $historyid = null) {
global $CFG;
// We only support feedback files for modules atm.
@ -1087,6 +1087,23 @@ class grade_grade extends grade_object {
return true;
}
/**
* Handles deleting feedback files in the gradebook.
*/
protected function delete_feedback_files() {
// We only support feedback files for modules atm.
if ($this->grade_item && $this->grade_item->is_external_item()) {
$context = $this->get_context();
$fs = new file_storage();
$fs->delete_area_files($context->id, GRADE_FILE_COMPONENT, GRADE_FEEDBACK_FILEAREA, $this->id);
// Grade history only gets deleted when we delete the whole grade item.
}
return true;
}
/**
* Deletes the grade_grade instance from the database.
*

View File

@ -421,6 +421,13 @@ class grade_item extends grade_object {
}
}
// Delete all the historical files.
// We only support feedback files for modules atm.
if ($this->is_external_item()) {
$fs = new file_storage();
$fs->delete_area_files($this->get_context()->id, GRADE_FILE_COMPONENT, GRADE_HISTORY_FEEDBACK_FILEAREA);
}
return true;
}

View File

@ -297,9 +297,12 @@ abstract class grade_object {
$data->loggeduser = $USER->id;
$DB->insert_record($this->table.'_history', $data);
}
$this->notify_changed(true);
return true;
$this->notify_changed(true);
$this->delete_feedback_files();
return true;
} else {
return false;
}
@ -435,6 +438,12 @@ abstract class grade_object {
protected function update_feedback_files(int $historyid = null) {
}
/**
* Handles deleting feedback files in the gradebook.
*/
protected function delete_feedback_files() {
}
/**
* Returns the current hidden state of this grade_item
*

View File

@ -39,6 +39,7 @@ class core_grade_grade_testcase extends grade_base_testcase {
$this->sub_test_grade_grade_is_locked();
$this->sub_test_grade_grade_set_hidden();
$this->sub_test_grade_grade_is_hidden();
$this->sub_test_grade_grade_deleted();
}
protected function sub_test_grade_grade_construct() {
@ -429,4 +430,80 @@ class core_grade_grade_testcase extends grade_base_testcase {
$CFG->grade_minmaxtouse = $initialminmaxtouse;
}
/**
* Tests when a grade_grade has been deleted.
*/
public function sub_test_grade_grade_deleted() {
$dg = $this->getDataGenerator();
// Create the data we need for the tests.
$fs = new file_storage();
$u1 = $dg->create_user();
$c1 = $dg->create_course();
$a1 = $dg->create_module('assign', ['course' => $c1->id]);
$a1context = context_module::instance($a1->cmid);
$gi = new grade_item($dg->create_grade_item(
[
'courseid' => $c1->id,
'itemtype' => 'mod',
'itemmodule' => 'assign',
'iteminstance' => $a1->id
]
), false);
// Add feedback files to copy as our update.
$this->add_feedback_file_to_copy();
$grades['feedback'] = 'Nice feedback!';
$grades['feedbackformat'] = FORMAT_MOODLE;
$grades['feedbackfiles'] = [
'contextid' => 1,
'component' => 'test',
'filearea' => 'testarea',
'itemid' => 1
];
$grades['userid'] = $u1->id;
grade_update('mod/assign', $gi->courseid, $gi->itemtype, $gi->itemmodule, $gi->iteminstance,
$gi->itemnumber, $grades);
// Feedback file area.
$files = $fs->get_area_files($a1context->id, GRADE_FILE_COMPONENT, GRADE_FEEDBACK_FILEAREA);
$this->assertEquals(2, count($files));
// History file area.
$files = $fs->get_area_files($a1context->id, GRADE_FILE_COMPONENT, GRADE_HISTORY_FEEDBACK_FILEAREA);
$this->assertEquals(2, count($files));
$gg = grade_grade::fetch(array('userid' => $u1->id, 'itemid' => $gi->id));
$gg->delete();
// Feedback file area.
$files = $fs->get_area_files($a1context->id, GRADE_FILE_COMPONENT, GRADE_FEEDBACK_FILEAREA);
$this->assertEquals(0, count($files));
// History file area.
$files = $fs->get_area_files($a1context->id, GRADE_FILE_COMPONENT, GRADE_HISTORY_FEEDBACK_FILEAREA);
$this->assertEquals(2, count($files));
}
/**
* Creates a feedback file to copy to the gradebook area.
*/
private function add_feedback_file_to_copy() {
$dummy = array(
'contextid' => 1,
'component' => 'test',
'filearea' => 'testarea',
'itemid' => 1,
'filepath' => '/',
'filename' => 'feedback1.txt'
);
$fs = get_file_storage();
$fs->create_file_from_string($dummy, '');
}
}

View File

@ -113,10 +113,33 @@ class core_grade_item_testcase extends grade_base_testcase {
$grade_item = new grade_item($this->grade_items[7], false); // Use a grade item not touched by previous (or future) tests.
$this->assertTrue(method_exists($grade_item, 'delete'));
// Add two files.
$dummy = array(
'contextid' => $grade_item->get_context()->id,
'component' => GRADE_FILE_COMPONENT,
'filearea' => GRADE_HISTORY_FEEDBACK_FILEAREA,
'itemid' => 1,
'filepath' => '/',
'filename' => 'feedback1.txt'
);
$fs = get_file_storage();
$fs->create_file_from_string($dummy, '');
$dummy['itemid'] = 2;
$fs->create_file_from_string($dummy, '');
$files = $fs->get_area_files($grade_item->get_context()->id, GRADE_FILE_COMPONENT, GRADE_HISTORY_FEEDBACK_FILEAREA);
// Includes directories.
$this->assertCount(4, $files);
$this->assertTrue($grade_item->delete());
$this->assertFalse($DB->get_record('grade_items', array('id' => $grade_item->id)));
$files = $fs->get_area_files($grade_item->get_context()->id, GRADE_FILE_COMPONENT, GRADE_HISTORY_FEEDBACK_FILEAREA);
$this->assertEmpty($files);
// Keep our reference collection the same as the database.
unset($this->grade_items[7]);
}