assign.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\Skill;
  4. use Chamilo\CoreBundle\Entity\SkillRelUser;
  5. /**
  6. * Page for assign skills to a user
  7. *
  8. * @autor: Jose Loguercio <jose.loguercio@beeznest.com>
  9. * @package chamilo.badge
  10. */
  11. require_once __DIR__.'/../inc/global.inc.php';
  12. if (!api_is_platform_admin(false, true) && !api_is_student_boss()) {
  13. api_not_allowed(true);
  14. }
  15. if (!isset($_REQUEST['user'])) {
  16. api_not_allowed(true);
  17. }
  18. $entityManager = Database::getManager();
  19. $skillRepo = $entityManager->getRepository('ChamiloCoreBundle:Skill');
  20. $skillRelSkill = $entityManager->getRepository('ChamiloCoreBundle:SkillRelSkill');
  21. $skillLevelRepo = $entityManager->getRepository('ChamiloSkillBundle:Level');
  22. $skillUserRepo = $entityManager->getRepository('ChamiloCoreBundle:SkillRelUser');
  23. $user = $entityManager->find('ChamiloUserBundle:User', $_REQUEST['user']);
  24. if (!$user) {
  25. Display::addFlash(
  26. Display::return_message(get_lang('NoUser'), 'error')
  27. );
  28. header('Location: '.api_get_path(WEB_PATH));
  29. exit;
  30. }
  31. $skills = $skillRepo->findBy([
  32. 'status' => Skill::STATUS_ENABLED
  33. ]);
  34. $url = api_get_path(WEB_CODE_PATH)."badge/assign.php?user=".$_REQUEST['user']."&id=";
  35. $htmlHeadXtra[] = '<script>
  36. $( document ).ready(function() {
  37. $("#skill").on("change", function() {
  38. $(location).attr("href", "'. $url.'"+$(this).val());
  39. });
  40. });
  41. </script>';
  42. $skillsOptions = [];
  43. $acquiredLevel = [];
  44. $formDefaultValues = [];
  45. foreach ($skills as $skill) {
  46. $skillsOptions[$skill->getId()] = $skill->getName();
  47. }
  48. $skillId = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : key($skillsOptions);
  49. $profile = $skillRepo->find($skillId)->getProfile();
  50. if (!$profile) {
  51. $skillRelSkill = new SkillRelSkill();
  52. $parents = $skillRelSkill->get_skill_parents($skillId);
  53. krsort($parents);
  54. foreach ($parents as $parent) {
  55. $skillParentId = $parent['skill_id'];
  56. $profile = $skillRepo->find($skillParentId)->getProfile();
  57. if ($profile) {
  58. break;
  59. }
  60. if (!$profile && $parent['parent_id'] == 0) {
  61. $profile = $skillLevelRepo->findAll();
  62. $profile = isset($profile[0]) ? $profile[0] : false;
  63. }
  64. }
  65. }
  66. if ($profile) {
  67. $profileId = $profile->getId();
  68. $levels = $skillLevelRepo->findBy([
  69. 'profile' => $profileId
  70. ]);
  71. foreach ($levels as $level) {
  72. $profileLevels[$level->getPosition()][$level->getId()] = $level->getName();
  73. }
  74. ksort($profileLevels); // Sort the array by Position.
  75. foreach ($profileLevels as $profileLevel) {
  76. $profileId = key($profileLevel);
  77. $acquiredLevel[$profileId] = $profileLevel[$profileId];
  78. }
  79. }
  80. $formDefaultValues = ['skill' => $skillId];
  81. $form = new FormValidator('assign_skill');
  82. $form->addText('user_name', get_lang('UserName'), false);
  83. $form->addSelect('skill', get_lang('Skill'), $skillsOptions, ['id' => 'skill']);
  84. $form->addHidden('user', $user->getId());
  85. $form->addHidden('id', $skillId);
  86. $form->addRule('skill', get_lang('ThisFieldIsRequired'), 'required');
  87. $form->addSelect('acquired_level', get_lang('AcquiredLevel'), $acquiredLevel);
  88. $form->addRule('acquired_level', get_lang('ThisFieldIsRequired'), 'required');
  89. $form->addTextarea('argumentation', get_lang('Argumentation'), ['rows' => 6]);
  90. $form->addRule('argumentation', get_lang('ThisFieldIsRequired'), 'required');
  91. $form->addRule('argumentation', sprintf(get_lang('ThisTextShouldBeAtLeastXCharsLong'), 10), 'mintext', 10);
  92. $form->applyFilter('argumentation', 'trim');
  93. $form->addButtonSave(get_lang('Save'));
  94. $form->setDefaults($formDefaultValues);
  95. if ($form->validate()) {
  96. $values = $form->exportValues();
  97. $skill = $skillRepo->find($values['skill']);
  98. if (!$skill) {
  99. Display::addFlash(
  100. Display::return_message(get_lang('SkillNotFound'), 'error')
  101. );
  102. header('Location: '.api_get_self().'?'.http_build_query(['user' => $user->getId()]));
  103. exit;
  104. }
  105. if ($user->hasSkill($skill)) {
  106. Display::addFlash(
  107. Display::return_message(
  108. sprintf(get_lang('TheUserXHasAlreadyAchievedTheSkillY'), $user->getCompleteName(), $skill->getName()),
  109. 'warning'
  110. )
  111. );
  112. header('Location: '.api_get_self().'?'.http_build_query(['user' => $user->getId()]));
  113. exit;
  114. }
  115. $skillUser = new SkillRelUser();
  116. $skillUser->setUser($user);
  117. $skillUser->setSkill($skill);
  118. $level = $skillLevelRepo->find(intval($values['acquired_level']));
  119. $skillUser->setAcquiredLevel($level);
  120. $skillUser->setArgumentation($values['argumentation']);
  121. $skillUser->setArgumentationAuthorId(api_get_user_id());
  122. $skillUser->setAcquiredSkillAt(new DateTime());
  123. $skillUser->setAssignedBy(0);
  124. $entityManager->persist($skillUser);
  125. $entityManager->flush();
  126. Display::addFlash(
  127. Display::return_message(
  128. sprintf(get_lang('SkillXAssignedToUserY'), $skill->getName(), $user->getCompleteName()),
  129. 'success'
  130. )
  131. );
  132. header('Location: '.api_get_path(WEB_PATH)."badge/{$skillUser->getId()}");
  133. exit;
  134. }
  135. $form->setDefaults(['user_name' => $user->getCompleteName()]);
  136. $form->freeze(['user_name']);
  137. $template = new Template('');
  138. $template->assign('header', get_lang('AssignSkill'));
  139. $template->assign('content', $form->returnForm());
  140. $template->display_one_col_template();