ILIAS  Release_4_4_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  $form->addCommandButton(self::CMD_SAVE_FORM, $this->lng->txt("save"));
266 
267  $form->setId("tst_form_dynamic_question_set_config");
268  $form->setTitle($this->lng->txt('tst_form_dynamic_question_set_config'));
269  $form->setTableWidth("100%");
270 
271  $hiddenInputTaxSelectOptAsyncUrl = new ilHiddenInputGUI('taxSelectOptAsyncUrl');
272  $hiddenInputTaxSelectOptAsyncUrl->setValue(
273  $this->ctrl->getLinkTarget($this, self::CMD_GET_TAXONOMY_OPTIONS_ASYNC, '', true)
274  );
275  $form->addItem($hiddenInputTaxSelectOptAsyncUrl);
276 
277  if( $this->testOBJ->participantDataExist() )
278  {
279  $pool = new ilNonEditableValueGUI($this->lng->txt('tst_input_dynamic_question_set_source_questionpool'), 'source_qpl_title');
280  $pool->setValue( $this->questionSetConfig->getSourceQuestionPoolSummaryString($this->lng, $this->tree) );
281  $pool->setDisabled(true);
282  $form->addItem($pool);
283  }
284  else
285  {
286  $poolInput = new ilSelectInputGUI($this->lng->txt('tst_input_dynamic_question_set_source_questionpool'), 'source_qpl_id');
288  $this->testOBJ->getAvailableQuestionpools(true, false, false, true, true)
289  ));
290  $poolInput->setValue( $this->questionSetConfig->getSourceQuestionPoolId() );
291  $poolInput->setRequired(true);
292  $form->addItem($poolInput);
293  }
294 
295  $questionOderingInput = new ilRadioGroupInputGUI(
296  $this->lng->txt('tst_input_dynamic_question_set_question_ordering'), 'question_ordering'
297  );
298  $questionOderingInput->setValue( $this->questionSetConfig->getOrderingTaxonomyId() ?
299  self::QUESTION_ORDERING_TYPE_TAXONOMY : self::QUESTION_ORDERING_TYPE_UPDATE_DATE
300  );
301  $optionOrderByDate = new ilRadioOption(
302  $this->lng->txt('tst_input_dynamic_question_set_question_ordering_by_date'),
304  $this->lng->txt('tst_inp_dyn_quest_set_quest_ordering_by_date_desc')
305  );
306  $questionOderingInput->addOption($optionOrderByDate);
307  $optionOrderByTax = new ilRadioOption(
308  $this->lng->txt('tst_input_dynamic_question_set_question_ordering_by_tax'),
310  $this->lng->txt('tst_inp_dyn_quest_set_quest_ordering_by_tax_desc')
311  );
312  $orderTaxInput = new ilSelectInputGUI($this->lng->txt('tst_input_dynamic_question_set_ordering_tax'), 'ordering_tax');
313  $orderTaxInput->setInfo($this->lng->txt('tst_input_dynamic_question_set_ordering_tax_description'));
314  $orderTaxInput->setValue($this->questionSetConfig->getOrderingTaxonomyId());
315  $orderTaxInput->setRequired(true);
316  $orderTaxInput->setOptions($this->buildTaxonomySelectInputOptionArray(
317  $this->questionSetConfig->getSourceQuestionPoolId()
318  ));
319  $optionOrderByTax->addSubItem($orderTaxInput);
320  $questionOderingInput->addOption($optionOrderByTax);
321  $form->addItem($questionOderingInput);
322 
323  $taxFilterInput = new ilCheckboxInputGUI($this->lng->txt('tst_input_dynamic_question_set_taxonomie_filter_enabled'), 'tax_filter_enabled');
324  $taxFilterInput->setValue(1);
325  $taxFilterInput->setChecked( $this->questionSetConfig->isTaxonomyFilterEnabled() );
326  $taxFilterInput->setRequired(true);
327  $form->addItem($taxFilterInput);
328 
329  $answStatusFilterInput = new ilCheckboxInputGUI(
330  $this->lng->txt('tst_input_dyn_quest_set_answer_status_filter_enabled'), 'answer_status_filter_enabled'
331  );
332  $answStatusFilterInput->setValue(1);
333  $answStatusFilterInput->setChecked( $this->questionSetConfig->isAnswerStatusFilterEnabled() );
334  $answStatusFilterInput->setRequired(true);
335  $form->addItem($answStatusFilterInput);
336 
337  $previousQuestionsListInput = new ilCheckboxInputGUI(
338  $this->lng->txt('tst_input_dyn_quest_set_prev_quest_list_enabled'), 'prev_quest_list_enabled'
339  );
340  $previousQuestionsListInput->setValue(1);
341  $previousQuestionsListInput->setChecked( $this->questionSetConfig->isPreviousQuestionsListEnabled() );
342  $previousQuestionsListInput->setRequired(true);
343  $form->addItem($previousQuestionsListInput);
344 
345  if( $this->testOBJ->participantDataExist() )
346  {
347  $questionOderingInput->setDisabled(true);
348  $taxFilterInput->setDisabled(true);
349  $answStatusFilterInput->setDisabled(true);
350  }
351 
352  return $form;
353  }
354 
361  private function buildQuestionPoolSelectInputOptionArray($questionPoolsData)
362  {
363  $questionPoolSelectInputOptions = array( '' => $this->lng->txt('please_select') );
364 
365  foreach($questionPoolsData as $qplId => $qplData)
366  {
367  $questionPoolSelectInputOptions[$qplId] = $qplData['title'];
368  }
369 
370  return $questionPoolSelectInputOptions;
371  }
372 
373  private function buildTaxonomySelectInputOptionArray($questionPoolId)
374  {
375  $taxSelectOptions = array(
376  0 => $this->lng->txt('please_select')
377  );
378 
379  if( $questionPoolId )
380  {
381  require_once 'Services/Taxonomy/classes/class.ilObjTaxonomy.php';
382 
383  $taxIds = ilObjTaxonomy::getUsageOfObject($questionPoolId);
384 
385  foreach($taxIds as $taxId)
386  {
387  $taxSelectOptions[$taxId] = ilObject::_lookupTitle($taxId);
388  }
389  }
390 
391  return $taxSelectOptions;
392  }
393 
394  private function buildTaxonomySelectInputOptionJson($questionPoolId)
395  {
396  $options = array();
397 
398  foreach($this->buildTaxonomySelectInputOptionArray($questionPoolId) as $optValue => $optLabel)
399  {
400  $options[] = array('value' => $optValue, 'label' => $optLabel);
401  }
402 
403  return json_encode($options);
404  }
405 }