ILIAS  Release_4_4_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.png");
163  $text_ok = $lng->txt("answer_is_right");
164  }
165  else
166  {
167  $icon_ok = ilUtil::getImagePath("icon_not_ok.png");
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  if(!$this->getDisabled())
420  {
421  $tpl->addJavaScript( "./Services/jQuery/js/jquery.nestable.js" );
422  }
425  $js_output = '';
426 
427  $js_include_tpl = new ilTemplate("tpl.il_as_qpl_nested_ordering_output_javascript.html", TRUE, TRUE, "Modules/TestQuestionPool");
428  $js_include_tpl->setCurrentBlock('js_include');
429 
430  if(!$this->getDisabled())
431  {
432  $js_include_tpl->setVariable( 'PERFORM_JAVASCRIPT', $this->getPerformJavascript() );
433  }
434  else
435  {
436  $js_include_tpl->setVariable( 'PERFORM_JAVASCRIPT', false );
437  }
438 
439  $js_include_tpl->setVariable( 'INSTANCE_ID', $this->getInstanceId() );
440 
441  $js_include_tpl->setVariable('HTML_OUTPUT', $html_output);
442 
443  $js_include_tpl->parseCurrentBlock();
444 
445  return $js_include_tpl->get();
446  }
447 
448  /***
449  * @param $a_solution_array (contains ordering_depth, answertext)
450  * @return string
451  */
452  public function getSolutionHTML($a_solution_array)
453  {
454  $html_output = '';
455  $html_output .= $this->renderBeginSortableContainer();
456  $html_output .= $this->renderBeginSubList();
457 
458  $i = 0;
459  $previous_depth = 0;
460 
461  $tmp_array = $a_solution_array;
462  $tmp_array[count($tmp_array)]['depth'] = 'END_OF_LIST';
463 
464  foreach($a_solution_array as $cur_answer)
465  {
466  $current_depth = $cur_answer['depth'];
467  $next_depth = $tmp_array[$i+1]['depth'];
468  if($next_depth == 'END_OF_LIST')
469  {
470  $next_depth = 0;
471  }
472 
473  if($previous_depth == $current_depth)
474  {
475  // pcn = Previous, Current, Next -> Depth
476  // pcn: 000, 001, 110, 220
477  if($current_depth == $next_depth)
478  {
479  // (1) pcn: 000
480  // echo"(1)";
481  $html_output .= $this->renderListItem($cur_answer, $i);
482  $html_output .= $this->renderEndListItem();
483  }
484  else if($current_depth > $next_depth)
485  {
486  if($previous_depth == $next_depth)
487  {
488  // (8) pcn: 110
489  // echo"(8)";
490  $html_output .= $this->renderListItem($cur_answer, $i);
491  $html_output .= $this->renderEndListItem();
492  $html_output .= $this->renderEndSubList();
493  $html_output .= $this->renderEndListItem();
494  }
495  else if($previous_depth > $next_depth)
496  {
497  // (12) pcn: 220
498  // echo"(12)";
499  $html_output .= $this->renderListItem($cur_answer, $i);
500 
501  for($openlists = $next_depth; $openlists < $current_depth; $openlists++)
502  {
503  $html_output .= $this->renderEndListItem();
504  $html_output .= $this->renderEndSubList();
505  $html_output .= $this->renderEndListItem();
506  }
507  }
508  }
509  else if($current_depth < $next_depth)
510  {
511  // (2) pcn: 001
512  // echo"(2)";
513  $html_output .= $this->renderListItem($cur_answer, $i);
514  $html_output .= $this->renderBeginSubList();
515 
516  }
517  }
518  else if($previous_depth > $current_depth)
519  {
520  if($current_depth == $next_depth)
521  {
522  // (6) pcn: 100
523  // echo"(6)";
524  $html_output .= $this->renderListItem($cur_answer, $i);
525  $html_output .= $this->renderEndListItem();
526  }
527  else if($current_depth > $next_depth)
528  {
529  // (11) pcn: 210
530  // echo"(11)";
531  $html_output .= $this->renderListItem($cur_answer, $i);
532  $html_output .= $this->renderEndListItem();
533  $html_output .= $this->renderEndSubList();
534  }
535  else if($current_depth < $next_depth)
536  {
537  if($previous_depth == $next_depth)
538  {
539  // (7) pcn: 101
540  // echo"(7)";
541  $html_output .= $this->renderListItem($cur_answer, $i);
542  $html_output .= $this->renderBeginSubList();
543  }
544  else if($previous_depth > $next_depth)
545  {
546  // (10) pcn: 201
547  // echo"(10)";
548  $html_output .= $this->renderListItem($cur_answer, $i);
549  for($openlists = $next_depth; $openlists < $current_depth; $openlists++)
550  {
551  $html_output .= $this->renderEndSubList();
552  }
553  $html_output .= $this->renderBeginSubList();
554  }
555  }
556  }
557  else if($previous_depth < $current_depth)
558  {
559  if($current_depth == $next_depth)
560  {
561  // (4) pcn: 011
562  // echo"(4)";
563  $html_output .= $this->renderListItem($cur_answer, $i);
564  $html_output .= $this->renderEndListItem();
565  }
566  else if($current_depth > $next_depth)
567  {
568  if($previous_depth == $next_depth)
569  {
570  // (3) pcn: 010,
571  // echo"(3)";
572  $html_output .= $this->renderListItem($cur_answer, $i);
573  $html_output .= $this->renderEndListItem();
574  $html_output .= $this->renderEndSubList();
575  $html_output .= $this->renderEndListItem();
576 
577  }
578  else if($previous_depth > $next_depth)
579  {
580  // (9) pcn: 120
581  // echo"(9)";
582  $html_output .= $this->renderListItem($cur_answer, $i);
583  for($openlists = $next_depth; $openlists < $current_depth; $openlists++)
584  {
585  $html_output .= $this->renderEndListItem();
586  $html_output .= $this->renderEndSubList();
587  }
588  }
589  }
590  else if($current_depth < $next_depth)
591  {
592  // (5) pcn: 012
593  // echo"(5)";
594  $html_output .= $this->renderListItem($cur_answer, $i);
595  $html_output .= $this->renderBeginSubList();
596  }
597  }
598  $previous_depth = $current_depth;
599  $i++;
600  }
601 
602  $html_output .= $this->renderEndSubList();
603  $html_output .= $this->renderEndSortableContainer();
604 
605 // $js_include_tpl = new ilTemplate("tpl.il_as_qpl_nested_ordering_output_solution.html", TRUE, TRUE, "Modules/TestQuestionPool");
606 
607  include_once "Services/jQuery/classes/class.iljQueryUtil.php";
608  global $tpl;
609 
610  $tpl->addJavaScript("./Services/jQuery/js/jquery.nestable.js");
613  $js_output = '';
614 
615  $js_include_tpl = new ilTemplate("tpl.il_as_qpl_nested_ordering_output_javascript.html", TRUE, TRUE, "Modules/TestQuestionPool");
616 
617  $js_include_tpl->setCurrentBlock('js_include');
618  $js_include_tpl->setVariable('PERFORM_JAVASCRIPT', $this->getPerformJavascript());
619 
621 
622  $js_include_tpl->setVariable('HTML_OUTPUT', $html_output);
623 
624  $js_include_tpl->parseCurrentBlock();
625 
626  return $js_include_tpl->get();
627  }
628 
629  public function setPerformJavascript($a_bool)
630  {
631  $this->perform_js = $a_bool;
632  }
633 
634  public function getPerformJavascript()
635  {
636  return (int)$this->perform_js;
637  }
638 }
639 ?>