ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilClozeGapInputBuilderGUI.php
Go to the documentation of this file.
1 <?php
2 
3 require_once 'Services/Form/classes/class.ilSubEnabledFormPropertyGUI.php';
4 require_once 'Services/Form/classes/class.ilTextInputGUI.php';
5 require_once 'Modules/TestQuestionPool/classes/class.assClozeGap.php';
6 require_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  {
28  $json = json_decode(ilUtil::stripSlashes($_POST['gap_json_post']));
29  return $json[0];
30  }
31  return $this->value;
32  }
33 
34  public function setValueCombination($value)
35  {
36  $this->value_combination = $value;
37  }
38 
43  public function getValueCombination()
44  {
45  $editOrOpen = $this->value;
46  if(isset($editOrOpen['author']))
47  {
48  $json = json_decode(ilUtil::stripSlashes($_POST['gap_json_combination_post']));
49  return $json;
50  }
51  return (array) $this->value_combination;
52  }
53 
54  public function setValueCombinationFromDb($value)
55  {
56  $return_array = array();
57  if($value)
58  {
59  foreach($value as $row)
60  {
61  if($row['row_id'] == 0)
62  {
63  $return_array[$row['cid']][0][] = $row['gap_fi'];
64  }
65  $return_array[$row['cid']][1][$row['row_id']][] = $row['answer'];
66  $return_array[$row['cid']][2][$row['row_id']] = $row['points']; //= array('key' => $row['cid'], 'points' => $row['points'], 'best_solution' => $row['best_solution']);
67 
68  }
69  $this->setValueCombination($return_array);
70  }
71  }
72 
73  public function checkInput()
74  {
75  $error = false;
76  $json = json_decode(ilUtil::stripSlashes($_POST['gap_json_post']));
78  $gaps_used_in_combination = array();
79  if(array_key_exists('gap_combination', $_POST))
80  {
81  $_POST['gap_combination'] = ilUtil::stripSlashesRecursive($_POST['gap_combination']);
82  $_POST['gap_combination_values'] = ilUtil::stripSlashesRecursive($_POST['gap_combination_values']);
83  $gap_with_points = array();
84 
85  for($i=0; $i < count($_POST['gap_combination']['select']); $i++)
86  {
87  foreach($_POST['gap_combination']['select'][$i] as $key => $item)
88  {
89  if($item == 'none_selected_minus_one')
90  {
91  return false;
92  }
93  $gaps_used_in_combination[$item] = $item;
94  $check_points_for_best_scoring = false;
95  foreach($_POST['gap_combination_values'][$i] as $index => $answeritems)
96  {
97  foreach($answeritems as $answeritem )
98  {
99  if($answeritem == 'none_selected_minus_one')
100  {
101  return false;
102  }
103  }
104  $points = $_POST['gap_combination']['points'][$i][$index];
105  if($points > 0)
106  {
107  $check_points_for_best_scoring = true;
108  }
109  }
110  if(!$check_points_for_best_scoring)
111  {
112  return false;
113  }
114  }
115 
116  }
117  }
118 
119  if(isset($_POST['gap']) && is_array($_POST['gap']))
120  {
121  foreach($_POST['gap'] as $key => $item)
122  {
123  $_POST['clozetype_' . $key] = ilUtil::stripSlashes($_POST['clozetype_' . $key]);
124  $getType = $_POST['clozetype_' . $key];
125  $gapsize = $_POST['gap_' . $key . '_gapsize'];
126  $json[0][$key]->text_field_length = $gapsize > 0 ? $gapsize : '';
127  $select_at_least_on_positive = false;
128  if($getType == CLOZE_TEXT || $getType == CLOZE_SELECT)
129  {
130  $_POST['gap_' . $key] = ilUtil::stripSlashesRecursive($_POST['gap_' . $key]);
131  $gapText = $_POST['gap_' . $key];
132  foreach($gapText['answer'] as $row => $answer)
133  {
134  if(!isset($answer) || $answer == '')
135  {
136  $error = true;
137  }
138  }
139  $points_sum = 0;
140  if(array_key_exists('points', $gapText))
141  {
142  foreach($gapText['points'] as $row => $points)
143  {
144  if(isset($points) && $points != '' && is_numeric($points))
145  {
146  $points_sum += $points;
147  if($points > 0)
148  {
149  $select_at_least_on_positive = true;
150  }
151  }
152  else
153  {
154  $error = true;
155  }
156  }
157  if(is_array($gap_with_points) && array_key_exists($key, $gap_with_points))
158  {
159  $points_sum += $gap_with_points[$key];
160  }
161  if($points_sum <= 0)
162  {
163  if(!array_key_exists($key, $gaps_used_in_combination) && (!$getType == 'select' || $select_at_least_on_positive == false))
164  {
165  $error = true;
166  }
167  }
168  if($getType == CLOZE_SELECT)
169  {
170  $_POST['shuffle_' . $key] = ilUtil::stripSlashes($_POST['shuffle_' . $key]);
171  if(!isset($_POST['shuffle_' . $key]))
172  {
173  $error = true;
174  }
175  }
176  }
177  else
178  {
179  $error = true;
180  }
181  }
182  if($getType == CLOZE_NUMERIC)
183  {
184 // fau: fixGapFormula - fix post indices, streamlined checks
185  include_once("./Services/Math/classes/class.EvalMath.php");
186  $eval = new EvalMath();
187  $eval->suppress_errors = true;
188 
189  $mark_errors = array('answer' => false, 'lower' => false, 'upper' => false, 'points' => false);
190  foreach (array( 'answer' => '_numeric',
191  'lower' => '_numeric_lower',
192  'upper' => '_numeric_upper',
193  'points' => '_numeric_points') as $part => $suffix)
194  {
195  $val = ilUtil::stripSlashes($_POST['gap_'.$key.$suffix], FALSE);
196  $val = str_replace(',', '.', $val);
197  if ($eval->e($val) === FALSE)
198  {
199  $mark_errors[$part] = true;
200  $error = true;
201  }
202 
203  if ($part == 'points')
204  {
205  $points = $val;
206  }
207  }
208 // fau.
209  if(is_array($gap_with_points) && array_key_exists($key, $gap_with_points))
210  {
211  $points += $gap_with_points[$key];
212  }
213 
214  if(!isset($points) || $points == '' || !is_numeric($points) || $points == 0)
215  {
216  if(!array_key_exists($key, $gaps_used_in_combination))
217  {
218  $error = true;
219  }
220  }
221 
222  $json[0][$key]->values[0]->error = $mark_errors;
223  }
224  }
225  }
226  $_POST['gap_json_post'] = json_encode($json);
227  return !$error;
228  }
229 
230  public function setValueByArray($data)
231  {
232  $this->setValue($data);
233  }
234 
238  public function insert(ilTemplate $template)
239  {
240  global $lng;
241  require_once("./Services/UIComponent/Modal/classes/class.ilModalGUI.php");
242  $modal = ilModalGUI::getInstance();
243  $modal->setHeading($lng->txt(''));
244  $modal->setId("ilGapModal");
245  //$modal->setBackdrop(ilModalGUI::BACKDROP_OFF);
246  $modal->setBody('');
247 
248  $custom_template = new ilTemplate('tpl.il_as_cloze_gap_builder.html', true, true, 'Modules/TestQuestionPool');
249  $custom_template->setVariable("MY_MODAL", $modal->getHTML());
250  $custom_template->setVariable('GAP_JSON', json_encode(array($this->getValue())));
251  $custom_template->setVariable('GAP', $lng->txt('gap'));
252  $custom_template->setVariable('GAP_COMBINATION_JSON', json_encode($this->getValueCombination()));
253  $custom_template->setVariable('TEXT_GAP', $lng->txt('text_gap'));
254  $custom_template->setVariable('SELECT_GAP', $lng->txt('select_gap'));
255  $custom_template->setVariable('NUMERIC_GAP', $lng->txt('numeric_gap'));
256  $custom_template->setVariable('GAP_SIZE', $lng->txt('cloze_fixed_textlength'));
257  $custom_template->setVariable('GAP_SIZE_INFO', $lng->txt('cloze_gap_size_info'));
258  $custom_template->setVariable('ANSWER_TEXT', $lng->txt('answer_text'));
259  $custom_template->setVariable('POINTS', $lng->txt('points'));
260  $custom_template->setVariable('VALUE', $lng->txt('value'));
261  $custom_template->setVariable('UPPER_BOUND', $lng->txt('range_upper_limit'));
262  $custom_template->setVariable('LOWER_BOUND', $lng->txt('range_lower_limit'));
263  $custom_template->setVariable('ACTIONS', $lng->txt('actions'));
264  $custom_template->setVariable('REMOVE_GAP', $lng->txt('remove_gap'));
265  $custom_template->setVariable('SHUFFLE_ANSWERS', $lng->txt('shuffle_answers'));
266  $custom_template->setVariable('POINTS_ERROR', $lng->txt('enter_enough_positive_points'));
267  $custom_template->setVariable('MISSING_VALUE', $lng->txt('msg_input_is_required'));
268  $custom_template->setVariable('NOT_A_FORMULA', $lng->txt('err_no_formula'));
269  $custom_template->setVariable('NOT_A_NUMBER', $lng->txt('err_no_numeric_value'));
270  $custom_template->setVariable('CLOSE', $lng->txt('close'));
271  $custom_template->setVariable('DELETE_GAP', $lng->txt('are_you_sure'));
272  $custom_template->setVariable('PLEASE_SELECT', $lng->txt('please_select'));
273  $custom_template->setVariable('BEST_POSSIBLE_SOLUTION_HEADER', $lng->txt('tst_best_solution_is'));
274  $custom_template->setVariable('BEST_POSSIBLE_SOLUTION', $lng->txt('value'));
275  $custom_template->setVariable('MAX_POINTS', $lng->txt('max_points'));
276  $custom_template->setVariable('OUT_OF_BOUND', $lng->txt('out_of_range'));
277  $custom_template->setVariable('TYPE', $lng->txt('type'));
278  $custom_template->setVariable('VALUES', $lng->txt('values'));
279  $custom_template->setVariable('GAP_COMBINATION', $lng->txt('gap_combination'));
280  $custom_template->setVariable('COPY', $lng->txt('copy_of'));
281  $custom_template->setVariable('OK', $lng->txt('ok'));
282  $custom_template->setVariable('CANCEL', $lng->txt('cancel'));
283  $custom_template->setVariable('WHITESPACE_FRONT', $lng->txt('cloze_textgap_whitespace_before'));
284  $custom_template->setVariable('WHITESPACE_BACK', $lng->txt('cloze_textgap_whitespace_after'));
285  $custom_template->setVariable('WHITESPACE_MULTIPLE', $lng->txt('cloze_textgap_multiple_whitespace'));
286  $template->setCurrentBlock('prop_generic');
287  $template->setVariable('PROP_GENERIC', $custom_template->get());
288  $template->parseCurrentBlock();
289  }
290 }