ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilAssQuestionHintsGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'Modules/TestQuestionPool/classes/class.ilAssQuestionHintAbstractGUI.php';
5 require_once 'Modules/TestQuestionPool/classes/class.ilAssQuestionHintGUI.php';
6 require_once 'Modules/TestQuestionPool/classes/class.ilAssQuestionHintsOrderingClipboard.php';
7 
23 {
27  const CMD_SHOW_LIST = 'showList';
28  const CMD_CONFIRM_DELETE = 'confirmDelete';
29  const CMD_PERFORM_DELETE = 'performDelete';
30  const CMD_SAVE_LIST_ORDER = 'saveListOrder';
31  const CMD_CUT_TO_ORDERING_CLIPBOARD = 'cutToOrderingClipboard';
32  const CMD_PASTE_FROM_ORDERING_CLIPBOARD_BEFORE = 'pasteFromOrderingClipboardBefore';
33  const CMD_PASTE_FROM_ORDERING_CLIPBOARD_AFTER = 'pasteFromOrderingClipboardAfter';
34  const CMD_RESET_ORDERING_CLIPBOARD = 'resetOrderingClipboard';
35  const CMD_CONFIRM_SYNC = 'confirmSync';
36 
43  private $hintOrderingClipboard = null;
44 
52  {
53  parent::__construct($questionGUI);
54 
55  $this->hintOrderingClipboard = new ilAssQuestionHintsOrderingClipboard($questionGUI->object);
56  }
57 
65  public function executeCommand()
66  {
67  global $ilCtrl, $ilTabs, $lng;
68 
69  $cmd = $ilCtrl->getCmd(self::CMD_SHOW_LIST);
70  $nextClass = $ilCtrl->getNextClass($this);
71 
72  switch($nextClass)
73  {
74  case 'ilassquestionhintgui':
75 
76  require_once 'Modules/TestQuestionPool/classes/class.ilAssQuestionHintGUI.php';
77  $gui = new ilAssQuestionHintGUI($this->questionGUI);
78  $ilCtrl->forwardCommand($gui);
79  break;
80 
81  case 'ilasshintpagegui':
82 
83  require_once 'Modules/TestQuestionPool/classes/class.ilAssQuestionHintPageObjectCommandForwarder.php';
84  $forwarder = new ilAssQuestionHintPageObjectCommandForwarder($this->questionOBJ, $ilCtrl, $ilTabs, $lng);
86  $forwarder->forward();
87  break;
88 
89  default:
90 
91  $cmd .= 'Cmd';
92  $this->$cmd();
93  break;
94  }
95  }
96 
103  private function showListCmd()
104  {
105  global $ilCtrl, $tpl, $lng;
106 
108 
109  require_once 'Services/UIComponent/Toolbar/classes/class.ilToolbarGUI.php';
110  require_once 'Modules/TestQuestionPool/classes/class.ilAssQuestionHintsTableGUI.php';
111 
112  $toolbar = new ilToolbarGUI();
113 
114  $questionHintList = ilAssQuestionHintList::getListByQuestionId( $this->questionOBJ->getId() );
115 
116  if( $this->hintOrderingClipboard->hasStored() )
117  {
118  $questionHintList = $this->getQuestionHintListWithoutHintStoredInOrderingClipboard($questionHintList);
119 
120  $toolbar->addButton(
121  $lng->txt('tst_questions_hints_toolbar_cmd_reset_ordering_clipboard'),
122  $ilCtrl->getLinkTarget($this, self::CMD_RESET_ORDERING_CLIPBOARD)
123  );
124  }
125  else
126  {
127  $toolbar->addButton(
128  $lng->txt('tst_questions_hints_toolbar_cmd_add_hint'),
129  $ilCtrl->getLinkTargetByClass('ilAssQuestionHintGUI', ilAssQuestionHintGUI::CMD_SHOW_FORM)
130  );
131  }
132 
133  $table = new ilAssQuestionHintsTableGUI(
134  $this->questionOBJ, $questionHintList, $this, self::CMD_SHOW_LIST,
135  ilAssQuestionHintsTableGUI::TBL_MODE_ADMINISTRATION, $this->hintOrderingClipboard
136  );
137 
138  $tpl->setContent( $ilCtrl->getHtml($toolbar) . $ilCtrl->getHtml($table) );
139  }
140 
149  private function confirmDeleteCmd()
150  {
151  global $ilCtrl, $tpl, $lng;
152 
153  $hintIds = self::fetchHintIdsParameter();
154 
155  if( !count($hintIds) )
156  {
157  ilUtil::sendFailure($lng->txt('tst_question_hints_delete_hints_missing_selection_msg'), true);
158  $ilCtrl->redirect($this);
159  }
160 
161  require_once 'Services/Utilities/classes/class.ilConfirmationGUI.php';
162  $confirmation = new ilConfirmationGUI();
163 
164  $confirmation->setHeaderText($lng->txt('tst_question_hints_delete_hints_confirm_header'));
165  $confirmation->setFormAction($ilCtrl->getFormAction($this));
166  $confirmation->setConfirm($lng->txt('tst_question_hints_delete_hints_confirm_cmd'), self::CMD_PERFORM_DELETE);
167  $confirmation->setCancel($lng->txt('cancel'), self::CMD_SHOW_LIST);
168 
169  $questionHintList = ilAssQuestionHintList::getListByQuestionId($this->questionOBJ->getId());
170 
171  foreach($questionHintList as $questionHint)
172  {
173  /* @var $questionHint ilAssQuestionHint */
174 
175  if( in_array($questionHint->getId(), $hintIds) )
176  {
177  $confirmation->addItem('hint_ids[]', $questionHint->getId(), sprintf(
178  $lng->txt('tst_question_hints_delete_hints_confirm_item'), $questionHint->getIndex(), $questionHint->getText()
179  ));
180  }
181  }
182 
183  $tpl->setContent( $ilCtrl->getHtml($confirmation) );
184  }
185 
193  private function performDeleteCmd()
194  {
195  global $ilCtrl, $tpl, $lng;
196 
197  $hintIds = self::fetchHintIdsParameter();
198 
199  if( !count($hintIds) )
200  {
201  ilUtil::sendFailure($lng->txt('tst_question_hints_delete_hints_missing_selection_msg'), true);
202  $ilCtrl->redirect($this);
203  }
204 
205  $questionCompleteHintList = ilAssQuestionHintList::getListByQuestionId($this->questionOBJ->getId());
206 
207  $questionRemainingHintList = new ilAssQuestionHintList();
208 
209  foreach($questionCompleteHintList as $listKey => $questionHint)
210  {
211  /* @var $questionHint ilAssQuestionHint */
212 
213  if( in_array($questionHint->getId(), $hintIds) )
214  {
215  $questionHint->delete();
216  }
217  else
218  {
219  $questionRemainingHintList->addHint($questionHint);
220  }
221  }
222 
223  $questionRemainingHintList->reIndex();
224 
225  ilUtil::sendSuccess($lng->txt('tst_question_hints_delete_success_msg'), true);
226 
227  $originalexists = $this->questionOBJ->_questionExistsInPool($this->questionOBJ->original_id);
228  include_once "./Modules/TestQuestionPool/classes/class.assQuestion.php";
229  global $ilUser;
230  if ($_GET["calling_test"] && $originalexists && assQuestion::_isWriteable($this->questionOBJ->original_id, $ilUser->getId()))
231  {
232  $ilCtrl->redirectByClass('ilAssQuestionHintsGUI', ilAssQuestionHintsGUI::CMD_CONFIRM_SYNC);
233  }
234 
235  $ilCtrl->redirect($this);
236  }
237 
246  private function saveListOrderCmd()
247  {
248  global $ilCtrl, $lng;
249 
250  $hintIndexes = self::orderHintIndexes(
251  self::fetchHintIndexesParameter()
252  );
253 
254  if( !count($hintIndexes) )
255  {
256  ilUtil::sendFailure($lng->txt('tst_question_hints_save_order_unkown_failure_msg'), true);
257  $ilCtrl->redirect($this);
258  }
259 
260  $curQuestionHintList = ilAssQuestionHintList::getListByQuestionId( $this->questionOBJ->getId() );
261 
262  $newQuestionHintList = new ilAssQuestionHintList();
263 
264  foreach($hintIndexes as $hintId => $hintIndex)
265  {
266  if( !$curQuestionHintList->hintExists($hintId) )
267  {
268  ilUtil::sendFailure($lng->txt('tst_question_hints_save_order_unkown_failure_msg'), true);
269  $ilCtrl->redirect($this);
270  }
271 
272  $questionHint = $curQuestionHintList->getHint($hintId);
273 
274  $newQuestionHintList->addHint($questionHint);
275  }
276 
277  $newQuestionHintList->reIndex();
278 
279  ilUtil::sendSuccess($lng->txt('tst_question_hints_save_order_success_msg'), true);
280 
281  $originalexists = $this->questionOBJ->_questionExistsInPool($this->questionOBJ->original_id);
282  include_once "./Modules/TestQuestionPool/classes/class.assQuestion.php";
283  global $ilUser;
284  if ($_GET["calling_test"] && $originalexists && assQuestion::_isWriteable($this->questionOBJ->original_id, $ilUser->getId()))
285  {
286  $ilCtrl->redirectByClass('ilAssQuestionHintsGUI', ilAssQuestionHintsGUI::CMD_CONFIRM_SYNC);
287  }
288 
289  $ilCtrl->redirect($this);
290  }
291 
298  private function cutToOrderingClipboardCmd()
299  {
300  global $ilCtrl;
301 
302  $moveHintIds = self::fetchHintIdsParameter();
303  $this->checkForSingleHintIdAndRedirectOnFailure($moveHintIds);
304 
305  $moveHintId = current($moveHintIds);
306 
308 
309  $this->hintOrderingClipboard->setStored($moveHintId);
310 
311  $ilCtrl->redirect($this, self::CMD_SHOW_LIST);
312  }
313 
322  {
323  global $ilCtrl, $lng;
324 
325  $targetHintIds = self::fetchHintIdsParameter();
326  $this->checkForSingleHintIdAndRedirectOnFailure($targetHintIds);
327 
328  $targetHintId = current($targetHintIds);
329 
331 
332  $curQuestionHintList = ilAssQuestionHintList::getListByQuestionId( $this->questionOBJ->getId() );
333  $newQuestionHintList = new ilAssQuestionHintList( $this->questionOBJ->getId() );
334 
335  foreach($curQuestionHintList as $questionHint)
336  {
337  /* @var $questionHint ilAssQuestionHint */
338 
339  if( $questionHint->getId() == $this->hintOrderingClipboard->getStored() )
340  {
341  continue;
342  }
343 
344  if( $questionHint->getId() == $targetHintId )
345  {
346  $targetQuestionHint = $questionHint;
347 
348  $pasteQuestionHint = ilAssQuestionHint::getInstanceById( $this->hintOrderingClipboard->getStored() );
349 
350  $newQuestionHintList->addHint($pasteQuestionHint);
351  }
352 
353  $newQuestionHintList->addHint($questionHint);
354  }
355 
356  $successMsg = sprintf(
357  $lng->txt('tst_question_hints_paste_before_success_msg'),
358  $pasteQuestionHint->getIndex(), $targetQuestionHint->getIndex()
359  );
360 
361  $newQuestionHintList->reIndex();
362 
363  $this->hintOrderingClipboard->resetStored();
364 
365  ilUtil::sendSuccess($successMsg, true);
366 
367  $ilCtrl->redirect($this, self::CMD_SHOW_LIST);
368  }
369 
378  {
379  global $ilCtrl, $lng;
380 
381  $targetHintIds = self::fetchHintIdsParameter();
382  $this->checkForSingleHintIdAndRedirectOnFailure($targetHintIds);
383 
384  $targetHintId = current($targetHintIds);
385 
387 
388  $curQuestionHintList = ilAssQuestionHintList::getListByQuestionId( $this->questionOBJ->getId() );
389  $newQuestionHintList = new ilAssQuestionHintList( $this->questionOBJ->getId() );
390 
391  foreach($curQuestionHintList as $questionHint)
392  {
393  /* @var $questionHint ilAssQuestionHint */
394 
395  if( $questionHint->getId() == $this->hintOrderingClipboard->getStored() )
396  {
397  continue;
398  }
399 
400  $newQuestionHintList->addHint($questionHint);
401 
402  if( $questionHint->getId() == $targetHintId )
403  {
404  $targetQuestionHint = $questionHint;
405 
406  $pasteQuestionHint = ilAssQuestionHint::getInstanceById( $this->hintOrderingClipboard->getStored() );
407 
408  $newQuestionHintList->addHint($pasteQuestionHint);
409  }
410  }
411 
412  $successMsg = sprintf(
413  $lng->txt('tst_question_hints_paste_after_success_msg'),
414  $pasteQuestionHint->getIndex(), $targetQuestionHint->getIndex()
415  );
416 
417  $newQuestionHintList->reIndex();
418 
419  $this->hintOrderingClipboard->resetStored();
420 
421  ilUtil::sendSuccess($successMsg, true);
422 
423  $ilCtrl->redirect($this, self::CMD_SHOW_LIST);
424  }
425 
433  private function resetOrderingClipboardCmd()
434  {
435  global $ilCtrl, $lng;
436 
437  $this->hintOrderingClipboard->resetStored();
438 
439  ilUtil::sendInfo($lng->txt('tst_question_hints_ordering_clipboard_resetted'), true);
440  $ilCtrl->redirect($this, self::CMD_SHOW_LIST);
441  }
442 
451  {
452  global $lng;
453 
454  if( !$this->hintOrderingClipboard->hasStored() )
455  {
456  return;
457  }
458 
459  $questionHint = ilAssQuestionHint::getInstanceById( $this->hintOrderingClipboard->getStored() );
460 
461  ilUtil::sendInfo(sprintf(
462  $lng->txt('tst_question_hints_item_stored_in_ordering_clipboard'), $questionHint->getIndex()
463  ));
464  }
465 
474  {
475  $questionHintList = ilAssQuestionHintList::getListByQuestionId( $this->questionOBJ->getId() );
476 
477  if( !$questionHintList->hintExists($hintId) )
478  {
479  ilUtil::sendFailure($lng->txt('tst_question_hints_invalid_hint_id'), true);
480  $ilCtrl->redirect($this, self::CMD_SHOW_LIST);
481  }
482  }
483 
493  {
494  $filteredQuestionHintList = new ilAssQuestionHintList();
495 
496  foreach($questionHintList as $questionHint)
497  {
498  /* @var $questionHint ilAssQuestionHint */
499 
500  if( $questionHint->getId() != $this->hintOrderingClipboard->getStored() )
501  {
502  $filteredQuestionHintList->addHint($questionHint);
503  }
504  }
505 
506  return $filteredQuestionHintList;
507  }
508 
518  private function checkForSingleHintIdAndRedirectOnFailure($hintIds)
519  {
520  global $ilCtrl, $lng;
521 
522  if( !count($hintIds) )
523  {
524  ilUtil::sendFailure($lng->txt('tst_question_hints_cut_hints_missing_selection_msg'), true);
525  $ilCtrl->redirect($this, self::CMD_SHOW_LIST);
526  }
527  elseif( count($hintIds) > 1 )
528  {
529  ilUtil::sendFailure($lng->txt('tst_question_hints_cut_hints_single_selection_msg'), true);
530  $ilCtrl->redirect($this, self::CMD_SHOW_LIST);
531  }
532  }
533 
542  private static function fetchHintIdsParameter()
543  {
544  $hintIds = array();
545 
546  if( isset($_POST['hint_ids']) && is_array($_POST['hint_ids']) )
547  {
548  foreach($_POST['hint_ids'] as $hintId)
549  {
550  if( (int)$hintId ) $hintIds[] = (int)$hintId;
551  }
552  }
553  elseif( isset($_GET['hint_id']) && (int)$_GET['hint_id'] )
554  {
555  $hintIds[] = (int)$_GET['hint_id'];
556  }
557 
558  return $hintIds;
559  }
560 
568  private static function fetchHintIndexesParameter()
569  {
570  $hintIndexes = array();
571 
572  if( isset($_POST['hint_indexes']) && is_array($_POST['hint_indexes']) )
573  {
574  foreach($_POST['hint_indexes'] as $hintId => $hintIndex)
575  {
576  if( (int)$hintId ) $hintIndexes[(int)$hintId] = $hintIndex;
577  }
578  }
579 
580  return $hintIndexes;
581  }
582 
591  private static function orderHintIndexes($hintIndexes)
592  {
593  asort($hintIndexes);
594 
595  return $hintIndexes;
596  }
597 
598  public function confirmSyncCmd()
599  {
600  $this->questionGUI->originalSyncForm('showHints');
601  }
602 }