Skip to content

Commit

Permalink
MDL-21437 data - checkbox and multimenu fields search working + requi…
Browse files Browse the repository at this point in the history
…re all too
  • Loading branch information
stronk7 committed Nov 17, 2010
1 parent 1675129 commit 15a1828
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
35 changes: 24 additions & 11 deletions mod/data/field/checkbox/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ function display_add_field($recordid=0) {
}

function display_search_field($value='') {
global $CFG, $DB, $OUTPUT;
global $CFG, $DB;

if (is_array($value)) {
$content = $value['checked'];
$allrequired = $value['allrequired'] ? 'checked = "checked"' : '';
$allrequired = $value['allrequired'] ? true : false;
} else {
$content = array();
$allrequired = '';
$allrequired = false;
}

$str = '';
Expand All @@ -90,7 +91,7 @@ function display_search_field($value='') {
return '';
}

$str .= html_writer::checkbox('f_'.$this->field->id.'_allreq', null, false, get_string('selectedrequired', 'data'));
$str .= html_writer::checkbox('f_'.$this->field->id.'_allreq', null, $allrequired, get_string('selectedrequired', 'data'));
return $str;
}

Expand All @@ -105,26 +106,38 @@ function parse_search_field() {
}

function generate_sql($tablealias, $value) {
global $DB;

static $i=0;
$i++;
$name = "df_checkbox_$i";
$name = "df_checkbox_{$i}_";
$params = array();
$varcharcontent = $DB->sql_compare_text("{$tablealias}.content", 255);

$allrequired = $value['allrequired'];
$selected = $value['checked'];

if ($selected) {
$conditions = array();
$j=0;
foreach ($selected as $sel) {
$j++;
$xname = $name.$j;
$likesel = str_replace('%', '\%', $sel);
$likeselsel = str_replace('_', '\_', $likesel);
$conditions[] = "({$tablealias}.fieldid = {$this->field->id} AND ({$tablealias}.content = '$sel'
OR {$tablealias}.content LIKE '$likesel##%'
OR {$tablealias}.content LIKE '%##$likesel'
OR {$tablealias}.content LIKE '%##$likesel##%'))";
$conditions[] = "({$tablealias}.fieldid = {$this->field->id} AND ({$varcharcontent} = :{$xname}a
OR {$tablealias}.content LIKE :{$xname}b
OR {$tablealias}.content LIKE :{$xname}c
OR {$tablealias}.content LIKE :{$xname}d))";
$params[$xname.'a'] = $sel;
$params[$xname.'b'] = "$likesel##%";
$params[$xname.'c'] = "%##$likesel";
$params[$xname.'d'] = "%##$likesel##%";
}
if ($allrequired) {
return array(" (".implode(" AND ", $conditions).") ", array($name=>$value));
return array(" (".implode(" AND ", $conditions).") ", $params);
} else {
return array(" (".implode(" OR ", $conditions).") ", array($name=>$value));
return array(" (".implode(" OR ", $conditions).") ", $params);
}
} else {
return array(" ", array());
Expand Down
13 changes: 7 additions & 6 deletions mod/data/field/multimenu/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ function display_search_field($value = '') {

if (is_array($value)){
$content = $value['selected'];
$allrequired = $value['allrequired'] ? 'checked = "checked"' : '';
$allrequired = $value['allrequired'] ? true : false;
} else {
$content = array();
$allrequired = '';
$allrequired = false;
}

static $c = 0;
Expand Down Expand Up @@ -115,9 +115,7 @@ function display_search_field($value = '') {

$str .= '</select>';

$str .= '&nbsp;<input name="f_'.$this->field->id.'_allreq" id="f_'.$this->field->id.'_allreq'.$c.'" type="checkbox" '.$allrequired.'/>';
$str .= '<label for="f_'.$this->field->id.'_allreq'.$c.'">'.get_string('selectedrequired', 'data').'</label>';
$c++;
$str .= html_writer::checkbox('f_'.$this->field->id.'_allreq', null, $allrequired, get_string('selectedrequired', 'data'));

return $str;

Expand All @@ -134,10 +132,13 @@ function parse_search_field() {
}

function generate_sql($tablealias, $value) {
global $DB;

static $i=0;
$i++;
$name = "df_multimenu_{$i}_";
$params = array();
$varcharcontent = $DB->sql_compare_text("{$tablealias}.content", 255);

$allrequired = $value['allrequired'];
$selected = $value['selected'];
Expand All @@ -150,7 +151,7 @@ function generate_sql($tablealias, $value) {
$xname = $name.$j;
$likesel = str_replace('%', '\%', $sel);
$likeselsel = str_replace('_', '\_', $likesel);
$conditions[] = "({$tablealias}.fieldid = {$this->field->id} AND ({$tablealias}.content = :{$xname}a
$conditions[] = "({$tablealias}.fieldid = {$this->field->id} AND ({$varcharcontent} = :{$xname}a
OR {$tablealias}.content LIKE :{$xname}b
OR {$tablealias}.content LIKE :{$xname}c
OR {$tablealias}.content LIKE :{$xname}d))";
Expand Down

0 comments on commit 15a1828

Please sign in to comment.