sequence.ajax.php 12 KB

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