skill.visualizer.lib.php 7.4 KB

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