ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilClozeGapInputBuilderGUI.php
Go to the documentation of this file.
1<?php
2
21
23{
25 private $value;
28
29 public function __construct(string $a_title = "", string $a_postvar = "")
30 {
31 parent::__construct($a_title, $a_postvar);
32 $this->post = $this->http->wrapper()->post();
33 $this->query = $this->http->wrapper()->query();
34 }
35
40 public function setValue($a_value): void
41 {
42 $this->value = $a_value;
43 }
44
48 public function getValue()
49 {
50 $editOrOpen = $this->value;
51 if (isset($editOrOpen['author'])) {
52 $json = json_decode($this->post->retrieve(
53 "gap_json_post",
54 $this->refinery->kindlyTo()->string()
55 ), true, 512, JSON_THROW_ON_ERROR);
56 return $json[0];
57 }
58 return $this->value;
59 }
60
61 public function setValueCombination($value): void
62 {
63 $this->value_combination = $value;
64 }
65
69 public function getValueCombination()
70 {
71 $editOrOpen = $this->value;
72 if (isset($editOrOpen['author'])) {
73 return json_decode($this->post->retrieve(
74 "gap_json_combination_post",
75 $this->refinery->kindlyTo()->string()
76 ), true, 512, JSON_THROW_ON_ERROR);
77 }
78 return (array) $this->value_combination;
79 }
80
81 public function setValueCombinationFromDb($value): void
82 {
83 $return_array = [];
84 if ($value) {
85 foreach ($value as $row) {
86 if ($row['row_id'] == 0) {
87 $return_array[$row['cid']][0][] = $row['gap_fi'];
88 }
89 $return_array[$row['cid']][1][$row['row_id']][] = $row['answer'];
90 $return_array[$row['cid']][2][$row['row_id']] = $row['points']; //= array('key' => $row['cid'], 'points' => $row['points'], 'best_solution' => $row['best_solution']);
91 }
92 $this->setValueCombination($return_array);
93 }
94 }
95
99 public function checkInput(): bool
100 {
101 $error = false;
103 json_decode(
104 $this->post->retrieve(
105 "gap_json_post",
106 $this->refinery->kindlyTo()->string()
107 ),
108 true,
109 512,
110 JSON_THROW_ON_ERROR
111 ),
112 false
113 );
114 $gap = self::stripSlashesRecursive($this->raw('gap'));
115 $gaps_used_in_combination = [];
116 if ($this->post->has('gap_combination')) {
117 $gapCombination = $this->post->retrieve(
118 "gap_combination",
119 $this->refinery->kindlyTo()->recordOf(
120 [
121 'select' => new ListTransformation(
122 new ListTransformation($this->refinery->kindlyTo()->string())
123 ),
124 'points' => new ListTransformation(
125 new ListTransformation($this->refinery->kindlyTo()->string())
126 ),
127 ]
128 )
129 );
130 $gapCombinationValues = $this->post->retrieve(
131 "gap_combination_values",
132 $this->refinery->kindlyTo()->listOf(
134 new ListTransformation($this->refinery->kindlyTo()->string())
135 )
136 )
137 );
138
139 $gap_with_points = [];
140
141 for ($i = 0, $iMax = count($gapCombination['select']); $i < $iMax; $i++) {
142 foreach ($gapCombination['select'][$i] as $key => $item) {
143 if ($item == 'none_selected_minus_one') {
144 return false;
145 }
146 $gaps_used_in_combination[$item] = $item;
147 $check_points_for_best_scoring = false;
148 foreach ($gapCombinationValues[$i] as $index => $answeritems) {
149 foreach ($answeritems as $answeritem) {
150 if ($answeritem == 'none_selected_minus_one') {
151 return false;
152 }
153 }
154 $points = $gapCombination['points'][$i][$index];
155 if ($points > 0) {
156 $check_points_for_best_scoring = true;
157 }
158 }
159 if (!$check_points_for_best_scoring) {
160 return false;
161 }
162 }
163 }
164 }
165
166 if (is_array($gap)) {
167 foreach ($gap as $key => $item) {
168 $getType = ilUtil::stripSlashes($this->raw('clozetype_' . $key));
169 $gapsize = $this->raw('gap_' . $key . '_gapsize');
170
171 $json[0][$key]['text_field_length'] = $gapsize > 0 ? $gapsize : '';
172
173 $select_at_least_on_positive = false;
174 if ($getType == assClozeGap::TYPE_TEXT || $getType == assClozeGap::TYPE_SELECT) {
175 $gapText = self::stripSlashesRecursive($this->raw('gap_' . $key), false);
176 foreach ($gapText['answer'] as $row => $answer) {
177 if (!isset($answer) || $answer == '') {
178 $error = true;
179 }
180 }
181 $points_sum = 0;
182 if (array_key_exists('points', $gapText)) {
183 foreach ($gapText['points'] as $row => $points) {
184 $points = str_replace(',', '.', $points);
185 if (isset($points) && $points != '' && is_numeric($points)) {
186 $points_sum += $points;
187 if ($points > 0) {
188 $select_at_least_on_positive = true;
189 }
190 } else {
191 $error = true;
192 }
193 }
194 if (isset($gap_with_points) && is_array($gap_with_points) && array_key_exists($key, $gap_with_points)) {
195 $points_sum += $gap_with_points[$key];
196 }
197 if ($points_sum <= 0) {
198 if (!array_key_exists($key, $gaps_used_in_combination) && (!$getType == 'select' || $select_at_least_on_positive == false)) {
199 $error = true;
200 }
201 }
202 } else {
203 $error = true;
204 }
205 }
206 if ($getType == assClozeGap::TYPE_NUMERIC) {
207 $eval = new EvalMath();
208 $eval->suppress_errors = true;
209
210 $mark_errors = ['answer' => false, 'lower' => false, 'upper' => false, 'points' => false];
211 foreach ([ 'answer' => '_numeric',
212 'lower' => '_numeric_lower',
213 'upper' => '_numeric_upper',
214 'points' => '_numeric_points'] as $part => $suffix) {
215 $val = ilUtil::stripSlashes($this->raw('gap_' . $key . $suffix) ?? '', false);
216 $val = str_replace(',', '.', $val);
217 if ($eval->e($val) === false) {
218 $mark_errors[$part] = true;
219 $error = true;
220 }
221
222 if ($part == 'points') {
223 $points = $val;
224 }
225 }
226
227 if (isset($gap_with_points) && is_array($gap_with_points) && array_key_exists($key, $gap_with_points)) {
228 $points += $gap_with_points[$key];
229 }
230
231 if (!isset($points) || $points == '' || !is_numeric($points) || $points == 0) {
232 if (!array_key_exists($key, $gaps_used_in_combination)) {
233 $error = true;
234 }
235 }
236
237 $json[0][$key]["values"][0]["error"] = $mark_errors;
238 }
239 }
240 }
241 return !$error;
242 }
243
244 public function setValueByArray($data): void
245 {
246 $this->setValue($data);
247 }
248
252 public function insert(ilTemplate $template): void
253 {
255 global $DIC;
256 $lng = $DIC['lng'];
257 $glyph_factory = $DIC->ui()->factory()->symbol()->glyph();
258 $renderer = $DIC->ui()->renderer();
259
260 $cloze_settings_js = 'ClozeSettings = {'
261 . ' gaps_php : ' . json_encode([$this->getValue()])
262 . ',gaps_combination : ' . json_encode($this->getValueCombination())
263 . ',gap_backup : []'
264 . ',unused_gaps_comb : []'
265 . ',outofbound_text : ' . '"' . $lng->txt('out_of_range') . '"'
266 . ',inrange_text : ' . '"' . $lng->txt('in_range') . '"'
267 . ',combination_error : ' . '"' . $lng->txt('please_select') . '"'
268 . ',combination_text : ' . '"' . $lng->txt('gap_combination') . '"'
269 . ',copy_of_combination : ' . '"' . $lng->txt('copy_of') . ' ' . $lng->txt('gap_combination') . '"'
270 . ',gap_in_more_than_one : ' . '""'
271 . ',gap_text : ' . '"' . $lng->txt('gap') . '"'
272 . ',ok_text : ' . '"' . $lng->txt('ok') . '"'
273 . ',cancel_text : ' . '"' . $lng->txt('cancel') . '"'
274 . '};';
275
276 $DIC->ui()->mainTemplate()->addOnLoadCode(
277 $cloze_settings_js
278 . 'ClozeQuestionGapBuilder.Init();'
279 );
280 $DIC->ui()->mainTemplate()->addJavascript(
281 'assets/js/clozeQuestionGapBuilder.js'
282 );
283
284 $custom_template = new ilTemplate('tpl.il_as_cloze_gap_builder.html', true, true, 'components/ILIAS/TestQuestionPool');
285 $custom_template->setVariable('GAP_JSON', json_encode([$this->getValue()]));
286 $custom_template->setVariable('GAP', $lng->txt('gap'));
287 $custom_template->setVariable('GAP_COMBINATION_JSON', json_encode($this->getValueCombination()));
288 $custom_template->setVariable('TEXT_GAP', $lng->txt('text_gap'));
289 $custom_template->setVariable('SELECT_GAP', $lng->txt('select_gap'));
290 $custom_template->setVariable('NUMERIC_GAP', $lng->txt('numeric_gap'));
291 $custom_template->setVariable('GAP_SIZE', $lng->txt('cloze_fixed_textlength'));
292 $custom_template->setVariable('GAP_SIZE_INFO', $lng->txt('cloze_gap_size_info'));
293 $custom_template->setVariable('ANSWER_TEXT', $lng->txt('answer_text'));
294 $custom_template->setVariable('ANSWER_BYLINE', $lng->txt('cloze_answer_text_info'));
295 $custom_template->setVariable('POINTS', $lng->txt('points'));
296 $custom_template->setVariable('VALUE', $lng->txt('value'));
297 $custom_template->setVariable('UPPER_BOUND', $lng->txt('range_upper_limit'));
298 $custom_template->setVariable('LOWER_BOUND', $lng->txt('range_lower_limit'));
299 $custom_template->setVariable('ACTIONS', $lng->txt('actions'));
300 $custom_template->setVariable('REMOVE_GAP', $lng->txt('remove_gap'));
301 $custom_template->setVariable('SHUFFLE_ANSWERS', $lng->txt('shuffle_answers'));
302 $custom_template->setVariable('POINTS_ERROR', $lng->txt('enter_enough_positive_points'));
303 $custom_template->setVariable('MISSING_VALUE', $lng->txt('msg_input_is_required'));
304 $custom_template->setVariable('NOT_A_FORMULA', $lng->txt('err_no_formula'));
305 $custom_template->setVariable('NOT_A_NUMBER', $lng->txt('err_no_numeric_value'));
306 $custom_template->setVariable('CLOSE', $lng->txt('close'));
307 $custom_template->setVariable('DELETE_GAP', $lng->txt('are_you_sure'));
308 $custom_template->setVariable('PLEASE_SELECT', $lng->txt('please_select'));
309 $custom_template->setVariable('BEST_POSSIBLE_SOLUTION_HEADER', $lng->txt('tst_best_solution_is'));
310 $custom_template->setVariable('BEST_POSSIBLE_SOLUTION', $lng->txt('value'));
311 $custom_template->setVariable('MAX_POINTS', $lng->txt('max_points'));
312 $custom_template->setVariable('TYPE', $lng->txt('type'));
313 $custom_template->setVariable('VALUES', $lng->txt('values'));
314 $custom_template->setVariable('GAP_COMBINATION', $lng->txt('gap_combination'));
315 $custom_template->setVariable('COPY', $lng->txt('copy_of'));
316 $custom_template->setVariable('ADD_BUTTON', $renderer->render(
317 $glyph_factory->add()->withAction('#')
318 ));
319 $custom_template->setVariable('REMOVE_BUTTON', $renderer->render(
320 $glyph_factory->remove()->withAction('#')
321 ));
322 $template->setCurrentBlock('prop_generic');
323 $template->setVariable('PROP_GENERIC', $custom_template->get());
324 $template->parseCurrentBlock();
325 }
326
331 public static function stripSlashesRecursive($a_data, bool $a_strip_html = true, string $a_allow = "")
332 {
333 if (is_array($a_data)) {
334 foreach ($a_data as $k => $v) {
335 if (is_array($v)) {
336 $a_data[$k] = self::stripSlashesRecursive($v, $a_strip_html, $a_allow);
337 } else {
338 $a_data[$k] = ilUtil::stripSlashes($v, $a_strip_html, $a_allow);
339 }
340 }
341 } else {
342 if ($a_data != null) {
343 $a_data = ilUtil::stripSlashes($a_data, $a_strip_html, $a_allow);
344 }
345 }
346
347 return $a_data;
348 }
349}
$renderer
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
__construct(string $a_title="", string $a_postvar="")
static stripSlashesRecursive($a_data, bool $a_strip_html=true, string $a_allow="")
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
This class represents a property that may include a sub form.
special template class to simplify handling of ITX/PEAR
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
static http()
Fetches the global http state from ILIAS.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26