skill_level.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 '../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. $tpl->assign('actions', Display::url(get_lang('List'), $listAction));
  62. break;
  63. case 'edit':
  64. $tpl->assign('form', $formToDisplay);
  65. $tpl->assign('actions', Display::url(get_lang('List'), $listAction));
  66. if ($form->validate()) {
  67. $values = $form->exportValues();
  68. $item->setName($values['name']);
  69. $item->setShortName($values['short_name']);
  70. $profile = $em->getRepository('ChamiloSkillBundle:Profile')->find($values['profile_id']);
  71. $item->setProfile($profile);
  72. $em->persist($item);
  73. $em->flush();
  74. header('Location: '.$listAction);
  75. exit;
  76. }
  77. break;
  78. case 'delete':
  79. $tpl->assign('actions', Display::url(get_lang('List'), $listAction));
  80. $em->remove($item);
  81. $em->flush();
  82. header('Location: '.$listAction);
  83. exit;
  84. break;
  85. default:
  86. $tpl->assign('actions', Display::url(get_lang('Add'), api_get_self().'?action=add'));
  87. }
  88. $tpl->assign('list', $list);
  89. $templateName = $tpl->get_template('admin/skill_level.tpl');
  90. $contentTemplate = $tpl->fetch($templateName);
  91. $tpl->assign('content', $contentTemplate);
  92. $tpl->display_one_col_template();