sequence.ajax.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls
  5. */
  6. use Chamilo\CoreBundle\Entity\SequenceResource;
  7. use Fhaculty\Graph\Graph;
  8. use Fhaculty\Graph\Vertex;
  9. use Graphp\GraphViz\GraphViz;
  10. require_once '../global.inc.php';
  11. api_protect_admin_script();
  12. $action = isset($_REQUEST['a']) ? $_REQUEST['a'] : null;
  13. $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
  14. $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : null;
  15. $manager = Database::getManager();
  16. $repository = $manager->getRepository('ChamiloCoreBundle:SequenceResource');
  17. switch ($action) {
  18. case 'get_icon':
  19. $link = '';
  20. switch ($type) {
  21. case 'session':
  22. $showDelete = isset($_REQUEST['show_delete']) ? $_REQUEST['show_delete'] : false;
  23. $image = Display::return_icon('window_list.png');
  24. $sessionInfo = api_get_session_info($id);
  25. if (!empty($sessionInfo)) {
  26. $linkDelete = '';
  27. if ($showDelete) {
  28. $linkDelete = Display::url(
  29. get_lang('Delete'),
  30. '#',
  31. ['class' => 'delete_vertex', 'data-id' => $id]
  32. );
  33. }
  34. $link = '<div class="parent" data-id="'.$id.'">'.
  35. $image.' '.$sessionInfo['name'].$linkDelete.
  36. '</div>';
  37. }
  38. break;
  39. }
  40. echo $link;
  41. break;
  42. case 'delete_vertex':
  43. $vertexId = isset($_REQUEST['vertex_id']) ? $_REQUEST['vertex_id'] : null;
  44. /** @var SequenceResource $resource */
  45. $resource = $repository->findOneByResourceId($id);
  46. if (empty($resource)) {
  47. exit;
  48. }
  49. $graph = $resource->getGraph();
  50. if (!empty($graph)) {
  51. /** @var Graph $graph */
  52. $graph = unserialize($graph);
  53. if ($graph->hasVertex($vertexId)) {
  54. $vertex = $graph->getVertex($vertexId);
  55. $vertex->destroy();
  56. $resource->setGraph(serialize($graph));
  57. $manager->persist($resource);
  58. $manager->flush();
  59. }
  60. }
  61. break;
  62. case 'load_resource':
  63. // children or parent
  64. $loadResourceType = isset($_REQUEST['load_resource_type']) ? $_REQUEST['load_resource_type'] : null;
  65. /** @var SequenceResource $resource */
  66. $resource = $repository->findOneByResourceId($id);
  67. if (empty($resource)) {
  68. exit;
  69. }
  70. $graph = $resource->getGraph();
  71. if (!empty($graph)) {
  72. /** @var Graph $graph */
  73. $graph = unserialize($graph);
  74. $graphviz = new GraphViz();
  75. //echo $graphviz->createImageHtml($graph);
  76. /** @var Vertex $mainVertice */
  77. if ($graph->hasVertex($id)) {
  78. $mainVertice = $graph->getVertex($id);
  79. if (!empty($mainVertice)) {
  80. $list = [];
  81. switch ($loadResourceType) {
  82. case 'parent':
  83. $verticeList = $mainVertice->getVerticesEdgeFrom();
  84. break;
  85. case 'children':
  86. $verticeList = $mainVertice->getVerticesEdgeTo();
  87. break;
  88. }
  89. foreach ($verticeList as $vertice) {
  90. $list[] = $vertice->getId();
  91. }
  92. if (!empty($list)) {
  93. echo implode(',', $list);
  94. }
  95. }
  96. }
  97. }
  98. break;
  99. case 'save_resource':
  100. $parents = isset($_REQUEST['parents']) ? $_REQUEST['parents'] : null;
  101. $parents = str_replace($id, '', $parents);
  102. $parents = explode(',', $parents);
  103. $parents = array_filter($parents);
  104. $graph = new Graph();
  105. switch ($type) {
  106. case 'session':
  107. $sessionInfo = api_get_session_info($id);
  108. $name = $sessionInfo['name'];
  109. $main = $graph->createVertex($id);
  110. foreach ($parents as $parentId) {
  111. $parent = $graph->createVertex($parentId);
  112. // Check if parent Id exists in the DB
  113. /** @var SequenceResource $resource */
  114. $resource = $repository->findOneByResourceId($parentId);
  115. if ($resource) {
  116. $parentGraph = $resource->getGraph();
  117. if (!empty($parentGraph)) {
  118. /** @var Graph $parentGraph */
  119. $parentGraph = unserialize($parentGraph);
  120. try {
  121. $vertex = $parentGraph->getVertex($parentId);
  122. $parentMain = $parentGraph->createVertex($id);
  123. $vertex->createEdgeTo($parentMain);
  124. $resource->setGraph(serialize($parentGraph));
  125. $manager->persist($resource);
  126. $manager->flush();
  127. /*
  128. $graphviz = new GraphViz();
  129. echo $graphviz->createImageHtml($parentGraph);*/
  130. } catch (Exception $e) {
  131. }
  132. }
  133. }
  134. $parent->createEdgeTo($main);
  135. }
  136. $graphviz = new GraphViz();
  137. //echo $graphviz->createImageHtml($graph);
  138. /** @var SequenceResource $sequence */
  139. $sequence = $repository->findOneByResourceId($id);
  140. if (empty($sequence)) {
  141. $sequence = new SequenceResource();
  142. $sequence
  143. ->setGraph(serialize($graph))
  144. ->setType(SequenceResource::SESSION_TYPE)
  145. ->setResourceId($id);
  146. } else {
  147. $sequence->setGraph(serialize($graph));
  148. }
  149. $manager->persist($sequence);
  150. $manager->flush();
  151. break;
  152. }
  153. break;
  154. }