ILIAS  eassessment Revision 61809
 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 $required;
48 
57  public function __construct($a_obj_id,$a_field_id = 0)
58  {
59  global $ilDB;
60 
61  $this->db = $ilDB;
62  $this->obj_id = $a_obj_id;
63  $this->id = $a_field_id;
64 
65  if($this->id)
66  {
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  {
84  $cdf = new ilCourseDefinedFieldDefinition($a_target_id);
85  $cdf->setName($field_obj->getName());
86  $cdf->setType($field_obj->getType());
87  $cdf->setValues($field_obj->getValues());
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)
108  {
110  }
111 
112  $query = "DELETE FROM crs_f_definitions ".
113  "WHERE obj_id = ".$ilDB->quote($a_container_id,'integer')." ";
114  $res = $ilDB->manipulate($query);
115  }
116 
123  public static function _hasFields($a_container_id)
124  {
125  return count(ilCourseDefinedFieldDefinition::_getFields($a_container_id));
126  }
127 
136  public static function _getFields($a_container_id,$a_sort = IL_CDF_SORT_NAME)
137  {
138  foreach(ilCourseDefinedFieldDefinition::_getFieldIds($a_container_id,$a_sort) as $field_id)
139  {
140  $fields[] = new ilCourseDefinedFieldDefinition($a_container_id,$field_id);
141  }
142  return $fields ? $fields : array();
143  }
144 
153  public static function _getRequiredFieldIds($a_obj_id)
154  {
155  global $ilDB;
156 
157  $query = "SELECT * FROM crs_f_definitions ".
158  "WHERE obj_id = ".$ilDB->quote($a_obj_id,'integer')." ".
159  "AND field_required = 1";
160  $res = $ilDB->query($query);
161  while($row = $ilDB->fetchObject($res))
162  {
163  $req_fields[] = $row->field_id;
164  }
165  return $req_fields ? $req_fields : array();
166  }
167 
176  public static function _fieldsToInfoString($a_obj_id)
177  {
178  global $ilDB;
179 
180 
181  $query = "SELECT field_name FROM crs_f_definitions ".
182  "WHERE obj_id = ".$ilDB->quote($a_obj_id,'integer');
183 
184  $res = $ilDB->query($query);
185  $fields = array();
186  while($row = $ilDB->fetchObject($res))
187  {
188  $fields[] = $row->field_name;
189  }
190  return implode('<br />',$fields);
191  }
192 
201  public static function _getFieldIds($a_container_id,$a_sort = IL_CDF_SORT_ID)
202  {
203  global $ilDB;
204 
205  $query = "SELECT field_id FROM crs_f_definitions ".
206  "WHERE obj_id = ".$ilDB->quote($a_container_id,'integer')." ".
207  "ORDER BY ".$a_sort;
208  $res = $ilDB->query($query);
209  while($row = $ilDB->fetchObject($res))
210  {
211  $field_ids[] = $row->field_id;
212  }
213  return $field_ids ? $field_ids : array();
214  }
215 
224  public static function _lookupName($a_field_id)
225  {
226  global $ilDB;
227 
228  $query = "SELECT * FROM crs_f_definitions ".
229  "WHERE field_id = ".$ilDB->quote($a_field_id,'integer');
230 
231  $res = $ilDB->query($query);
232  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
233 
234  return $row->field_name ? $row->field_name : '';
235  }
236 
237  public function getObjId()
238  {
239  return $this->obj_id;
240  }
241  public function getId()
242  {
243  return $this->id;
244  }
245  public function getType()
246  {
247  return $this->type;
248  }
249  public function setType($a_type)
250  {
251  $this->type = $a_type;
252  }
253  public function getName()
254  {
255  return $this->name;
256  }
257  public function setName($a_name)
258  {
259  $this->name = $a_name;
260  }
261  public function getValues()
262  {
263  return $this->values ? $this->values : array();
264  }
265  public function setValues($a_values)
266  {
267  $this->values = $a_values;
268  }
269  public function getValueById($a_id)
270  {
271  if(is_array($this->values) and array_key_exists($a_id,$this->values))
272  {
273  return $this->values[$a_id];
274  }
275  return '';
276  }
277  public function getIdByValue($a_value)
278  {
279  return ($pos = array_search($a_value,$this->values) === false) ? -1 : $pos;
280  }
281 
282  public function isRequired()
283  {
284  return (bool) $this->required;
285  }
286  public function enableRequired($a_status)
287  {
288  $this->required = $a_status;
289  }
290 
298  public function prepareSelectBox()
299  {
300  global $lng;
301 
302  $options = array();
303  $options[0] = $lng->txt('select_one');
304 
305  foreach($this->values as $value)
306  {
307  $options[$value] = $value;
308  }
309  return $options;
310  }
311 
318  public function prepareValues($a_values)
319  {
320  $tmp_values = array();
321 
322  if(!is_array($a_values))
323  {
324  return false;
325  }
326  foreach($a_values as $value)
327  {
328  $value = trim(ilUtil::stripSlashes($value));
329  if(strlen($value))
330  {
331  $tmp_values[] = $value;
332  }
333  }
334  sort($tmp_values);
335  return $tmp_values ? $tmp_values : array();
336  }
337 
343  public function appendValues($a_values)
344  {
345  if(!is_array($a_values))
346  {
347  return false;
348  }
349  $this->values = array_unique(array_merge($this->values,$a_values));
350  sort($this->values);
351  return true;
352  }
353 
359  public function deleteValue($a_id)
360  {
361  if(!isset($this->values[$a_id]))
362  {
363  return false;
364  }
365  unset($this->values[$a_id]);
366  array_merge($this->values);
367  $this->update();
368  return true;
369  }
370 
377  public function save()
378  {
379  global $ilDB;
380 
381  $next_id = $ilDB->nextId('crs_f_definitions');
382  $query = "INSERT INTO crs_f_definitions (field_id,obj_id,field_name,field_type,field_values,field_required) ".
383  "VALUES ( ".
384  $ilDB->quote($next_id,'integer').", ".
385  $this->db->quote($this->getObjId(),'integer').", ".
386  $this->db->quote($this->getName(),"text").", ".
387  $this->db->quote($this->getType(),'integer').", ".
388  $this->db->quote(serialize($this->getValues()),'text').", ".
389  $ilDB->quote($this->isRequired(),'integer')." ".
390  ") ";
391  $res = $ilDB->manipulate($query);
392  $this->id = $next_id;
393 
394  return true;
395  }
396 
402  public function update()
403  {
404  global $ilDB;
405 
406  $query = "UPDATE crs_f_definitions ".
407  "SET field_name = ".$this->db->quote($this->getName(),'text').", ".
408  "field_type = ".$this->db->quote($this->getType(),'integer').", ".
409  "field_values = ".$this->db->quote(serialize($this->getValues()),'text').", ".
410  "field_required = ".$ilDB->quote($this->isRequired(),'integer')." ".
411  "WHERE field_id = ".$this->db->quote($this->getId(),'integer')." ".
412  "AND obj_id = ".$this->db->quote($this->getObjId(),'integer');
413  $res = $ilDB->manipulate($query);
414  return true;
415  }
416 
424  public function delete()
425  {
426  global $ilDB;
427 
428  include_once('Modules/Course/classes/Export/class.ilCourseUserData.php');
430 
431  $query = "DELETE FROM crs_f_definitions ".
432  "WHERE field_id = ".$this->db->quote($this->getId(),'integer')." ";
433  $res = $ilDB->manipulate($query);
434  }
435 
442  private function read()
443  {
444  $query = "SELECT * FROM crs_f_definitions ".
445  "WHERE field_id = ".$this->db->quote($this->getId(),'integer')." ".
446  "AND obj_id = ".$this->db->quote($this->getObjId(),'integer')." ";
447 
448  $res = $this->db->query($query);
449  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
450 
451  $this->setName($row->field_name);
452  $this->setType($row->field_type);
453  $this->setValues(unserialize($row->field_values));
454  $this->enableRequired($row->field_required);
455  }
456 }
457 ?>