assign.php 6.7 KB

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