skill_level.php 5.1 KB

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