@@ -411,4 +411,67 @@ protected function get_date_sql(string $tablealias, string $fieldname): conditio
411
411
412
412
return new condition_sql ('' , $ where , $ params );
413
413
}
414
+
415
+ /**
416
+ * Get SQl data for autocomplete type fields.
417
+ *
418
+ * @param string $tablealias Alias for a table.
419
+ * @param string $fieldname Field name.
420
+ * @return condition_sql
421
+ */
422
+ protected function get_autocomplete_sql (string $ tablealias , string $ fieldname ): condition_sql {
423
+ global $ DB ;
424
+
425
+ $ fieldvalue = $ this ->get_field_value ();
426
+ $ operatorvalue = $ this ->get_operator_value ();
427
+
428
+ if ($ this ->is_broken ()) {
429
+ return new condition_sql ('' , '' , []);
430
+ }
431
+
432
+ // User data for autocomplete field is stored like Option 1, Option 2, Option 3.
433
+ // So to be accurate in our SQL we have to cover three scenarios:
434
+ // 1. Value is in the beginning of the string.
435
+ // 2. Value is somewhere in the middle.
436
+ // 3. Value is at the end of the string.
437
+ // So our SQL should like:
438
+ // WHERE data like 'value%' OR data like '% value, %' OR data like '%, value'
439
+ // This is a bit hacky, but should give us accurate results.
440
+ $ startparam = condition_sql::generate_param_alias ();
441
+ $ middleparam = condition_sql::generate_param_alias ();
442
+ $ endparam = condition_sql::generate_param_alias ();
443
+
444
+ switch ($ operatorvalue ) {
445
+ case self ::TEXT_IS_EQUAL_TO :
446
+ $ value = $ DB ->sql_like_escape ($ fieldvalue );
447
+
448
+ $ where = $ DB ->sql_like ("$ tablealias. $ fieldname " , ": $ startparam " , false , false );
449
+ $ params [$ startparam ] = "$ value% " ;
450
+
451
+ $ where .= ' OR ' . $ DB ->sql_like ("$ tablealias. $ fieldname " , ": $ middleparam " , false , false );
452
+ $ params [$ middleparam ] = "%, $ value, % " ;
453
+
454
+ $ where .= ' OR ' . $ DB ->sql_like ("$ tablealias. $ fieldname " , ": $ endparam " , false , false );
455
+ $ params [$ endparam ] = "%, $ value " ;
456
+
457
+ break ;
458
+ case self ::TEXT_IS_NOT_EQUAL_TO :
459
+ $ value = $ DB ->sql_like_escape ($ fieldvalue );
460
+
461
+ $ where = $ DB ->sql_like ("$ tablealias. $ fieldname " , ": $ startparam " , false , false , true );
462
+ $ params [$ startparam ] = "$ value% " ;
463
+
464
+ $ where .= ' AND ' . $ DB ->sql_like ("$ tablealias. $ fieldname " , ": $ middleparam " , false , false , true );
465
+ $ params [$ middleparam ] = "%, $ value, % " ;
466
+
467
+ $ where .= ' AND ' . $ DB ->sql_like ("$ tablealias. $ fieldname " , ": $ endparam " , false , false , true );
468
+ $ params [$ endparam ] = "%, $ value " ;
469
+
470
+ break ;
471
+ default :
472
+ return new condition_sql ('' , '' , []);
473
+ }
474
+
475
+ return new condition_sql ('' , $ where , $ params );
476
+ }
414
477
}
0 commit comments