ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilAssQuestionHintsGUI.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 require_once 'Modules/TestQuestionPool/classes/class.ilAssQuestionHintAbstractGUI.php';
6 require_once 'Modules/TestQuestionPool/classes/class.ilAssQuestionHintGUI.php';
7 require_once 'Modules/TestQuestionPool/classes/class.ilAssQuestionHintsOrderingClipboard.php';
8 
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;
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 
79  return $ilCtrl->forwardCommand($gui);
80 
81  default:
82 
83  $cmd .= 'Cmd';
84  return $this->$cmd();
85  }
86  }
87 
94  private function showListCmd()
95  {
96  global $ilCtrl, $tpl, $lng;
97 
99 
100  require_once 'Services/UIComponent/Toolbar/classes/class.ilToolbarGUI.php';
101  require_once 'Modules/TestQuestionPool/classes/class.ilAssQuestionHintsTableGUI.php';
102 
103  $toolbar = new ilToolbarGUI();
104 
105  $questionHintList = ilAssQuestionHintList::getListByQuestionId( $this->questionOBJ->getId() );
106 
107  if( $this->hintOrderingClipboard->hasStored() )
108  {
109  $questionHintList = $this->getQuestionHintListWithoutHintStoredInOrderingClipboard($questionHintList);
110 
111  $toolbar->addButton(
112  $lng->txt('tst_questions_hints_toolbar_cmd_reset_ordering_clipboard'),
113  $ilCtrl->getLinkTarget($this, self::CMD_RESET_ORDERING_CLIPBOARD)
114  );
115  }
116  else
117  {
118  $toolbar->addButton(
119  $lng->txt('tst_questions_hints_toolbar_cmd_add_hint'),
120  $ilCtrl->getLinkTargetByClass('ilAssQuestionHintGUI', ilAssQuestionHintGUI::CMD_SHOW_FORM)
121  );
122  }
123 
124  $table = new ilAssQuestionHintsTableGUI(
125  $this->questionOBJ, $questionHintList, $this, self::CMD_SHOW_LIST,
126  ilAssQuestionHintsTableGUI::TBL_MODE_ADMINISTRATION, $this->hintOrderingClipboard
127  );
128 
129  $tpl->setContent( $ilCtrl->getHtml($toolbar) . $ilCtrl->getHtml($table) );
130  }
131 
140  private function confirmDeleteCmd()
141  {
142  global $ilCtrl, $tpl, $lng;
143 
144  $hintIds = self::fetchHintIdsParameter();
145 
146  if( !count($hintIds) )
147  {
148  ilUtil::sendFailure($lng->txt('tst_question_hints_delete_hints_missing_selection_msg'), true);
149  $ilCtrl->redirect($this);
150  }
151 
152  require_once 'Services/Utilities/classes/class.ilConfirmationGUI.php';
153  $confirmation = new ilConfirmationGUI();
154 
155  $confirmation->setHeaderText($lng->txt('tst_question_hints_delete_hints_confirm_header'));
156  $confirmation->setFormAction($ilCtrl->getFormAction($this));
157  $confirmation->setConfirm($lng->txt('tst_question_hints_delete_hints_confirm_cmd'), self::CMD_PERFORM_DELETE);
158  $confirmation->setCancel($lng->txt('cancel'), self::CMD_SHOW_LIST);
159 
160  $questionHintList = ilAssQuestionHintList::getListByQuestionId($this->questionOBJ->getId());
161 
162  foreach($questionHintList as $questionHint)
163  {
164  /* @var $questionHint ilAssQuestionHint */
165 
166  if( in_array($questionHint->getId(), $hintIds) )
167  {
168  $confirmation->addItem('hint_ids[]', $questionHint->getId(), sprintf(
169  $lng->txt('tst_question_hints_delete_hints_confirm_item'), $questionHint->getIndex(), $questionHint->getText()
170  ));
171  }
172  }
173 
174  $tpl->setContent( $ilCtrl->getHtml($confirmation) );
175  }
176 
184  private function performDeleteCmd()
185  {
186  global $ilCtrl, $tpl, $lng;
187 
188  $hintIds = self::fetchHintIdsParameter();
189 
190  if( !count($hintIds) )
191  {
192  ilUtil::sendFailure($lng->txt('tst_question_hints_delete_hints_missing_selection_msg'), true);
193  $ilCtrl->redirect($this);
194  }
195 
196  $questionCompleteHintList = ilAssQuestionHintList::getListByQuestionId($this->questionOBJ->getId());
197 
198  $questionRemainingHintList = new ilAssQuestionHintList();
199 
200  foreach($questionCompleteHintList as $listKey => $questionHint)
201  {
202  /* @var $questionHint ilAssQuestionHint */
203 
204  if( in_array($questionHint->getId(), $hintIds) )
205  {
206  $questionHint->delete();
207  }
208  else
209  {
210  $questionRemainingHintList->addHint($questionHint);
211  }
212  }
213 
214  $questionRemainingHintList->reIndex();
215 
216  ilUtil::sendSuccess($lng->txt('tst_question_hints_delete_success_msg'), true);
217 
218  $originalexists = $this->questionOBJ->_questionExistsInPool($this->questionOBJ->original_id);
219  include_once "./Modules/TestQuestionPool/classes/class.assQuestion.php";
220  global $ilUser;
221  if ($_GET["calling_test"] && $originalexists && assQuestion::_isWriteable($this->questionOBJ->original_id, $ilUser->getId()))
222  {
223  $ilCtrl->redirectByClass('ilAssQuestionHintsGUI', ilAssQuestionHintsGUI::CMD_CONFIRM_SYNC);
224  }
225 
226  $ilCtrl->redirect($this);
227  }
228 
237  private function saveListOrderCmd()
238  {
239  global $ilCtrl, $lng;
240 
241  $hintIndexes = self::orderHintIndexes(
242  self::fetchHintIndexesParameter()
243  );
244 
245  if( !count($hintIndexes) )
246  {
247  ilUtil::sendFailure($lng->txt('tst_question_hints_save_order_unkown_failure_msg'), true);
248  $ilCtrl->redirect($this);
249  }
250 
251  $curQuestionHintList = ilAssQuestionHintList::getListByQuestionId( $this->questionOBJ->getId() );
252 
253  $newQuestionHintList = new ilAssQuestionHintList();
254 
255  foreach($hintIndexes as $hintId => $hintIndex)
256  {
257  if( !$curQuestionHintList->hintExists($hintId) )
258  {
259  ilUtil::sendFailure($lng->txt('tst_question_hints_save_order_unkown_failure_msg'), true);
260  $ilCtrl->redirect($this);
261  }
262 
263  $questionHint = $curQuestionHintList->getHint($hintId);
264 
265  $newQuestionHintList->addHint($questionHint);
266  }
267 
268  $newQuestionHintList->reIndex();
269 
270  ilUtil::sendSuccess($lng->txt('tst_question_hints_save_order_success_msg'), true);
271 
272  $originalexists = $this->questionOBJ->_questionExistsInPool($this->questionOBJ->original_id);
273  include_once "./Modules/TestQuestionPool/classes/class.assQuestion.php";
274  global $ilUser;
275  if ($_GET["calling_test"] && $originalexists && assQuestion::_isWriteable($this->questionOBJ->original_id, $ilUser->getId()))
276  {
277  $ilCtrl->redirectByClass('ilAssQuestionHintsGUI', ilAssQuestionHintsGUI::CMD_CONFIRM_SYNC);
278  }
279 
280  $ilCtrl->redirect($this);
281  }
282 
289  private function cutToOrderingClipboardCmd()
290  {
291  global $ilCtrl;
292 
293  $moveHintIds = self::fetchHintIdsParameter();
294  $this->checkForSingleHintIdAndRedirectOnFailure($moveHintIds);
295 
296  $moveHintId = current($moveHintIds);
297 
299 
300  $this->hintOrderingClipboard->setStored($moveHintId);
301 
302  $ilCtrl->redirect($this, self::CMD_SHOW_LIST);
303  }
304 
313  {
314  global $ilCtrl, $lng;
315 
316  $targetHintIds = self::fetchHintIdsParameter();
317  $this->checkForSingleHintIdAndRedirectOnFailure($targetHintIds);
318 
319  $targetHintId = current($targetHintIds);
320 
322 
323  $curQuestionHintList = ilAssQuestionHintList::getListByQuestionId( $this->questionOBJ->getId() );
324  $newQuestionHintList = new ilAssQuestionHintList( $this->questionOBJ->getId() );
325 
326  foreach($curQuestionHintList as $questionHint)
327  {
328  /* @var $questionHint ilAssQuestionHint */
329 
330  if( $questionHint->getId() == $this->hintOrderingClipboard->getStored() )
331  {
332  continue;
333  }
334 
335  if( $questionHint->getId() == $targetHintId )
336  {
337  $targetQuestionHint = $questionHint;
338 
339  $pasteQuestionHint = ilAssQuestionHint::getInstanceById( $this->hintOrderingClipboard->getStored() );
340 
341  $newQuestionHintList->addHint($pasteQuestionHint);
342  }
343 
344  $newQuestionHintList->addHint($questionHint);
345  }
346 
347  $successMsg = sprintf(
348  $lng->txt('tst_question_hints_paste_before_success_msg'),
349  $pasteQuestionHint->getIndex(), $targetQuestionHint->getIndex()
350  );
351 
352  $newQuestionHintList->reIndex();
353 
354  $this->hintOrderingClipboard->resetStored();
355 
356  ilUtil::sendSuccess($successMsg, true);
357 
358  $ilCtrl->redirect($this, self::CMD_SHOW_LIST);
359  }
360 
369  {
370  global $ilCtrl, $lng;
371 
372  $targetHintIds = self::fetchHintIdsParameter();
373  $this->checkForSingleHintIdAndRedirectOnFailure($targetHintIds);
374 
375  $targetHintId = current($targetHintIds);
376 
378 
379  $curQuestionHintList = ilAssQuestionHintList::getListByQuestionId( $this->questionOBJ->getId() );
380  $newQuestionHintList = new ilAssQuestionHintList( $this->questionOBJ->getId() );
381 
382  foreach($curQuestionHintList as $questionHint)
383  {
384  /* @var $questionHint ilAssQuestionHint */
385 
386  if( $questionHint->getId() == $this->hintOrderingClipboard->getStored() )
387  {
388  continue;
389  }
390 
391  $newQuestionHintList->addHint($questionHint);
392 
393  if( $questionHint->getId() == $targetHintId )
394  {
395  $targetQuestionHint = $questionHint;
396 
397  $pasteQuestionHint = ilAssQuestionHint::getInstanceById( $this->hintOrderingClipboard->getStored() );
398 
399  $newQuestionHintList->addHint($pasteQuestionHint);
400  }
401  }
402 
403  $successMsg = sprintf(
404  $lng->txt('tst_question_hints_paste_after_success_msg'),
405  $pasteQuestionHint->getIndex(), $targetQuestionHint->getIndex()
406  );
407 
408  $newQuestionHintList->reIndex();
409 
410  $this->hintOrderingClipboard->resetStored();
411 
412  ilUtil::sendSuccess($successMsg, true);
413 
414  $ilCtrl->redirect($this, self::CMD_SHOW_LIST);
415  }
416 
424  private function resetOrderingClipboardCmd()
425  {
426  global $ilCtrl, $lng;
427 
428  $this->hintOrderingClipboard->resetStored();
429 
430  ilUtil::sendInfo($lng->txt('tst_question_hints_ordering_clipboard_resetted'), true);
431  $ilCtrl->redirect($this, self::CMD_SHOW_LIST);
432  }
433 
442  {
443  global $lng;
444 
445  if( !$this->hintOrderingClipboard->hasStored() )
446  {
447  return;
448  }
449 
450  $questionHint = ilAssQuestionHint::getInstanceById( $this->hintOrderingClipboard->getStored() );
451 
452  ilUtil::sendInfo(sprintf(
453  $lng->txt('tst_question_hints_item_stored_in_ordering_clipboard'), $questionHint->getIndex()
454  ));
455  }
456 
465  {
466  $questionHintList = ilAssQuestionHintList::getListByQuestionId( $this->questionOBJ->getId() );
467 
468  if( !$questionHintList->hintExists($hintId) )
469  {
470  ilUtil::sendFailure($lng->txt('tst_question_hints_invalid_hint_id'), true);
471  $ilCtrl->redirect($this, self::CMD_SHOW_LIST);
472  }
473  }
474 
484  {
485  $filteredQuestionHintList = new ilAssQuestionHintList();
486 
487  foreach($questionHintList as $questionHint)
488  {
489  /* @var $questionHint ilAssQuestionHint */
490 
491  if( $questionHint->getId() != $this->hintOrderingClipboard->getStored() )
492  {
493  $filteredQuestionHintList->addHint($questionHint);
494  }
495  }
496 
497  return $filteredQuestionHintList;
498  }
499 
509  private function checkForSingleHintIdAndRedirectOnFailure($hintIds)
510  {
511  global $ilCtrl, $lng;
512 
513  if( !count($hintIds) )
514  {
515  ilUtil::sendFailure($lng->txt('tst_question_hints_cut_hints_missing_selection_msg'), true);
516  $ilCtrl->redirect($this, self::CMD_SHOW_LIST);
517  }
518  elseif( count($hintIds) > 1 )
519  {
520  ilUtil::sendFailure($lng->txt('tst_question_hints_cut_hints_single_selection_msg'), true);
521  $ilCtrl->redirect($this, self::CMD_SHOW_LIST);
522  }
523  }
524 
533  private static function fetchHintIdsParameter()
534  {
535  $hintIds = array();
536 
537  if( isset($_POST['hint_ids']) && is_array($_POST['hint_ids']) )
538  {
539  foreach($_POST['hint_ids'] as $hintId)
540  {
541  if( (int)$hintId ) $hintIds[] = (int)$hintId;
542  }
543  }
544  elseif( isset($_GET['hint_id']) && (int)$_GET['hint_id'] )
545  {
546  $hintIds[] = (int)$_GET['hint_id'];
547  }
548 
549  return $hintIds;
550  }
551 
559  private static function fetchHintIndexesParameter()
560  {
561  $hintIndexes = array();
562 
563  if( isset($_POST['hint_indexes']) && is_array($_POST['hint_indexes']) )
564  {
565  foreach($_POST['hint_indexes'] as $hintId => $hintIndex)
566  {
567  if( (int)$hintId ) $hintIndexes[(int)$hintId] = $hintIndex;
568  }
569  }
570 
571  return $hintIndexes;
572  }
573 
582  private static function orderHintIndexes($hintIndexes)
583  {
584  asort($hintIndexes);
585 
586  return $hintIndexes;
587  }
588 
589  public function confirmSyncCmd()
590  {
591  $this->questionGUI->originalSyncForm('showHints');
592  }
593 }