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