assign.php 5.1 KB

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