ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilCourseDefinedFieldDefinition.php
Go to the documentation of this file.
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
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
58 public function __construct($a_obj_id, $a_field_id = 0)
59 {
60 global $ilDB;
61
62 $this->db = $ilDB;
63 $this->obj_id = $a_obj_id;
64 $this->id = $a_field_id;
65
66 if ($this->id) {
67 $this->read();
68 }
69 }
70
80 public static function _clone($a_source_id, $a_target_id)
81 {
82 foreach (ilCourseDefinedFieldDefinition::_getFields($a_source_id) as $field_obj) {
83 $cdf = new ilCourseDefinedFieldDefinition($a_target_id);
84 $cdf->setName($field_obj->getName());
85 $cdf->setType($field_obj->getType());
86 $cdf->setValues($field_obj->getValues());
87 $cdf->setValueOptions($field_obj->getValueOptions());
88 $cdf->enableRequired($field_obj->isRequired());
89 $cdf->save();
90 }
91 }
92
101 public static function _deleteByContainer($a_container_id)
102 {
103 global $ilDB;
104
105 // Delete user entries
106 include_once('Modules/Course/classes/Export/class.ilCourseUserData.php');
107 foreach (ilCourseDefinedFieldDefinition::_getFieldIds($a_container_id) as $field_id) {
109 }
110
111 $query = "DELETE FROM crs_f_definitions " .
112 "WHERE obj_id = " . $ilDB->quote($a_container_id, 'integer') . " ";
113 $res = $ilDB->manipulate($query);
114 }
115
122 public static function _hasFields($a_container_id)
123 {
124 return count(ilCourseDefinedFieldDefinition::_getFields($a_container_id));
125 }
126
135 public static function _getFields($a_container_id, $a_sort = IL_CDF_SORT_NAME)
136 {
137 foreach (ilCourseDefinedFieldDefinition::_getFieldIds($a_container_id, IL_CDF_SORT_ID) as $field_id) {
138 $fields[] = new ilCourseDefinedFieldDefinition($a_container_id, $field_id);
139 }
140 return $fields ? $fields : array();
141 }
142
151 public static function _getRequiredFieldIds($a_obj_id)
152 {
153 global $ilDB;
154
155 $query = "SELECT * FROM crs_f_definitions " .
156 "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " " .
157 "AND field_required = 1";
158 $res = $ilDB->query($query);
159 while ($row = $ilDB->fetchObject($res)) {
160 $req_fields[] = $row->field_id;
161 }
162 return $req_fields ? $req_fields : array();
163 }
164
173 public static function _fieldsToInfoString($a_obj_id)
174 {
175 global $ilDB;
176
177
178 $query = "SELECT field_name FROM crs_f_definitions " .
179 "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer');
180
181 $res = $ilDB->query($query);
182 $fields = array();
183 while ($row = $ilDB->fetchObject($res)) {
184 $fields[] = $row->field_name;
185 }
186 return implode('<br />', $fields);
187 }
188
197 public static function _getFieldIds($a_container_id, $a_sort = IL_CDF_SORT_ID)
198 {
199 global $ilDB;
200
201 $query = "SELECT field_id FROM crs_f_definitions " .
202 "WHERE obj_id = " . $ilDB->quote($a_container_id, 'integer') . " " .
203 "ORDER BY " . IL_CDF_SORT_ID;
204 $res = $ilDB->query($query);
205 while ($row = $ilDB->fetchObject($res)) {
206 $field_ids[] = $row->field_id;
207 }
208 return $field_ids ? $field_ids : array();
209 }
210
219 public static function _lookupName($a_field_id)
220 {
221 global $ilDB;
222
223 $query = "SELECT * FROM crs_f_definitions " .
224 "WHERE field_id = " . $ilDB->quote($a_field_id, 'integer');
225
226 $res = $ilDB->query($query);
228
229 return $row->field_name ? $row->field_name : '';
230 }
231
232 public function getObjId()
233 {
234 return $this->obj_id;
235 }
236 public function getId()
237 {
238 return $this->id;
239 }
240 public function getType()
241 {
242 return $this->type;
243 }
244 public function setType($a_type)
245 {
246 $this->type = $a_type;
247 }
248 public function getName()
249 {
250 return $this->name;
251 }
252 public function setName($a_name)
253 {
254 $this->name = $a_name;
255 }
256 public function getValues()
257 {
258 return $this->values ? $this->values : array();
259 }
260 public function setValues($a_values)
261 {
262 $this->values = $a_values;
263 }
264 public function getValueById($a_id)
265 {
266 if (is_array($this->values) and array_key_exists($a_id, $this->values)) {
267 return $this->values[$a_id];
268 }
269 return '';
270 }
271 public function getIdByValue($a_value)
272 {
273 return (($pos = array_search($a_value, $this->values)) === false) ? -1 : $pos;
274 }
275
276 public function isRequired()
277 {
278 return (bool) $this->required;
279 }
280 public function enableRequired($a_status)
281 {
282 $this->required = $a_status;
283 }
284
285 public function setValueOptions($a_options)
286 {
287 $this->value_options = $a_options;
288 }
289
290 public function getValueOptions()
291 {
292 return (array) $this->value_options;
293 }
294
295
303 public function prepareSelectBox()
304 {
305 global $lng;
306
307 $options = array();
308 $options[0] = $lng->txt('select_one');
309
310 foreach ($this->values as $key => $value) {
311 $options[$this->getId() . '_' . $key] = $value;
312 }
313 return $options;
314 }
315
322 public function prepareValues($a_values)
323 {
324 $tmp_values = array();
325
326 if (!is_array($a_values)) {
327 return false;
328 }
329 foreach ($a_values as $idx => $value) {
330 if (strlen($value)) {
331 $tmp_values[$idx] = $value;
332 }
333 }
334 return $tmp_values ? $tmp_values : array();
335 }
336
342 public function appendValues($a_values)
343 {
344 if (!is_array($a_values)) {
345 return false;
346 }
347 $this->values = array_unique(array_merge($this->values, $a_values));
348 #sort($this->values);
349 return true;
350 }
351
357 public function deleteValue($a_id)
358 {
359 if (!isset($this->values[$a_id])) {
360 return false;
361 }
362 unset($this->values[$a_id]);
363 array_merge($this->values);
364 $this->update();
365 return true;
366 }
367
374 public function save()
375 {
376 global $ilDB;
377
378 $next_id = $ilDB->nextId('crs_f_definitions');
379 $query = "INSERT INTO crs_f_definitions (field_id,obj_id,field_name,field_type,field_values,field_required,field_values_opt) " .
380 "VALUES ( " .
381 $ilDB->quote($next_id, 'integer') . ", " .
382 $this->db->quote($this->getObjId(), 'integer') . ", " .
383 $this->db->quote($this->getName(), "text") . ", " .
384 $this->db->quote($this->getType(), 'integer') . ", " .
385 $this->db->quote(serialize($this->getValues()), 'text') . ", " .
386 $ilDB->quote($this->isRequired(), 'integer') . ", " .
387 $ilDB->quote(serialize($this->getValueOptions()), 'text') . ' ' .
388 ") ";
389 $res = $ilDB->manipulate($query);
390 $this->id = $next_id;
391
392 return true;
393 }
394
400 public function update()
401 {
402 global $ilDB;
403
404 $query = "UPDATE crs_f_definitions " .
405 "SET field_name = " . $this->db->quote($this->getName(), 'text') . ", " .
406 "field_type = " . $this->db->quote($this->getType(), 'integer') . ", " .
407 "field_values = " . $this->db->quote(serialize($this->getValues()), 'text') . ", " .
408 "field_required = " . $ilDB->quote($this->isRequired(), 'integer') . ", " .
409 'field_values_opt = ' . $ilDB->quote(serialize($this->getValueOptions()), 'text') . ' ' .
410 "WHERE field_id = " . $this->db->quote($this->getId(), 'integer') . " " .
411 "AND obj_id = " . $this->db->quote($this->getObjId(), 'integer');
412 $res = $ilDB->manipulate($query);
413 return true;
414 }
415
423 public function delete()
424 {
425 global $ilDB;
426
427 include_once('Modules/Course/classes/Export/class.ilCourseUserData.php');
429
430 $query = "DELETE FROM crs_f_definitions " .
431 "WHERE field_id = " . $this->db->quote($this->getId(), 'integer') . " ";
432 $res = $ilDB->manipulate($query);
433 }
434
441 private function read()
442 {
443 $query = "SELECT * FROM crs_f_definitions " .
444 "WHERE field_id = " . $this->db->quote($this->getId(), 'integer') . " " .
445 "AND obj_id = " . $this->db->quote($this->getObjId(), 'integer') . " ";
446
447 $res = $this->db->query($query);
449
450 $this->setName($row->field_name);
451 $this->setType($row->field_type);
452 $this->setValues(unserialize($row->field_values));
453 $this->setValueOptions(unserialize($row->field_values_opt));
454 $this->enableRequired($row->field_required);
455 }
456}
if(!isset( $_REQUEST[ 'ReturnTo'])) if(!isset($_REQUEST['AuthId'])) $options
Definition: as_login.php:20
An exception for terminatinating execution or to throw for unit testing.
static _getFieldIds($a_container_id, $a_sort=IL_CDF_SORT_ID)
Get all field ids of a container.
prepareValues($a_values)
Prepare values from POST.
static _deleteByContainer($a_container_id)
Delete all fields of a container.
static _getFields($a_container_id, $a_sort=IL_CDF_SORT_NAME)
Get all fields of a container.
static _hasFields($a_container_id)
Check if there are any define fields.
static _getRequiredFieldIds($a_obj_id)
Get required filed id's.
static _clone($a_source_id, $a_target_id)
Clone fields.
prepareSelectBox()
Prepare an array of options for ilUtil::formSelect()
__construct($a_obj_id, $a_field_id=0)
Constructor.
static _fieldsToInfoString($a_obj_id)
Fields to info string.
static _lookupName($a_field_id)
Lookup field name.
static _deleteByField($a_field_id)
Delete by field.
$key
Definition: croninfo.php:18
global $lng
Definition: privfeed.php:17
$query
foreach($_POST as $key=> $value) $res
global $ilDB
$a_type
Definition: workflow.php:92