ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilClozeGapInputBuilderGUI.php
Go to the documentation of this file.
1<?php
2
3require_once 'Services/Form/classes/class.ilSubEnabledFormPropertyGUI.php';
4require_once 'Services/Form/classes/class.ilTextInputGUI.php';
5require_once 'Modules/TestQuestionPool/classes/class.assClozeGap.php';
6require_once 'Modules/TestQuestionPool/classes/class.assClozeTest.php';
7
9{
14 public function setValue($a_value)
15 {
16 $this->value = $a_value;
17 }
18
23 public function getValue()
24 {
25 $editOrOpen = $this->value;
26 if (isset($editOrOpen['author'])) {
27 $json = json_decode(ilUtil::stripSlashes($_POST['gap_json_post']));
28 return $json[0];
29 }
30 return $this->value;
31 }
32
33 public function setValueCombination($value)
34 {
35 $this->value_combination = $value;
36 }
37
42 public function getValueCombination()
43 {
44 $editOrOpen = $this->value;
45 if (isset($editOrOpen['author'])) {
46 $json = json_decode(ilUtil::stripSlashes($_POST['gap_json_combination_post']));
47 return $json;
48 }
49 return (array) $this->value_combination;
50 }
51
52 public function setValueCombinationFromDb($value)
53 {
54 $return_array = array();
55 if ($value) {
56 foreach ($value as $row) {
57 if ($row['row_id'] == 0) {
58 $return_array[$row['cid']][0][] = $row['gap_fi'];
59 }
60 $return_array[$row['cid']][1][$row['row_id']][] = $row['answer'];
61 $return_array[$row['cid']][2][$row['row_id']] = $row['points']; //= array('key' => $row['cid'], 'points' => $row['points'], 'best_solution' => $row['best_solution']);
62 }
63 $this->setValueCombination($return_array);
64 }
65 }
66
67 public function checkInput()
68 {
69 $error = false;
70 $json = ilUtil::stripSlashesRecursive(json_decode($_POST['gap_json_post']), false);
72 $gaps_used_in_combination = array();
73 if (array_key_exists('gap_combination', $_POST)) {
74 $_POST['gap_combination'] = ilUtil::stripSlashesRecursive($_POST['gap_combination']);
75 $_POST['gap_combination_values'] = ilUtil::stripSlashesRecursive($_POST['gap_combination_values']);
76 $gap_with_points = array();
77
78 for ($i = 0; $i < count($_POST['gap_combination']['select']); $i++) {
79 foreach ($_POST['gap_combination']['select'][$i] as $key => $item) {
80 if ($item == 'none_selected_minus_one') {
81 return false;
82 }
83 $gaps_used_in_combination[$item] = $item;
84 $check_points_for_best_scoring = false;
85 foreach ($_POST['gap_combination_values'][$i] as $index => $answeritems) {
86 foreach ($answeritems as $answeritem) {
87 if ($answeritem == 'none_selected_minus_one') {
88 return false;
89 }
90 }
91 $points = $_POST['gap_combination']['points'][$i][$index];
92 if ($points > 0) {
93 $check_points_for_best_scoring = true;
94 }
95 }
96 if (!$check_points_for_best_scoring) {
97 return false;
98 }
99 }
100 }
101 }
102
103 if (isset($_POST['gap']) && is_array($_POST['gap'])) {
104 foreach ($_POST['gap'] as $key => $item) {
105 $_POST['clozetype_' . $key] = ilUtil::stripSlashes($_POST['clozetype_' . $key]);
106 $getType = $_POST['clozetype_' . $key];
107 $gapsize = $_POST['gap_' . $key . '_gapsize'];
108 $json[0][$key]->text_field_length = $gapsize > 0 ? $gapsize : '';
109 $select_at_least_on_positive = false;
110 if ($getType == CLOZE_TEXT || $getType == CLOZE_SELECT) {
111 $_POST['gap_' . $key] = ilUtil::stripSlashesRecursive($_POST['gap_' . $key], false);
112 $gapText = $_POST['gap_' . $key];
113 foreach ($gapText['answer'] as $row => $answer) {
114 if (!isset($answer) || $answer == '') {
115 $error = true;
116 }
117 }
118 $points_sum = 0;
119 if (array_key_exists('points', $gapText)) {
120 foreach ($gapText['points'] as $row => $points) {
121 if (isset($points) && $points != '' && is_numeric($points)) {
122 $points_sum += $points;
123 if ($points > 0) {
124 $select_at_least_on_positive = true;
125 }
126 } else {
127 $error = true;
128 }
129 }
130 if (is_array($gap_with_points) && array_key_exists($key, $gap_with_points)) {
131 $points_sum += $gap_with_points[$key];
132 }
133 if ($points_sum <= 0) {
134 if (!array_key_exists($key, $gaps_used_in_combination) && (!$getType == 'select' || $select_at_least_on_positive == false)) {
135 $error = true;
136 }
137 }
138 if ($getType == CLOZE_SELECT) {
139 $_POST['shuffle_' . $key] = ilUtil::stripSlashes($_POST['shuffle_' . $key]);
140 if (!isset($_POST['shuffle_' . $key])) {
141 $error = true;
142 }
143 }
144 } else {
145 $error = true;
146 }
147 }
148 if ($getType == CLOZE_NUMERIC) {
149 // fau: fixGapFormula - fix post indices, streamlined checks
150 include_once("./Services/Math/classes/class.EvalMath.php");
151 $eval = new EvalMath();
152 $eval->suppress_errors = true;
153
154 $mark_errors = array('answer' => false, 'lower' => false, 'upper' => false, 'points' => false);
155 foreach (array( 'answer' => '_numeric',
156 'lower' => '_numeric_lower',
157 'upper' => '_numeric_upper',
158 'points' => '_numeric_points') as $part => $suffix) {
159 $val = ilUtil::stripSlashes($_POST['gap_' . $key . $suffix], false);
160 $val = str_replace(',', '.', $val);
161 if ($eval->e($val) === false) {
162 $mark_errors[$part] = true;
163 $error = true;
164 }
165
166 if ($part == 'points') {
167 $points = $val;
168 }
169 }
170 // fau.
171 if (is_array($gap_with_points) && array_key_exists($key, $gap_with_points)) {
172 $points += $gap_with_points[$key];
173 }
174
175 if (!isset($points) || $points == '' || !is_numeric($points) || $points == 0) {
176 if (!array_key_exists($key, $gaps_used_in_combination)) {
177 $error = true;
178 }
179 }
180
181 $json[0][$key]->values[0]->error = $mark_errors;
182 }
183 }
184 }
185 $_POST['gap_json_post'] = json_encode($json);
186 return !$error;
187 }
188
189 public function setValueByArray($data)
190 {
191 $this->setValue($data);
192 }
193
197 public function insert(ilTemplate $template)
198 {
199 global $DIC;
200 $lng = $DIC['lng'];
201 require_once("./Services/UIComponent/Modal/classes/class.ilModalGUI.php");
202 $modal = ilModalGUI::getInstance();
203 $modal->setHeading($lng->txt(''));
204 $modal->setId("ilGapModal");
205 //$modal->setBackdrop(ilModalGUI::BACKDROP_OFF);
206 $modal->setBody('');
207
208 $custom_template = new ilTemplate('tpl.il_as_cloze_gap_builder.html', true, true, 'Modules/TestQuestionPool');
209 $custom_template->setVariable("MY_MODAL", $modal->getHTML());
210 $custom_template->setVariable('GAP_JSON', json_encode(array($this->getValue())));
211 $custom_template->setVariable('GAP', $lng->txt('gap'));
212 $custom_template->setVariable('GAP_COMBINATION_JSON', json_encode($this->getValueCombination()));
213 $custom_template->setVariable('TEXT_GAP', $lng->txt('text_gap'));
214 $custom_template->setVariable('SELECT_GAP', $lng->txt('select_gap'));
215 $custom_template->setVariable('NUMERIC_GAP', $lng->txt('numeric_gap'));
216 $custom_template->setVariable('GAP_SIZE', $lng->txt('cloze_fixed_textlength'));
217 $custom_template->setVariable('GAP_SIZE_INFO', $lng->txt('cloze_gap_size_info'));
218 $custom_template->setVariable('ANSWER_TEXT', $lng->txt('answer_text'));
219 $custom_template->setVariable('POINTS', $lng->txt('points'));
220 $custom_template->setVariable('VALUE', $lng->txt('value'));
221 $custom_template->setVariable('UPPER_BOUND', $lng->txt('range_upper_limit'));
222 $custom_template->setVariable('LOWER_BOUND', $lng->txt('range_lower_limit'));
223 $custom_template->setVariable('ACTIONS', $lng->txt('actions'));
224 $custom_template->setVariable('REMOVE_GAP', $lng->txt('remove_gap'));
225 $custom_template->setVariable('SHUFFLE_ANSWERS', $lng->txt('shuffle_answers'));
226 $custom_template->setVariable('POINTS_ERROR', $lng->txt('enter_enough_positive_points'));
227 $custom_template->setVariable('MISSING_VALUE', $lng->txt('msg_input_is_required'));
228 $custom_template->setVariable('NOT_A_FORMULA', $lng->txt('err_no_formula'));
229 $custom_template->setVariable('NOT_A_NUMBER', $lng->txt('err_no_numeric_value'));
230 $custom_template->setVariable('CLOSE', $lng->txt('close'));
231 $custom_template->setVariable('DELETE_GAP', $lng->txt('are_you_sure'));
232 $custom_template->setVariable('PLEASE_SELECT', $lng->txt('please_select'));
233 $custom_template->setVariable('BEST_POSSIBLE_SOLUTION_HEADER', $lng->txt('tst_best_solution_is'));
234 $custom_template->setVariable('BEST_POSSIBLE_SOLUTION', $lng->txt('value'));
235 $custom_template->setVariable('MAX_POINTS', $lng->txt('max_points'));
236 $custom_template->setVariable('OUT_OF_BOUND', $lng->txt('out_of_range'));
237 $custom_template->setVariable('TYPE', $lng->txt('type'));
238 $custom_template->setVariable('VALUES', $lng->txt('values'));
239 $custom_template->setVariable('GAP_COMBINATION', $lng->txt('gap_combination'));
240 $custom_template->setVariable('COPY', $lng->txt('copy_of'));
241 $custom_template->setVariable('OK', $lng->txt('ok'));
242 $custom_template->setVariable('CANCEL', $lng->txt('cancel'));
243 $custom_template->setVariable('WHITESPACE_FRONT', $lng->txt('cloze_textgap_whitespace_before'));
244 $custom_template->setVariable('WHITESPACE_BACK', $lng->txt('cloze_textgap_whitespace_after'));
245 $custom_template->setVariable('WHITESPACE_MULTIPLE', $lng->txt('cloze_textgap_multiple_whitespace'));
246 $template->setCurrentBlock('prop_generic');
247 $template->setVariable('PROP_GENERIC', $custom_template->get());
248 $template->parseCurrentBlock();
249 }
250}
$_POST["username"]
An exception for terminatinating execution or to throw for unit testing.
checkInput()
Check input, strip slashes etc.
static getInstance()
Get instance.
This class represents a property that may include a sub form.
special template class to simplify handling of ITX/PEAR
static stripSlashesRecursive($a_data, $a_strip_html=true, $a_allow="")
Strip slashes from array and sub-arrays.
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
$template
$key
Definition: croninfo.php:18
$i
Definition: disco.tpl.php:19
const CLOZE_NUMERIC
const CLOZE_SELECT
const CLOZE_TEXT
Cloze question constants.
$index
Definition: metadata.php:60
$row
global $DIC
Definition: saml.php:7
$data
Definition: bench.php:6