Skip to content

Commit b100ce6

Browse files
committed
issue #94: add support for autocomplete user profile field
1 parent 89bc942 commit b100ce6

File tree

3 files changed

+82
-4
lines changed

3 files changed

+82
-4
lines changed

classes/condition_base.php

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ abstract class condition_base {
5555
*/
5656
public const FIELD_DATA_TYPE_DATETIME = 'datetime';
5757

58+
/**
59+
* Value for date field types.
60+
*/
61+
public const FIELD_DATA_TYPE_AUTOCOMPLETE = 'autocomplete';
62+
5863
/**
5964
* Value for operator text contains.
6065
*/

classes/local/tool_dynamic_cohorts/condition/fields_trait.php

+63
Original file line numberDiff line numberDiff line change
@@ -411,4 +411,67 @@ protected function get_date_sql(string $tablealias, string $fieldname): conditio
411411

412412
return new condition_sql('', $where, $params);
413413
}
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+
}
414477
}

classes/local/tool_dynamic_cohorts/condition/user_custom_profile.php

+14-4
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,13 @@ public function get_name(): string {
5353
* @return array
5454
*/
5555
protected function get_supported_custom_fields(): array {
56-
return [self::FIELD_DATA_TYPE_TEXT, self::FIELD_DATA_TYPE_MENU,
57-
self::FIELD_DATA_TYPE_CHECKBOX, self::FIELD_DATA_TYPE_DATETIME];
56+
return [
57+
self::FIELD_DATA_TYPE_TEXT,
58+
self::FIELD_DATA_TYPE_MENU,
59+
self::FIELD_DATA_TYPE_CHECKBOX,
60+
self::FIELD_DATA_TYPE_DATETIME,
61+
self::FIELD_DATA_TYPE_AUTOCOMPLETE,
62+
];
5863
}
5964

6065
/**
@@ -79,7 +84,8 @@ protected function get_fields_info(): array {
7984

8085
switch ($field->datatype) {
8186
case self::FIELD_DATA_TYPE_MENU:
82-
$options = explode("\n", $field->param1);
87+
case self::FIELD_DATA_TYPE_AUTOCOMPLETE:
88+
$options = explode("\n", $field->param1);
8389
$field->param1 = array_combine($options, $options);
8490
break;
8591
case self::FIELD_DATA_TYPE_TEXT:
@@ -124,7 +130,8 @@ public function config_form_add(\MoodleQuickForm $mform): void {
124130
$this->add_text_field($mform, $group, $field, $shortname);
125131
break;
126132
case self::FIELD_DATA_TYPE_MENU:
127-
$this->add_menu_field($mform, $group, $field, $shortname);
133+
case self::FIELD_DATA_TYPE_AUTOCOMPLETE:
134+
$this->add_menu_field($mform, $group, $field, $shortname);
128135
break;
129136
case self::FIELD_DATA_TYPE_CHECKBOX:
130137
$this->add_checkbox_field($mform, $group, $field, $shortname);
@@ -192,6 +199,9 @@ public function get_sql(): condition_sql {
192199
case self::FIELD_DATA_TYPE_DATETIME:
193200
$result = $this->get_date_sql($ud, 'data');
194201
break;
202+
case self::FIELD_DATA_TYPE_AUTOCOMPLETE:
203+
$result = $this->get_autocomplete_sql($ud, 'data');
204+
break;
195205
}
196206

197207
if (!empty($result->get_params())) {

0 commit comments

Comments
 (0)