Skip to content

Commit 2ef7f1b

Browse files
committed
refactor: delete options files the way god intended
1 parent 4c78586 commit 2ef7f1b

File tree

6 files changed

+26
-29
lines changed

6 files changed

+26
-29
lines changed

classes/constants.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class constants {
3737
/** @var string */
3838
public const QT_VAR_RESPONSE = 'qpy_response';
3939

40+
/** @var string */
41+
public const FILEAREA_OPTIONS = 'options';
42+
/** @var string */
43+
public const FILEAREA_STATIC = 'static';
44+
4045
/** @var string */
4146
public const QPY_OPTIONS_URL_PATTERN = ';qpy://options/(?P<fileref>[a-zA-Z0-9\-_=]{1,64});';
4247

classes/local/files/options_file_service.php

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use file_exception;
2424
use moodle_exception;
2525
use moodle_url;
26+
use qtype_questionpy\constants;
2627
use qtype_questionpy\local\form\elements\file_upload_element;
2728
use qtype_questionpy\local\form\elements\file_upload_options;
2829
use qtype_questionpy_question;
@@ -40,10 +41,6 @@
4041
class options_file_service implements handles_qpy_url_type {
4142
// TODO: Support subdirectories.
4243

43-
/** @var string */
44-
public const FILEAREA_UPLOADS = 'options';
45-
46-
4744
/**
4845
* Saves all the files in the given draft item to the permanent file area for the given question.
4946
*
@@ -61,7 +58,7 @@ public function save_draft_area_files(int $contextid, int $questionid, int $user
6158
$existingfiles = $fs->get_area_files(
6259
$contextid,
6360
'qtype_questionpy',
64-
self::FILEAREA_UPLOADS,
61+
constants::FILEAREA_OPTIONS,
6562
$questionid,
6663
includedirs: false
6764
);
@@ -85,18 +82,6 @@ public function save_draft_area_files(int $contextid, int $questionid, int $user
8582
}
8683
}
8784

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-
10085
/**
10186
* Populates the given draft area with files listed in `$filemetas` and stored in the permanent question file area.
10287
*
@@ -113,7 +98,7 @@ public function delete_all_saved_files_belonging_to_question(int $contextid, int
11398
*/
11499
public function prepare_draft_area(int $contextid, int $questionid, array $filemetas, int $userid, int $draftitemid): void {
115100
$fs = get_file_storage();
116-
$files = $fs->get_area_files($contextid, 'qtype_questionpy', self::FILEAREA_UPLOADS, $questionid, includedirs: false);
101+
$files = $fs->get_area_files($contextid, 'qtype_questionpy', constants::FILEAREA_OPTIONS, $questionid, includedirs: false);
117102

118103
foreach ($filemetas as $filemetadata) {
119104
$matchingfiles = array_filter($files, fn($file) => $file->get_filename() === $filemetadata->fileref);
@@ -223,7 +208,7 @@ public function check_upload_restrictions(
223208
$existingfiles = $questionid ? $fs->get_area_files(
224209
$contextid,
225210
'qtype_questionpy',
226-
self::FILEAREA_UPLOADS,
211+
constants::FILEAREA_OPTIONS,
227212
$questionid,
228213
includedirs: false
229214
) : [];
@@ -258,7 +243,7 @@ public function check_upload_restrictions(
258243
*/
259244
public function get_saved_file(int $contextid, int $questionid, string $fileref): ?stored_file {
260245
$fs = get_file_storage();
261-
$files = $fs->get_area_files($contextid, 'qtype_questionpy', self::FILEAREA_UPLOADS, $questionid, includedirs: false);
246+
$files = $fs->get_area_files($contextid, 'qtype_questionpy', constants::FILEAREA_OPTIONS, $questionid, includedirs: false);
262247
foreach ($files as $file) {
263248
if ($file->get_filename() === $fileref) {
264249
return $file;
@@ -296,7 +281,7 @@ public function resolve_qpy_url(string $path, qtype_questionpy_question $questio
296281
return moodle_url::make_pluginfile_url(
297282
$question->contextid,
298283
'qtype_questionpy',
299-
self::FILEAREA_UPLOADS,
284+
constants::FILEAREA_OPTIONS,
300285
$question->id,
301286
'/',
302287
$path

classes/local/files/static_file_service.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use GuzzleHttp\Exception\GuzzleException;
2323
use invalid_dataroot_permissions;
2424
use moodle_url;
25+
use qtype_questionpy\constants;
2526
use qtype_questionpy\exception\request_error;
2627
use qtype_questionpy\local\api\api;
2728
use qtype_questionpy\package_file_service;
@@ -104,7 +105,7 @@ public function resolve_qpy_url(string $path, qtype_questionpy_question $questio
104105
return moodle_url::make_pluginfile_url(
105106
$question->contextid,
106107
'qtype_questionpy',
107-
'static',
108+
constants::FILEAREA_STATIC,
108109
null,
109110
'/' . $question->packagehash . dirname($path) . '/',
110111
basename($path)

classes/question_service.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,11 @@ 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.
228227
* @throws dml_exception
229228
*/
230-
public function delete_question(int $questionid, int $contextid) {
229+
public function delete_question(int $questionid) {
231230
global $DB;
232231
$DB->delete_records(self::QUESTION_TABLE, ['questionid' => $questionid]);
233232
// 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);
236233
}
237234
}

questiontype.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
*/
2424

2525
use core\di;
26+
use qtype_questionpy\constants;
2627
use qtype_questionpy\local\api\api;
28+
use qtype_questionpy\local\files\options_file_service;
2729
use qtype_questionpy\package_file_service;
2830
use qtype_questionpy\question_service;
2931

@@ -49,6 +51,9 @@ class qtype_questionpy extends question_type {
4951
/** @var question_service */
5052
private question_service $questionservice;
5153

54+
/** @var options_file_service */
55+
private options_file_service $ofs;
56+
5257
/**
5358
* Initializes the instance. Called by Moodle.
5459
*/
@@ -57,6 +62,7 @@ public function __construct() {
5762
$this->api = di::get(api::class);
5863
$this->packagefileservice = di::get(package_file_service::class);
5964
$this->questionservice = di::get(question_service::class);
65+
$this->ofs = di::get(options_file_service::class);
6066
}
6167

6268
/**
@@ -88,7 +94,7 @@ public function is_question_manual_graded($question, $otherquestionsinuse) {
8894
* @throws moodle_exception
8995
*/
9096
public function delete_question($questionid, $contextid): void {
91-
$this->questionservice->delete_question($questionid, $contextid);
97+
$this->questionservice->delete_question($questionid);
9298
parent::delete_question($questionid, $contextid);
9399
}
94100

@@ -116,13 +122,16 @@ public function move_files($questionid, $oldcontextid, $newcontextid) {
116122
* @param int $contextid
117123
* @return void
118124
*/
119-
protected function delete_files($questionid, $contextid) {
125+
protected function delete_files($questionid, $contextid): void {
120126
parent::delete_files($questionid, $contextid);
121127
$this->delete_files_in_answers($questionid, $contextid);
122128
$this->delete_files_in_hints($questionid, $contextid);
123129

124130
$fs = get_file_storage();
125131
$fs->delete_area_files($contextid, 'qtype_questionpy', 'package', $questionid);
132+
133+
$fs1 = get_file_storage();
134+
$fs1->delete_area_files($contextid, 'qtype_questionpy', constants::FILEAREA_OPTIONS, $questionid);
126135
}
127136

128137
/**

tests/question_service_test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ public function test_delete_question(): void {
434434
global $DB, $PAGE;
435435
$this->assertEquals(1, $DB->count_records('qtype_questionpy'));
436436

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

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

0 commit comments

Comments
 (0)