Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/data/field/multimenu/field.class.php
Go to the documentation of this file.
00001 <?php
00003 //                                                                       //
00004 // NOTICE OF COPYRIGHT                                                   //
00005 //                                                                       //
00006 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
00007 //          http://moodle.org                                            //
00008 //                                                                       //
00009 // Copyright (C) 1999-onwards Moodle Pty Ltd  http://moodle.com          //
00010 //                                                                       //
00011 // This program is free software; you can redistribute it and/or modify  //
00012 // it under the terms of the GNU General Public License as published by  //
00013 // the Free Software Foundation; either version 2 of the License, or     //
00014 // (at your option) any later version.                                   //
00015 //                                                                       //
00016 // This program is distributed in the hope that it will be useful,       //
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
00019 // GNU General Public License for more details:                          //
00020 //                                                                       //
00021 //          http://www.gnu.org/copyleft/gpl.html                         //
00022 //                                                                       //
00024 
00025 class data_field_multimenu extends data_field_base {
00026 
00027     var $type = 'multimenu';
00028 
00029     function display_add_field($recordid=0) {
00030         global $DB;
00031 
00032         if ($recordid){
00033             $content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid));
00034             $content = explode('##', $content);
00035         } else {
00036             $content = array();
00037         }
00038 
00039         $str = '<div title="'.s($this->field->description).'">';
00040         $str .= '<input name="field_' . $this->field->id . '[xxx]" type="hidden" value="xxx"/>'; // hidden field - needed for empty selection
00041         $str .= '<select name="field_' . $this->field->id . '[]" id="field_' . $this->field->id . '" multiple="multiple">';
00042 
00043         foreach (explode("\n",$this->field->param1) as $option) {
00044             $option = trim($option);
00045             $str .= '<option value="' . s($option) . '"';
00046 
00047             if (in_array($option, $content)) {
00048                 // Selected by user.
00049                 $str .= ' selected = "selected"';
00050             }
00051 
00052             $str .= '>';
00053             $str .= $option . '</option>';
00054         }
00055         $str .= '</select>';
00056         $str .= '</div>';
00057 
00058         return $str;
00059     }
00060 
00061     function display_search_field($value = '') {
00062         global $CFG, $DB;
00063 
00064         if (is_array($value)){
00065             $content     = $value['selected'];
00066             $allrequired = $value['allrequired'] ? true : false;
00067         } else {
00068             $content     = array();
00069             $allrequired = false;
00070         }
00071 
00072         static $c = 0;
00073 
00074         $str = '<select name="f_'.$this->field->id.'[]" multiple="multiple">';
00075 
00076         // display only used options
00077         $varcharcontent =  $DB->sql_compare_text('content', 255);
00078         $sql = "SELECT DISTINCT $varcharcontent AS content
00079                   FROM {data_content}
00080                  WHERE fieldid=? AND content IS NOT NULL";
00081 
00082         $usedoptions = array();
00083         if ($used = $DB->get_records_sql($sql, array($this->field->id))) {
00084             foreach ($used as $data) {
00085                 $valuestr = $data->content;
00086                 if ($valuestr === '') {
00087                     continue;
00088                 }
00089                 $values = explode('##', $valuestr);
00090                 foreach ($values as $value) {
00091                     $usedoptions[$value] = $value;
00092                 }
00093             }
00094         }
00095 
00096         $found = false;
00097         foreach (explode("\n",$this->field->param1) as $option) {
00098             $option = trim($option);
00099             if (!isset($usedoptions[$option])) {
00100                 continue;
00101             }
00102             $found = true;
00103             $str .= '<option value="' . s($option) . '"';
00104 
00105             if (in_array($option, $content)) {
00106                 // Selected by user.
00107                 $str .= ' selected = "selected"';
00108             }
00109             $str .= '>' . $option . '</option>';
00110         }
00111         if (!$found) {
00112             // oh, nothing to search for
00113             return '';
00114         }
00115 
00116         $str .= '</select>';
00117 
00118         $str .= html_writer::checkbox('f_'.$this->field->id.'_allreq', null, $allrequired, get_string('selectedrequired', 'data'));
00119 
00120         return $str;
00121 
00122     }
00123 
00124     function parse_search_field() {
00125         $selected    = optional_param_array('f_'.$this->field->id, array(), PARAM_NOTAGS);
00126         $allrequired = optional_param('f_'.$this->field->id.'_allreq', 0, PARAM_BOOL);
00127         if (empty($selected)) {
00128             // no searching
00129             return '';
00130         }
00131         return array('selected'=>$selected, 'allrequired'=>$allrequired);
00132     }
00133 
00134     function generate_sql($tablealias, $value) {
00135         global $DB;
00136 
00137         static $i=0;
00138         $i++;
00139         $name = "df_multimenu_{$i}_";
00140         $params = array();
00141         $varcharcontent = $DB->sql_compare_text("{$tablealias}.content", 255);
00142 
00143         $allrequired = $value['allrequired'];
00144         $selected    = $value['selected'];
00145 
00146         if ($selected) {
00147             $conditions = array();
00148             $j=0;
00149             foreach ($selected as $sel) {
00150                 $j++;
00151                 $xname = $name.$j;
00152                 $likesel = str_replace('%', '\%', $sel);
00153                 $likeselsel = str_replace('_', '\_', $likesel);
00154                 $conditions[] = "({$tablealias}.fieldid = {$this->field->id} AND ({$varcharcontent} = :{$xname}a
00155                                                                                OR {$tablealias}.content LIKE :{$xname}b
00156                                                                                OR {$tablealias}.content LIKE :{$xname}c
00157                                                                                OR {$tablealias}.content LIKE :{$xname}d))";
00158                 $params[$xname.'a'] = $sel;
00159                 $params[$xname.'b'] = "$likesel##%";
00160                 $params[$xname.'c'] = "%##$likesel";
00161                 $params[$xname.'d'] = "%##$likesel##%";
00162             }
00163             if ($allrequired) {
00164                 return array(" (".implode(" AND ", $conditions).") ", $params);
00165             } else {
00166                 return array(" (".implode(" OR ", $conditions).") ", $params);
00167             }
00168         } else {
00169             return array(" ", array());
00170         }
00171     }
00172 
00173     function update_content($recordid, $value, $name='') {
00174         global $DB;
00175 
00176         $content = new stdClass();
00177         $content->fieldid  = $this->field->id;
00178         $content->recordid = $recordid;
00179         $content->content  = $this->format_data_field_multimenu_content($value);
00180 
00181         if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
00182             $content->id = $oldcontent->id;
00183             return $DB->update_record('data_content', $content);
00184         } else {
00185             return $DB->insert_record('data_content', $content);
00186         }
00187     }
00188 
00189     function format_data_field_multimenu_content($content) {
00190         if (!is_array($content)) {
00191             return NULL;
00192         }
00193         $options = explode("\n", $this->field->param1);
00194         $options = array_map('trim', $options);
00195 
00196         $vals = array();
00197         foreach ($content as $key=>$val) {
00198             if ($key === 'xxx') {
00199                 continue;
00200             }
00201             if (!in_array($val, $options)) {
00202                 continue;
00203             }
00204             $vals[] = $val;
00205         }
00206 
00207         if (empty($vals)) {
00208             return NULL;
00209         }
00210 
00211         return implode('##', $vals);
00212     }
00213 
00214 
00215     function display_browse_field($recordid, $template) {
00216         global $DB;
00217 
00218         if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
00219             if (empty($content->content)) {
00220                 return false;
00221             }
00222 
00223             $options = explode("\n",$this->field->param1);
00224             $options = array_map('trim', $options);
00225 
00226             $contentArr = explode('##', $content->content);
00227             $str = '';
00228             foreach ($contentArr as $line) {
00229                 if (!in_array($line, $options)) {
00230                     // hmm, looks like somebody edited the field definition
00231                     continue;
00232                 }
00233                 $str .= $line . "<br />\n";
00234             }
00235             return $str;
00236         }
00237         return false;
00238     }
00239 }
00240 
 All Data Structures Namespaces Files Functions Variables Enumerations