ILIAS  release_8 Revision v8.24
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 = 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 = array();
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 = array();
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 $json[0][$key]['text_field_length'] = $gapsize > 0 ? $gapsize : '';
173
174 $select_at_least_on_positive = false;
175 if ($getType == CLOZE_TEXT || $getType == CLOZE_SELECT) {
176 $gapText = self::stripSlashesRecursive($this->raw('gap_' . $key), false);
177 foreach ($gapText['answer'] as $row => $answer) {
178 if (!isset($answer) || $answer == '') {
179 $error = true;
180 }
181 }
182 $points_sum = 0;
183 if (array_key_exists('points', $gapText)) {
184 foreach ($gapText['points'] as $row => $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 == CLOZE_NUMERIC) {
207 $eval = new EvalMath();
208 $eval->suppress_errors = true;
209
210 $mark_errors = array('answer' => false, 'lower' => false, 'upper' => false, 'points' => false);
211 foreach (array( '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 //$_POST['gap_json_post'] = json_encode($json);
242 return !$error;
243 }
244
245 public function setValueByArray($data): void
246 {
247 $this->setValue($data);
248 }
249
253 public function insert(ilTemplate $template): void
254 {
255 global $DIC;
256 $lng = $DIC['lng'];
257 $modal = ilModalGUI::getInstance();
258 $modal->setHeading($lng->txt(''));
259 $modal->setId("ilGapModal");
260 $modal->setBody('');
261
262 $cloze_settings_js = 'ClozeSettings = {'
263 . ' gaps_php : ' . json_encode(array($this->getValue()))
264 . ',gaps_combination : ' . json_encode($this->getValueCombination())
265 . ',gap_backup : []'
266 . ',unused_gaps_comb : []'
267 . ',outofbound_text : ' . '"' . $lng->txt('out_of_range') . '"'
268 . ',combination_error : ' . '"' . $lng->txt('please_select') . '"'
269 . ',combination_text : ' . '"' . $lng->txt('gap_combination') . '"'
270 . ',copy_of_combination : ' . '"' . $lng->txt('copy_of') . ' ' . $lng->txt('gap_combination') . '"'
271 . ',gap_in_more_than_one : ' . '""'
272 . ',gap_text : ' . '"' . $lng->txt('gap') . '"'
273 . ',ok_text : ' . '"' . $lng->txt('ok') . '"'
274 . ',cancel_text : ' . '"' . $lng->txt('cancel') . '"'
275 . '};';
276
277 $DIC->ui()->mainTemplate()->addOnLoadCode(
278 $cloze_settings_js
279 . 'ClozeQuestionGapBuilder.Init();'
280 );
281 $DIC->ui()->mainTemplate()->addJavascript(
282 './Modules/TestQuestionPool/templates/default/clozeQuestionGapBuilder.js'
283 );
284
285
286 $custom_template = new ilTemplate('tpl.il_as_cloze_gap_builder.html', true, true, 'Modules/TestQuestionPool');
287 $custom_template->setVariable("MY_MODAL", $modal->getHTML());
288 $custom_template->setVariable('GAP_JSON', json_encode(array($this->getValue())));
289 $custom_template->setVariable('GAP', $lng->txt('gap'));
290 $custom_template->setVariable('GAP_COMBINATION_JSON', json_encode($this->getValueCombination()));
291 $custom_template->setVariable('TEXT_GAP', $lng->txt('text_gap'));
292 $custom_template->setVariable('SELECT_GAP', $lng->txt('select_gap'));
293 $custom_template->setVariable('NUMERIC_GAP', $lng->txt('numeric_gap'));
294 $custom_template->setVariable('GAP_SIZE', $lng->txt('cloze_fixed_textlength'));
295 $custom_template->setVariable('GAP_SIZE_INFO', $lng->txt('cloze_gap_size_info'));
296 $custom_template->setVariable('ANSWER_TEXT', $lng->txt('answer_text'));
297 $custom_template->setVariable('ANSWER_BYLINE', $lng->txt('cloze_answer_text_info'));
298 $custom_template->setVariable('POINTS', $lng->txt('points'));
299 $custom_template->setVariable('VALUE', $lng->txt('value'));
300 $custom_template->setVariable('UPPER_BOUND', $lng->txt('range_upper_limit'));
301 $custom_template->setVariable('LOWER_BOUND', $lng->txt('range_lower_limit'));
302 $custom_template->setVariable('ACTIONS', $lng->txt('actions'));
303 $custom_template->setVariable('REMOVE_GAP', $lng->txt('remove_gap'));
304 $custom_template->setVariable('SHUFFLE_ANSWERS', $lng->txt('shuffle_answers'));
305 $custom_template->setVariable('POINTS_ERROR', $lng->txt('enter_enough_positive_points'));
306 $custom_template->setVariable('MISSING_VALUE', $lng->txt('msg_input_is_required'));
307 $custom_template->setVariable('NOT_A_FORMULA', $lng->txt('err_no_formula'));
308 $custom_template->setVariable('NOT_A_NUMBER', $lng->txt('err_no_numeric_value'));
309 $custom_template->setVariable('CLOSE', $lng->txt('close'));
310 $custom_template->setVariable('DELETE_GAP', $lng->txt('are_you_sure'));
311 $custom_template->setVariable('PLEASE_SELECT', $lng->txt('please_select'));
312 $custom_template->setVariable('BEST_POSSIBLE_SOLUTION_HEADER', $lng->txt('tst_best_solution_is'));
313 $custom_template->setVariable('BEST_POSSIBLE_SOLUTION', $lng->txt('value'));
314 $custom_template->setVariable('MAX_POINTS', $lng->txt('max_points'));
315 $custom_template->setVariable('TYPE', $lng->txt('type'));
316 $custom_template->setVariable('VALUES', $lng->txt('values'));
317 $custom_template->setVariable('GAP_COMBINATION', $lng->txt('gap_combination'));
318 $custom_template->setVariable('COPY', $lng->txt('copy_of'));
319 $template->setCurrentBlock('prop_generic');
320 $template->setVariable('PROP_GENERIC', $custom_template->get());
321 $template->parseCurrentBlock();
322 }
323
328 public static function stripSlashesRecursive($a_data, bool $a_strip_html = true, string $a_allow = "")
329 {
330 if (is_array($a_data)) {
331 foreach ($a_data as $k => $v) {
332 if (is_array($v)) {
333 $a_data[$k] = self::stripSlashesRecursive($v, $a_strip_html, $a_allow);
334 } else {
335 $a_data[$k] = ilUtil::stripSlashes($v, $a_strip_html, $a_allow);
336 }
337 }
338 } else {
339 if ($a_data != null) {
340 $a_data = ilUtil::stripSlashes($a_data, $a_strip_html, $a_allow);
341 }
342 }
343
344 return $a_data;
345 }
346}
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:514
__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...
static getInstance()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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="")
global $DIC
Definition: feed.php:28
const CLOZE_NUMERIC
const CLOZE_SELECT
const CLOZE_TEXT
Cloze question constants.
$index
Definition: metadata.php:145
$i
Definition: metadata.php:41
static http()
Fetches the global http state from ILIAS.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
string $key
Consumer key/client ID value.
Definition: System.php:193