ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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("ELEMENT_VALUE", ilLegacyFormElementsUtil::prepareFormOutput($element));
175 $template->parseCurrentBlock();
176 }
177
178 if (($active_id > 0) && (!$show_correct_solution)) {
179 if ($this->object->getStep() === null) {
180 $reached_points = $this->object->getReachedPoints($active_id, $pass);
181 } else {
182 $reached_points = $this->object->calculateReachedPoints($active_id, $pass);
183 }
184 if ($graphical_output) {
185 $correctness_icon = $this->generateCorrectnessIconsForCorrectness(self::CORRECTNESS_NOT_OK);
186 if ($reached_points == $this->object->getMaximumPoints()) {
187 $correctness_icon = $this->generateCorrectnessIconsForCorrectness(self::CORRECTNESS_OK);
188 } elseif ($reached_points > 0) {
189 $correctness_icon = $this->generateCorrectnessIconsForCorrectness(self::CORRECTNESS_MOSTLY_OK);
190 }
191 $template->setCurrentBlock("icon_ok");
192 $template->setVariable("ICON_OK", $correctness_icon);
193 $template->parseCurrentBlock();
194 }
195 } else {
196 $reached_points = $this->object->getPoints();
197 }
198
199 if ($result_output) {
200 $resulttext = ($reached_points == 1) ? "(%s " . $this->lng->txt("point") . ")" : "(%s " . $this->lng->txt("points") . ")";
201 $template->setVariable("RESULT_OUTPUT", sprintf($resulttext, $reached_points));
202 }
203 if ($show_question_text == true) {
204 $template->setVariable("QUESTIONTEXT", $this->renderLatex($this->object->getQuestionForHTMLOutput()));
205 }
206 // $template->setVariable("SOLUTION_TEXT", ilUtil::prepareFormOutput($solutionvalue));
207 if ($this->object->getTextSize() >= 10) {
208 $template->setVariable("STYLE", " style=\"font-size: " . $this->object->getTextSize() . "%;\"");
209 }
210
211 $questionoutput = $template->get();
212 $solutiontemplate = new ilTemplate("tpl.il_as_tst_solution_output.html", true, true, "components/ILIAS/TestQuestionPool");
213 $solutiontemplate->setVariable("SOLUTION_OUTPUT", $questionoutput);
214
215
216 $feedback = '';
217 if ($show_feedback) {
218 if (!$this->isTestPresentationContext()) {
219 $fb = $this->getGenericFeedbackOutput((int) $active_id, $pass);
220 $feedback .= strlen($fb) ? $fb : '';
221 }
222 }
223 if (strlen($feedback)) {
224 $cssClass = (
225 $this->hasCorrectSolution($active_id, $pass) ?
227 );
228
229 $solutiontemplate->setVariable("ILC_FB_CSS_CLASS", $cssClass);
230 $solutiontemplate->setVariable("FEEDBACK", ilLegacyFormElementsUtil::prepareTextareaOutput($feedback, true));
231 }
232 $solutionoutput = $solutiontemplate->get();
233 if (!$show_question_only) {
234 // get page object output
235 $solutionoutput = $this->getILIASPage($solutionoutput);
236 }
237 return $solutionoutput;
238 }
239
240
241 public function getPreview(
242 bool $show_question_only = false,
243 bool $show_inline_feedback = false
244 ): string {
245 $template = new ilTemplate('tpl.il_as_qpl_orderinghorizontal_output.html', true, true, 'components/ILIAS/TestQuestionPool');
246 $this->initializePlayerJS();
247
248 if ($this->getPreviewSession() !== null
249 && $this->getPreviewSession()->getParticipantsSolution() !== null) {
250 $elements = $this->object->splitAndTrimOrderElementText(
251 (string) $this->getPreviewSession()->getParticipantsSolution(),
252 $this->object->getAnswerSeparator()
253 );
254 } else {
255 $elements = $this->object->getRandomOrderingElements();
256 }
257
258 foreach ($elements as $id => $element) {
259 $template->setCurrentBlock('element');
260 $template->setVariable('ELEMENT_ID', "e_{$this->object->getId()}_{$id}");
261 $template->setVariable('ELEMENT_VALUE', ilLegacyFormElementsUtil::prepareFormOutput($element));
262 $template->parseCurrentBlock();
263 }
264 $template->setVariable('QUESTION_ID', $this->object->getId());
265 $template->setVariable('VALUE_ORDERRESULT', ' value="' . join('{::}', $elements) . '"');
266 if ($this->object->getTextSize() >= 10) {
267 $template->setVariable('STYLE', ' style="font-size: ' . $this->object->getTextSize() . '%;"');
268 }
269 $template->setVariable('QUESTIONTEXT', $this->renderLatex(
270 $this->object->getQuestionForHTMLOutput()
271 ));
272 if ($show_question_only) {
273 return $template->get();
274 }
275 return $this->getILIASPage($template->get());
276 }
277
278 public function getTestOutput(
279 int $active_id,
280 int $pass,
281 bool $is_question_postponed = false,
282 array|bool $user_post_solutions = false,
283 bool $show_specific_inline_feedback = false
284 ): string {
285 $template = new ilTemplate('tpl.il_as_qpl_orderinghorizontal_output.html', true, true, 'components/ILIAS/TestQuestionPool');
286 $this->initializePlayerJS();
287
288 $elements = $this->object->getRandomOrderingElements();
289 if ($active_id) {
290 $solutions = $this->object->getTestOutputSolutions($active_id, $pass);
291 if (count($solutions) == 1) {
292 $elements = explode('{::}', $solutions[0]['value1']);
293 }
294 }
295 if (!is_array($solutions) || count($solutions) == 0) {
296 ilSession::set('qst_ordering_horizontal_elements', $elements);
297 } else {
298 ilSession::clear('qst_ordering_horizontal_elements');
299 }
300 foreach ($elements as $id => $element) {
301 $template->setCurrentBlock('element');
302 $template->setVariable('ELEMENT_ID', "e_{$this->object->getId()}_{$id}");
303 $template->setVariable('ELEMENT_VALUE', ilLegacyFormElementsUtil::prepareFormOutput($element));
304 $template->parseCurrentBlock();
305 }
306 $template->setVariable('QUESTION_ID', $this->object->getId());
307 if ($this->object->getTextSize() >= 10) {
308 $template->setVariable('STYLE', ' style="font-size: ' . $this->object->getTextSize() . '%;"');
309 }
310 $template->setVariable('VALUE_ORDERRESULT', ' value="' . join('{::}', $elements) . '"');
311 $template->setVariable('QUESTIONTEXT', $this->renderLatex($this->object->getQuestionForHTMLOutput()));
312 return $this->outQuestionPage("", $is_question_postponed, $active_id, $template->get());
313 }
314
315 public function getSpecificFeedbackOutput(array $userSolution): string
316 {
317 return '';
318 }
319
321 {
322 $this->object->setTextSize($this->request_data_collector->float('textsize'));
323 $this->object->setOrderText($this->request_data_collector->raw('ordertext'));
324 $this->object->setPoints($this->request_data_collector->float('points'));
325 }
326
337 {
338 return [];
339 }
340
342 {
343 // ordertext
344 $ordertext = new ilTextAreaInputGUI($this->lng->txt("ordertext"), "ordertext");
345 $ordertext->setValue((string) self::prepareTextareaOutput($this->object->getOrderText(), false, true));
346 $ordertext->setRequired(true);
347 $ordertext->setInfo(sprintf($this->lng->txt("ordertext_info"), $this->object->getSeparator()));
348 $ordertext->setRows(10);
349 $ordertext->setCols(80);
350 $form->addItem($ordertext);
351 // textsize
352 $textsize = new ilNumberInputGUI($this->lng->txt("textsize"), "textsize");
353 $textsize->setValue($this->object->getTextSize());
354 $textsize->setInfo($this->lng->txt("textsize_info"));
355 $textsize->setSize(6);
356 $textsize->setMinValue(10);
357 $textsize->setRequired(false);
358 $form->addItem($textsize);
359 // points
360 $points = new ilNumberInputGUI($this->lng->txt("points"), "points");
361
362 $points->allowDecimals(true);
363 // mbecker: Fix for mantis bug 7866: Predefined values schould make sense.
364 // This implements a default value of "1" for this question type.
365 if ($this->object->getPoints() == null) {
366 $points->setValue("1");
367 } else {
368 $points->setValue($this->object->getPoints());
369 }
370 $points->setRequired(true);
371 $points->setSize(3);
372 $points->setMinValue(0.0);
373 $points->setMinvalueShouldBeGreater(true);
374 $form->addItem($points);
375 return $form;
376 }
377
378 public function getAnswersFrequency($relevantAnswers, $questionIndex): array
379 {
380 $answers = [];
381
382 foreach ($relevantAnswers as $ans) {
383 $md5 = md5($ans['value1']);
384
385 if (!isset($answers[$md5])) {
386 $answer = str_replace(
387 $this->object->getAnswerSeparator(),
388 '&nbsp;&nbsp;-&nbsp;&nbsp;',
389 $ans['value1']
390 );
391
392 $answers[$md5] = [
393 'answer' => $answer, 'frequency' => 0
394 ];
395 }
396
397 $answers[$md5]['frequency']++;
398 }
399
400 return $answers;
401 }
402
404 {
405 // points
406 $points = new ilNumberInputGUI($this->lng->txt("points"), "points");
407
408 $points->allowDecimals(true);
409 $points->setValue($this->object->getPoints());
410 $points->setRequired(true);
411 $points->setSize(3);
412 $points->setMinValue(0.0);
413 $points->setMinvalueShouldBeGreater(true);
414 $form->addItem($points);
415 }
416
418 {
419 $this->object->setPoints((float) str_replace(',', '.', $form->getInput('points')));
420 }
421
422 private function initializePlayerJS(): void
423 {
424 $this->tpl->addJavascript('assets/js/orderinghorizontal.js');
425 $this->tpl->addOnLoadCode(
426 "il.test.orderinghorizontal.init(document.querySelector('#horizontal_{$this->object->getId()}'));"
427 );
428 }
429}
$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