skill.visualizer.lib.php 6.5 KB

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