skill.visualizer.lib.php 6.6 KB

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