Skip to content

Commit 33f3a08

Browse files
committed
feat: delete options files when question is deleted
1 parent 1f8c7ab commit 33f3a08

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

classes/local/files/options_file_service.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ public function save_draft_area_files(int $contextid, int $questionid, int $user
8585
}
8686
}
8787

88+
/**
89+
* Deletes all files in the permanent file area of the given question.
90+
*
91+
* @param int $contextid
92+
* @param int $questionid
93+
* @return void
94+
*/
95+
public function delete_all_saved_files_belonging_to_question(int $contextid, int $questionid): void {
96+
$fs = get_file_storage();
97+
$fs->delete_area_files($contextid, 'qtype_questionpy', self::FILEAREA_UPLOADS, $questionid);
98+
}
99+
88100
/**
89101
* Populates the given draft area with files listed in `$filemetas` and stored in the permanent question file area.
90102
*

classes/question_service.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,14 @@ public function upsert_question(object $question): void {
224224
* Deletes all QuestionPy-specific data for the given question.
225225
*
226226
* @param int $questionid
227+
* @param int $contextid The context this question belongs to.
227228
* @throws dml_exception
228229
*/
229-
public static function delete_question(int $questionid) {
230+
public function delete_question(int $questionid, int $contextid) {
230231
global $DB;
231232
$DB->delete_records(self::QUESTION_TABLE, ['questionid' => $questionid]);
232233
// TODO: Also delete packages when they are no longer used by any question.
234+
235+
$this->ofs->delete_all_saved_files_belonging_to_question($contextid, $questionid);
233236
}
234237
}

questiontype.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ public function is_question_manual_graded($question, $otherquestionsinuse) {
8787
* @param int $contextid the context this question belongs to.
8888
* @throws moodle_exception
8989
*/
90-
public function delete_question($questionid, $contextid) {
91-
question_service::delete_question($questionid);
90+
public function delete_question($questionid, $contextid): void {
91+
$this->questionservice->delete_question($questionid, $contextid);
9292
parent::delete_question($questionid, $contextid);
9393
}
9494

tests/question_service_test.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,10 @@ public function test_delete_question(): void {
431431
$pvi->upsert();
432432
$this->setup_question($pvi->versions[0]->hash);
433433

434-
global $DB;
434+
global $DB, $PAGE;
435435
$this->assertEquals(1, $DB->count_records('qtype_questionpy'));
436436

437-
question_service::delete_question(1);
437+
$this->questionservice->delete_question(1, $PAGE->context->id);
438438

439439
$this->assertEquals(0, $DB->count_records('qtype_questionpy'));
440440
}

0 commit comments

Comments
 (0)