ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 
24 define("IL_CDF_SORT_ID", 'field_id');
25 define("IL_CDF_SORT_NAME", 'field_name');
26 
27 define('IL_CDF_TYPE_TEXT', 1);
28 define('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 $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 
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 
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) {
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 
126  public static function _hasFields($a_container_id)
127  {
128  return count(ilCourseDefinedFieldDefinition::_getFields($a_container_id));
129  }
130 
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 
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 
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 
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 
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);
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 
315  public function prepareSelectBox()
316  {
317  global $DIC;
318 
319  $lng = $DIC['lng'];
320 
321  $options = array();
322  $options[0] = $lng->txt('select_one');
323 
324  foreach ($this->values as $key => $value) {
325  $options[$this->getId() . '_' . $key] = $value;
326  }
327  return $options;
328  }
329 
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 
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 
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 
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 
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 
441  public function delete()
442  {
443  global $DIC;
444 
445  $ilDB = $DIC['ilDB'];
446 
447  include_once('Modules/Course/classes/Export/class.ilCourseUserData.php');
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 
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);
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 }
static _fieldsToInfoString($a_obj_id)
Fields to info string.
global $DIC
Definition: saml.php:7
static _getFieldIds($a_container_id, $a_sort=IL_CDF_SORT_ID)
Get all field ids of a container.
static _getRequiredFieldIds($a_obj_id)
Get required filed id&#39;s.
static _deleteByField($a_field_id)
Delete by field.
static _deleteByContainer($a_container_id)
Delete all fields of a container.
static _clone($a_source_id, $a_target_id)
Clone fields.
prepareSelectBox()
Prepare an array of options for ilUtil::formSelect()
$a_type
Definition: workflow.php:92
__construct($a_obj_id, $a_field_id=0)
Constructor.
foreach($_POST as $key=> $value) $res
$lng
$query
static _hasFields($a_container_id)
Check if there are any define fields.
$row
global $ilDB
prepareValues($a_values)
Prepare values from POST.
static _lookupName($a_field_id)
Lookup field name.
$key
Definition: croninfo.php:18
static _getFields($a_container_id, $a_sort=IL_CDF_SORT_NAME)
Get all fields of a container.