skill.visualizer.lib.php 7.1 KB

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