• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

Services/User/classes/class.ilUserDefinedFields.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00033 define('UDF_TYPE_TEXT',1);
00034 define('UDF_TYPE_SELECT',2);
00035 define('UDF_NO_VALUES',1);
00036 define('UDF_DUPLICATE_VALUES',2);
00037 
00038 class ilUserDefinedFields
00039 {
00040         var $db = null;
00041         var $definitions = array();
00042 
00043 
00051         private function __construct()
00052         {
00053                 global $ilDB;
00054 
00055                 $this->db =& $ilDB;
00056 
00057                 $this->__read();
00058         }
00059 
00060         function _getInstance()
00061         {
00062                 static $udf = null;
00063 
00064                 if(!is_object($udf))
00065                 {
00066                         return $udf = new ilUserDefinedFields();
00067                 }
00068                 return $udf;
00069         }
00070 
00071         function fetchFieldIdFromImportId($a_import_id)
00072         {
00073                 global $ilSetting;
00074 
00075                 if(!strlen($a_import_id))
00076                 {
00077                         return 0;
00078                 }
00079                 $parts = explode('_',$a_import_id);
00080 
00081                 if($parts[0] != 'il')
00082                 {
00083                         return 0;
00084                 }
00085                 if($parts[1] != $ilSetting->get('inst_id',0))
00086                 {
00087                         return 0;
00088                 }
00089                 if($parts[2] != 'udf')
00090                 {
00091                         return 0;
00092                 }
00093                 if($parts[3])
00094                 {
00095                         // Check if field exists
00096                         if(is_array($this->definitions["$parts[3]"]))
00097                         {
00098                                 return $parts[3];
00099                         }
00100                 }
00101                 return 0;
00102         }
00103         function fetchFieldIdFromName($a_name)
00104         {
00105                 foreach($this->definitions as $definition)
00106                 {
00107                         if($definition['field_name'] == $a_name)
00108                         {
00109                                 return $definition['field_id'];
00110                         }
00111                 }
00112                 return 0;
00113         }
00114 
00115         function getDefinitions()
00116         {
00117                 return $this->definitions ? $this->definitions : array();
00118         }
00119 
00120         function getDefinition($a_id)
00121         {
00122                 return is_array($this->definitions[$a_id]) ? $this->definitions[$a_id] : array();
00123         }
00124 
00125         function getVisibleDefinitions()
00126         {
00127                 foreach($this->definitions as $id => $definition)
00128                 {
00129                         if($definition['visible'])
00130                         {
00131                                 $visible_definition[$id] = $definition;
00132                         }
00133                 }
00134                 return $visible_definition ? $visible_definition : array();
00135         }
00136 
00137         function getSearchableDefinitions()
00138         {
00139                 foreach($this->definitions as $id => $definition)
00140                 {
00141                         if($definition['searchable'])
00142                         {
00143                                 $searchable_definition[$id] = $definition;
00144                         }
00145                 }
00146                 return $searchable_definition ? $searchable_definition : array();
00147         }
00148 
00156         public function getCourseExportableFields()
00157         {
00158                 foreach($this->definitions as $id => $definition)
00159                 {
00160                         if($definition['course_export'])
00161                         {
00162                                 $cexp_definition[$id] = $definition;
00163                         }
00164                 }
00165                 return $cexp_definition ? $cexp_definition : array();
00166         }
00167 
00168 
00169         function setFieldName($a_name)
00170         {
00171                 $this->field_name = $a_name;
00172         }
00173         function getFieldName()
00174         {
00175                 return $this->field_name;
00176         }
00177         function setFieldType($a_type)
00178         {
00179                 $this->field_type = $a_type;
00180         }
00181         function getFieldType()
00182         {
00183                 return $this->field_type;
00184         }
00185         function setFieldValues($a_values)
00186         {
00187                 $this->field_values = array();
00188                 foreach($a_values as $value)
00189                 {
00190                         if(strlen($value))
00191                         {
00192                                 $this->field_values[] = $value;
00193                         }
00194                 }
00195         }
00196         function getFieldValues()
00197         {
00198                 return $this->field_values ? $this->field_values : array();
00199         }
00200 
00201         function enableVisible($a_visible)
00202         {
00203                 $this->field_visible = $a_visible;
00204         }
00205         function enabledVisible()
00206         {
00207                 return $this->field_visible;
00208         }
00209         function enableChangeable($a_changeable)
00210         {
00211                 $this->field_changeable = $a_changeable;
00212         }
00213         function enabledChangeable()
00214         {
00215                 return $this->field_changeable;
00216         }
00217         function enableRequired($a_required)
00218         {
00219                 $this->field_required = $a_required;
00220         }
00221         function enabledRequired()
00222         {
00223                 return $this->field_required;
00224         }
00225         function enableSearchable($a_searchable)
00226         {
00227                 $this->field_searchable = $a_searchable;
00228         }
00229         function enabledSearchable()
00230         {
00231                 return $this->field_searchable;
00232         }
00233         function enableExport($a_export)
00234         {
00235                 $this->field_export = $a_export;
00236         }
00237         function enabledExport()
00238         {
00239                 return $this->field_export;
00240         }
00241         function enableCourseExport($a_course_export)
00242         {
00243                 $this->field_course_export = $a_course_export;
00244         }
00245         function enabledCourseExport()
00246         {
00247                 return $this->field_course_export;
00248         }
00249 
00250 
00251         function fieldValuesToSelectArray($a_values)
00252         {
00253                 foreach($a_values as $value)
00254                 {
00255                         $values[$value] = $value;
00256                 }
00257                 return $values ? $values : array();
00258         }
00259 
00260         function validateValues()
00261         {
00262                 $number = 0;
00263                 $unique = array();
00264                 foreach($this->getFieldValues() as $value)
00265                 {
00266                         if(!strlen($value))
00267                         {
00268                                 continue;
00269                         }
00270                         $number++;
00271                         $unique[$value] = $value;
00272                 }
00273 
00274                 if(!count($unique))
00275                 {
00276                         return UDF_NO_VALUES;
00277                 }
00278                 if($number != count($unique))
00279                 {
00280                         return UDF_DUPLICATE_VALUES;
00281                 }
00282                 return 0;
00283         }
00284 
00285         function nameExists($a_field_name)
00286         {
00287                 $query = "SELECT * FROM user_defined_field_definition ".
00288                         "WHERE field_name = ".$this->db->quote($a_field_name)." ";
00289 
00290                 $res = $this->db->query($query);
00291 
00292                 return (bool) $res->numRows();
00293         }
00294 
00295         function add()
00296         {
00297                 // Add definition entry
00298                 $query = "INSERT INTO user_defined_field_definition ".
00299                         "SET field_name = ".$this->db->quote($this->getFieldName()).", ".
00300                         "field_type = ".$this->db->quote($this->getFieldType()).", ".
00301                         "field_values = '".addslashes(serialize($this->getFieldValues()))."', ".
00302                         "visible = '".(int) $this->enabledVisible()."', ".
00303                         "changeable = '".(int) $this->enabledChangeable()."', ".
00304                         "required = '".(int) $this->enabledRequired()."', ".
00305                         "searchable = '".(int) $this->enabledSearchable()."', ".
00306                         "export = '".(int) $this->enabledExport()."', ".
00307                         "course_export = '".(int) $this->enabledCourseExport()."'";
00308 
00309                 $this->db->query($query);
00310 
00311                 // add table field in usr_defined_data
00312                 $field_id = $this->db->getLastInsertId();
00313 
00314                 $query = "ALTER TABLE usr_defined_data ADD `".(int) $field_id."` TEXT NOT NULL";
00315                 $this->db->query($query);
00316 
00317                 $this->__read();
00318 
00319                 return true;
00320         }
00321         function delete($a_id)
00322         {
00323                 // Delete definitions
00324                 $query = "DELETE FROM user_defined_field_definition ".
00325                         "WHERE field_id = ".$this->db->quote($a_id)." ";
00326                 $this->db->query($query);
00327 
00328                 // Delete usr_data entries
00329                 $query = "ALTER TABLE usr_defined_data DROP `".(int) $a_id."`";
00330                 $this->db->query($query);
00331 
00332                 $this->__read();
00333 
00334                 return true;
00335         }
00336 
00337         function update($a_id)
00338         {
00339                 $query = "UPDATE user_defined_field_definition ".
00340                         "SET field_name = ".$this->db->quote($this->getFieldName()).", ".
00341                         "field_type = ".$this->db->quote($this->getFieldType()).", ".
00342                         "field_values = '".addslashes(serialize($this->getFieldValues()))."', ".
00343                         "visible = '".(int) $this->enabledVisible()."', ".
00344                         "changeable = '".(int) $this->enabledChangeable()."', ".
00345                         "required = '".(int) $this->enabledRequired()."', ".
00346                         "searchable = '".(int) $this->enabledSearchable()."', ".
00347                         "export = '".(int) $this->enabledExport()."', ".
00348                         "course_export = '".(int) $this->enabledCourseExport()."' ".
00349                         "WHERE field_id = ".$this->db->quote($a_id)." ";
00350 
00351                 $this->db->query($query);
00352                 $this->__read();
00353 
00354                 return true;
00355         }
00356 
00357 
00358 
00359         // Private
00360         function __read()
00361         {
00362                 global $ilSetting;
00363 
00364                 $query = "SELECT * FROM user_defined_field_definition ";
00365                 $res = $this->db->query($query);
00366 
00367                 $this->definitions = array();
00368                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00369                 {
00370                         $this->definitions[$row->field_id]['field_id'] = $row->field_id;
00371                         $this->definitions[$row->field_id]['field_name'] = $row->field_name;
00372                         $this->definitions[$row->field_id]['field_type'] = $row->field_type;
00373                         $this->definitions[$row->field_id]['il_id'] = 'il_'.$ilSetting->get('inst_id',0).'_udf_'.$row->field_id;
00374 
00375                         $tmp = unserialize(stripslashes($row->field_values));
00376                         sort($tmp);
00377                         $this->definitions[$row->field_id]['field_values'] = $tmp;
00378 
00379                         $this->definitions[$row->field_id]['visible'] = $row->visible;
00380                         $this->definitions[$row->field_id]['changeable'] = $row->changeable;
00381                         $this->definitions[$row->field_id]['required'] = $row->required;
00382                         $this->definitions[$row->field_id]['searchable'] = $row->searchable;
00383                         $this->definitions[$row->field_id]['export'] = $row->export;
00384                         $this->definitions[$row->field_id]['course_export'] = $row->course_export;
00385 
00386                 }
00387 
00388                 return true;
00389         }
00390 
00391         function deleteValue($a_field_id,$a_value_id)
00392         {
00393                 $definition = $this->getDefinition($a_field_id);
00394 
00395                 $counter = 0;
00396                 $new_values = array();
00397                 foreach($definition['field_values'] as $value)
00398                 {
00399                         if($counter++ != $a_value_id)
00400                         {
00401                                 $new_values[] = $value;
00402                         }
00403                         else
00404                         {
00405                                 $old_value = $value;
00406                         }
00407                 }
00408                 $query = "UPDATE user_defined_field_definition ".
00409                         "SET field_values = '".addslashes(serialize($new_values))."' ".
00410                         "WHERE field_id = ".$this->db->quote($a_field_id)."";
00411 
00412                 $this->db->query($query);
00413 
00414                 // Update usr_data
00415                 $query = "UPDATE usr_defined_data ".
00416                         "SET `".$this->db->quote($a_field_id)."` = '' ".
00417                         "WHERE `".$this->db->quote($a_field_id)."` = ".$this->db->quote($old_value)."";
00418                 $this->db->query($query);
00419 
00420                 // fianally read data
00421                 $this->__read();
00422 
00423                 return true;
00424         }
00425 
00426         function toXML()
00427         {
00428                 include_once 'classes/class.ilXmlWriter.php';
00429                 $xml_writer = new ilXmlWriter();
00430 
00431                 $this->addToXML ($xml_writer);
00432 
00433                 return $xml_writer->xmlDumpMem(false);
00434         }
00435 
00440         function addToXML($xml_writer)
00441         {
00442             $xml_writer->xmlStartTag ("UDFDefinitions");
00443                 foreach($this->getDefinitions() as $definition)
00444                 {
00445             $attributes = array(
00446                 "Id" => $definition ["il_id"],
00447                 "Type" => $definition["field_type"] == UDF_TYPE_SELECT? "SELECT" : "TEXT",
00448                 "Visible" => $definition["visible"]? "TRUE" : "FALSE",
00449                 "Changeable" => $definition["changeable"]? "TRUE" : "FALSE",
00450                 "Required" => $definition["required"]? "TRUE" : "FALSE",
00451                 "Searchable" => $definition["searchable"]? "TRUE" : "FALSE",
00452                 "CourseExport" => $definition["course_export"]? "TRUE" : "FALSE",
00453                 "Export" => $definition["export"]? "TRUE" : "FALSE",
00454             );
00455                     $xml_writer->xmlStartTag ("UDFDefinition", $attributes);
00456                     $xml_writer->xmlElement('UDFName', null, $definition['field_name']);
00457                     if ($definition["field_type"] == UDF_TYPE_SELECT ) {
00458                         $field_values = $definition["field_values"];
00459                         foreach ($field_values as $field_value)
00460                         {
00461                            $xml_writer->xmlElement('UDFValue', null, $field_value);
00462                         }
00463                     }
00464                     $xml_writer->xmlEndTag ("UDFDefinition");
00465                 }
00466             $xml_writer->xmlEndTag ("UDFDefinitions");
00467 
00468         }
00469 }
00470 ?>

Generated on Fri Dec 13 2013 17:57:02 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1