MDL-21437 data - checkbox and multimenu fields search working + require all too
authorEloy Lafuente <stronk7@moodle.org>
Wed, 17 Nov 2010 19:13:26 +0000 (19:13 +0000)
committerEloy Lafuente <stronk7@moodle.org>
Wed, 17 Nov 2010 19:13:26 +0000 (19:13 +0000)
mod/data/field/checkbox/field.class.php
mod/data/field/multimenu/field.class.php

index edaf71f..8b5c898 100755 (executable)
@@ -65,13 +65,14 @@ class data_field_checkbox extends data_field_base {
     }
 
     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 = '';
@@ -90,7 +91,7 @@ class data_field_checkbox extends data_field_base {
             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;
     }
 
@@ -105,26 +106,38 @@ class data_field_checkbox extends data_field_base {
     }
 
     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());
index a03f8c6..170af17 100755 (executable)
@@ -63,10 +63,10 @@ class data_field_multimenu extends data_field_base {
 
         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;
@@ -115,9 +115,7 @@ class data_field_multimenu extends data_field_base {
 
         $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;
 
@@ -134,10 +132,13 @@ class data_field_multimenu extends data_field_base {
     }
 
     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'];
@@ -150,7 +151,7 @@ class data_field_multimenu extends data_field_base {
                 $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))";