skill_level.php 4.3 KB

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