sequence.ajax.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls
  5. */
  6. use Chamilo\CoreBundle\Entity\Sequence;
  7. use Chamilo\CoreBundle\Entity\SequenceResource;
  8. use Fhaculty\Graph\Graph;
  9. use Fhaculty\Graph\Vertex;
  10. require_once '../global.inc.php';
  11. $action = isset($_REQUEST['a']) ? $_REQUEST['a'] : null;
  12. $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
  13. $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : null;
  14. $sequenceId = isset($_REQUEST['sequence_id']) ? $_REQUEST['sequence_id'] : 0;
  15. $em = Database::getManager();
  16. $repository = $em->getRepository('ChamiloCoreBundle:SequenceResource');
  17. switch ($action) {
  18. case 'graph':
  19. api_block_anonymous_users();
  20. api_protect_admin_script();
  21. switch ($type) {
  22. case 'session':
  23. $type = SequenceResource::SESSION_TYPE;
  24. /** @var Sequence $sequence */
  25. $sequence = $em->getRepository('ChamiloCoreBundle:Sequence')->find($sequenceId);
  26. if (empty($sequence)) {
  27. exit;
  28. }
  29. if ($sequence->hasGraph()) {
  30. $graph = $sequence->getUnSerializeGraph();
  31. $graph->setAttribute('graphviz.node.fontname', 'arial');
  32. $graphviz = new \Graphp\GraphViz\GraphViz();
  33. echo $graphviz->createImageHtml($graph);
  34. }
  35. break;
  36. }
  37. break;
  38. case 'get_icon':
  39. api_block_anonymous_users();
  40. api_protect_admin_script();
  41. $link = '';
  42. switch ($type) {
  43. case 'session':
  44. $type = SequenceResource::SESSION_TYPE;
  45. $showDelete = isset($_REQUEST['show_delete']) ? $_REQUEST['show_delete'] : false;
  46. $image = Display::return_icon('item-sequence.png', null, null, ICON_SIZE_LARGE);
  47. $sessionInfo = api_get_session_info($id);
  48. if (!empty($sessionInfo)) {
  49. $linkDelete = $linkUndo = '';
  50. if ($showDelete) {
  51. $linkDelete = Display::toolbarButton(
  52. get_lang('Delete'),
  53. '#',
  54. 'trash',
  55. 'default',
  56. [
  57. 'class' => 'delete_vertex btn btn-block btn-xs',
  58. 'data-id' => $id
  59. ]
  60. );
  61. $linkUndo = Display::toolbarButton(
  62. get_lang('Undo'),
  63. '#',
  64. 'undo',
  65. 'default',
  66. [
  67. 'class' => 'undo_delete btn btn-block btn-xs',
  68. 'style' => 'display: none;',
  69. 'data-id' => $id
  70. ]
  71. );
  72. }
  73. $link = '<div class="parent" data-id="' . $id . '">';
  74. $link .= '<div class="big-icon">';
  75. $link .= $image;
  76. $link .= '<div class="sequence-course">' . $sessionInfo['name'] . '</div>';
  77. $link .= '<a href="#" class="sequence-id">' . $id . '</a>';
  78. $link .= $linkDelete;
  79. $link .= $linkUndo;
  80. $link .= '</div></div>';
  81. }
  82. break;
  83. }
  84. echo $link;
  85. break;
  86. case 'delete_vertex':
  87. api_block_anonymous_users();
  88. api_protect_admin_script();
  89. $vertexId = isset($_REQUEST['vertex_id']) ? $_REQUEST['vertex_id'] : null;
  90. $type = SequenceResource::SESSION_TYPE;
  91. /** @var Sequence $sequence */
  92. $sequence = $em->getRepository('ChamiloCoreBundle:Sequence')->find($sequenceId);
  93. if (empty($sequence)) {
  94. exit;
  95. }
  96. /** @var SequenceResource $sequenceResource */
  97. $sequenceResource = $repository->findOneBy(
  98. ['resourceId' => $id, 'type' => $type, 'sequence' => $sequence]
  99. );
  100. if (empty($sequenceResource)) {
  101. exit;
  102. }
  103. if ($sequenceResource->getSequence()->hasGraph()) {
  104. $graph = $sequenceResource->getSequence()->getUnSerializeGraph();
  105. if ($graph->hasVertex($vertexId)) {
  106. $vertex = $graph->getVertex($vertexId);
  107. $vertex->destroy();
  108. /** @var SequenceResource $sequenceResource */
  109. $sequenceResourceToDelete = $repository->findOneBy(
  110. [
  111. 'resourceId' => $vertexId,
  112. 'type' => $type,
  113. 'sequence' => $sequence
  114. ]
  115. );
  116. $em->remove($sequenceResourceToDelete);
  117. $sequence->setGraphAndSerialize($graph);
  118. $em->merge($sequence);
  119. $em->flush();
  120. }
  121. }
  122. break;
  123. case 'load_resource':
  124. api_block_anonymous_users();
  125. api_protect_admin_script();
  126. // children or parent
  127. $loadResourceType = isset($_REQUEST['load_resource_type']) ? $_REQUEST['load_resource_type'] : null;
  128. $sequenceId = isset($_REQUEST['sequence_id']) ? $_REQUEST['sequence_id'] : 0;
  129. $type = SequenceResource::SESSION_TYPE;
  130. /** @var Sequence $sequence */
  131. $sequence = $em->getRepository('ChamiloCoreBundle:Sequence')->find($sequenceId);
  132. if (empty($sequence)) {
  133. exit;
  134. }
  135. /** @var SequenceResource $sequenceResource */
  136. $sequenceResource = $repository->findOneBy(
  137. ['resourceId' => $id, 'type' => $type, 'sequence' => $sequence]
  138. );
  139. if (empty($sequenceResource)) {
  140. exit;
  141. }
  142. if ($sequenceResource->hasGraph()) {
  143. $graph = $sequenceResource->getSequence()->getUnSerializeGraph();
  144. /** @var Vertex $mainVertice */
  145. if ($graph->hasVertex($id)) {
  146. $mainVertex = $graph->getVertex($id);
  147. if (!empty($mainVertex)) {
  148. $vertexList = null;
  149. switch ($loadResourceType) {
  150. case 'parent':
  151. $vertexList = $mainVertex->getVerticesEdgeFrom();
  152. break;
  153. case 'children':
  154. $vertexList = $mainVertex->getVerticesEdgeTo();
  155. break;
  156. }
  157. $list = [];
  158. if (!empty($vertexList)) {
  159. foreach ($vertexList as $vertex) {
  160. $list[] = $vertex->getId();
  161. }
  162. }
  163. if (!empty($list)) {
  164. echo implode(',', $list);
  165. }
  166. }
  167. }
  168. }
  169. break;
  170. case 'save_resource':
  171. api_block_anonymous_users();
  172. api_protect_admin_script();
  173. $parents = isset($_REQUEST['parents']) ? $_REQUEST['parents'] : '';
  174. $sequenceId = isset($_REQUEST['sequence_id']) ? $_REQUEST['sequence_id'] : 0;
  175. $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : '';
  176. if (empty($parents) || empty($sequenceId) || empty($type)) {
  177. exit;
  178. }
  179. /** @var Sequence $sequence */
  180. $sequence = $em->getRepository('ChamiloCoreBundle:Sequence')->find($sequenceId);
  181. if (empty($sequence)) {
  182. exit;
  183. }
  184. $parents = str_replace($id, '', $parents);
  185. $parents = explode(',', $parents);
  186. $parents = array_filter($parents);
  187. if ($sequence->hasGraph()) {
  188. $graph = $sequence->getUnSerializeGraph();
  189. } else {
  190. $graph = new Graph();
  191. }
  192. switch ($type) {
  193. case 'session':
  194. $type = SequenceResource::SESSION_TYPE;
  195. $sessionInfo = api_get_session_info($id);
  196. $name = $sessionInfo['name'];
  197. if ($graph->hasVertex($id)) {
  198. $main = $graph->getVertex($id);
  199. } else {
  200. $main = $graph->createVertex($id);
  201. }
  202. foreach ($parents as $parentId) {
  203. if ($graph->hasVertex($parentId)) {
  204. $parent = $graph->getVertex($parentId);
  205. if (!$parent->hasEdgeTo($main)) {
  206. $parent->createEdgeTo($main);
  207. }
  208. } else {
  209. $parent = $graph->createVertex($parentId);
  210. $parent->createEdgeTo($main);
  211. }
  212. }
  213. foreach ($parents as $parentId) {
  214. $sequenceResourceParent = $repository->findOneBy(
  215. ['resourceId' => $parentId, 'type' => $type, 'sequence' => $sequence]
  216. );
  217. if (empty($sequenceResourceParent)) {
  218. $sequenceResourceParent = new SequenceResource();
  219. $sequenceResourceParent
  220. ->setSequence($sequence)
  221. ->setType(SequenceResource::SESSION_TYPE)
  222. ->setResourceId($parentId);
  223. $em->persist($sequenceResourceParent);
  224. }
  225. }
  226. //$graphviz = new GraphViz();
  227. //echo $graphviz->createImageHtml($graph);
  228. /** @var SequenceResource $sequenceResource */
  229. $sequenceResource = $repository->findOneBy(
  230. ['resourceId' => $id, 'type' => $type, 'sequence' => $sequence]
  231. );
  232. if (empty($sequenceResource)) {
  233. // Create
  234. $sequence->setGraphAndSerialize($graph);
  235. $sequenceResource = new SequenceResource();
  236. $sequenceResource
  237. ->setSequence($sequence)
  238. ->setType(SequenceResource::SESSION_TYPE)
  239. ->setResourceId($id);
  240. } else {
  241. // Update
  242. $sequenceResource->getSequence()->setGraphAndSerialize($graph);
  243. }
  244. $em->persist($sequenceResource);
  245. $em->flush();
  246. echo Display::return_message(get_lang('Saved'), 'success');
  247. break;
  248. }
  249. break;
  250. case 'get_requirements':
  251. $userId = api_get_user_id();
  252. switch ($type) {
  253. case SequenceResource::SESSION_TYPE:
  254. $session = api_get_session_info($id);
  255. $sequences = $repository->getRequirements(
  256. $session['id'],
  257. $type
  258. );
  259. if (count($sequences) === 0) {
  260. break;
  261. }
  262. $sequenceList = SequenceResourceManager::checkRequirementsForUser($sequences, $type, $userId);
  263. $allowSubscription = SequenceResourceManager::checkSequenceAreCompleted($sequenceList);
  264. $courseController = new CoursesController();
  265. $view = new Template(null, false, false, false, false, false);
  266. $view->assign('sequences', $sequenceList);
  267. $view->assign('allow_subscription', $allowSubscription);
  268. if ($allowSubscription) {
  269. $view->assign(
  270. 'subscribe_button',
  271. $courseController->getRegisteredInSessionButton(
  272. $session['id'],
  273. $session['name'],
  274. false
  275. )
  276. );
  277. }
  278. $template = $view->get_template(
  279. 'sequence_resource/session_requirements.tpl'
  280. );
  281. $view->display($template);
  282. break;
  283. }
  284. break;
  285. }