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

Modules/Course/classes/Export/class.ilCourseDefinedFieldDefinition.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2006 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 
00024 define("IL_CDF_SORT_ID",'field_id');
00025 define("IL_CDF_SORT_NAME",'field_name');
00026 
00027 define('IL_CDF_TYPE_TEXT',1);
00028 define('IL_CDF_TYPE_SELECT',2);
00029 
00030 
00038 class ilCourseDefinedFieldDefinition
00039 {
00040         private $db;
00041         private $obj_id;
00042 
00043         private $id;
00044         private $name;
00045         private $type;
00046         private $values;
00047         private $required;
00048         
00057         public function __construct($a_obj_id,$a_field_id = 0)
00058         {
00059                 global $ilDB;
00060                 
00061                 $this->db = $ilDB;
00062                 $this->obj_id = $a_obj_id;
00063                 $this->id = $a_field_id;
00064                 
00065                 if($this->id)
00066                 {
00067                         $this->read();
00068                 }
00069         }
00070         
00080         public static function _clone($a_source_id,$a_target_id)
00081         {
00082                 foreach(ilCourseDefinedFieldDefinition::_getFields($a_source_id) as $field_obj)
00083                 {
00084                         $cdf = new ilCourseDefinedFieldDefinition($a_target_id);
00085                         $cdf->setName($field_obj->getName());
00086                         $cdf->setType($field_obj->getType());
00087                         $cdf->setValues($field_obj->getValues());
00088                         $cdf->enableRequired($field_obj->isRequired());
00089                         $cdf->save();
00090                 }
00091         }
00092         
00101         public static function _deleteByContainer($a_container_id)
00102         {
00103                 global $ilDB;
00104                 
00105                 // Delete user entries
00106                 include_once('Modules/Course/classes/Export/class.ilCourseUserData.php');
00107                 foreach(ilCourseDefinedFieldDefinition::_getFieldIds($a_container_id) as $field_id)
00108                 {
00109                         ilCourseUserData::_deleteByField($field_id);
00110                 }
00111 
00112                 $query = "DELETE FROM crs_defined_field_definitions ".
00113                         "WHERE obj_id = ".$ilDB->quote($a_container_id)." ";
00114                 $ilDB->query($query);
00115         }
00116         
00123         public static function _hasFields($a_container_id)
00124         {
00125                 return count(ilCourseDefinedFieldDefinition::_getFields($a_container_id));
00126         }
00127         
00136         public static function _getFields($a_container_id,$a_sort = IL_CDF_SORT_NAME)
00137         {
00138                 foreach(ilCourseDefinedFieldDefinition::_getFieldIds($a_container_id,$a_sort) as $field_id)
00139                 {
00140                         $fields[] = new ilCourseDefinedFieldDefinition($a_container_id,$field_id);
00141                 }
00142                 return $fields ? $fields : array();     
00143         }
00144         
00153         public static function _getRequiredFieldIds($a_obj_id)
00154         {
00155                 global $ilDB;
00156                 
00157                 $query = "SELECT * FROM crs_defined_field_definitions ".
00158                         "WHERE obj_id = ".$ilDB->quote($a_obj_id)." ".
00159                         "AND field_required = 1";
00160                 $res = $ilDB->query($query);
00161                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00162                 {
00163                         $req_fields[] = $row->field_id;
00164                 }
00165                 return $req_fields ? $req_fields : array();
00166         }
00167         
00176         public static function _fieldsToInfoString($a_obj_id)
00177         {
00178                 global $ilDB;
00179                 
00180                 
00181                 $query = "SELECT field_name FROM crs_defined_field_definitions ".
00182                         "WHERE obj_id = ".$ilDB->quote($a_obj_id);
00183                 
00184                 $res = $ilDB->query($query);
00185                 $fields = array();
00186                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00187                 {
00188                         $fields[] = $row->field_name;
00189                 }
00190                 return implode('<br />',$fields);               
00191         }
00192         
00201         public static function _getFieldIds($a_container_id,$a_sort = IL_CDF_SORT_ID)
00202         {
00203                 global $ilDB;
00204                 
00205                 $query = "SELECT field_id FROM crs_defined_field_definitions ".
00206                         "WHERE obj_id = ".$ilDB->quote($a_container_id)." ".
00207                         "ORDER BY ".$ilDB->quote($a_sort);
00208                 $res = $ilDB->query($query);
00209                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00210                 {
00211                         $field_ids[] = $row->field_id;
00212                 }
00213                 return $field_ids ? $field_ids : array();       
00214         }
00215                 
00224         public static function _lookupName($a_field_id)
00225         {
00226                 global $ilDB;
00227                 
00228                 $query = "SELECT * FROM crs_defined_field_definitions ".
00229                         "WHERE field_id = ".$ilDB->quote($a_field_id);
00230                 
00231                 $res = $ilDB->query($query);
00232                 $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
00233                 
00234                 return $row->field_name ? $row->field_name : '';
00235         }
00236         
00237         public function getObjId()
00238         {
00239                 return $this->obj_id;
00240         }
00241         public function getId()
00242         {
00243                 return $this->id;
00244         }
00245         public function getType()
00246         {
00247                 return $this->type;
00248         }
00249         public function setType($a_type)
00250         {
00251                 $this->type = $a_type;
00252         }
00253         public function getName()
00254         {
00255                 return $this->name;
00256         }
00257         public function setName($a_name)
00258         {
00259                 $this->name = $a_name;
00260         }
00261         public function getValues()
00262         {
00263                 return $this->values ? $this->values : array();
00264         }
00265         public function setValues($a_values)
00266         {
00267                 $this->values = $a_values;
00268         }
00269         public function getValueById($a_id)
00270         {
00271                 if(is_array($this->values) and array_key_exists($a_id,$this->values))
00272                 {
00273                         return $this->values[$a_id];
00274                 }
00275                 return '';
00276         }
00277         public function getIdByValue($a_value)
00278         {
00279                 return ($pos = array_search($a_value,$this->values) === false) ? -1 : $pos;
00280         }
00281         
00282         public function isRequired()
00283         {
00284                 return (bool) $this->required;
00285         }
00286         public function enableRequired($a_status)
00287         {
00288                 $this->required = $a_status;
00289         }
00290         
00298         public function prepareSelectBox()
00299         {
00300                 global $lng;
00301                 
00302                 $options = array();
00303                 $options[-1] = $lng->txt('select_one');
00304                 
00305                 foreach($this->values as $value)
00306                 {
00307                         $options[$value] = $value;
00308                 }
00309                 return $options;
00310         }
00311         
00318         public function prepareValues($a_values)
00319         {
00320                 $tmp_values = array();
00321                 
00322                 if(!is_array($a_values))
00323                 {
00324                         return false;
00325                 }
00326                 foreach($a_values as $value)
00327                 {
00328                         $value = trim(ilUtil::stripSlashes($value));
00329                         if(strlen($value))
00330                         {
00331                                 $tmp_values[] = $value;
00332                         }
00333                 }
00334                 sort($tmp_values);
00335                 return $tmp_values ? $tmp_values : array();
00336         }
00337         
00343         public function appendValues($a_values)
00344         {
00345                 if(!is_array($a_values))
00346                 {
00347                         return false;
00348                 }
00349                 $this->values = array_unique(array_merge($this->values,$a_values));
00350                 sort($this->values);
00351                 return true;
00352         }
00353         
00359         public function deleteValue($a_id)
00360         {
00361                 if(!isset($this->values[$a_id]))
00362                 {
00363                         return false;
00364                 }
00365                 unset($this->values[$a_id]);
00366                 array_merge($this->values);
00367                 $this->update();
00368                 return true;
00369         }
00370         
00377         public function save()
00378         {
00379                 global $ilDB;
00380                 
00381                 $query = "INSERT INTO crs_defined_field_definitions ".
00382                         "SET obj_id = ".$this->db->quote($this->getObjId()).", ".
00383                         "field_name = ".$this->db->quote($this->getName()).", ".
00384                         "field_type = ".$this->db->quote($this->getType()).", ".
00385                         "field_values = ".$this->db->quote(serialize($this->getValues())).", ".
00386                         "field_required = ".$ilDB->quote($this->isRequired())." ";
00387                 $res = $this->db->query($query);
00388                 $this->id = $this->db->getLastInsertId();
00389                         
00390                 return true;                    
00391         }
00392         
00398         public function update()
00399         {
00400                 global $ilDB;
00401                 
00402                 $query = "UPDATE crs_defined_field_definitions ".
00403                         "SET field_name = ".$this->db->quote($this->getName()).", ".
00404                         "field_type = ".$this->db->quote($this->getType()).", ".
00405                         "field_values = ".$this->db->quote(serialize($this->getValues())).", ".
00406                         "field_required = ".$ilDB->quote($this->isRequired())." ".
00407                         "WHERE field_id = ".$this->db->quote($this->getId())." ".
00408                         "AND obj_id = ".$this->db->quote($this->getObjId());
00409                 $this->db->query($query);
00410                 return true;
00411         }
00412         
00420         public function delete()
00421         {
00422                 global $ilDB;
00423                 
00424                 include_once('Modules/Course/classes/Export/class.ilCourseUserData.php');
00425                 ilCourseUserData::_deleteByField($this->getId());
00426                 
00427                 $query = "DELETE FROM crs_defined_field_definitions ".
00428                         "WHERE field_id = ".$this->db->quote($this->getId())." ";
00429                 $this->db->query($query);
00430         }
00431         
00438         private function read()
00439         {
00440                 $query = "SELECT * FROM crs_defined_field_definitions ".
00441                         "WHERE field_id = ".$this->db->quote($this->getId())." ".
00442                         "AND obj_id = ".$this->db->quote($this->getObjId())." ";
00443                 
00444                 $res = $this->db->query($query);
00445                 $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
00446                 
00447                 $this->setName($row->field_name);
00448                 $this->setType($row->field_type);
00449                 $this->setValues(unserialize($row->field_values));
00450                 $this->enableRequired($row->field_required);
00451         }
00452 }
00453 
00454 
00455 ?>

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