@@ -167,11 +167,13 @@ public static function find_course_field(string $table): string {
167
167
* @param string $table The table to search.
168
168
* @param database_column_info $column The column to search.
169
169
* @param int $limit The maximum number of results to return.
170
+ * @param $stream The resource to write the results to.
170
171
* @return array The results of the search.
171
172
* @throws \dml_exception
172
173
*/
173
174
public static function plain_text_search (string $ search , string $ table ,
174
- database_column_info $ column , int $ limit = 0 ): array {
175
+ database_column_info $ column , bool $ summary = false ,
176
+ $ stream = null ): array {
175
177
global $ DB ;
176
178
177
179
$ results = [];
@@ -198,9 +200,33 @@ public static function plain_text_search(string $search, string $table,
198
200
}
199
201
200
202
if ($ column ->meta_type === 'X ' || $ column ->meta_type === 'C ' ) {
201
- $ records = $ DB ->get_records_sql ($ sql , [$ searchparam ], 0 , $ limit );
202
- if ($ records ) {
203
- $ results [$ table ][$ column ->name ] = $ records ;
203
+ $ limit = $ summary ? 1 : 0 ;
204
+ $ records = $ DB ->get_recordset_sql ($ sql , [$ searchparam ], 0 , $ limit );
205
+ if ($ records ->valid ()) {
206
+ if (!empty ($ stream )) {
207
+ if ($ summary ) {
208
+ fputcsv ($ stream , [
209
+ $ table ,
210
+ $ column ->name ,
211
+ ]);
212
+
213
+ // Return empty array to skip the rest of the function.
214
+ return $ results ;
215
+ }
216
+
217
+ foreach ($ records as $ record ) {
218
+ fputcsv ($ stream , [
219
+ $ table ,
220
+ $ column ->name ,
221
+ $ record ->courseid ?? '' ,
222
+ $ record ->courseidnumber ?? '' ,
223
+ $ record ->id ,
224
+ $ record ->$ columnname ,
225
+ ]);
226
+ }
227
+ } else {
228
+ $ results [$ table ][$ column ->name ] = $ records ;
229
+ }
204
230
}
205
231
}
206
232
@@ -215,10 +241,12 @@ public static function plain_text_search(string $search, string $table,
215
241
* @param string $table The table to search.
216
242
* @param database_column_info $column The column to search.
217
243
* @param int $limit The maximum number of results to return.
244
+ * @param $stream The resource to write the results to.
218
245
* @return array
219
246
*/
220
247
public static function regular_expression_search (string $ search , string $ table ,
221
- database_column_info $ column , int $ limit = 0 ): array {
248
+ database_column_info $ column , bool $ summary = false ,
249
+ $ stream = null ): array {
222
250
global $ DB ;
223
251
224
252
// Check if the database supports regular expression searches.
@@ -249,10 +277,46 @@ public static function regular_expression_search(string $search, string $table,
249
277
$ sql = "SELECT id, $ columnname FROM { " .$ table ."} $ tablealias WHERE $ select " ;
250
278
}
251
279
252
- $ records = $ DB ->get_records_sql ($ sql , $ params , 0 , $ limit );
253
-
254
- if ($ records ) {
255
- $ results [$ table ][$ column ->name ] = $ records ;
280
+ $ limit = $ summary ? 1 : 0 ;
281
+ $ records = $ DB ->get_recordset_sql ($ sql , $ params , 0 , $ limit );
282
+
283
+ if ($ records ->valid ()) {
284
+ if (!empty ($ stream )) {
285
+ if ($ summary ) {
286
+ fputcsv ($ stream , [
287
+ $ table ,
288
+ $ column ->name ,
289
+ ]);
290
+
291
+ // Return empty array to skip the rest of the function.
292
+ return $ results ;
293
+ }
294
+
295
+ foreach ($ records as $ record ) {
296
+ $ data = $ record ->$ columnname ;
297
+ // Replace "/" with "\/", as it is used as delimiters.
298
+ $ pattern = str_replace ('/ ' , '\\/ ' , $ search );
299
+
300
+ // Perform the regular expression search.
301
+ preg_match_all ( "/ " . $ pattern . "/ " , $ data , $ matches );
302
+
303
+ if (!empty ($ matches [0 ])) {
304
+ // Show the result foreach match.
305
+ foreach ($ matches [0 ] as $ match ) {
306
+ fputcsv ($ stream , [
307
+ $ table ,
308
+ $ column ->name ,
309
+ $ record ->courseid ?? '' ,
310
+ $ record ->courseidnumber ?? '' ,
311
+ $ record ->id ,
312
+ $ match ,
313
+ ]);
314
+ }
315
+ }
316
+ }
317
+ } else {
318
+ $ results [$ table ][$ column ->name ] = $ records ;
319
+ }
256
320
}
257
321
}
258
322
return $ results ;
0 commit comments