ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 $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  {
68  $this->read();
69  }
70  }
71 
81  public static function _clone($a_source_id,$a_target_id)
82  {
83  foreach(ilCourseDefinedFieldDefinition::_getFields($a_source_id) as $field_obj)
84  {
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 $ilDB;
106 
107  // Delete user entries
108  include_once('Modules/Course/classes/Export/class.ilCourseUserData.php');
109  foreach(ilCourseDefinedFieldDefinition::_getFieldIds($a_container_id) as $field_id)
110  {
112  }
113 
114  $query = "DELETE FROM crs_f_definitions ".
115  "WHERE obj_id = ".$ilDB->quote($a_container_id,'integer')." ";
116  $res = $ilDB->manipulate($query);
117  }
118 
125  public static function _hasFields($a_container_id)
126  {
127  return count(ilCourseDefinedFieldDefinition::_getFields($a_container_id));
128  }
129 
138  public static function _getFields($a_container_id,$a_sort = IL_CDF_SORT_NAME)
139  {
140  foreach(ilCourseDefinedFieldDefinition::_getFieldIds($a_container_id,IL_CDF_SORT_ID) as $field_id)
141  {
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 $ilDB;
158 
159  $query = "SELECT * FROM crs_f_definitions ".
160  "WHERE obj_id = ".$ilDB->quote($a_obj_id,'integer')." ".
161  "AND field_required = 1";
162  $res = $ilDB->query($query);
163  while($row = $ilDB->fetchObject($res))
164  {
165  $req_fields[] = $row->field_id;
166  }
167  return $req_fields ? $req_fields : array();
168  }
169 
178  public static function _fieldsToInfoString($a_obj_id)
179  {
180  global $ilDB;
181 
182 
183  $query = "SELECT field_name FROM crs_f_definitions ".
184  "WHERE obj_id = ".$ilDB->quote($a_obj_id,'integer');
185 
186  $res = $ilDB->query($query);
187  $fields = array();
188  while($row = $ilDB->fetchObject($res))
189  {
190  $fields[] = $row->field_name;
191  }
192  return implode('<br />',$fields);
193  }
194 
203  public static function _getFieldIds($a_container_id,$a_sort = IL_CDF_SORT_ID)
204  {
205  global $ilDB;
206 
207  $query = "SELECT field_id FROM crs_f_definitions ".
208  "WHERE obj_id = ".$ilDB->quote($a_container_id,'integer')." ".
209  "ORDER BY ".IL_CDF_SORT_ID;
210  $res = $ilDB->query($query);
211  while($row = $ilDB->fetchObject($res))
212  {
213  $field_ids[] = $row->field_id;
214  }
215  return $field_ids ? $field_ids : array();
216  }
217 
226  public static function _lookupName($a_field_id)
227  {
228  global $ilDB;
229 
230  $query = "SELECT * FROM crs_f_definitions ".
231  "WHERE field_id = ".$ilDB->quote($a_field_id,'integer');
232 
233  $res = $ilDB->query($query);
234  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
235 
236  return $row->field_name ? $row->field_name : '';
237  }
238 
239  public function getObjId()
240  {
241  return $this->obj_id;
242  }
243  public function getId()
244  {
245  return $this->id;
246  }
247  public function getType()
248  {
249  return $this->type;
250  }
251  public function setType($a_type)
252  {
253  $this->type = $a_type;
254  }
255  public function getName()
256  {
257  return $this->name;
258  }
259  public function setName($a_name)
260  {
261  $this->name = $a_name;
262  }
263  public function getValues()
264  {
265  return $this->values ? $this->values : array();
266  }
267  public function setValues($a_values)
268  {
269  $this->values = $a_values;
270  }
271  public function getValueById($a_id)
272  {
273  if(is_array($this->values) and array_key_exists($a_id,$this->values))
274  {
275  return $this->values[$a_id];
276  }
277  return '';
278  }
279  public function getIdByValue($a_value)
280  {
281  return (($pos = array_search($a_value,$this->values)) === false) ? -1 : $pos;
282  }
283 
284  public function isRequired()
285  {
286  return (bool) $this->required;
287  }
288  public function enableRequired($a_status)
289  {
290  $this->required = $a_status;
291  }
292 
293  public function setValueOptions($a_options)
294  {
295  $this->value_options = $a_options;
296  }
297 
298  public function getValueOptions()
299  {
300  return (array) $this->value_options;
301  }
302 
303 
311  public function prepareSelectBox()
312  {
313  global $lng;
314 
315  $options = array();
316  $options[0] = $lng->txt('select_one');
317 
318  foreach($this->values as $key => $value)
319  {
320  $options[$this->getId().'_'.$key] = $value;
321  }
322  return $options;
323  }
324 
331  public function prepareValues($a_values)
332  {
333  $tmp_values = array();
334 
335  if(!is_array($a_values))
336  {
337  return false;
338  }
339  foreach($a_values as $idx => $value)
340  {
341  if(strlen($value))
342  {
343  $tmp_values[$idx] = $value;
344  }
345  }
346  return $tmp_values ? $tmp_values : array();
347  }
348 
354  public function appendValues($a_values)
355  {
356  if(!is_array($a_values))
357  {
358  return false;
359  }
360  $this->values = array_unique(array_merge($this->values,$a_values));
361  #sort($this->values);
362  return true;
363  }
364 
370  public function deleteValue($a_id)
371  {
372  if(!isset($this->values[$a_id]))
373  {
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 $ilDB;
391 
392  $next_id = $ilDB->nextId('crs_f_definitions');
393  $query = "INSERT INTO crs_f_definitions (field_id,obj_id,field_name,field_type,field_values,field_required,field_values_opt) ".
394  "VALUES ( ".
395  $ilDB->quote($next_id,'integer').", ".
396  $this->db->quote($this->getObjId(),'integer').", ".
397  $this->db->quote($this->getName(),"text").", ".
398  $this->db->quote($this->getType(),'integer').", ".
399  $this->db->quote(serialize($this->getValues()),'text').", ".
400  $ilDB->quote($this->isRequired(),'integer').", ".
401  $ilDB->quote(serialize($this->getValueOptions()),'text').' '.
402  ") ";
403  $res = $ilDB->manipulate($query);
404  $this->id = $next_id;
405 
406  return true;
407  }
408 
414  public function update()
415  {
416  global $ilDB;
417 
418  $query = "UPDATE crs_f_definitions ".
419  "SET field_name = ".$this->db->quote($this->getName(),'text').", ".
420  "field_type = ".$this->db->quote($this->getType(),'integer').", ".
421  "field_values = ".$this->db->quote(serialize($this->getValues()),'text').", ".
422  "field_required = ".$ilDB->quote($this->isRequired(),'integer').", ".
423  'field_values_opt = '.$ilDB->quote(serialize($this->getValueOptions()),'text').' '.
424  "WHERE field_id = ".$this->db->quote($this->getId(),'integer')." ".
425  "AND obj_id = ".$this->db->quote($this->getObjId(),'integer');
426  $res = $ilDB->manipulate($query);
427  return true;
428  }
429 
437  public function delete()
438  {
439  global $ilDB;
440 
441  include_once('Modules/Course/classes/Export/class.ilCourseUserData.php');
443 
444  $query = "DELETE FROM crs_f_definitions ".
445  "WHERE field_id = ".$this->db->quote($this->getId(),'integer')." ";
446  $res = $ilDB->manipulate($query);
447  }
448 
455  private function read()
456  {
457  $query = "SELECT * FROM crs_f_definitions ".
458  "WHERE field_id = ".$this->db->quote($this->getId(),'integer')." ".
459  "AND obj_id = ".$this->db->quote($this->getObjId(),'integer')." ";
460 
461  $res = $this->db->query($query);
462  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
463 
464  $this->setName($row->field_name);
465  $this->setType($row->field_type);
466  $this->setValues(unserialize($row->field_values));
467  $this->setValueOptions(unserialize($row->field_values_opt));
468  $this->enableRequired($row->field_required);
469  }
470 }
471 ?>