ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
class.assOrderingHorizontalGUI.php
Go to the documentation of this file.
1<?php
2
34{
43 public function __construct($id = -1)
44 {
46 $this->object = new assOrderingHorizontal();
47 $this->setErrorMessage($this->lng->txt("msg_form_save_error"));
48 if ($id >= 0) {
49 $this->object->loadFromDb($id);
50 }
51 }
52
53 public function getCommand($cmd)
54 {
55 return $cmd;
56 }
57
61 protected function writePostData(bool $always = false): int
62 {
63 $hasErrors = (!$always) ? $this->editQuestion(true) : false;
64 if (!$hasErrors) {
68 return 0;
69 }
70 return 1;
71 }
72
73 public function editQuestion(
74 bool $checkonly = false,
75 ?bool $is_save_cmd = null
76 ): bool {
77 $save = $is_save_cmd ?? $this->isSaveCommand();
78
79 $form = new ilPropertyFormGUI();
80 $this->editForm = $form;
81
82 $form->setFormAction($this->ctrl->getFormAction($this));
83 $form->setTitle($this->outQuestionType());
84 $form->setMultipart(false);
85 $form->setTableWidth("100%");
86 $form->setId("orderinghorizontal");
87
90
91
92 $this->populateTaxonomyFormSection($form);
93
95
96 $errors = false;
97
98 if ($save) {
99 $form->setValuesByPost();
100 $errors = !$form->checkInput();
101 $form->setValuesByPost(); // again, because checkInput now performs the whole stripSlashes handling and we need this if we don't want to have duplication of backslashes
102 if ($errors) {
103 $checkonly = false;
104 }
105 }
106
107 if (!$checkonly) {
108 $this->renderEditForm($form);
109 }
110 return $errors;
111 }
112
113 public function getSolutionOutput(
114 int $active_id,
115 ?int $pass = null,
116 bool $graphical_output = false,
117 bool $result_output = false,
118 bool $show_question_only = true,
119 bool $show_feedback = false,
120 bool $show_correct_solution = false,
121 bool $show_manual_scoring = false,
122 bool $show_question_text = true,
123 bool $show_inline_feedback = true
124 ): string {
125 $user_solutions = [];
126 if (($active_id > 0) && (!$show_correct_solution)) {
127 $user_solutions = $this->object->getSolutionValues($active_id, $pass);
128 }
129
130 return $this->renderSolutionOutput(
131 $user_solutions,
132 $active_id,
133 $pass,
134 $graphical_output,
135 $result_output,
136 $show_question_only,
137 $show_feedback,
138 $show_correct_solution,
139 $show_manual_scoring,
140 $show_question_text,
141 false,
142 $show_inline_feedback,
143 );
144 }
145
146 public function renderSolutionOutput(
147 mixed $user_solutions,
148 int $active_id,
149 ?int $pass,
150 bool $graphical_output = false,
151 bool $result_output = false,
152 bool $show_question_only = true,
153 bool $show_feedback = false,
154 bool $show_correct_solution = false,
155 bool $show_manual_scoring = false,
156 bool $show_question_text = true,
157 bool $show_autosave_title = false,
158 bool $show_inline_feedback = false,
159 ): ?string {
160 $template = new ilTemplate("tpl.il_as_qpl_orderinghorizontal_output_solution.html", true, true, "components/ILIAS/TestQuestionPool");
161
162 $elements = [];
163 if (count($user_solutions) && strlen($user_solutions[0]["value1"])) {
164 $elements = explode("{::}", $user_solutions[0]["value1"]);
165 }
166
167 if (!count($elements)) {
168 $elements = $show_correct_solution ? $this->object->getOrderingElements() : $this->object->getRandomOrderingElements();
169 }
170
171 foreach ($elements as $id => $element) {
172 $template->setCurrentBlock("element");
173 $template->setVariable("ELEMENT_ID", "sol_e_" . $this->object->getId() . "_$id");
174 $template->setVariable(
175 "ELEMENT_VALUE",
176 $this->renderLatex(
178 )
179 );
180 $template->parseCurrentBlock();
181 }
182
183 if (($active_id > 0) && (!$show_correct_solution)) {
184 if ($this->object->getStep() === null) {
185 $reached_points = $this->object->getReachedPoints($active_id, $pass);
186 } else {
187 $reached_points = $this->object->calculateReachedPoints($active_id, $pass);
188 }
189 if ($graphical_output) {
190 $correctness_icon = $this->generateCorrectnessIconsForCorrectness(self::CORRECTNESS_NOT_OK);
191 if ($reached_points == $this->object->getMaximumPoints()) {
192 $correctness_icon = $this->generateCorrectnessIconsForCorrectness(self::CORRECTNESS_OK);
193 } elseif ($reached_points > 0) {
194 $correctness_icon = $this->generateCorrectnessIconsForCorrectness(self::CORRECTNESS_MOSTLY_OK);
195 }
196 $template->setCurrentBlock("icon_ok");
197 $template->setVariable("ICON_OK", $correctness_icon);
198 $template->parseCurrentBlock();
199 }
200 } else {
201 $reached_points = $this->object->getPoints();
202 }
203
204 if ($result_output) {
205 $resulttext = ($reached_points == 1) ? "(%s " . $this->lng->txt("point") . ")" : "(%s " . $this->lng->txt("points") . ")";
206 $template->setVariable("RESULT_OUTPUT", sprintf($resulttext, $reached_points));
207 }
208 if ($show_question_text == true) {
209 $template->setVariable("QUESTIONTEXT", $this->renderLatex($this->object->getQuestionForHTMLOutput()));
210 }
211 // $template->setVariable("SOLUTION_TEXT", ilUtil::prepareFormOutput($solutionvalue));
212 if ($this->object->getTextSize() >= 10) {
213 $template->setVariable("STYLE", " style=\"font-size: " . $this->object->getTextSize() . "%;\"");
214 }
215
216 $questionoutput = $template->get();
217 $solutiontemplate = new ilTemplate("tpl.il_as_tst_solution_output.html", true, true, "components/ILIAS/TestQuestionPool");
218 $solutiontemplate->setVariable("SOLUTION_OUTPUT", $questionoutput);
219
220
221 $feedback = '';
222 if ($show_feedback) {
223 if (!$this->isTestPresentationContext()) {
224 $fb = $this->getGenericFeedbackOutput((int) $active_id, $pass);
225 $feedback .= strlen($fb) ? $fb : '';
226 }
227 }
228 if (strlen($feedback)) {
229 $cssClass = (
230 $this->hasCorrectSolution($active_id, $pass) ?
232 );
233
234 $solutiontemplate->setVariable("ILC_FB_CSS_CLASS", $cssClass);
235 $solutiontemplate->setVariable("FEEDBACK", ilLegacyFormElementsUtil::prepareTextareaOutput($feedback, true));
236 }
237 $solutionoutput = $solutiontemplate->get();
238 if (!$show_question_only) {
239 // get page object output
240 $solutionoutput = $this->getILIASPage($solutionoutput);
241 }
242 return $solutionoutput;
243 }
244
245
246 public function getPreview(
247 bool $show_question_only = false,
248 bool $show_inline_feedback = false
249 ): string {
250 $template = new ilTemplate('tpl.il_as_qpl_orderinghorizontal_output.html', true, true, 'components/ILIAS/TestQuestionPool');
251 $this->initializePlayerJS();
252
253 if ($this->getPreviewSession() !== null
254 && $this->getPreviewSession()->getParticipantsSolution() !== null) {
255 $elements = $this->object->splitAndTrimOrderElementText(
256 (string) $this->getPreviewSession()->getParticipantsSolution(),
257 $this->object->getAnswerSeparator()
258 );
259 } else {
260 $elements = $this->object->getRandomOrderingElements();
261 }
262
263 foreach ($elements as $id => $element) {
264 $template->setCurrentBlock('element');
265 $template->setVariable('ELEMENT_ID', "e_{$this->object->getId()}_{$id}");
266 $template->setVariable('ELEMENT_VALUE', ilLegacyFormElementsUtil::prepareFormOutput($element));
267 $template->parseCurrentBlock();
268 }
269 $template->setVariable('QUESTION_ID', $this->object->getId());
270 $template->setVariable('VALUE_ORDERRESULT', ' value="' . join('{::}', $elements) . '"');
271 if ($this->object->getTextSize() >= 10) {
272 $template->setVariable('STYLE', ' style="font-size: ' . $this->object->getTextSize() . '%;"');
273 }
274 $template->setVariable('QUESTIONTEXT', $this->renderLatex(
275 $this->object->getQuestionForHTMLOutput()
276 ));
277 if ($show_question_only) {
278 return $template->get();
279 }
280 return $this->getILIASPage($template->get());
281 }
282
283 public function getTestOutput(
284 int $active_id,
285 int $pass,
286 bool $is_question_postponed = false,
287 array|bool $user_post_solutions = false,
288 bool $show_specific_inline_feedback = false
289 ): string {
290 $template = new ilTemplate('tpl.il_as_qpl_orderinghorizontal_output.html', true, true, 'components/ILIAS/TestQuestionPool');
291 $this->initializePlayerJS();
292
293 $elements = $this->object->getRandomOrderingElements();
294 if ($active_id) {
295 $solutions = $this->object->getTestOutputSolutions($active_id, $pass);
296 if (count($solutions) == 1) {
297 $elements = explode('{::}', $solutions[0]['value1']);
298 }
299 }
300 if (!is_array($solutions) || count($solutions) == 0) {
301 ilSession::set('qst_ordering_horizontal_elements', $elements);
302 } else {
303 ilSession::clear('qst_ordering_horizontal_elements');
304 }
305 foreach ($elements as $id => $element) {
306 $template->setCurrentBlock('element');
307 $template->setVariable('ELEMENT_ID', "e_{$this->object->getId()}_{$id}");
308 $template->setVariable('ELEMENT_VALUE', ilLegacyFormElementsUtil::prepareFormOutput($element));
309 $template->parseCurrentBlock();
310 }
311 $template->setVariable('QUESTION_ID', $this->object->getId());
312 if ($this->object->getTextSize() >= 10) {
313 $template->setVariable('STYLE', ' style="font-size: ' . $this->object->getTextSize() . '%;"');
314 }
315 $template->setVariable('VALUE_ORDERRESULT', ' value="' . join('{::}', $elements) . '"');
316 $template->setVariable('QUESTIONTEXT', $this->renderLatex($this->object->getQuestionForHTMLOutput()));
317 return $this->outQuestionPage("", $is_question_postponed, $active_id, $template->get());
318 }
319
320 public function getSpecificFeedbackOutput(array $userSolution): string
321 {
322 return '';
323 }
324
326 {
327 $this->object->setTextSize($this->request_data_collector->float('textsize'));
328 $this->object->setOrderText($this->request_data_collector->raw('ordertext'));
329 $this->object->setPoints($this->request_data_collector->float('points'));
330 }
331
342 {
343 return [];
344 }
345
347 {
348 // ordertext
349 $ordertext = new ilTextAreaInputGUI($this->lng->txt("ordertext"), "ordertext");
350 $ordertext->setValue((string) self::prepareTextareaOutput($this->object->getOrderText(), false, true));
351 $ordertext->setRequired(true);
352 $ordertext->setInfo(sprintf($this->lng->txt("ordertext_info"), $this->object->getSeparator()));
353 $ordertext->setRows(10);
354 $ordertext->setCols(80);
355 $form->addItem($ordertext);
356 // textsize
357 $textsize = new ilNumberInputGUI($this->lng->txt("textsize"), "textsize");
358 $textsize->setValue($this->object->getTextSize());
359 $textsize->setInfo($this->lng->txt("textsize_info"));
360 $textsize->setSize(6);
361 $textsize->setMinValue(10);
362 $textsize->setRequired(false);
363 $form->addItem($textsize);
364 // points
365 $points = new ilNumberInputGUI($this->lng->txt("points"), "points");
366
367 $points->allowDecimals(true);
368 // mbecker: Fix for mantis bug 7866: Predefined values schould make sense.
369 // This implements a default value of "1" for this question type.
370 if ($this->object->getPoints() == null) {
371 $points->setValue("1");
372 } else {
373 $points->setValue($this->object->getPoints());
374 }
375 $points->setRequired(true);
376 $points->setSize(3);
377 $points->setMinValue(0.0);
378 $points->setMinvalueShouldBeGreater(true);
379 $form->addItem($points);
380 return $form;
381 }
382
383 public function getAnswersFrequency($relevantAnswers, $questionIndex): array
384 {
385 $answers = [];
386
387 foreach ($relevantAnswers as $ans) {
388 $md5 = md5($ans['value1']);
389
390 if (!isset($answers[$md5])) {
391 $answer = str_replace(
392 $this->object->getAnswerSeparator(),
393 '&nbsp;&nbsp;-&nbsp;&nbsp;',
394 $ans['value1']
395 );
396
397 $answers[$md5] = [
398 'answer' => $answer,
399 'frequency' => 0
400 ];
401 }
402
403 $answers[$md5]['frequency']++;
404 }
405
406 return $answers;
407 }
408
410 {
411 // points
412 $points = new ilNumberInputGUI($this->lng->txt("points"), "points");
413
414 $points->allowDecimals(true);
415 $points->setValue($this->object->getPoints());
416 $points->setRequired(true);
417 $points->setSize(3);
418 $points->setMinValue(0.0);
419 $points->setMinvalueShouldBeGreater(true);
420 $form->addItem($points);
421 }
422
424 {
425 $this->object->setPoints((float) str_replace(',', '.', $form->getInput('points')));
426 }
427
428 private function initializePlayerJS(): void
429 {
430 $this->tpl->addJavascript('assets/js/orderinghorizontal.js');
431 $this->tpl->addOnLoadCode(
432 "il.test.orderinghorizontal.init(document.querySelector('#horizontal_{$this->object->getId()}'));"
433 );
434 }
435}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
return true
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getSolutionOutput(int $active_id, ?int $pass=null, bool $graphical_output=false, bool $result_output=false, bool $show_question_only=true, bool $show_feedback=false, bool $show_correct_solution=false, bool $show_manual_scoring=false, bool $show_question_text=true, bool $show_inline_feedback=true)
populateCorrectionsFormProperties(ilPropertyFormGUI $form)
writePostData(bool $always=false)
{Evaluates a posted edit form and writes the form data in the question object.integer A positive valu...
renderSolutionOutput(mixed $user_solutions, int $active_id, ?int $pass, bool $graphical_output=false, bool $result_output=false, bool $show_question_only=true, bool $show_feedback=false, bool $show_correct_solution=false, bool $show_manual_scoring=false, bool $show_question_text=true, bool $show_autosave_title=false, bool $show_inline_feedback=false,)
editQuestion(bool $checkonly=false, ?bool $is_save_cmd=null)
saveCorrectionsFormProperties(ilPropertyFormGUI $form)
getTestOutput(int $active_id, int $pass, bool $is_question_postponed=false, array|bool $user_post_solutions=false, bool $show_specific_inline_feedback=false)
getPreview(bool $show_question_only=false, bool $show_inline_feedback=false)
writeQuestionSpecificPostData(ilPropertyFormGUI $form)
Extracts the question specific values from the request and applies them to the data object.
populateQuestionSpecificFormPart(\ilPropertyFormGUI $form)
getAnswersFrequency($relevantAnswers, $questionIndex)
__construct($id=-1)
assOrderingHorizontalGUI constructor
getAfterParticipationSuppressionQuestionPostVars()
Returns a list of postvars which will be suppressed in the form output when used in scoring adjustmen...
getSpecificFeedbackOutput(array $userSolution)
Returns the answer specific feedback for the question.
Class for horizontal ordering questions.
populateTaxonomyFormSection(ilPropertyFormGUI $form)
addBasicQuestionFormProperties(ilPropertyFormGUI $form)
renderEditForm(ilPropertyFormGUI $form)
addQuestionFormCommandButtons(ilPropertyFormGUI $form)
setErrorMessage(string $errormessage)
static prepareTextareaOutput(string $txt_output, bool $prepare_for_latex_output=false, bool $omitNl2BrWhenTextArea=false)
Prepares a string for a text area output where latex code may be in it If the text is HTML-free,...
static prepareFormOutput($a_str, bool $a_strip=false)
This class represents a number property in a property form.
This class represents a property form user interface.
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
static clear(string $a_var)
static set(string $a_var, $a_val)
Set a value.
special template class to simplify handling of ITX/PEAR
This class represents a text area property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc