@@ -69,7 +69,7 @@ public function save_draft_area_files(int $contextid, int $questionid, int $user
69
69
$ draftfiles = $ fs ->get_area_files (context_user::instance ($ userid )->id , 'user ' , 'draft ' , $ draftitemid , includedirs: false );
70
70
foreach ($ draftfiles as $ draftfile ) {
71
71
$ fileref = qpy_file_ref::from_stored_file ($ draftfile );
72
- // If the user didn't modify/remove the file, it would already be saved and the file ref not changed .
72
+ // If the file ref already exists, the user just didn't modify/remove the file.
73
73
if (!in_array (strval ($ fileref ), $ existingfilerefs )) {
74
74
$ fs ->create_file_from_storedfile ([
75
75
'component ' => 'qtype_questionpy ' ,
@@ -84,34 +84,36 @@ public function save_draft_area_files(int $contextid, int $questionid, int $user
84
84
}
85
85
86
86
/**
87
- * Populates the given draft area with files listed in `$metadata ` and stored in the permanent question file area.
87
+ * Populates the given draft area with files listed in `$filemetas ` and stored in the permanent question file area.
88
88
*
89
89
* (The inverse of {@see save_draft_area_files}.)
90
90
*
91
91
* @param int $contextid Context id of the question (NOT the draft area).
92
92
* @param int $questionid
93
- * @param array $metadata array of {@see file_metadata} by filename
93
+ * @param file_metadata[] $filemetas
94
94
* @param int $userid
95
95
* @param int $draftitemid
96
96
* @throws file_exception
97
97
* @throws stored_file_creation_exception
98
98
* @throws coding_exception
99
99
*/
100
- public function prepare_draft_area (int $ contextid , int $ questionid , array $ metadata , int $ userid , int $ draftitemid ): void {
100
+ public function prepare_draft_area (int $ contextid , int $ questionid , array $ filemetas , int $ userid , int $ draftitemid ): void {
101
101
$ fs = get_file_storage ();
102
102
$ files = $ fs ->get_area_files ($ contextid , 'qtype_questionpy ' , self ::FILEAREA_UPLOADS , $ questionid , includedirs: false );
103
103
104
- foreach ($ metadata as $ mfilename => $ filemetadata ) {
104
+ foreach ($ filemetas as $ filemetadata ) {
105
105
$ matchingfiles = array_filter ($ files , fn ($ file ) => $ file ->get_filename () === $ filemetadata ->fileref );
106
106
if (!$ matchingfiles ) {
107
- debugging ("Options file ' $ mfilename' with file_ref ' $ filemetadata ->fileref ' could be found in storage. " );
107
+ debugging ("Options file ' $ filemetadata ->filename ' with file_ref ' $ filemetadata ->fileref ' could not be found in "
108
+ . 'storage. ' );
108
109
continue ;
109
110
}
110
111
111
112
$ count = count ($ matchingfiles );
112
113
if ($ count > 1 ) {
113
114
// This would mean that the file area contains two files with the same filename, which I'm not sure is possible.
114
- debugging ("Options file ' $ mfilename' has ambiguous file_ref ' $ filemetadata ->fileref ' which matches $ count files. " );
115
+ debugging ("Options file ' $ filemetadata ->filename ' has ambiguous file_ref ' $ filemetadata ->fileref ' which matches "
116
+ . "$ count files. " );
115
117
}
116
118
117
119
$ file = reset ($ matchingfiles );
@@ -122,7 +124,7 @@ public function prepare_draft_area(int $contextid, int $questionid, array $metad
122
124
'itemid ' => $ draftitemid ,
123
125
'contextid ' => context_user::instance ($ userid )->id ,
124
126
'filepath ' => '/ ' ,
125
- 'filename ' => $ mfilename ,
127
+ 'filename ' => $ filemetadata -> filename ,
126
128
], $ file );
127
129
}
128
130
}
@@ -132,7 +134,7 @@ public function prepare_draft_area(int $contextid, int $questionid, array $metad
132
134
*
133
135
* @param int $userid
134
136
* @param int $draftitemid
135
- * @return array<string, file_metadata>
137
+ * @return file_metadata[]
136
138
* @throws moodle_exception
137
139
* @throws coding_exception
138
140
*/
@@ -143,7 +145,8 @@ public function get_qpy_files_metadata_from_draftitem(int $userid, int $draftite
143
145
$ metadata = [];
144
146
foreach ($ files as $ file ) {
145
147
$ fileref = qpy_file_ref::from_stored_file ($ file );
146
- $ metadata [$ file ->get_filename ()] = new file_metadata (
148
+ $ metadata [] = new file_metadata (
149
+ filename: $ file ->get_filename (),
147
150
fileref: $ fileref ,
148
151
uploadedat: DateTimeImmutable::createFromFormat ('U ' , $ file ->get_timemodified ()),
149
152
mimetype: $ file ->get_mimetype ()
0 commit comments