ILIAS  eassessment Revision 61809
 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  if($condition['trigger_type'] == 'crsg')
221  {
222  continue;
223  }
224 
225  $this->tpl->setCurrentBlock("table_content");
226 
227  $this->tpl->setVariable('TRIGGER_SRC',ilUtil::getImagePath('icon_'.$condition['trigger_type'].'_s.gif'));
228  $this->tpl->setVariable('TRIGGER_ALT',$this->lng->txt('obj_'.$condition['trigger_type']));
229  $this->tpl->setVariable("ROWCOL", ilUtil::switchColor($counter++,"tblrow1","tblrow2"));
230  $this->tpl->setVariable("CHECKBOX",ilUtil::formCheckbox(0,"conditions[]",$condition['id']));
231  $this->tpl->setVariable("TITLE",$ilObjDataCache->lookupTitle($condition['trigger_obj_id']));
232  if(strlen($desc = $ilObjDataCache->lookupDescription($condition['trigger_obj_id'])))
233  {
234  $this->tpl->setVariable("DESCRIPTION",$desc);
235  }
236  $this->tpl->setVariable("OBJ_CONDITION",$this->lng->txt('condition_'.$condition['operator']));
237 
238  // Edit link
239  $this->tpl->setVariable("EDIT",$this->lng->txt('edit'));
240 
241  $this->ctrl->setParameter($this,'condition_id',$condition['id']);
242  $this->tpl->setVariable("EDIT_LINK",$this->ctrl->getLinkTarget($this,'edit'));
243  $this->ctrl->clearParameters($this);
244  $this->tpl->parseCurrentBlock();
245  }
246  return true;
247  }
248 
249  function edit()
250  {
251  global $ilObjDataCache;
252 
253  if(!$_GET['condition_id'])
254  {
255  ilUtil::sendFailure("Missing id: condition_id");
256  $this->listConditions();
257  return false;
258  }
259  $condition = ilConditionHandler::_getCondition((int) $_GET['condition_id']);
260 
261  $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.condition_handler_edit_condition.html');
262  $this->ctrl->setParameter($this,'condition_id',(int) $_GET['condition_id']);
263 
264  $this->initFormCondition($condition['trigger_ref_id'],(int) $_GET['condition_id'],'edit');
265  $this->tpl->setVariable('CONDITION_TABLE',$this->form->getHTML());
266  }
267 
268  function updateCondition()
269  {
270  global $ilObjDataCache;
271 
272  if(!$_GET['condition_id'])
273  {
274  ilUtil::sendFailure("Missing id: condition_id");
275  $this->listConditions();
276  return false;
277  }
278 
279  // Update condition
280  include_once 'classes/class.ilConditionHandler.php';
281  $condition_handler = new ilConditionHandler();
282 
283  $condition = ilConditionHandler::_getCondition((int) $_GET['condition_id']);
284  $condition_handler->setOperator($_POST['operator']);
285  $condition_handler->setTargetRefId($this->getTargetRefId());
286  $condition_handler->setValue('');
287  switch($this->getTargetType())
288  {
289  case 'st':
290  $condition_handler->setReferenceHandlingType($_POST['ref_handling']);
291  break;
292 
293  default:
294  $condition_handler->setReferenceHandlingType(ilConditionHandler::UNIQUE_CONDITIONS);
295  break;
296  }
297  $condition_handler->updateCondition($condition['id']);
298 
299  // Update relevant sco's
300  if($condition['trigger_type'] == 'sahs')
301  {
302  include_once 'Services/Tracking/classes/class.ilLPCollections.php';
303  $lp_collection = new ilLPCollections($condition['trigger_obj_id']);
304  $lp_collection->deleteAll();
305 
306  $items = is_array($_POST['item_ids']) ? $_POST['item_ids'] : array();
307  foreach($items as $item_id)
308  {
309  $lp_collection->add($item_id);
310  }
311  }
312 
313  ilUtil::sendSuccess($this->lng->txt('settings_saved'));
314  $this->listConditions();
315  }
316 
317 
318  function delete()
319  {
320  if(!count($_POST['conditions']))
321  {
322  ilUtil::sendFailure('no_condition_selected');
323  $this->listConditions();
324  return true;
325  }
326 
327  foreach($_POST['conditions'] as $condition_id)
328  {
329  $this->ch_obj->deleteCondition($condition_id);
330  }
331  ilUtil::sendSuccess($this->lng->txt('condition_deleted'));
332  $this->listConditions();
333 
334  return true;
335  }
336 
337  function selector()
338  {
339  include_once ("classes/class.ilConditionSelector.php");
340 
341  $this->tpl->addBlockFile('ADM_CONTENT', "adm_content", "tpl.condition_selector.html");
342 
343  ilUtil::sendInfo($this->lng->txt("condition_select_object"));
344 
345  $exp = new ilConditionSelector($this->ctrl->getLinkTarget($this,'copySelector'));
346  $exp->setExpand($_GET["condition_selector_expand"] ? $_GET["condition_selector_expand"] : $this->tree->readRootId());
347  $exp->setExpandTarget($this->ctrl->getLinkTarget($this,'selector'));
348  $exp->setTargetGet("ref_id");
349  $exp->setRefId($this->getTargetRefId());
350 
351  $exp->addFilter('crs');
352  $exp->addFilter('tst');
353  $exp->addFilter('sahs');
354  $exp->addFilter('svy');
355 
356  $exp->setSelectableTypes($this->ch_obj->getTriggerTypes());
357  $exp->setControlClass($this);
358  // build html-output
359  $exp->setOutput(0);
360 
361  $this->tpl->setCurrentBlock("adm_content");
362  $this->tpl->setVariable("EXPLORER",$exp->getOutput());
363  $this->tpl->parseCurrentBlock();
364  }
365 
366  function add()
367  {
368  global $ilObjDataCache;
369 
370  if(!$_GET['source_id'])
371  {
372  ilUtil::sendFailure("Missing id: condition_id");
373  $this->selector();
374  return false;
375  }
376 
377  $this->initFormCondition((int) $_GET['source_id'],0,'add');
378  $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.condition_handler_add.html');
379  $this->tpl->setVariable('CONDITION_TABLE',$this->form->getHTML());
380  }
381 
382 
386  function assign()
387  {
388  if(!isset($_GET['source_id']))
389  {
390  echo "class.ilConditionHandlerInterface: no source_id given";
391 
392  return false;
393  }
394  if(!strlen($_POST['operator']))
395  {
396  ilUtil::sendFailure($this->lng->txt('no_operator_selected'));
397  $this->add();
398 
399  return false;
400  }
401 
402 
403  $this->ch_obj->setTargetRefId($this->getTargetRefId());
404  $this->ch_obj->setTargetObjId($this->getTargetId());
405  $this->ch_obj->setTargetType($this->getTargetType());
406 
407  switch($this->getTargetType())
408  {
409  case 'st':
410  $this->ch_obj->setReferenceHandlingType($_POST['ref_handling']);
411  break;
412 
413  default:
414  $this->ch_obj->setReferenceHandlingType(ilConditionHandler::UNIQUE_CONDITIONS);
415  break;
416  }
417  // this has to be changed, if non referenced trigger are implemted
418  if(!$trigger_obj =& ilObjectFactory::getInstanceByRefId((int) $_GET['source_id'],false))
419  {
420  echo 'ilConditionHandler: Trigger object does not exist';
421  }
422  $this->ch_obj->setTriggerRefId($trigger_obj->getRefId());
423  $this->ch_obj->setTriggerObjId($trigger_obj->getId());
424  $this->ch_obj->setTriggerType($trigger_obj->getType());
425  $this->ch_obj->setOperator($_POST['operator']);
426  $this->ch_obj->setValue('');
427 
428  // Save assigned sco's
429  if($this->ch_obj->getTriggerType() == 'sahs')
430  {
431  include_once 'Services/Tracking/classes/class.ilLPCollections.php';
432  $lp_collection = new ilLPCollections($this->ch_obj->getTriggerObjId());
433  $lp_collection->deleteAll();
434 
435  $items = is_array($_POST['item_ids']) ? $_POST['item_ids'] : array();
436  foreach($items as $item_id)
437  {
438  $lp_collection->add($item_id);
439  }
440  }
441 
442  $this->ch_obj->enableAutomaticValidation($this->getAutomaticValidation());
443  if(!$this->ch_obj->storeCondition())
444  {
445  ilUtil::sendFailure($this->ch_obj->getErrorMessage());
446  }
447  else
448  {
449  ilUtil::sendSuccess($this->lng->txt('added_new_condition'));
450  }
451 
452  $this->listConditions();
453 
454  return true;
455  }
456 
457  function chi_update()
458  {
459  #if(in_array('',$_POST['operator']))
460  #{
461  # ilUtil::sendInfo($this->lng->txt('select_one_operator'));
462 
463  # return false;
464  #}
465  foreach($this->__getConditionsOfTarget() as $condition)
466  {
467  $this->ch_obj->setOperator($_POST['operator'][$condition["id"]]);
468  $this->ch_obj->setValue($_POST['value'][$condition["id"]]);
469  $this->ch_obj->updateCondition($condition['id']);
470 
471  }
472  ilUtil::sendSuccess($this->lng->txt('conditions_updated'));
473 
474  $this->ctrl->returnToParent($this);
475 
476  return true;
477  }
479  {
480  include_once './classes/class.ilConditionHandler.php';
481 
482  foreach(ilConditionHandler::_getConditionsOfTarget($this->getTargetRefId(),$this->getTargetId(), $this->getTargetType()) as $condition)
483  {
484  if($condition['operator'] == 'not_member')
485  {
486  continue;
487  }
488  else
489  {
490  $cond[] = $condition;
491  }
492  }
493  return $cond ? $cond : array();
494  }
495 
496  function __showButtons()
497  {
498  if(!$this->getBackButtons())
499  {
500  return false;
501  }
502 
503  $this->tpl->addBlockfile("BUTTONS", "buttons", "tpl.buttons.html");
504  foreach($this->getBackButtons() as $name => $link)
505  {
506  $this->tpl->setCurrentBlock("btn_cell");
507  $this->tpl->setVariable("BTN_LINK",$link);
508  $this->tpl->setVariable("BTN_TXT",$this->lng->txt($name));
509  $this->tpl->parseCurrentBlock();
510  }
511  }
512 
520  private function initFormCondition($a_source_id,$a_condition_id = 0,$a_mode = 'add')
521  {
522  $trigger_obj_id = ilObject::_lookupObjId($a_source_id);
523  $trigger_type = ilObject::_lookupType($trigger_obj_id);
524 
525  $condition = ilConditionHandler::_getCondition($a_condition_id);
526 
527  if(is_object($this->form))
528  {
529  return true;
530  }
531  include_once('Services/Form/classes/class.ilPropertyFormGUI.php');
532  $this->form = new ilPropertyFormGUI();
533  $this->ctrl->setParameter($this,'source_id',$a_source_id);
534  $this->form->setFormAction($this->ctrl->getFormAction($this));
535 
536  $sel = new ilSelectInputGUI($this->lng->txt('condition'),'operator');
537  include_once "./classes/class.ilConditionHandler.php";
538  $ch_obj = new ilConditionHandler();
539  $operators[0] = $this->lng->txt('select_one');
540  foreach($ch_obj->getOperatorsByTargetType($trigger_type) as $operator)
541  {
542  $operators[$operator] = $this->lng->txt('condition_'.$operator);
543  }
544  $sel->setValue(isset($condition['operator']) ? $condition['operator'] : 0);
545  $sel->setOptions($operators);
546  $sel->setRequired(true);
547  $this->form->addItem($sel);
548 
550  {
551  $rad_opt = new ilRadioGroupInputGUI($this->lng->txt('cond_ref_handling'),'ref_handling');
552  $rad_opt->setValue(isset($condition['ref_handling']) ? $condition['ref_handling'] : ilConditionHandler::SHARED_CONDITIONS);
553 
554  $opt2 = new ilRadioOption($this->lng->txt('cond_ref_shared'),ilConditionHandler::SHARED_CONDITIONS);
555  $rad_opt->addOption($opt2);
556 
557  $opt1 = new ilRadioOption($this->lng->txt('cond_ref_unique'),ilConditionHandler::UNIQUE_CONDITIONS);
558  $rad_opt->addOption($opt1);
559 
560  $this->form->addItem($rad_opt);
561  }
562 
563  // Additional settings for SCO's
564  if($trigger_type == 'sahs')
565  {
566  $this->lng->loadLanguageModule('trac');
567  include_once 'Services/Tracking/classes/class.ilLPCollections.php';
568  $lp_collections = new ilLPCollections($trigger_obj_id);
569 
570  $cus = new ilCustomInputGUI($this->lng->txt('trac_sahs_relevant_items'),'item_ids[]');
571  $cus->setRequired(true);
572 
573  $tpl = new ilTemplate('tpl.condition_handler_sco_row.html',true,true);
574  $counter = 0;
575 
576  foreach(ilLPCollections::_getPossibleSAHSItems($trigger_obj_id) as $item_id => $sahs_item)
577  {
578  $tpl->setCurrentBlock("sco_row");
579  $tpl->setVariable('SCO_ID',$item_id);
580  $tpl->setVariable('SCO_TITLE',$sahs_item['title']);
581  $tpl->setVariable('CHECKED',$lp_collections->isAssigned($item_id) ? 'checked="checked"' : '');
582  $tpl->parseCurrentBlock();
583  $counter++;
584  }
585  $tpl->setVariable('INFO_SEL',$this->lng->txt('trac_lp_determination_info_sco'));
586  $cus->setHTML($tpl->get());
587  $this->form->addItem($cus);
588  }
589  switch($a_mode)
590  {
591  case 'edit':
592  $this->form->setTitleIcon(ilUtil::getImagePath('icon_'.$this->getTargetType().'.gif'));
593  $this->form->setTitle($this->lng->txt('precondition').' ('.
594  $this->getTargetTitle().' -> '.
596  $this->form->addCommandButton('updateCondition',$this->lng->txt('save'));
597  $this->form->addCommandButton('listConditions',$this->lng->txt('cancel'));
598  break;
599 
600 
601  case 'add':
602  $this->form->setTitleIcon(ilUtil::getImagePath('icon_'.$this->getTargetType().'.gif'));
603  $this->form->setTitle($this->lng->txt('add_condition').' ('.
604  $this->getTargetTitle().' -> '.
606  $this->form->addCommandButton('assign',$this->lng->txt('save'));
607  $this->form->addCommandButton('selector',$this->lng->txt('back'));
608  break;
609  }
610  return true;
611  }
612 }
613 ?>