1<?php 2/* 3 +-----------------------------------------------------------------------------+ 4 | ILIAS open source | 5 +-----------------------------------------------------------------------------+ 6 | Copyright (c) 1998-2006 ILIAS open source, University of Cologne | 7 | | 8 | This program is free software; you can redistribute it and/or | 9 | modify it under the terms of the GNU General Public License | 10 | as published by the Free Software Foundation; either version 2 | 11 | of the License, or (at your option) any later version. | 12 | | 13 | This program is distributed in the hope that it will be useful, | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 16 | GNU General Public License for more details. | 17 | | 18 | You should have received a copy of the GNU General Public License | 19 | along with this program; if not, write to the Free Software | 20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 21 +-----------------------------------------------------------------------------+ 22*/ 23 24define("IL_CDF_SORT_ID", 'field_id'); 25define("IL_CDF_SORT_NAME", 'field_name'); 26 27define('IL_CDF_TYPE_TEXT', 1); 28define('IL_CDF_TYPE_SELECT', 2); 29 30 31/** 32* @author Stefan Meyer <meyer@leifos.com> 33* @version $Id$ 34* 35* 36* @ingroup Modules/Course 37*/ 38class ilCourseDefinedFieldDefinition 39{ 40 private $db; 41 private $obj_id; 42 43 private $id; 44 private $name; 45 private $type; 46 private $values; 47 private $value_options = array(); 48 private $required; 49 50 /** 51 * Constructor 52 * 53 * @access public 54 * @param int course obj_id 55 * @param int field_id 56 * 57 */ 58 public function __construct($a_obj_id, $a_field_id = 0) 59 { 60 global $DIC; 61 62 $ilDB = $DIC['ilDB']; 63 64 $this->db = $ilDB; 65 $this->obj_id = $a_obj_id; 66 $this->id = $a_field_id; 67 68 if ($this->id) { 69 $this->read(); 70 } 71 } 72 73 /** 74 * Clone fields 75 * 76 * @access public 77 * @static 78 * 79 * @param int source obj id 80 * @param int target obj_id 81 */ 82 public static function _clone($a_source_id, $a_target_id) 83 { 84 foreach (ilCourseDefinedFieldDefinition::_getFields($a_source_id) as $field_obj) { 85 $cdf = new ilCourseDefinedFieldDefinition($a_target_id); 86 $cdf->setName($field_obj->getName()); 87 $cdf->setType($field_obj->getType()); 88 $cdf->setValues($field_obj->getValues()); 89 $cdf->setValueOptions($field_obj->getValueOptions()); 90 $cdf->enableRequired($field_obj->isRequired()); 91 $cdf->save(); 92 } 93 } 94 95 /** 96 * Delete all fields of a container 97 * 98 * @access public 99 * @static 100 * @param int container_id 101 * 102 */ 103 public static function _deleteByContainer($a_container_id) 104 { 105 global $DIC; 106 107 $ilDB = $DIC['ilDB']; 108 109 // Delete user entries 110 include_once('Modules/Course/classes/Export/class.ilCourseUserData.php'); 111 foreach (ilCourseDefinedFieldDefinition::_getFieldIds($a_container_id) as $field_id) { 112 ilCourseUserData::_deleteByField($field_id); 113 } 114 115 $query = "DELETE FROM crs_f_definitions " . 116 "WHERE obj_id = " . $ilDB->quote($a_container_id, 'integer') . " "; 117 $res = $ilDB->manipulate($query); 118 } 119 120 /** 121 * Check if there are any define fields 122 * 123 * @access public 124 * @param int container_id 125 */ 126 public static function _hasFields($a_container_id) 127 { 128 return count(ilCourseDefinedFieldDefinition::_getFields($a_container_id)); 129 } 130 131 /** 132 * Get all fields of a container 133 * 134 * @access public 135 * @static 136 * @param int container obj_id 137 * @return ilCourseDefinedFieldDefinitions[] 138 */ 139 public static function _getFields($a_container_id, $a_sort = IL_CDF_SORT_NAME) 140 { 141 foreach (ilCourseDefinedFieldDefinition::_getFieldIds($a_container_id, IL_CDF_SORT_ID) as $field_id) { 142 $fields[] = new ilCourseDefinedFieldDefinition($a_container_id, $field_id); 143 } 144 return $fields ? $fields : array(); 145 } 146 147 /** 148 * Get required filed id's 149 * 150 * @access public 151 * @static 152 * 153 * @param int container id 154 */ 155 public static function _getRequiredFieldIds($a_obj_id) 156 { 157 global $DIC; 158 159 $ilDB = $DIC['ilDB']; 160 161 $query = "SELECT * FROM crs_f_definitions " . 162 "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " " . 163 "AND field_required = 1"; 164 $res = $ilDB->query($query); 165 while ($row = $ilDB->fetchObject($res)) { 166 $req_fields[] = $row->field_id; 167 } 168 return $req_fields ? $req_fields : array(); 169 } 170 171 /** 172 * Fields to info string 173 * 174 * @access public 175 * @static 176 * 177 * @param int obj_id 178 */ 179 public static function _fieldsToInfoString($a_obj_id) 180 { 181 global $DIC; 182 183 $ilDB = $DIC['ilDB']; 184 185 186 $query = "SELECT field_name FROM crs_f_definitions " . 187 "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer'); 188 189 $res = $ilDB->query($query); 190 $fields = array(); 191 while ($row = $ilDB->fetchObject($res)) { 192 $fields[] = $row->field_name; 193 } 194 return implode('<br />', $fields); 195 } 196 197 /** 198 * Get all field ids of a container 199 * 200 * @access public 201 * @static 202 * @param int container obj_id 203 * @return array array of field ids 204 */ 205 public static function _getFieldIds($a_container_id, $a_sort = IL_CDF_SORT_ID) 206 { 207 global $DIC; 208 209 $ilDB = $DIC['ilDB']; 210 211 $query = "SELECT field_id FROM crs_f_definitions " . 212 "WHERE obj_id = " . $ilDB->quote($a_container_id, 'integer') . " " . 213 "ORDER BY " . IL_CDF_SORT_ID; 214 $res = $ilDB->query($query); 215 while ($row = $ilDB->fetchObject($res)) { 216 $field_ids[] = $row->field_id; 217 } 218 return $field_ids ? $field_ids : array(); 219 } 220 221 /** 222 * Lookup field name 223 * 224 * @access public 225 * @static 226 * 227 * @param int field_id 228 */ 229 public static function _lookupName($a_field_id) 230 { 231 global $DIC; 232 233 $ilDB = $DIC['ilDB']; 234 235 $query = "SELECT * FROM crs_f_definitions " . 236 "WHERE field_id = " . $ilDB->quote($a_field_id, 'integer'); 237 238 $res = $ilDB->query($query); 239 $row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT); 240 241 return $row->field_name ? $row->field_name : ''; 242 } 243 244 public function getObjId() 245 { 246 return $this->obj_id; 247 } 248 public function getId() 249 { 250 return $this->id; 251 } 252 public function getType() 253 { 254 return $this->type; 255 } 256 public function setType($a_type) 257 { 258 $this->type = $a_type; 259 } 260 public function getName() 261 { 262 return $this->name; 263 } 264 public function setName($a_name) 265 { 266 $this->name = $a_name; 267 } 268 public function getValues() 269 { 270 return $this->values ? $this->values : array(); 271 } 272 public function setValues($a_values) 273 { 274 $this->values = $a_values; 275 } 276 public function getValueById($a_id) 277 { 278 if (is_array($this->values) and array_key_exists($a_id, $this->values)) { 279 return $this->values[$a_id]; 280 } 281 return ''; 282 } 283 public function getIdByValue($a_value) 284 { 285 return (($pos = array_search($a_value, $this->values)) === false) ? -1 : $pos; 286 } 287 288 public function isRequired() 289 { 290 return (bool) $this->required; 291 } 292 public function enableRequired($a_status) 293 { 294 $this->required = $a_status; 295 } 296 297 public function setValueOptions($a_options) 298 { 299 $this->value_options = $a_options; 300 } 301 302 public function getValueOptions() 303 { 304 return (array) $this->value_options; 305 } 306 307 308 /** 309 * Prepare an array of options for ilUtil::formSelect() 310 * 311 * @access public 312 * @param 313 * 314 */ 315 public function prepareSelectBox() 316 { 317 global $DIC; 318 319 $lng = $DIC['lng']; 320 321 $options = array(); 322 $options[''] = $lng->txt('select_one'); 323 324 foreach ($this->values as $key => $value) { 325 $options[$this->getId() . '_' . $key] = $value; 326 } 327 return $options; 328 } 329 330 /** 331 * Prepare values from POST 332 * 333 * @param array array of values 334 * @access public 335 */ 336 public function prepareValues($a_values) 337 { 338 $tmp_values = array(); 339 340 if (!is_array($a_values)) { 341 return false; 342 } 343 foreach ($a_values as $idx => $value) { 344 if (strlen($value)) { 345 $tmp_values[$idx] = $value; 346 } 347 } 348 return $tmp_values ? $tmp_values : array(); 349 } 350 351 /** 352 * Append Values 353 * 354 * @access public 355 */ 356 public function appendValues($a_values) 357 { 358 if (!is_array($a_values)) { 359 return false; 360 } 361 $this->values = array_unique(array_merge($this->values, $a_values)); 362 #sort($this->values); 363 return true; 364 } 365 366 /** 367 * Delete value by id 368 * 369 * @access public 370 */ 371 public function deleteValue($a_id) 372 { 373 if (!isset($this->values[$a_id])) { 374 return false; 375 } 376 unset($this->values[$a_id]); 377 array_merge($this->values); 378 $this->update(); 379 return true; 380 } 381 382 /** 383 * Save 384 * 385 * @access public 386 * 387 */ 388 public function save() 389 { 390 global $DIC; 391 392 $ilDB = $DIC['ilDB']; 393 394 $next_id = $ilDB->nextId('crs_f_definitions'); 395 $query = "INSERT INTO crs_f_definitions (field_id,obj_id,field_name,field_type,field_values,field_required,field_values_opt) " . 396 "VALUES ( " . 397 $ilDB->quote($next_id, 'integer') . ", " . 398 $this->db->quote($this->getObjId(), 'integer') . ", " . 399 $this->db->quote($this->getName(), "text") . ", " . 400 $this->db->quote($this->getType(), 'integer') . ", " . 401 $this->db->quote(serialize($this->getValues()), 'text') . ", " . 402 $ilDB->quote($this->isRequired(), 'integer') . ", " . 403 $ilDB->quote(serialize($this->getValueOptions()), 'text') . ' ' . 404 ") "; 405 $res = $ilDB->manipulate($query); 406 $this->id = $next_id; 407 408 return true; 409 } 410 411 /** 412 * Update a field 413 * 414 * @access public 415 */ 416 public function update() 417 { 418 global $DIC; 419 420 $ilDB = $DIC['ilDB']; 421 422 $query = "UPDATE crs_f_definitions " . 423 "SET field_name = " . $this->db->quote($this->getName(), 'text') . ", " . 424 "field_type = " . $this->db->quote($this->getType(), 'integer') . ", " . 425 "field_values = " . $this->db->quote(serialize($this->getValues()), 'text') . ", " . 426 "field_required = " . $ilDB->quote($this->isRequired(), 'integer') . ", " . 427 'field_values_opt = ' . $ilDB->quote(serialize($this->getValueOptions()), 'text') . ' ' . 428 "WHERE field_id = " . $this->db->quote($this->getId(), 'integer') . " " . 429 "AND obj_id = " . $this->db->quote($this->getObjId(), 'integer'); 430 $res = $ilDB->manipulate($query); 431 return true; 432 } 433 434 /** 435 * Delete a field 436 * 437 * @access public 438 * @param 439 * 440 */ 441 public function delete() 442 { 443 global $DIC; 444 445 $ilDB = $DIC['ilDB']; 446 447 include_once('Modules/Course/classes/Export/class.ilCourseUserData.php'); 448 ilCourseUserData::_deleteByField($this->getId()); 449 450 $query = "DELETE FROM crs_f_definitions " . 451 "WHERE field_id = " . $this->db->quote($this->getId(), 'integer') . " "; 452 $res = $ilDB->manipulate($query); 453 } 454 455 /** 456 * Read DB entries 457 * 458 * @access private 459 * 460 */ 461 private function read() 462 { 463 $query = "SELECT * FROM crs_f_definitions " . 464 "WHERE field_id = " . $this->db->quote($this->getId(), 'integer') . " " . 465 "AND obj_id = " . $this->db->quote($this->getObjId(), 'integer') . " "; 466 467 $res = $this->db->query($query); 468 $row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT); 469 470 $this->setName($row->field_name); 471 $this->setType($row->field_type); 472 $this->setValues(unserialize($row->field_values)); 473 $this->setValueOptions(unserialize($row->field_values_opt)); 474 $this->enableRequired($row->field_required); 475 } 476} 477