skill.visualizer.lib.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. public function prepare_skill_box($skill, $position, $class)
  32. {
  33. $block_id = $skill['id'];
  34. $extra_class = 'third_window';
  35. if ($skill['parent_id'] == 0) {
  36. $extra_class = 'second_window';
  37. }
  38. $this->html .= '<div id="block_'.$block_id.'" class = "open_block window '.$extra_class.' '.$class.'" style = "top:'.$position['y'].'px; left:'.$position['x'].'px;">';
  39. $content = $skill['name'];
  40. $content .= '<div class="btn-group">';
  41. $content .= Display::url(get_lang('Edit'), '#', array('id'=>'edit_block_'.$block_id, 'class'=>'edit_block btn'));
  42. $content .= Display::url('+', '#', array('id'=>'edit_block_'.$block_id, 'class'=>'edit_block btn'));
  43. $content .= '</div>';
  44. $this->html .= $content.' '.$gradebook_string;
  45. if ($this->type == 'edit' && $skill['parent_id'] != 0) {
  46. //$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'));
  47. //$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'));
  48. //$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'));
  49. //$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'));
  50. //$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'));
  51. }
  52. $this->html .= '</div>';
  53. }
  54. /**
  55. * Adds a node using jplumb
  56. */
  57. private function add_item($skill, $position)
  58. {
  59. $block_id = $skill['id'];
  60. $end_point = 'readEndpoint';
  61. $class = 'default_window';
  62. if ($this->type == 'edit') {
  63. $class = 'edit_window';
  64. $end_point = 'editEndpoint';
  65. } else {
  66. if ($skill['done_by_user'] == 1) {
  67. $class = 'done_window';
  68. $end_point = 'doneEndpoint';
  69. } else {
  70. $end_point = 'defaultEndpoint';
  71. }
  72. }
  73. $this->prepare_skill_box($skill, $position, $class);
  74. if ($skill['parent_id'] == 0) {
  75. return;
  76. }
  77. //default_arrow_color
  78. $this->js .= 'var e'.$block_id.' = prepare("block_'.$block_id.'", '.$end_point.');'."\n";
  79. $this->js .= 'var e'.$skill['parent_id'].' = prepare("block_'.$skill['parent_id'].'", '.$end_point.');'."\n";
  80. $this->js .= 'jsPlumb.connect({source: e'.$block_id.', target:e'.$skill['parent_id'].'});'."\n"; ;
  81. }
  82. /**
  83. * Displays the HTMl part of jplumb
  84. */
  85. public function display_html()
  86. {
  87. echo $this->return_html();
  88. }
  89. /**
  90. * Displays the Javascript part of jplumb
  91. */
  92. public function display_js()
  93. {
  94. echo $this->return_js();
  95. }
  96. public function return_js()
  97. {
  98. return $this->get_js();
  99. }
  100. public function return_html()
  101. {
  102. if (empty($this->skills)) {
  103. return '';
  104. }
  105. $skill_count = sizeof($this->skills);
  106. //$corner = 360 / $skill_count;
  107. $count = 0;
  108. $brothers = array();
  109. foreach ($this->skills as &$skill) {
  110. if (!in_array($skill['parent_id'], array(0, 1))) {
  111. continue;
  112. }
  113. $childs = isset($skill['children']) ? count($skill['children']) : 0;
  114. //$x = round($this->offsetX * sin(deg2rad($corner * $count)));
  115. //$y = round($this->offsetY * cos(deg2rad($corner * $count)));
  116. /*if (isset($brothers[$skill['parent_id']])) {
  117. $brothers[$skill['parent_id']] +=2;
  118. } else {
  119. $brothers[$skill['parent_id']] = 1;
  120. }*/
  121. $brother_count = $brothers[$skill['id']];
  122. $my_count = 0;
  123. $parent_x = 0;
  124. if ($skill['parent_id'] == 0) {
  125. //$x = 130*$childs/2;
  126. //$x = $this->space_between_blocks_x*$childs/2;
  127. $x = $this->canvas_x / 2 - $this->block_size / 2;
  128. } else {
  129. $max = isset($this->skills[$skill['parent_id']]['children']) ? count($this->skills[$skill['parent_id']]['children']) : 0;
  130. foreach ($this->skills[$skill['parent_id']]['children'] as $id => $sk) {
  131. if ($skill['id'] == $sk['id']) {
  132. break;
  133. }
  134. $my_count++;
  135. }
  136. $parent_x = isset($this->skills[$skill['parent_id']]['x']) ? $this->skills[$skill['parent_id']]['x'] : 0;
  137. //$x = $my_count*$this->space_between_blocks_x + $parent_x + $this->block_size - ($this->space_between_blocks_x*$max/2) ;
  138. $x = $my_count * $this->space_between_blocks_x + $parent_x + $this->block_size - ($this->canvas_x / 2);
  139. }
  140. $y = $skill['level'] * $this->space_between_blocks_y;
  141. $skill['x'] = $x;
  142. $skill['y'] = $y;
  143. //$skill['description'] = "{$brothers[$skill['parent_id']]} $x - $y";
  144. //$skill['name'] = $skill['name']." | $x = $my_count * 150 + $parent_x - (150* $max/2) - 10*$childs ";
  145. $this->add_item($skill, array('x' => $this->offset_x + $x, 'y' => $this->offset_y + $y));
  146. }
  147. return $this->get_html();
  148. }
  149. private function get_html()
  150. {
  151. return $this->html;
  152. }
  153. private function get_js()
  154. {
  155. return $this->js;
  156. }
  157. }