skill.visualizer.lib.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. * @todo lib not use. Only the class variables not the functions
  6. */
  7. class SkillVisualizer
  8. {
  9. public $block_size = 120; //see CSS window class
  10. public $canvas_x = 1024;
  11. public $canvas_y = 800;
  12. public $offset_x = 0;
  13. public $offset_y = 50;
  14. public $space_between_blocks_x = 100;
  15. public $space_between_blocks_y = 150;
  16. public $center_x = null;
  17. private $html = '';
  18. private $type = 'read';
  19. private $js = '';
  20. /**
  21. * SkillVisualizer constructor.
  22. * @param $skills
  23. * @param string $type
  24. */
  25. public function __construct($skills, $type = 'read')
  26. {
  27. $this->skills = $skills;
  28. $this->type = $type;
  29. $this->center_x = intval($this->offset_x + $this->canvas_x / 2 - $this->block_size / 2);
  30. }
  31. /**
  32. * @param $skill
  33. * @param $position
  34. * @param $class
  35. */
  36. public function prepare_skill_box($skill, $position, $class)
  37. {
  38. $block_id = $skill['id'];
  39. $extra_class = 'third_window';
  40. if ($skill['parent_id'] == 0) {
  41. $extra_class = 'second_window';
  42. }
  43. $this->html .= '<div id="block_'.$block_id.'" class = "open_block window '.$extra_class.' '.$class.'" style = "top:'.$position['y'].'px; left:'.$position['x'].'px;">';
  44. $content = $skill['name'];
  45. $content .= '<div class="btn-group">';
  46. $content .= Display::url(get_lang('Edit'), '#', ['id'=>'edit_block_'.$block_id, 'class'=>'edit_block btn']);
  47. $content .= Display::url('+', '#', ['id'=>'edit_block_'.$block_id, 'class'=>'edit_block btn']);
  48. $content .= '</div>';
  49. $this->html .= $content;
  50. if ($this->type == 'edit' && $skill['parent_id'] != 0) {
  51. //$this->html .= Display::url(Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL), '#', array('id'=>'edit_block_'.$block_id,'class'=>'edit_block'));
  52. //$this->html .= Display::url(Display::return_icon('add.png', get_lang('Add'), array(), ICON_SIZE_SMALL), '#', array('id'=>'edit_block_'.$block_id,'class'=>'edit_block'));
  53. //$this->html .= Display::url(Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL), '#', array('id=>"edit_block_'.$block_id,'class'=>'edit_block'));
  54. //$this->html .= Display::url(Display::return_icon('up.png', get_lang('Close'), array(), ICON_SIZE_SMALL), '#', array('id'=>'close_block_'.$block_id,'class'=>'close_block'));
  55. //$this->html .= Display::url(Display::return_icon('down.png', get_lang('Open'), array(), ICON_SIZE_SMALL), '#', array('id'=>'open_block_'.$block_id,'class'=>'open_block'));
  56. }
  57. $this->html .= '</div>';
  58. }
  59. /**
  60. * Adds a node using jplumb
  61. */
  62. private function add_item($skill, $position)
  63. {
  64. $block_id = $skill['id'];
  65. $end_point = 'readEndpoint';
  66. $class = 'default_window';
  67. if ($this->type == 'edit') {
  68. $class = 'edit_window';
  69. $end_point = 'editEndpoint';
  70. } else {
  71. if ($skill['done_by_user'] == 1) {
  72. $class = 'done_window';
  73. $end_point = 'doneEndpoint';
  74. } else {
  75. $end_point = 'defaultEndpoint';
  76. }
  77. }
  78. $this->prepare_skill_box($skill, $position, $class);
  79. if ($skill['parent_id'] == 0) {
  80. return;
  81. }
  82. //default_arrow_color
  83. $this->js .= 'var e'.$block_id.' = prepare("block_'.$block_id.'", '.$end_point.');'."\n";
  84. $this->js .= 'var e'.$skill['parent_id'].' = prepare("block_'.$skill['parent_id'].'", '.$end_point.');'."\n";
  85. $this->js .= 'jsPlumb.connect({source: e'.$block_id.', target:e'.$skill['parent_id'].'});'."\n";
  86. }
  87. /**
  88. * Displays the HTMl part of jplumb
  89. */
  90. public function display_html()
  91. {
  92. echo $this->return_html();
  93. }
  94. /**
  95. * Displays the Javascript part of jplumb
  96. */
  97. public function display_js()
  98. {
  99. echo $this->return_js();
  100. }
  101. public function return_js()
  102. {
  103. return $this->get_js();
  104. }
  105. public function return_html()
  106. {
  107. if (empty($this->skills)) {
  108. return '';
  109. }
  110. $skill_count = sizeof($this->skills);
  111. //$corner = 360 / $skill_count;
  112. $count = 0;
  113. $brothers = [];
  114. foreach ($this->skills as &$skill) {
  115. if (!in_array($skill['parent_id'], [0, 1])) {
  116. continue;
  117. }
  118. $childs = isset($skill['children']) ? count($skill['children']) : 0;
  119. //$x = round($this->offsetX * sin(deg2rad($corner * $count)));
  120. //$y = round($this->offsetY * cos(deg2rad($corner * $count)));
  121. /*if (isset($brothers[$skill['parent_id']])) {
  122. $brothers[$skill['parent_id']] +=2;
  123. } else {
  124. $brothers[$skill['parent_id']] = 1;
  125. }*/
  126. $brother_count = $brothers[$skill['id']];
  127. $my_count = 0;
  128. $parent_x = 0;
  129. if ($skill['parent_id'] == 0) {
  130. //$x = 130*$childs/2;
  131. //$x = $this->space_between_blocks_x*$childs/2;
  132. $x = $this->canvas_x / 2 - $this->block_size / 2;
  133. } else {
  134. $max = isset($this->skills[$skill['parent_id']]['children']) ? count($this->skills[$skill['parent_id']]['children']) : 0;
  135. foreach ($this->skills[$skill['parent_id']]['children'] as $id => $sk) {
  136. if ($skill['id'] == $sk['id']) {
  137. break;
  138. }
  139. $my_count++;
  140. }
  141. $parent_x = isset($this->skills[$skill['parent_id']]['x']) ? $this->skills[$skill['parent_id']]['x'] : 0;
  142. //$x = $my_count*$this->space_between_blocks_x + $parent_x + $this->block_size - ($this->space_between_blocks_x*$max/2) ;
  143. $x = $my_count * $this->space_between_blocks_x + $parent_x + $this->block_size - ($this->canvas_x / 2);
  144. }
  145. $y = $skill['level'] * $this->space_between_blocks_y;
  146. $skill['x'] = $x;
  147. $skill['y'] = $y;
  148. //$skill['description'] = "{$brothers[$skill['parent_id']]} $x - $y";
  149. //$skill['name'] = $skill['name']." | $x = $my_count * 150 + $parent_x - (150* $max/2) - 10*$childs ";
  150. $this->add_item($skill, ['x' => $this->offset_x + $x, 'y' => $this->offset_y + $y]);
  151. }
  152. return $this->get_html();
  153. }
  154. private function get_html()
  155. {
  156. return $this->html;
  157. }
  158. private function get_js()
  159. {
  160. return $this->js;
  161. }
  162. }