skill.visualizer.lib.php 6.9 KB

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