ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjTestDynamicQuestionSetConfigGUI.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/Test/classes/class.ilObjTestDynamicQuestionSetConfig.php';
5 
17 {
21  const CMD_SHOW_FORM = 'showForm';
22  const CMD_SAVE_FORM = 'saveForm';
23  const CMD_GET_TAXONOMY_OPTIONS_ASYNC = 'getTaxonomyOptionsAsync';
24 
30  protected $ctrl = null;
31 
37  protected $access = null;
38 
44  protected $tabs = null;
45 
51  protected $lng = null;
52 
58  protected $tpl = null;
59 
65  protected $db = null;
66 
72  protected $tree = null;
73 
79  protected $testOBJ = null;
80 
86  protected $questionSetConfig = null;
87 
88  const QUESTION_ORDERING_TYPE_UPDATE_DATE = 'ordering_by_date';
89  const QUESTION_ORDERING_TYPE_TAXONOMY = 'ordering_by_tax';
90 
95  {
96  $this->ctrl = $ctrl;
97  $this->access = $access;
98  $this->tabs = $tabs;
99  $this->lng = $lng;
100  $this->tpl = $tpl;
101  $this->db = $db;
102  $this->tree = $tree;
103  $this->pluginAdmin = $pluginAdmin;
104 
105  $this->testOBJ = $testOBJ;
106 
107  $this->questionSetConfig = new ilObjTestDynamicQuestionSetConfig($this->tree, $this->db, $this->pluginAdmin, $this->testOBJ);
108  }
109 
113  public function executeCommand()
114  {
115  // allow only write access
116 
117  if (!$this->access->checkAccess("write", "", $this->testOBJ->getRefId()))
118  {
119  ilUtil::sendInfo($this->lng->txt("cannot_edit_test"), true);
120  $this->ctrl->redirectByClass('ilObjTestGUI', "infoScreen");
121  }
122 
123  // activate corresponding tab (auto activation does not work in ilObjTestGUI-Tabs-Salad)
124 
125  $this->tabs->activateTab('assQuestions');
126 
127  // process command
128 
129  $nextClass = $this->ctrl->getNextClass();
130 
131  switch($nextClass)
132  {
133  default:
134  $cmd = $this->ctrl->getCmd(self::CMD_SHOW_FORM).'Cmd';
135  $this->$cmd();
136  }
137  }
138 
139  public function getTaxonomyOptionsAsyncCmd()
140  {
141  $questionPoolId = (int)$_POST['question_pool_id'];
142 
143  echo $this->buildTaxonomySelectInputOptionJson($questionPoolId);
144  exit;
145  }
146 
152  public function showFormCmd(ilPropertyFormGUI $form = null)
153  {
154  $this->questionSetConfig->loadFromDb();
155 
156  if( $this->questionSetConfig->areDepenciesBroken($this->tree) )
157  {
158  ilUtil::sendFailure( $this->questionSetConfig->getDepenciesBrokenMessage($this->lng) );
159  }
160  elseif( $this->questionSetConfig->areDepenciesInVulnerableState($this->tree) )
161  {
162  ilUtil::sendInfo( $this->questionSetConfig->getDepenciesInVulnerableStateMessage($this->lng) );
163  }
164 
165  if( $form === null )
166  {
167  $form = $this->buildForm();
168  }
169 
170  $this->tpl->setContent( $this->ctrl->getHTML($form) );
171 
172  $this->tpl->addJavaScript('Modules/Test/js/ilTestDynamicQuestionSetConfig.js');
173  }
174 
181  public function saveFormCmd()
182  {
183  $form = $this->buildForm();
184 
185  if( $this->testOBJ->participantDataExist() )
186  {
187  ilUtil::sendFailure($this->lng->txt("tst_msg_cannot_modify_dynamic_question_set_conf_due_to_part"), true);
188  return $this->showFormCmd($form);
189  }
190 
191  $errors = !$form->checkInput(); // ALWAYS CALL BEFORE setValuesByPost()
192  $form->setValuesByPost(); // NEVER CALL THIS BEFORE checkInput()
193 
194  if($errors)
195  {
196  ilUtil::sendFailure($this->lng->txt('form_input_not_valid'));
197  return $this->showFormCmd($form);
198  }
199 
200  $this->performSaveForm($form);
201 
202  $this->testOBJ->saveCompleteStatus( $this->questionSetConfig );
203 
204  ilUtil::sendSuccess($this->lng->txt("tst_msg_dynamic_question_set_config_modified"), true);
205  $this->ctrl->redirect($this, self::CMD_SHOW_FORM);
206  }
207 
213  private function performSaveForm(ilPropertyFormGUI $form)
214  {
215  $this->questionSetConfig->setSourceQuestionPoolId(
216  $form->getItemByPostVar('source_qpl_id')->getValue()
217  );
218 
219  $this->questionSetConfig->setSourceQuestionPoolTitle( ilObject::_lookupTitle(
220  $form->getItemByPostVar('source_qpl_id')->getValue()
221  ));
222 
223  switch( $form->getItemByPostVar('question_ordering')->getValue() )
224  {
225  case self::QUESTION_ORDERING_TYPE_UPDATE_DATE:
226  $this->questionSetConfig->setOrderingTaxonomyId(null);
227  break;
228 
229  case self::QUESTION_ORDERING_TYPE_TAXONOMY:
230  $this->questionSetConfig->setOrderingTaxonomyId(
231  $form->getItemByPostVar('ordering_tax')->getValue()
232  );
233  break;
234  }
235 
236  $this->questionSetConfig->setTaxonomyFilterEnabled(
237  $form->getItemByPostVar('tax_filter_enabled')->getChecked()
238  );
239 
240  $this->questionSetConfig->setAnswerStatusFilterEnabled(
241  $form->getItemByPostVar('answer_status_filter_enabled')->getChecked()
242  );
243 
244  $this->questionSetConfig->setPreviousQuestionsListEnabled(
245  $form->getItemByPostVar('prev_quest_list_enabled')->getChecked()
246  );
247 
248  $this->questionSetConfig->saveToDb( $this->testOBJ->getTestId() );
249  }
250 
257  private function buildForm()
258  {
259  $this->questionSetConfig->loadFromDb( $this->testOBJ->getTestId() );
260 
261  require_once 'Services/Form/classes/class.ilPropertyFormGUI.php';
262  $form = new ilPropertyFormGUI();
263 
264  $form->setFormAction($this->ctrl->getFormAction($this));
265 
266  $form->setId("tst_form_dynamic_question_set_config");
267  $form->setTitle($this->lng->txt('tst_form_dynamic_question_set_config'));
268  $form->setTableWidth("100%");
269 
270  $hiddenInputTaxSelectOptAsyncUrl = new ilHiddenInputGUI('taxSelectOptAsyncUrl');
271  $hiddenInputTaxSelectOptAsyncUrl->setValue(
272  $this->ctrl->getLinkTarget($this, self::CMD_GET_TAXONOMY_OPTIONS_ASYNC, '', true)
273  );
274  $form->addItem($hiddenInputTaxSelectOptAsyncUrl);
275 
276  if( $this->testOBJ->participantDataExist() )
277  {
278  $pool = new ilNonEditableValueGUI($this->lng->txt('tst_input_dynamic_question_set_source_questionpool'), 'source_qpl_title');
279  $pool->setValue( $this->questionSetConfig->getSourceQuestionPoolSummaryString($this->lng, $this->tree) );
280  $pool->setDisabled(true);
281  $form->addItem($pool);
282  }
283  else
284  {
285  $poolInput = new ilSelectInputGUI($this->lng->txt('tst_input_dynamic_question_set_source_questionpool'), 'source_qpl_id');
287  $this->testOBJ->getAvailableQuestionpools(true, false, false, true, true)
288  ));
289  $poolInput->setValue( $this->questionSetConfig->getSourceQuestionPoolId() );
290  $poolInput->setRequired(true);
291  $form->addItem($poolInput);
292  }
293 
294  $questionOderingInput = new ilRadioGroupInputGUI(
295  $this->lng->txt('tst_input_dynamic_question_set_question_ordering'), 'question_ordering'
296  );
297  $questionOderingInput->setValue( $this->questionSetConfig->getOrderingTaxonomyId() ?
298  self::QUESTION_ORDERING_TYPE_TAXONOMY : self::QUESTION_ORDERING_TYPE_UPDATE_DATE
299  );
300  $optionOrderByDate = new ilRadioOption(
301  $this->lng->txt('tst_input_dynamic_question_set_question_ordering_by_date'),
303  $this->lng->txt('tst_inp_dyn_quest_set_quest_ordering_by_date_desc')
304  );
305  $questionOderingInput->addOption($optionOrderByDate);
306  $optionOrderByTax = new ilRadioOption(
307  $this->lng->txt('tst_input_dynamic_question_set_question_ordering_by_tax'),
309  $this->lng->txt('tst_inp_dyn_quest_set_quest_ordering_by_tax_desc')
310  );
311  $orderTaxInput = new ilSelectInputGUI($this->lng->txt('tst_input_dynamic_question_set_ordering_tax'), 'ordering_tax');
312  $orderTaxInput->setInfo($this->lng->txt('tst_input_dynamic_question_set_ordering_tax_description'));
313  $orderTaxInput->setValue($this->questionSetConfig->getOrderingTaxonomyId());
314  $orderTaxInput->setRequired(true);
315  $orderTaxInput->setOptions($this->buildTaxonomySelectInputOptionArray(
316  $this->questionSetConfig->getSourceQuestionPoolId()
317  ));
318  $optionOrderByTax->addSubItem($orderTaxInput);
319  $questionOderingInput->addOption($optionOrderByTax);
320  $form->addItem($questionOderingInput);
321 
322  $taxFilterInput = new ilCheckboxInputGUI($this->lng->txt('tst_input_dynamic_question_set_taxonomie_filter_enabled'), 'tax_filter_enabled');
323  $taxFilterInput->setValue(1);
324  $taxFilterInput->setChecked( $this->questionSetConfig->isTaxonomyFilterEnabled() );
325  $form->addItem($taxFilterInput);
326 
327  $answStatusFilterInput = new ilCheckboxInputGUI(
328  $this->lng->txt('tst_input_dyn_quest_set_answer_status_filter_enabled'), 'answer_status_filter_enabled'
329  );
330  $answStatusFilterInput->setValue(1);
331  $answStatusFilterInput->setChecked( $this->questionSetConfig->isAnswerStatusFilterEnabled() );
332  $form->addItem($answStatusFilterInput);
333 
334  $previousQuestionsListInput = new ilCheckboxInputGUI(
335  $this->lng->txt('tst_input_dyn_quest_set_prev_quest_list_enabled'), 'prev_quest_list_enabled'
336  );
337  $previousQuestionsListInput->setValue(1);
338  $previousQuestionsListInput->setChecked( $this->questionSetConfig->isPreviousQuestionsListEnabled() );
339  $form->addItem($previousQuestionsListInput);
340 
341  if( $this->testOBJ->participantDataExist() )
342  {
343  $questionOderingInput->setDisabled(true);
344  $taxFilterInput->setDisabled(true);
345  $answStatusFilterInput->setDisabled(true);
346  $previousQuestionsListInput->setDisabled(true);
347  }
348  else
349  {
350  $form->addCommandButton(self::CMD_SAVE_FORM, $this->lng->txt('save'));
351  }
352 
353  return $form;
354  }
355 
362  private function buildQuestionPoolSelectInputOptionArray($questionPoolsData)
363  {
364  $questionPoolSelectInputOptions = array( '' => $this->lng->txt('please_select') );
365 
366  foreach($questionPoolsData as $qplId => $qplData)
367  {
368  $questionPoolSelectInputOptions[$qplId] = $qplData['title'];
369  }
370 
371  return $questionPoolSelectInputOptions;
372  }
373 
374  private function buildTaxonomySelectInputOptionArray($questionPoolId)
375  {
376  $taxSelectOptions = array(
377  0 => $this->lng->txt('please_select')
378  );
379 
380  if( $questionPoolId )
381  {
382  require_once 'Services/Taxonomy/classes/class.ilObjTaxonomy.php';
383 
384  $taxIds = ilObjTaxonomy::getUsageOfObject($questionPoolId);
385 
386  foreach($taxIds as $taxId)
387  {
388  $taxSelectOptions[$taxId] = ilObject::_lookupTitle($taxId);
389  }
390  }
391 
392  return $taxSelectOptions;
393  }
394 
395  private function buildTaxonomySelectInputOptionJson($questionPoolId)
396  {
397  $options = array();
398 
399  foreach($this->buildTaxonomySelectInputOptionArray($questionPoolId) as $optValue => $optLabel)
400  {
401  $options[] = array('value' => $optValue, 'label' => $optLabel);
402  }
403 
404  return json_encode($options);
405  }
406 }