ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilNestedOrderingGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once "./Modules/Test/classes/inc.AssessmentConstants.php";
5 
14 {
15  protected $instanceId = 'default';
16 
17  public $answers = array();
18  public $ordering_type = NULL;
19  public $image_path = null;
20  public $image_path_web = null;
21  public $thumb_prefix = 'thumb.';
22 
23  private $perform_js = 1;
24 
25  private $graphical_output = FALSE;
26 
27  public function __construct($a_title = "", $a_postvar = "", $graphical_output = false)
28  {
29  parent::__construct($a_title, $a_postvar);
30  $this->graphical_output = (bool)$graphical_output;
31  }
32 
33  public function setInstanceId($instanceId)
34  {
35  $this->instanceId = $instanceId;
36  }
37 
38  public function getInstanceId()
39  {
40  return $this->instanceId;
41  }
42 
47  public function insert(&$a_tpl)
48  {
52  $a_tpl->setCurrentBlock("prop_generic");
53  $a_tpl->setVariable("PROP_GENERIC", $this->getHtml());
54  $a_tpl->parseCurrentBlock();
55  }
56 
57 
58  public function setOrderingType($a_ordering_type)
59  {
60  $this->ordering_type = $a_ordering_type;
61  }
62  public function getOrderingType()
63  {
64  return $this->ordering_type;
65  }
66 
67  public function setImagePath($a_image_path)
68  {
69  $this->image_path = $a_image_path;
70  }
71  public function getImagePath()
72  {
73  return $this->image_path;
74  }
75  public function setImagePathWeb($a_image_path_web)
76  {
77  $this->image_path_web = $a_image_path_web;
78  }
79  public function getImagePathWeb()
80  {
81  return $this->image_path_web;
82  }
83  public function setThumbPrefix($a_thumb_prefix)
84  {
85  $this->thumb_prefix = $a_thumb_prefix;
86  }
87  public function getThumbPrefix()
88  {
89  return 'thumb.';
90  }
91 
96  {
97  $this->graphical_output = $graphical_output;
98  }
99 
100  public function getGraphicalOutput()
101  {
103  }
104 
105 
106 
107  /***
108  * @param array $a_answers_array contain objects
109  * @param bool $shuffle
110  */
111  public function setObjAnswersArray($a_answers_array, $shuffle = false)
112  {
113  if($shuffle == true)
114  {
115  shuffle($a_answers_array);
116  }
117 
118  $i = 0;
119  foreach($a_answers_array as $obj_answer)
120  {
121  $this->answers[$i]['random_id'] = $obj_answer->getRandomId();
122  $this->answers[$i]['answertext'] = (string)$obj_answer->getAnswertext();
123  $this->answers[$i]['answer_id'] = $obj_answer->getAnswerId();
124  $this->answers[$i]['ordering_depth'] = $obj_answer->getOrderingDepth();
125 
126  $i++;
127  }
128  }
129 
131  {
132  $js_include_tpl = new ilTemplate("tpl.il_as_qpl_nested_ordering_output_html.html", TRUE, TRUE, "Modules/TestQuestionPool");
133  $js_include_tpl->setCurrentBlock('begin_sortablecontainer');
134  $js_include_tpl->setVariable('INSTANCE_ID', $this->getInstanceId());
135  $js_include_tpl->parseCurrentBlock();
136 
137  return $js_include_tpl->get();
138  }
139  public function renderEndSortableContainer()
140  {
141  $js_include_tpl = new ilTemplate("tpl.il_as_qpl_nested_ordering_output_html.html", TRUE, TRUE, "Modules/TestQuestionPool");
142  $js_include_tpl->touchBlock('end_sortablecontainer');
143 
144  return $js_include_tpl->get();
145  }
146 
147  public function renderListItem($cur_answer, $i)
148  {
149  $js_include_tpl = new ilTemplate("tpl.il_as_qpl_nested_ordering_output_html.html", TRUE, TRUE, "Modules/TestQuestionPool");
150  $js_include_tpl->touchBlock('begin_list_item');
151 
152  $js_include_tpl->setCurrentBlock('item_value');
153  $js_include_tpl->setVariable('LIST_ITEM_VALUE', $this->renderAnswerOutput($cur_answer['answertext'], $i));
154  $js_include_tpl->setVariable('LIST_ITEM_ID', $i);
155 
156  if(isset($cur_answer['ok']) && $this->getGraphicalOutput() == true)
157  {
158  global $lng;
159 
160  if($cur_answer['ok'] === true)
161  {
162  $icon_ok = ilUtil::getImagePath("icon_ok.svg");
163  $text_ok = $lng->txt("answer_is_right");
164  }
165  else
166  {
167  $icon_ok = ilUtil::getImagePath("icon_not_ok.svg");
168  $text_ok = $lng->txt("answer_is_wrong");
169  }
170 
171  $js_include_tpl->setVariable('ICON_OK', $icon_ok);
172  $js_include_tpl->setVariable('TEXT_OK', $text_ok);
173  }
174 
175  $js_include_tpl->parseCurrentBlock();
176 
177  return $js_include_tpl->get();
178  }
179 
180  public function renderEndListItem()
181  {
182  $js_include_tpl = new ilTemplate("tpl.il_as_qpl_nested_ordering_output_html.html", TRUE, TRUE, "Modules/TestQuestionPool");
183  $js_include_tpl->touchBlock('end_list_item');
184 
185  return $js_include_tpl->get();
186  }
187 
188  public function renderBeginSubList()
189  {
190  $js_include_tpl = new ilTemplate("tpl.il_as_qpl_nested_ordering_output_html.html", TRUE, TRUE, "Modules/TestQuestionPool");
191  $js_include_tpl->touchBlock('begin_sublist');
192 
193  return $js_include_tpl->get();
194  }
195 
196  public function renderEndSubList()
197  {
198  $js_include_tpl = new ilTemplate("tpl.il_as_qpl_nested_ordering_output_html.html", TRUE, TRUE, "Modules/TestQuestionPool");
199  $js_include_tpl->touchBlock('end_sublist');
200 
201  return $js_include_tpl->get();
202  }
203 
209  public function renderAnswerOutput($text_value, $i)
210  {
211  global $lng;
212  $input_tpl = new ilTemplate("tpl.prop_nestable_value.html", true, true, "Modules/TestQuestionPool");
213 
214  if(strlen($text_value))
215  {
216  if($this->getOrderingType() == OQ_NESTED_TERMS)
217  {
218  $input_tpl->setCurrentBlock('nested_terms');
219  $input_tpl->setVariable("VALUE", ilUtil::prepareFormOutput($text_value));
220  $input_tpl->setVariable("INPUT_POST_VAR", $this->getPostVar() . "[$i]");
221  $input_tpl->setVariable("NON_EDITABLE_ID", $this->getPostVar() . "[$i]");
222  $input_tpl->setVariable("HVALUE", ilUtil::prepareFormOutput($text_value));
223 
224  $input_tpl->setVariable("ID", $this->getFieldId() . "[$i]");
225  }
226  else if($this->getOrderingType() == OQ_NESTED_PICTURES)
227  {
228  $input_tpl->setCurrentBlock('nested_pictures');
229 
230  $thumbweb = $this->getImagePathWeb() . $this->getThumbPrefix() .$text_value;
231 
232  $input_tpl->setVariable("PICTURE_HREF",$thumbweb);
233  $input_tpl->setVariable("THUMB_ALT", $lng->txt("thumbnail"));
234  $input_tpl->setVariable("THUMB_TITLE", $lng->txt("thumbnail"));
235  $input_tpl->setVariable("NON_EDITABLE_ID", $this->getPostVar() . "[$i]");
236  $input_tpl->setVariable("HVALUE", $text_value);
237  $input_tpl->setVariable("ID", $this->getFieldId() . "[$i]");
238  }
239  }
240 
241 
242  $input_tpl->parseCurrentBlock();
243 
244  return $input_tpl->get();
245  }
246 
247  public function checkInput()
248  {
249  return true;
250  }
251 
252  public function getHtml($shuffle_depth = 0)
253  {
254  $html_output = '';
255  $html_output .= $this->renderBeginSortableContainer();
256  $html_output .= $this->renderBeginSubList();
257 
258  $i = 0;
259 
260  if($shuffle_depth == 1)
261  {
262  // this is for output in test
263  foreach($this->answers as $cur_answer)
264  {
265  $html_output .= $this->renderListItem($cur_answer,$cur_answer['random_id']);
266  $html_output .= $this->renderEndListItem();
267  }
268  }
269  else
270  {
271  $previous_depth = 0;
272 
273  $tmp_array = $this->answers;
274  $tmp_array[count($tmp_array)]['ordering_depth'] = 'END_OF_LIST';
275 
276  foreach($this->answers as $cur_answer)
277  {
278  $current_depth = $cur_answer['ordering_depth'];
279  $next_depth = $tmp_array[$i+1]['ordering_depth'];
280 
281  if($next_depth == 'END_OF_LIST')
282  {
283  $next_depth = 0;
284  }
285 
286  if($previous_depth == $current_depth)
287  {
288  // pcn = Previous, Current, Next -> Depth
289  // pcn: 000, 001, 110, 220
290  if($current_depth == $next_depth)
291  {
292  // (1) pcn: 000
293 // echo"(1)";
294  $html_output .= $this->renderListItem($cur_answer, $cur_answer['random_id']);
295  $html_output .= $this->renderEndListItem();
296  }
297  else if($current_depth > $next_depth)
298  {
299  if($previous_depth == $next_depth)
300  {
301  // (8) pcn: 110
302 // echo"(8)";
303  $html_output .= $this->renderListItem($cur_answer, $cur_answer['random_id']);
304  $html_output .= $this->renderEndListItem();
305  $html_output .= $this->renderEndSubList();
306  $html_output .= $this->renderEndListItem();
307  }
308  else if($previous_depth > $next_depth)
309  {
310  // (12) pcn: 220
311 // echo"(12)";
312  $html_output .= $this->renderListItem($cur_answer, $cur_answer['random_id']);
313 
314  for($openlists = $next_depth; $openlists < $current_depth; $openlists++)
315  {
316  $html_output .= $this->renderEndListItem();
317  $html_output .= $this->renderEndSubList();
318  $html_output .= $this->renderEndListItem();
319  }
320  }
321  }
322  else if($current_depth < $next_depth)
323  {
324  // (2) pcn: 001
325 // echo"(2)";
326  $html_output .= $this->renderListItem($cur_answer, $cur_answer['random_id']);
327  $html_output .= $this->renderBeginSubList();
328  }
329  }
330  else if($previous_depth > $current_depth)
331  {
332  if($current_depth == $next_depth)
333  {
334  // (6) pcn: 100
335 // echo"(6)";
336  $html_output .= $this->renderListItem($cur_answer, $cur_answer['random_id']);
337  $html_output .= $this->renderEndListItem();
338  }
339  else if($current_depth > $next_depth)
340  {
341  // (11) pcn: 210
342 // echo"(11)";
343  $html_output .= $this->renderListItem($cur_answer, $cur_answer['random_id']);
344  $html_output .= $this->renderEndListItem();
345  $html_output .= $this->renderEndSubList();
346  }
347  else if($current_depth < $next_depth)
348  {
349  if($previous_depth == $next_depth)
350  {
351  // (7) pcn: 101
352 // echo"(7)";
353  $html_output .= $this->renderListItem($cur_answer, $cur_answer['random_id']);
354  $html_output .= $this->renderBeginSubList();
355  }
356  else if($previous_depth > $next_depth)
357  {
358  // (10) pcn: 201
359 // echo"(10)";
360  $html_output .= $this->renderListItem($cur_answer, $cur_answer['random_id']);
361  for($openlists = $next_depth; $openlists < $current_depth; $openlists++)
362  {
363  $html_output .= $this->renderEndSubList();
364  }
365  $html_output .= $this->renderBeginSubList();
366  }
367  }
368  }
369  else if($previous_depth < $current_depth)
370  {
371  if($current_depth == $next_depth)
372  {
373  // (4) pcn: 011
374 // echo"(4)";
375  $html_output .= $this->renderListItem($cur_answer, $cur_answer['random_id']);
376  $html_output .= $this->renderEndListItem();
377  }
378  else if($current_depth > $next_depth)
379  {
380  if($previous_depth == $next_depth)
381  {
382  // (3) pcn: 010,
383 // echo"(3)";
384  $html_output .= $this->renderListItem($cur_answer, $cur_answer['random_id']);
385  $html_output .= $this->renderEndListItem();
386  $html_output .= $this->renderEndSubList();
387  $html_output .= $this->renderEndListItem();
388 
389  }
390  else if($previous_depth > $next_depth)
391  {
392  // (9) pcn: 120
393 // echo"(9)";
394  $html_output .= $this->renderListItem($cur_answer, $cur_answer['random_id']);
395  for($openlists = $next_depth; $openlists < $current_depth; $openlists++)
396  {
397  $html_output .= $this->renderEndListItem();
398  $html_output .= $this->renderEndSubList();
399  }
400  }
401  }
402  else if($current_depth < $next_depth)
403  {
404  // (5) pcn: 012
405 // echo"(5)";
406  $html_output .= $this->renderListItem($cur_answer, $cur_answer['random_id']);
407  $html_output .= $this->renderBeginSubList();
408  }
409  }
410  $previous_depth = $current_depth;
411  $i++;
412  }
413  }
414  $html_output .= $this->renderEndSubList();
415  $html_output .= $this->renderEndSortableContainer();
416 
417  include_once "Services/jQuery/classes/class.iljQueryUtil.php";
418  global $tpl;
419 
420  $tpl->addJavaScript("./Services/jQuery/js/jquery.nestable.js");
423  $js_output = '';
424 
425  $js_include_tpl = new ilTemplate("tpl.il_as_qpl_nested_ordering_output_javascript.html", TRUE, TRUE, "Modules/TestQuestionPool");
426  if(!array_key_exists("pdf", $_GET) || $_GET["pdf"] != 1) // #14199
427  {
428  $js_include_tpl->touchBlock("css_include");
429  }
430  $js_include_tpl->setCurrentBlock('js_include');
431  $js_include_tpl->setVariable('PERFORM_JAVASCRIPT', $this->getPerformJavascript());
432  $js_include_tpl->setVariable('INSTANCE_ID', $this->getInstanceId());
433  $js_include_tpl->setVariable('HTML_OUTPUT', $html_output);
434  $js_include_tpl->parseCurrentBlock();
435 
436  return $js_include_tpl->get();
437  }
438 
439  /***
440  * @param $a_solution_array (contains ordering_depth, answertext)
441  * @return string
442  */
443  public function getSolutionHTML($a_solution_array)
444  {
445  $html_output = '';
446  $html_output .= $this->renderBeginSortableContainer();
447  $html_output .= $this->renderBeginSubList();
448 
449  $i = 0;
450  $previous_depth = 0;
451 
452  $tmp_array = $a_solution_array;
453  $tmp_array[count($tmp_array)]['depth'] = 'END_OF_LIST';
454 
455  foreach($a_solution_array as $cur_answer)
456  {
457  $current_depth = $cur_answer['depth'];
458  $next_depth = $tmp_array[$i+1]['depth'];
459  if($next_depth == 'END_OF_LIST')
460  {
461  $next_depth = 0;
462  }
463 
464  if($previous_depth == $current_depth)
465  {
466  // pcn = Previous, Current, Next -> Depth
467  // pcn: 000, 001, 110, 220
468  if($current_depth == $next_depth)
469  {
470  // (1) pcn: 000
471  // echo"(1)";
472  $html_output .= $this->renderListItem($cur_answer, $i);
473  $html_output .= $this->renderEndListItem();
474  }
475  else if($current_depth > $next_depth)
476  {
477  if($previous_depth == $next_depth)
478  {
479  // (8) pcn: 110
480  // echo"(8)";
481  $html_output .= $this->renderListItem($cur_answer, $i);
482  $html_output .= $this->renderEndListItem();
483  $html_output .= $this->renderEndSubList();
484  $html_output .= $this->renderEndListItem();
485  }
486  else if($previous_depth > $next_depth)
487  {
488  // (12) pcn: 220
489  // echo"(12)";
490  $html_output .= $this->renderListItem($cur_answer, $i);
491 
492  for($openlists = $next_depth; $openlists < $current_depth; $openlists++)
493  {
494  $html_output .= $this->renderEndListItem();
495  $html_output .= $this->renderEndSubList();
496  $html_output .= $this->renderEndListItem();
497  }
498  }
499  }
500  else if($current_depth < $next_depth)
501  {
502  // (2) pcn: 001
503  // echo"(2)";
504  $html_output .= $this->renderListItem($cur_answer, $i);
505  $html_output .= $this->renderBeginSubList();
506 
507  }
508  }
509  else if($previous_depth > $current_depth)
510  {
511  if($current_depth == $next_depth)
512  {
513  // (6) pcn: 100
514  // echo"(6)";
515  $html_output .= $this->renderListItem($cur_answer, $i);
516  $html_output .= $this->renderEndListItem();
517  }
518  else if($current_depth > $next_depth)
519  {
520  // (11) pcn: 210
521  // echo"(11)";
522  $html_output .= $this->renderListItem($cur_answer, $i);
523  $html_output .= $this->renderEndListItem();
524  $html_output .= $this->renderEndSubList();
525  }
526  else if($current_depth < $next_depth)
527  {
528  if($previous_depth == $next_depth)
529  {
530  // (7) pcn: 101
531  // echo"(7)";
532  $html_output .= $this->renderListItem($cur_answer, $i);
533  $html_output .= $this->renderBeginSubList();
534  }
535  else if($previous_depth > $next_depth)
536  {
537  // (10) pcn: 201
538  // echo"(10)";
539  $html_output .= $this->renderListItem($cur_answer, $i);
540  for($openlists = $next_depth; $openlists < $current_depth; $openlists++)
541  {
542  $html_output .= $this->renderEndSubList();
543  }
544  $html_output .= $this->renderBeginSubList();
545  }
546  }
547  }
548  else if($previous_depth < $current_depth)
549  {
550  if($current_depth == $next_depth)
551  {
552  // (4) pcn: 011
553  // echo"(4)";
554  $html_output .= $this->renderListItem($cur_answer, $i);
555  $html_output .= $this->renderEndListItem();
556  }
557  else if($current_depth > $next_depth)
558  {
559  if($previous_depth == $next_depth)
560  {
561  // (3) pcn: 010,
562  // echo"(3)";
563  $html_output .= $this->renderListItem($cur_answer, $i);
564  $html_output .= $this->renderEndListItem();
565  $html_output .= $this->renderEndSubList();
566  $html_output .= $this->renderEndListItem();
567 
568  }
569  else if($previous_depth > $next_depth)
570  {
571  // (9) pcn: 120
572  // echo"(9)";
573  $html_output .= $this->renderListItem($cur_answer, $i);
574  for($openlists = $next_depth; $openlists < $current_depth; $openlists++)
575  {
576  $html_output .= $this->renderEndListItem();
577  $html_output .= $this->renderEndSubList();
578  }
579  }
580  }
581  else if($current_depth < $next_depth)
582  {
583  // (5) pcn: 012
584  // echo"(5)";
585  $html_output .= $this->renderListItem($cur_answer, $i);
586  $html_output .= $this->renderBeginSubList();
587  }
588  }
589  $previous_depth = $current_depth;
590  $i++;
591  }
592 
593  $html_output .= $this->renderEndSubList();
594  $html_output .= $this->renderEndSortableContainer();
595 
596 // $js_include_tpl = new ilTemplate("tpl.il_as_qpl_nested_ordering_output_solution.html", TRUE, TRUE, "Modules/TestQuestionPool");
597 
598  include_once "Services/jQuery/classes/class.iljQueryUtil.php";
599  global $tpl;
600 
601  $tpl->addJavaScript("./Services/jQuery/js/jquery.nestable.js");
604  $js_output = '';
605 
606  $js_include_tpl = new ilTemplate("tpl.il_as_qpl_nested_ordering_output_javascript.html", TRUE, TRUE, "Modules/TestQuestionPool");
607  if(!array_key_exists("pdf", $_GET) || $_GET["pdf"] != 1) // #14199
608  {
609  $js_include_tpl->touchBlock("css_include");
610  }
611  $js_include_tpl->setCurrentBlock('js_include');
612  $js_include_tpl->setVariable('PERFORM_JAVASCRIPT', $this->getPerformJavascript());
613 
615 
616  $js_include_tpl->setVariable('HTML_OUTPUT', $html_output);
617 
618  $js_include_tpl->parseCurrentBlock();
619 
620  return $js_include_tpl->get();
621  }
622 
623  public function setPerformJavascript($a_bool)
624  {
625  $this->perform_js = $a_bool;
626  }
627 
628  public function getPerformJavascript()
629  {
630  return (int)$this->perform_js;
631  }
632 }
633 ?>