ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilConditionHandlerInterface.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 
16 {
17  var $ctrl = null;
18 
19  var $lng;
20  var $tpl;
21  var $tree;
22 
23  var $ch_obj;
30 
32 
33  function ilConditionHandlerInterface(&$gui_obj,$a_ref_id = null)
34  {
35  global $lng,$tpl,$tree,$ilCtrl;
36 
37  include_once "./classes/class.ilConditionHandler.php";
38 
39  $this->ch_obj =& new ilConditionHandler();
40 
41  $this->ctrl =& $ilCtrl;
42  $this->gui_obj =& $gui_obj;
43  $this->lng =& $lng;
44  $this->tpl =& $tpl;
45  $this->tree =& $tree;
46 
47  if($a_ref_id)
48  {
49  $this->target_obj =& ilObjectFactory::getInstanceByRefId($a_ref_id);
50  }
51  else
52  {
53  $this->target_obj =& $this->gui_obj->object;
54  }
55 
56  // this only works for ilObject derived objects (other objects
57  // should call set() methods manually
58  if (is_object($this->target_obj))
59  {
60  $this->setTargetId($this->target_obj->getId());
61  $this->setTargetRefId($this->target_obj->getRefId());
62  $this->setTargetType($this->target_obj->getType());
63  $this->setTargetTitle($this->target_obj->getTitle());
64  }
65 
66  }
67 
68  function setBackButtons($a_btn_arr)
69  {
70  $_SESSION['precon_btn'] = $a_btn_arr;
71  }
72  function getBackButtons()
73  {
74  return $_SESSION['precon_btn'] ? $_SESSION['precon_btn'] : array();
75  }
76 
77  function &executeCommand()
78  {
79  global $ilErr, $ilAccess, $lng;
80 
81  if(!$ilAccess->checkAccess('write','',$this->getTargetRefId()))
82  {
83  $ilErr->raiseError($lng->txt('permission_denied'),$ilErr->WARNING);
84  }
85 
86  $next_class = $this->ctrl->getNextClass($this);
87  $cmd = $this->ctrl->getCmd();
88 
89  switch ($next_class)
90  {
91  default:
92  if (empty($cmd))
93  {
94  $cmd = "view";
95  }
96  $this->$cmd();
97  break;
98  }
99  }
100 
101  function setAutomaticValidation($a_status)
102  {
103  $this->automatic_validation = $a_status;
104  }
106  {
108  }
109 
110 
114  function setTargetId($a_target_id)
115  {
116  $this->target_id = $a_target_id;
117  }
118 
122  function getTargetId()
123  {
124  return $this->target_id;
125  }
126 
130  function setTargetRefId($a_target_ref_id)
131  {
132  $this->target_ref_id = $a_target_ref_id;
133  }
134 
138  function getTargetRefId()
139  {
140  return $this->target_ref_id;
141  }
142 
146  function setTargetType($a_target_type)
147  {
148  $this->target_type = $a_target_type;
149  }
150 
154  function getTargetType()
155  {
156  return $this->target_type;
157  }
158 
162  function setTargetTitle($a_target_title)
163  {
164  $this->target_title = $a_target_title;
165  }
166 
170  function getTargetTitle()
171  {
172  return $this->target_title;
173  }
174 
175  function chi_init(&$chi_target_obj,$a_ref_id = null)
176  {
177  echo 'deprecated';
178 
179  include_once "./classes/class.ilConditionHandler.php";
180 
181  $this->ch_obj =& new ilConditionHandler();
182 
183  if($a_ref_id)
184  {
185  $this->target_obj =& ilObjectFactory::getInstanceByRefId($a_ref_id);
186  }
187  else
188  {
189  $this->target_obj =& $this->object;
190  }
191 
192  return true;
193  }
194 
195 
196  function listConditions()
197  {
198  global $ilObjDataCache;
199 
200  $this->lng->loadLanguageModule('crs');
201 
202  $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.condition_handler_edit.html');
203  $this->tpl->setVariable("FORMACTION",$this->ctrl->getFormAction($this));
204  $this->tpl->setVariable("COLL_TITLE_IMG",ilUtil::getImagePath('icon_'.$this->getTargetType().'.gif'));
205  $this->tpl->setVariable("COLL_TITLE_IMG_ALT",$this->lng->txt('obj_'.$this->getTargetType()));
206  $this->tpl->setVariable("TABLE_TITLE",$this->getTargetTitle().' ('.$this->lng->txt('preconditions').')');
207 
208  // Table header
209  $this->tpl->setVariable("HEAD_TITLE",$this->lng->txt('title'));
210  $this->tpl->setVariable("HEAD_CONDITION",$this->lng->txt('condition'));
211 
212  // Table footer
213  $this->tpl->setVariable("BTN_DELETE",$this->lng->txt('delete'));
214  $this->tpl->setVariable("BTN_ADD",$this->lng->txt('add_condition'));
215  $this->tpl->setVariable("DOWNRIGHT",ilUtil::getImagePath('arrow_downright.gif'));
216 
217 
218  if(!count($conditions = ilConditionHandler::_getConditionsOfTarget($this->getTargetRefId(),$this->getTargetId(), $this->getTargetType())))
219  {
220  $this->tpl->setVariable("EMPTY_TXT",$this->lng->txt('no_conditions_found'));
221  return true;
222  }
223 
224  $counter = 0;
225  foreach($conditions as $condition)
226  {
227  if($condition['trigger_type'] == 'crsg')
228  {
229  continue;
230  }
231 
232  $this->tpl->setCurrentBlock("table_content");
233 
234  $this->tpl->setVariable('TRIGGER_SRC',ilUtil::getImagePath('icon_'.$condition['trigger_type'].'_s.gif'));
235  $this->tpl->setVariable('TRIGGER_ALT',$this->lng->txt('obj_'.$condition['trigger_type']));
236  $this->tpl->setVariable("ROWCOL", ilUtil::switchColor($counter++,"tblrow1","tblrow2"));
237  $this->tpl->setVariable("CHECKBOX",ilUtil::formCheckbox(0,"conditions[]",$condition['id']));
238  $this->tpl->setVariable("TITLE",$ilObjDataCache->lookupTitle($condition['trigger_obj_id']));
239  if(strlen($desc = $ilObjDataCache->lookupDescription($condition['trigger_obj_id'])))
240  {
241  $this->tpl->setVariable("DESCRIPTION",$desc);
242  }
243  $this->tpl->setVariable("OBJ_CONDITION",$this->lng->txt('condition_'.$condition['operator']));
244 
245  // Edit link
246  $this->tpl->setVariable("EDIT",$this->lng->txt('edit'));
247 
248  $this->ctrl->setParameter($this,'condition_id',$condition['id']);
249  $this->tpl->setVariable("EDIT_LINK",$this->ctrl->getLinkTarget($this,'edit'));
250  $this->ctrl->clearParameters($this);
251  $this->tpl->parseCurrentBlock();
252  }
253  return true;
254  }
255 
256  function edit()
257  {
258  global $ilObjDataCache;
259 
260  if(!$_GET['condition_id'])
261  {
262  ilUtil::sendFailure("Missing id: condition_id");
263  $this->listConditions();
264  return false;
265  }
266  $condition = ilConditionHandler::_getCondition((int) $_GET['condition_id']);
267 
268  $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.condition_handler_edit_condition.html');
269  $this->ctrl->setParameter($this,'condition_id',(int) $_GET['condition_id']);
270 
271  $this->initFormCondition($condition['trigger_ref_id'],(int) $_GET['condition_id'],'edit');
272  $this->tpl->setVariable('CONDITION_TABLE',$this->form->getHTML());
273  }
274 
275  function updateCondition()
276  {
277  global $ilObjDataCache;
278 
279  if(!$_GET['condition_id'])
280  {
281  ilUtil::sendFailure("Missing id: condition_id");
282  $this->listConditions();
283  return false;
284  }
285 
286  // Update condition
287  include_once 'classes/class.ilConditionHandler.php';
288  $condition_handler = new ilConditionHandler();
289 
290  $condition = ilConditionHandler::_getCondition((int) $_GET['condition_id']);
291  $condition_handler->setOperator($_POST['operator']);
292  $condition_handler->setTargetRefId($this->getTargetRefId());
293  $condition_handler->setValue('');
294  switch($this->getTargetType())
295  {
296  case 'st':
297  $condition_handler->setReferenceHandlingType($_POST['ref_handling']);
298  break;
299 
300  default:
301  $condition_handler->setReferenceHandlingType(ilConditionHandler::UNIQUE_CONDITIONS);
302  break;
303  }
304  $condition_handler->updateCondition($condition['id']);
305 
306  // Update relevant sco's
307  if($condition['trigger_type'] == 'sahs')
308  {
309  include_once 'Services/Tracking/classes/class.ilLPCollections.php';
310  $lp_collection = new ilLPCollections($condition['trigger_obj_id']);
311  $lp_collection->deleteAll();
312 
313  $items = is_array($_POST['item_ids']) ? $_POST['item_ids'] : array();
314  foreach($items as $item_id)
315  {
316  $lp_collection->add($item_id);
317  }
318  }
319 
320  ilUtil::sendSuccess($this->lng->txt('settings_saved'));
321  $this->listConditions();
322  }
323 
324 
325  function delete()
326  {
327  if(!count($_POST['conditions']))
328  {
329  ilUtil::sendFailure('no_condition_selected');
330  $this->listConditions();
331  return true;
332  }
333 
334  foreach($_POST['conditions'] as $condition_id)
335  {
336  $this->ch_obj->deleteCondition($condition_id);
337  }
338  ilUtil::sendSuccess($this->lng->txt('condition_deleted'));
339  $this->listConditions();
340 
341  return true;
342  }
343 
344  function selector()
345  {
346  include_once ("classes/class.ilConditionSelector.php");
347 
348  $this->tpl->addBlockFile('ADM_CONTENT', "adm_content", "tpl.condition_selector.html");
349 
350  ilUtil::sendInfo($this->lng->txt("condition_select_object"));
351 
352  $exp = new ilConditionSelector($this->ctrl->getLinkTarget($this,'copySelector'));
353  $exp->setExpand($_GET["condition_selector_expand"] ? $_GET["condition_selector_expand"] : $this->tree->readRootId());
354  $exp->setExpandTarget($this->ctrl->getLinkTarget($this,'selector'));
355  $exp->setTargetGet("ref_id");
356  $exp->setRefId($this->getTargetRefId());
357 
358  $exp->addFilter('crs');
359  $exp->addFilter('tst');
360  $exp->addFilter('sahs');
361  $exp->addFilter('svy');
362 
363  $exp->setSelectableTypes($this->ch_obj->getTriggerTypes());
364  $exp->setControlClass($this);
365  // build html-output
366  $exp->setOutput(0);
367 
368  $this->tpl->setCurrentBlock("adm_content");
369  $this->tpl->setVariable("EXPLORER",$exp->getOutput());
370  $this->tpl->parseCurrentBlock();
371  }
372 
373  function add()
374  {
375  global $ilObjDataCache;
376 
377  if(!$_GET['source_id'])
378  {
379  ilUtil::sendFailure("Missing id: condition_id");
380  $this->selector();
381  return false;
382  }
383 
384  $this->initFormCondition((int) $_GET['source_id'],0,'add');
385  $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.condition_handler_add.html');
386  $this->tpl->setVariable('CONDITION_TABLE',$this->form->getHTML());
387  }
388 
389 
393  function assign()
394  {
395  if(!isset($_GET['source_id']))
396  {
397  echo "class.ilConditionHandlerInterface: no source_id given";
398 
399  return false;
400  }
401  if(!strlen($_POST['operator']))
402  {
403  ilUtil::sendFailure($this->lng->txt('no_operator_selected'));
404  $this->add();
405 
406  return false;
407  }
408 
409 
410  $this->ch_obj->setTargetRefId($this->getTargetRefId());
411  $this->ch_obj->setTargetObjId($this->getTargetId());
412  $this->ch_obj->setTargetType($this->getTargetType());
413 
414  switch($this->getTargetType())
415  {
416  case 'st':
417  $this->ch_obj->setReferenceHandlingType($_POST['ref_handling']);
418  break;
419 
420  default:
421  $this->ch_obj->setReferenceHandlingType(ilConditionHandler::UNIQUE_CONDITIONS);
422  break;
423  }
424  // this has to be changed, if non referenced trigger are implemted
425  if(!$trigger_obj =& ilObjectFactory::getInstanceByRefId((int) $_GET['source_id'],false))
426  {
427  echo 'ilConditionHandler: Trigger object does not exist';
428  }
429  $this->ch_obj->setTriggerRefId($trigger_obj->getRefId());
430  $this->ch_obj->setTriggerObjId($trigger_obj->getId());
431  $this->ch_obj->setTriggerType($trigger_obj->getType());
432  $this->ch_obj->setOperator($_POST['operator']);
433  $this->ch_obj->setValue('');
434 
435  // Save assigned sco's
436  if($this->ch_obj->getTriggerType() == 'sahs')
437  {
438  include_once 'Services/Tracking/classes/class.ilLPCollections.php';
439  $lp_collection = new ilLPCollections($this->ch_obj->getTriggerObjId());
440  $lp_collection->deleteAll();
441 
442  $items = is_array($_POST['item_ids']) ? $_POST['item_ids'] : array();
443  foreach($items as $item_id)
444  {
445  $lp_collection->add($item_id);
446  }
447  }
448 
449  $this->ch_obj->enableAutomaticValidation($this->getAutomaticValidation());
450  if(!$this->ch_obj->storeCondition())
451  {
452  ilUtil::sendFailure($this->ch_obj->getErrorMessage());
453  }
454  else
455  {
456  ilUtil::sendSuccess($this->lng->txt('added_new_condition'));
457  }
458 
459  $this->listConditions();
460 
461  return true;
462  }
463 
464  function chi_update()
465  {
466  #if(in_array('',$_POST['operator']))
467  #{
468  # ilUtil::sendInfo($this->lng->txt('select_one_operator'));
469 
470  # return false;
471  #}
472  foreach($this->__getConditionsOfTarget() as $condition)
473  {
474  $this->ch_obj->setOperator($_POST['operator'][$condition["id"]]);
475  $this->ch_obj->setValue($_POST['value'][$condition["id"]]);
476  $this->ch_obj->updateCondition($condition['id']);
477 
478  }
479  ilUtil::sendSuccess($this->lng->txt('conditions_updated'));
480 
481  $this->ctrl->returnToParent($this);
482 
483  return true;
484  }
486  {
487  include_once './classes/class.ilConditionHandler.php';
488 
489  foreach(ilConditionHandler::_getConditionsOfTarget($this->getTargetRefId(),$this->getTargetId(), $this->getTargetType()) as $condition)
490  {
491  if($condition['operator'] == 'not_member')
492  {
493  continue;
494  }
495  else
496  {
497  $cond[] = $condition;
498  }
499  }
500  return $cond ? $cond : array();
501  }
502 
503  function __showButtons()
504  {
505  if(!$this->getBackButtons())
506  {
507  return false;
508  }
509 
510  $this->tpl->addBlockfile("BUTTONS", "buttons", "tpl.buttons.html");
511  foreach($this->getBackButtons() as $name => $link)
512  {
513  $this->tpl->setCurrentBlock("btn_cell");
514  $this->tpl->setVariable("BTN_LINK",$link);
515  $this->tpl->setVariable("BTN_TXT",$this->lng->txt($name));
516  $this->tpl->parseCurrentBlock();
517  }
518  }
519 
527  private function initFormCondition($a_source_id,$a_condition_id = 0,$a_mode = 'add')
528  {
529  $trigger_obj_id = ilObject::_lookupObjId($a_source_id);
530  $trigger_type = ilObject::_lookupType($trigger_obj_id);
531 
532  $condition = ilConditionHandler::_getCondition($a_condition_id);
533 
534  if(is_object($this->form))
535  {
536  return true;
537  }
538  include_once('Services/Form/classes/class.ilPropertyFormGUI.php');
539  $this->form = new ilPropertyFormGUI();
540  $this->ctrl->setParameter($this,'source_id',$a_source_id);
541  $this->form->setFormAction($this->ctrl->getFormAction($this));
542 
543  $sel = new ilSelectInputGUI($this->lng->txt('condition'),'operator');
544  include_once "./classes/class.ilConditionHandler.php";
545  $ch_obj = new ilConditionHandler();
546  $operators[0] = $this->lng->txt('select_one');
547  foreach($ch_obj->getOperatorsByTargetType($trigger_type) as $operator)
548  {
549  $operators[$operator] = $this->lng->txt('condition_'.$operator);
550  }
551  $sel->setValue(isset($condition['operator']) ? $condition['operator'] : 0);
552  $sel->setOptions($operators);
553  $sel->setRequired(true);
554  $this->form->addItem($sel);
555 
557  {
558  $rad_opt = new ilRadioGroupInputGUI($this->lng->txt('cond_ref_handling'),'ref_handling');
559  $rad_opt->setValue(isset($condition['ref_handling']) ? $condition['ref_handling'] : ilConditionHandler::SHARED_CONDITIONS);
560 
561  $opt2 = new ilRadioOption($this->lng->txt('cond_ref_shared'),ilConditionHandler::SHARED_CONDITIONS);
562  $rad_opt->addOption($opt2);
563 
564  $opt1 = new ilRadioOption($this->lng->txt('cond_ref_unique'),ilConditionHandler::UNIQUE_CONDITIONS);
565  $rad_opt->addOption($opt1);
566 
567  $this->form->addItem($rad_opt);
568  }
569 
570  // Additional settings for SCO's
571  if($trigger_type == 'sahs')
572  {
573  $this->lng->loadLanguageModule('trac');
574  include_once 'Services/Tracking/classes/class.ilLPCollections.php';
575  $lp_collections = new ilLPCollections($trigger_obj_id);
576 
577  $cus = new ilCustomInputGUI($this->lng->txt('trac_sahs_relevant_items'),'item_ids[]');
578  $cus->setRequired(true);
579 
580  $tpl = new ilTemplate('tpl.condition_handler_sco_row.html',true,true);
581  $counter = 0;
582 
583  foreach(ilLPCollections::_getPossibleSAHSItems($trigger_obj_id) as $item_id => $sahs_item)
584  {
585  $tpl->setCurrentBlock("sco_row");
586  $tpl->setVariable('SCO_ID',$item_id);
587  $tpl->setVariable('SCO_TITLE',$sahs_item['title']);
588  $tpl->setVariable('CHECKED',$lp_collections->isAssigned($item_id) ? 'checked="checked"' : '');
589  $tpl->parseCurrentBlock();
590  $counter++;
591  }
592  $tpl->setVariable('INFO_SEL',$this->lng->txt('trac_lp_determination_info_sco'));
593  $cus->setHTML($tpl->get());
594  $this->form->addItem($cus);
595  }
596  switch($a_mode)
597  {
598  case 'edit':
599  $this->form->setTitleIcon(ilUtil::getImagePath('icon_'.$this->getTargetType().'.gif'));
600  $this->form->setTitle($this->lng->txt('precondition').' ('.
601  $this->getTargetTitle().' -> '.
603  $this->form->addCommandButton('updateCondition',$this->lng->txt('save'));
604  $this->form->addCommandButton('listConditions',$this->lng->txt('cancel'));
605  break;
606 
607 
608  case 'add':
609  $this->form->setTitleIcon(ilUtil::getImagePath('icon_'.$this->getTargetType().'.gif'));
610  $this->form->setTitle($this->lng->txt('add_condition').' ('.
611  $this->getTargetTitle().' -> '.
613  $this->form->addCommandButton('assign',$this->lng->txt('save'));
614  $this->form->addCommandButton('selector',$this->lng->txt('back'));
615  break;
616  }
617  return true;
618  }
619 }
620 ?>