Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00034 define('UDF_TYPE_TEXT',1);
00035 define('UDF_TYPE_SELECT',2);
00036 define('UDF_NO_VALUES',1);
00037 define('UDF_DUPLICATE_VALUES',2);
00038
00039 class ilUserDefinedFields
00040 {
00041 var $db = null;
00042 var $definitions = array();
00043
00044
00052 function ilUserDefinedFields()
00053 {
00054 global $ilDB;
00055
00056 $this->db =& $ilDB;
00057
00058 $this->__read();
00059 }
00060
00061 function &_getInstance()
00062 {
00063 static $udf = null;
00064
00065 if(!is_object($udf))
00066 {
00067 return $udf = new ilUserDefinedFields();
00068 }
00069 return $udf;
00070 }
00071
00072 function fetchFieldIdFromImportId($a_import_id)
00073 {
00074 global $ilSetting;
00075
00076 if(!strlen($a_import_id))
00077 {
00078 return 0;
00079 }
00080 $parts = explode('_',$a_import_id);
00081
00082 if($parts[0] != 'il')
00083 {
00084 return 0;
00085 }
00086 if($parts[1] != $ilSetting->get('inst_id',0))
00087 {
00088 return 0;
00089 }
00090 if($parts[2] != 'udf')
00091 {
00092 return 0;
00093 }
00094 if($parts[3])
00095 {
00096
00097 if(is_array($this->definitions["$parts[3]"]))
00098 {
00099 return $parts[3];
00100 }
00101 }
00102 return 0;
00103 }
00104 function fetchFieldIdFromName($a_name)
00105 {
00106 foreach($this->definitions as $definition)
00107 {
00108 if($definition['field_name'] == $a_name)
00109 {
00110 return $definition['field_id'];
00111 }
00112 }
00113 return 0;
00114 }
00115
00116 function getDefinitions()
00117 {
00118 return $this->definitions ? $this->definitions : array();
00119 }
00120
00121 function getDefinition($a_id)
00122 {
00123 return is_array($this->definitions[$a_id]) ? $this->definitions[$a_id] : array();
00124 }
00125
00126 function getVisibleDefinitions()
00127 {
00128 foreach($this->definitions as $id => $definition)
00129 {
00130 if($definition['visible'])
00131 {
00132 $visible_definition[$id] = $definition;
00133 }
00134 }
00135 return $visible_definition ? $visible_definition : array();
00136 }
00137
00138 function getSearchableDefinitions()
00139 {
00140 foreach($this->definitions as $id => $definition)
00141 {
00142 if($definition['searchable'])
00143 {
00144 $searchable_definition[$id] = $definition;
00145 }
00146 }
00147 return $searchable_definition ? $searchable_definition : array();
00148 }
00149
00150
00151 function setFieldName($a_name)
00152 {
00153 $this->field_name = $a_name;
00154 }
00155 function getFieldName()
00156 {
00157 return $this->field_name;
00158 }
00159 function setFieldType($a_type)
00160 {
00161 $this->field_type = $a_type;
00162 }
00163 function getFieldType()
00164 {
00165 return $this->field_type;
00166 }
00167 function setFieldValues($a_values)
00168 {
00169 $this->field_values = array();
00170 foreach($a_values as $value)
00171 {
00172 if(strlen($value))
00173 {
00174 $this->field_values[] = $value;
00175 }
00176 }
00177 }
00178 function getFieldValues()
00179 {
00180 return $this->field_values ? $this->field_values : array();
00181 }
00182
00183 function enableVisible($a_visible)
00184 {
00185 $this->field_visible = $a_visible;
00186 }
00187 function enabledVisible()
00188 {
00189 return $this->field_visible;
00190 }
00191 function enableChangeable($a_changeable)
00192 {
00193 $this->field_changeable = $a_changeable;
00194 }
00195 function enabledChangeable()
00196 {
00197 return $this->field_changeable;
00198 }
00199 function enableRequired($a_required)
00200 {
00201 $this->field_required = $a_required;
00202 }
00203 function enabledRequired()
00204 {
00205 return $this->field_required;
00206 }
00207 function enableSearchable($a_searchable)
00208 {
00209 $this->field_searchable = $a_searchable;
00210 }
00211 function enabledSearchable()
00212 {
00213 return $this->field_searchable;
00214 }
00215
00216
00217 function fieldValuesToSelectArray($a_values)
00218 {
00219 foreach($a_values as $value)
00220 {
00221 $values[$value] = $value;
00222 }
00223 return $values ? $values : array();
00224 }
00225
00226 function validateValues()
00227 {
00228 $number = 0;
00229 $unique = array();
00230 foreach($this->getFieldValues() as $value)
00231 {
00232 if(!strlen($value))
00233 {
00234 continue;
00235 }
00236 $number++;
00237 $unique[$value] = $value;
00238 }
00239
00240 if(!count($unique))
00241 {
00242 return UDF_NO_VALUES;
00243 }
00244 if($number != count($unique))
00245 {
00246 return UDF_DUPLICATE_VALUES;
00247 }
00248 return 0;
00249 }
00250
00251 function nameExists($a_field_name)
00252 {
00253 $query = "SELECT * FROM user_defined_field_definition ".
00254 "WHERE field_name = '".$a_field_name."'";
00255
00256 $res = $this->db->query($query);
00257
00258 return (bool) $res->numRows();
00259 }
00260
00261 function add()
00262 {
00263
00264 $query = "INSERT INTO user_defined_field_definition ".
00265 "SET field_name = '".$this->getFieldName()."', ".
00266 "field_type = '".$this->getFieldType()."', ".
00267 "field_values = '".addslashes(serialize($this->getFieldValues()))."', ".
00268 "visible = '".(int) $this->enabledVisible()."', ".
00269 "changeable = '".(int) $this->enabledChangeable()."', ".
00270 "required = '".(int) $this->enabledRequired()."', ".
00271 "searchable = '".(int) $this->enabledSearchable()."'";
00272
00273 $this->db->query($query);
00274
00275
00276 $field_id = $this->db->getLastInsertId();
00277
00278 $query = "ALTER TABLE usr_defined_data ADD `".$field_id."` TEXT NOT NULL";
00279 $this->db->query($query);
00280
00281 $this->__read();
00282
00283 return true;
00284 }
00285 function delete($a_id)
00286 {
00287
00288 $query = "DELETE FROM user_defined_field_definition ".
00289 "WHERE field_id = '".$a_id."'";
00290 $this->db->query($query);
00291
00292
00293 $query = "ALTER TABLE usr_defined_data DROP `".$a_id."`";
00294 $this->db->query($query);
00295
00296 $this->__read();
00297
00298 return true;
00299 }
00300
00301 function update($a_id)
00302 {
00303 $query = "UPDATE user_defined_field_definition ".
00304 "SET field_name = '".$this->getFieldName()."', ".
00305 "field_type = '".$this->getFieldType()."', ".
00306 "field_values = '".addslashes(serialize($this->getFieldValues()))."', ".
00307 "visible = '".(int) $this->enabledVisible()."', ".
00308 "changeable = '".(int) $this->enabledChangeable()."', ".
00309 "required = '".(int) $this->enabledRequired()."', ".
00310 "searchable = '".(int) $this->enabledSearchable()."' ".
00311 "WHERE field_id = '".$a_id."'";
00312
00313 $this->db->query($query);
00314 $this->__read();
00315
00316 return true;
00317 }
00318
00319
00320
00321
00322 function __read()
00323 {
00324 global $ilSetting;
00325
00326 $query = "SELECT * FROM user_defined_field_definition ";
00327 $res = $this->db->query($query);
00328
00329 $this->definitions = array();
00330 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00331 {
00332 $this->definitions[$row->field_id]['field_id'] = $row->field_id;
00333 $this->definitions[$row->field_id]['field_name'] = $row->field_name;
00334 $this->definitions[$row->field_id]['field_type'] = $row->field_type;
00335 $this->definitions[$row->field_id]['il_id'] = 'il_'.$ilSetting->get('inst_id',0).'_udf_'.$row->field_id;
00336
00337 $tmp = unserialize(stripslashes($row->field_values));
00338 sort($tmp);
00339 $this->definitions[$row->field_id]['field_values'] = $tmp;
00340
00341 $this->definitions[$row->field_id]['visible'] = $row->visible;
00342 $this->definitions[$row->field_id]['changeable'] = $row->changeable;
00343 $this->definitions[$row->field_id]['required'] = $row->required;
00344 $this->definitions[$row->field_id]['searchable'] = $row->searchable;
00345
00346 }
00347
00348 return true;
00349 }
00350
00351 function deleteValue($a_field_id,$a_value_id)
00352 {
00353 $definition = $this->getDefinition($a_field_id);
00354
00355 $counter = 0;
00356 $new_values = array();
00357 foreach($definition['field_values'] as $value)
00358 {
00359 if($counter++ != $a_value_id)
00360 {
00361 $new_values[] = $value;
00362 }
00363 else
00364 {
00365 $old_value = $value;
00366 }
00367 }
00368 $query = "UPDATE user_defined_field_definition ".
00369 "SET field_values = '".addslashes(serialize($new_values))."' ".
00370 "WHERE field_id = '".$a_field_id."'";
00371
00372 $this->db->query($query);
00373
00374
00375 $query = "UPDATE usr_defined_data ".
00376 "SET `".$a_field_id."` = '' ".
00377 "WHERE `".$a_field_id."` = '".$old_value."'";
00378 $this->db->query($query);
00379
00380
00381 $this->__read();
00382
00383 return true;
00384 }
00385 }
00386 ?>