skill_profile.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\SkillBundle\Entity\Level;
  4. use Chamilo\SkillBundle\Entity\Profile;
  5. /**
  6. * Add a skill Profile.
  7. *
  8. * @package chamilo.skill
  9. */
  10. $cidReset = true;
  11. require_once __DIR__.'/../inc/global.inc.php';
  12. api_protect_admin_script();
  13. $em = Database::getManager();
  14. $list = $em->getRepository('ChamiloSkillBundle:Profile')->findAll();
  15. $listAction = api_get_self();
  16. $action = '';
  17. if (isset($_GET['action']) && in_array($_GET['action'], ['add', 'edit', 'delete', 'move_up', 'move_down'])) {
  18. $action = $_GET['action'];
  19. }
  20. $id = isset($_GET['id']) ? $_GET['id'] : '';
  21. $item = null;
  22. if (!empty($id)) {
  23. $item = $em->getRepository('ChamiloSkillBundle:Profile')->find($id);
  24. if (!$item) {
  25. api_not_allowed();
  26. }
  27. }
  28. $form = new FormValidator('Profile', 'GET', api_get_self().'?action='.$action.'&id='.$id);
  29. $form->addText('name', get_lang('Name'));
  30. $form->addHidden('action', $action);
  31. $form->addHidden('id', $id);
  32. $form->addButtonSave(get_lang('Save'));
  33. if (!empty($item)) {
  34. $form->setDefaults(['name' => $item->getName()]);
  35. }
  36. $formToDisplay = $form->returnForm();
  37. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('Administration')];
  38. $interbreadcrumb[] = ['url' => api_get_path(WEB_CODE_PATH).'admin/skill.php', 'name' => get_lang('Manage skills levels')];
  39. $interbreadcrumb[] = ['url' => api_get_self(), 'name' => get_lang('Skill profile')];
  40. $toolbar = null;
  41. $tpl = new Template($action);
  42. switch ($action) {
  43. case 'move_up':
  44. /** @var Level $item */
  45. $item = $em->getRepository('ChamiloSkillBundle:Level')->find($_GET['level_id']);
  46. if ($item) {
  47. $position = $item->getPosition();
  48. if (!empty($position)) {
  49. $item->setPosition($position - 1);
  50. }
  51. $em->persist($item);
  52. $em->flush();
  53. Display::addFlash(Display::return_message(get_lang('Update successful')));
  54. }
  55. header('Location: '.$listAction);
  56. exit;
  57. break;
  58. case 'move_down':
  59. /** @var Level $item */
  60. $item = $em->getRepository('ChamiloSkillBundle:Level')->find($_GET['level_id']);
  61. if ($item) {
  62. $position = $item->getPosition();
  63. $item->setPosition($position + 1);
  64. $em->persist($item);
  65. $em->flush();
  66. Display::addFlash(Display::return_message(get_lang('Update successful')));
  67. }
  68. header('Location: '.$listAction);
  69. exit;
  70. break;
  71. case 'add':
  72. $tpl->assign('form', $formToDisplay);
  73. if ($form->validate()) {
  74. $values = $form->exportValues();
  75. $item = new Profile();
  76. $item->setName($values['name']);
  77. $em->persist($item);
  78. $em->flush();
  79. Display::addFlash(Display::return_message(get_lang('Added')));
  80. header('Location: '.$listAction);
  81. exit;
  82. }
  83. $toolbar = Display::url(
  84. Display::return_icon(
  85. 'list_badges.png',
  86. get_lang('List'),
  87. null,
  88. ICON_SIZE_MEDIUM
  89. ),
  90. $listAction,
  91. ['title' => get_lang('List')]
  92. );
  93. break;
  94. case 'edit':
  95. $tpl->assign('form', $formToDisplay);
  96. $toolbar = Display::url(
  97. Display::return_icon(
  98. 'list_badges.png',
  99. get_lang('List'),
  100. null,
  101. ICON_SIZE_MEDIUM
  102. ),
  103. $listAction,
  104. ['title' => get_lang('List')]
  105. );
  106. if ($form->validate()) {
  107. $values = $form->exportValues();
  108. $item->setName($values['name']);
  109. $em->persist($item);
  110. $em->flush();
  111. Display::addFlash(Display::return_message(get_lang('Update successful')));
  112. header('Location: '.$listAction);
  113. exit;
  114. }
  115. break;
  116. case 'delete':
  117. $toolbar = Display::url(
  118. Display::return_icon(
  119. 'list_badges.png',
  120. get_lang('List'),
  121. null,
  122. ICON_SIZE_MEDIUM
  123. ),
  124. $listAction,
  125. ['title' => get_lang('List')]
  126. );
  127. try {
  128. $em->remove($item);
  129. $em->flush();
  130. Display::addFlash(Display::return_message(get_lang('Deleted')));
  131. } catch (Exception $e) {
  132. Display::addFlash(Display::return_message(get_lang('Delete error'), 'error'));
  133. }
  134. header('Location: '.$listAction);
  135. exit;
  136. break;
  137. default:
  138. $toolbar = Display::url(
  139. Display::return_icon(
  140. 'add.png',
  141. get_lang('Add'),
  142. null,
  143. ICON_SIZE_MEDIUM
  144. ),
  145. api_get_self().'?action=add',
  146. ['title' => get_lang('Add')]
  147. );
  148. }
  149. $tpl->assign('list', $list);
  150. $templateName = $tpl->get_template('admin/skill_profile.tpl');
  151. $contentTemplate = $tpl->fetch($templateName);
  152. if ($toolbar) {
  153. $tpl->assign(
  154. 'actions',
  155. Display::toolbarAction('toolbar', [$toolbar])
  156. );
  157. }
  158. $tpl->assign('content', $contentTemplate);
  159. $tpl->display_one_col_template();