]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-14018 advanced search for multiselect menu improved; merged from MOODLE_19_STABLE
authorskodak <skodak>
Wed, 16 Apr 2008 13:20:46 +0000 (13:20 +0000)
committerskodak <skodak>
Wed, 16 Apr 2008 13:20:46 +0000 (13:20 +0000)
lang/en_utf8/data.php
mod/data/field/multimenu/field.class.php

index 015c9ee505f4b5c1fe420efdb384f4948d734356..82db93600841219c0c4a310e66e236ad690cf819 100644 (file)
@@ -217,6 +217,7 @@ $string['savesettings'] = 'Save settings';
 $string['savesuccess'] = 'Saved successfully. Your preset will now be available across the site.';
 $string['savetemplate'] = 'Save template';
 $string['search'] = 'Search';
+$string['selectedrequired'] = 'All selected required';
 $string['sendinratings'] = 'Send in my latest ratings';
 $string['single'] = 'View single';
 $string['singletemplate'] = 'Single template';
index ec37adb8e54675f82234d0f65fc9f6c426bd0a86..82760cfcae29ffe30b1d9f376bfa5d8b5324a4bc 100755 (executable)
@@ -63,23 +63,68 @@ class data_field_multimenu extends data_field_base {
     
     function display_search_field($value = '') {
         global $CFG;
-        $temp = get_records_sql_menu('SELECT id, content from '.$CFG->prefix.'data_content WHERE fieldid='.$this->field->id.' GROUP BY content ORDER BY content');
-        $options = array();
-        if(!empty($temp)) {
-            $options[''] = '';              //Make first index blank.
-            foreach ($temp as $key) {
-                $options[$key] = $key;  //Build following indicies from the sql.
+
+        if (is_array($value)){
+            $content     = $value['selected'];
+            $allrequired = $value['allrequired'] ? 'checked = "checked"' : '';
+        } else {
+            $content     = array();
+            $allrequired = ''; 
+        }
+
+        static $c = 0;
+
+        $str = '<select name="f_'.$this->field->id.'[]" multiple="multiple">';
+
+        foreach (explode("\n",$this->field->param1) as $option) {
+            $option = trim($option);
+            $str .= '<option value="' . s($option) . '"';
+
+            if (array_search($option, $content) !== false) {
+                // Selected by user.
+                $str .= ' selected >';
+            } else {
+                $str .= '>';
             }
+            $str .= $option . '</option>';
         }
-        return choose_from_menu($options, 'f_'.$this->field->id, $value, 'choose', '',  0, true);
+        $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++;
+
+        return $str;
+
     }
     
     function parse_search_field() {
-        return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
+        $selected    = optional_param('f_'.$this->field->id, array(), PARAM_NOTAGS);
+        $allrequired = optional_param('f_'.$this->field->id.'_allreq', 0, PARAM_BOOL);
+        if (empty($selected)) {
+            // no searching
+            return '';
+        }
+        return array('selected'=>$selected, 'allrequired'=>$allrequired);
     }
 
     function generate_sql($tablealias, $value) {
-        return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content = '$value') "; 
+        $allrequired = $value['allrequired'];
+        $selected    = $value['selected'];
+
+        if ($selected) {
+            $conditions = array();
+            foreach ($selected as $sel) {
+                $conditions[] = "({$tablealias}.fieldid = {$this->field->id} AND ({$tablealias}.content LIKE '$sel##%' OR {$tablealias}.content LIKE '%##$sel' OR {$tablealias}.content LIKE '%##$sel##%'))";
+            }
+            if ($allrequired) {
+                return " (".implode(" AND ", $conditions).") ";
+            } else {
+                return " (".implode(" OR ", $conditions).") ";
+            }
+        } else {
+            return " ";
+        } 
     }
 
     function update_content($recordid, $value, $name='') {