123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- <?php
- /* For licensing terms, see /license.txt */
- use Chamilo\CoreBundle\Entity\Skill;
- use Chamilo\CoreBundle\Entity\SkillRelUser;
- use Chamilo\UserBundle\Entity\User;
- use Skill as SkillManager;
- /**
- * Page for assign skills to a user.
- *
- * @author: Jose Loguercio <jose.loguercio@beeznest.com>
- *
- * @package chamilo.badge
- */
- require_once __DIR__.'/../inc/global.inc.php';
- $userId = isset($_REQUEST['user']) ? (int) $_REQUEST['user'] : 0;
- if (empty($userId)) {
- api_not_allowed(true);
- }
- SkillManager::isAllowed($userId);
- $user = api_get_user_entity($userId);
- if (!$user) {
- api_not_allowed(true);
- }
- $entityManager = Database::getManager();
- $skillManager = new SkillManager();
- $skillRepo = $entityManager->getRepository('ChamiloCoreBundle:Skill');
- $skillRelSkill = $entityManager->getRepository('ChamiloCoreBundle:SkillRelSkill');
- $skillLevelRepo = $entityManager->getRepository('ChamiloSkillBundle:Level');
- $skillUserRepo = $entityManager->getRepository('ChamiloCoreBundle:SkillRelUser');
- $skillLevels = api_get_configuration_value('skill_levels_names');
- $skillsOptions = ['' => get_lang('Select')];
- $acquiredLevel = ['' => get_lang('None')];
- $formDefaultValues = [];
- if (empty($skillLevels)) {
- $skills = $skillRepo->findBy([
- 'status' => Skill::STATUS_ENABLED,
- ]);
- /** @var Skill $skill */
- foreach ($skills as $skill) {
- $skillsOptions[$skill->getId()] = $skill->getName();
- }
- } else {
- // Get only root elements
- $skills = $skillManager->getChildren(1);
- foreach ($skills as $skill) {
- $skillsOptions[$skill['data']['id']] = $skill['data']['name'];
- }
- }
- $skillIdFromGet = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
- $currentValue = isset($_REQUEST['current_value']) ? (int) $_REQUEST['current_value'] : 0;
- $currentLevel = isset($_REQUEST['current']) ? (int) str_replace('sub_skill_id_', '', $_REQUEST['current']) : 0;
- $subSkillList = isset($_REQUEST['sub_skill_list']) ? explode(',', $_REQUEST['sub_skill_list']) : [];
- $subSkillList = array_unique($subSkillList);
- if (!empty($subSkillList)) {
- // Compare asked skill with current level
- $correctLevel = false;
- if (isset($subSkillList[$currentLevel]) && $subSkillList[$currentLevel] == $currentValue) {
- $correctLevel = true;
- }
- // Level is wrong probably user change the level. Fix the subSkillList array
- if (!$correctLevel) {
- $newSubSkillList = [];
- $counter = 0;
- foreach ($subSkillList as $subSkillId) {
- if ($counter == $currentLevel) {
- $subSkillId = $currentValue;
- }
- $newSubSkillList[$counter] = $subSkillId;
- if ($counter == $currentLevel) {
- break;
- }
- $counter++;
- }
- $subSkillList = $newSubSkillList;
- }
- }
- if (!empty($currentLevel)) {
- $level = $currentLevel + 1;
- if ($level < count($subSkillList)) {
- $remove = count($subSkillList) - $currentLevel;
- $newSubSkillList = array_slice($subSkillList, 0, count($subSkillList) - $level);
- $subSkillList = $newSubSkillList;
- }
- }
- $skillId = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : key($skillsOptions);
- $skill = $skillRepo->find($skillId);
- $profile = false;
- if ($skill) {
- $profile = $skill->getProfile();
- }
- if (!empty($subSkillList)) {
- $skillFromLastSkill = $skillRepo->find(end($subSkillList));
- if ($skillFromLastSkill) {
- $profile = $skillFromLastSkill->getProfile();
- }
- }
- if (!$profile) {
- $skillRelSkill = new SkillRelSkill();
- $parents = $skillRelSkill->getSkillParents($skillId);
- krsort($parents);
- foreach ($parents as $parent) {
- $skillParentId = $parent['skill_id'];
- $profile = $skillRepo->find($skillParentId)->getProfile();
- if ($profile) {
- break;
- }
- if (!$profile && $parent['parent_id'] == 0) {
- $profile = $skillLevelRepo->findAll();
- $profile = isset($profile[0]) ? $profile[0] : false;
- }
- }
- }
- if ($profile) {
- $profileId = $profile->getId();
- $levels = $skillLevelRepo->findBy([
- 'profile' => $profileId,
- ]);
- $profileLevels = [];
- foreach ($levels as $level) {
- $profileLevels[$level->getPosition()][$level->getId()] = $level->getName();
- }
- ksort($profileLevels); // Sort the array by Position.
- foreach ($profileLevels as $profileLevel) {
- $profileId = key($profileLevel);
- $acquiredLevel[$profileId] = $profileLevel[$profileId];
- }
- }
- $formDefaultValues = ['skill' => $skillId];
- $newSubSkillList = [];
- $disableList = [];
- $currentUrl = api_get_self().'?user='.$userId.'¤t='.$currentLevel;
- $form = new FormValidator('assign_skill', 'POST', $currentUrl);
- $form->addHeader(get_lang('AssignSkill'));
- $form->addText('user_name', get_lang('UserName'), false);
- $levelName = get_lang('Skill');
- if (!empty($skillLevels)) {
- if (isset($skillLevels['levels'][1])) {
- $levelName = get_lang($skillLevels['levels'][1]);
- }
- }
- $form->addSelect('skill', $levelName, $skillsOptions, ['id' => 'skill']);
- if (!empty($skillIdFromGet)) {
- if (empty($subSkillList)) {
- $subSkillList[] = $skillIdFromGet;
- }
- $oldSkill = $skillRepo->find($skillIdFromGet);
- $counter = 0;
- foreach ($subSkillList as $subSkillId) {
- $children = $skillManager->getChildren($subSkillId);
- if (isset($subSkillList[$counter - 1])) {
- $oldSkill = $skillRepo->find($subSkillList[$counter]);
- }
- $skillsOptions = [];
- if ($oldSkill) {
- $skillsOptions = [$oldSkill->getId() => ' -- '.$oldSkill->getName()];
- }
- if ($counter < count($subSkillList) - 1) {
- $disableList[] = 'sub_skill_id_'.($counter + 1);
- }
- foreach ($children as $child) {
- $skillsOptions[$child['id']] = $child['data']['name'];
- }
- $levelName = get_lang('SubSkill');
- if (!empty($skillLevels)) {
- if (isset($skillLevels['levels'][$counter + 2])) {
- $levelName = get_lang($skillLevels['levels'][$counter + 2]);
- }
- }
- $form->addSelect(
- 'sub_skill_id_'.($counter + 1),
- $levelName,
- $skillsOptions,
- [
- 'id' => 'sub_skill_id_'.($counter + 1),
- 'class' => 'sub_skill',
- ]
- );
- if (isset($subSkillList[$counter + 1])) {
- $nextSkill = $skillRepo->find($subSkillList[$counter + 1]);
- if ($nextSkill) {
- $formDefaultValues['sub_skill_id_'.($counter + 1)] = $nextSkill->getId();
- }
- }
- $newSubSkillList[] = $subSkillId;
- $counter++;
- }
- $subSkillList = $newSubSkillList;
- }
- $subSkillListToString = implode(',', $subSkillList);
- $currentUrl = api_get_self().'?user='.$userId.'¤t='.$currentLevel.'&sub_skill_list='.$subSkillListToString;
- $form->addHidden('sub_skill_list', $subSkillListToString);
- $form->addHidden('user', $user->getId());
- $form->addHidden('id', $skillId);
- $form->addRule('skill', get_lang('ThisFieldIsRequired'), 'required');
- $showLevels = api_get_configuration_value('hide_skill_levels') === false;
- if ($showLevels) {
- $form->addSelect('acquired_level', get_lang('AcquiredLevel'), $acquiredLevel);
- //$form->addRule('acquired_level', get_lang('ThisFieldIsRequired'), 'required');
- }
- $form->addTextarea('argumentation', get_lang('Argumentation'), ['rows' => 6]);
- $form->addRule('argumentation', get_lang('ThisFieldIsRequired'), 'required');
- $form->addRule(
- 'argumentation',
- sprintf(get_lang('ThisTextShouldBeAtLeastXCharsLong'), 10),
- 'mintext',
- 10
- );
- $form->applyFilter('argumentation', 'trim');
- $form->addButtonSave(get_lang('Save'));
- $form->setDefaults($formDefaultValues);
- if ($form->validate()) {
- $values = $form->exportValues();
- $skillToProcess = $values['id'];
- if (!empty($subSkillList)) {
- $counter = 1;
- foreach ($subSkillList as $subSkill) {
- if (isset($values["sub_skill_id_$counter"])) {
- $skillToProcess = $values["sub_skill_id_$counter"];
- }
- $counter++;
- }
- }
- $skill = $skillRepo->find($skillToProcess);
- if (!$skill) {
- Display::addFlash(
- Display::return_message(get_lang('SkillNotFound'), 'error')
- );
- header('Location: '.api_get_self().'?'.$currentUrl);
- exit;
- }
- if ($user->hasSkill($skill)) {
- Display::addFlash(
- Display::return_message(
- sprintf(
- get_lang('TheUserXHasAlreadyAchievedTheSkillY'),
- $user->getCompleteName(),
- $skill->getName()
- ),
- 'warning'
- )
- );
- header('Location: '.$currentUrl);
- exit;
- }
- $skillUser = new SkillRelUser();
- $skillUser->setUser($user);
- $skillUser->setSkill($skill);
- if ($showLevels) {
- $level = $skillLevelRepo->find(intval($values['acquired_level']));
- $skillUser->setAcquiredLevel($level);
- }
- $skillUser->setArgumentation($values['argumentation']);
- $skillUser->setArgumentationAuthorId(api_get_user_id());
- $skillUser->setAcquiredSkillAt(new DateTime());
- $skillUser->setAssignedBy(0);
- $entityManager->persist($skillUser);
- $entityManager->flush();
- // Send email depending of children_auto_threshold
- $skillRelSkill = new SkillRelSkill();
- $skillModel = new \Skill();
- $parents = $skillModel->getDirectParents($skillToProcess);
- $extraFieldValue = new ExtraFieldValue('skill');
- foreach ($parents as $parentInfo) {
- $parentId = $parentInfo['skill_id'];
- $parentData = $skillModel->get($parentId);
- $data = $extraFieldValue->get_values_by_handler_and_field_variable($parentId, 'children_auto_threshold');
- if (!empty($data) && !empty($data['value'])) {
- // Search X children
- $requiredSkills = $data['value'];
- $children = $skillRelSkill->getChildren($parentId);
- $counter = 0;
- foreach ($children as $child) {
- if ($skillModel->userHasSkill($userId, $child['id'])) {
- $counter++;
- }
- }
- if ($counter >= $requiredSkills) {
- $bossList = UserManager::getStudentBossList($userId);
- if (!empty($bossList)) {
- Display::addFlash(Display::return_message(get_lang('MessageSent')));
- $url = api_get_path(WEB_CODE_PATH).'badge/assign.php?user='.$userId.'&id='.$parentId;
- $link = Display::url($url, $url);
- $subject = get_lang("StudentHadEnoughSkills");
- $message = sprintf(
- get_lang("StudentXHadEnoughSkillsToGetSkillXToAssignClickHereX"),
- $user->getCompleteName(),
- $parentData['name'],
- $link
- );
- foreach ($bossList as $boss) {
- MessageManager::send_message_simple(
- $boss['boss_id'],
- $subject,
- $message
- );
- }
- }
- break;
- }
- }
- }
- Display::addFlash(
- Display::return_message(
- sprintf(
- get_lang('SkillXAssignedToUserY'),
- $skill->getName(),
- $user->getCompleteName()
- ),
- 'success'
- )
- );
- header('Location: '.api_get_path(WEB_PATH)."badge/{$skillUser->getId()}");
- exit;
- }
- $form->setDefaults(['user_name' => $user->getCompleteNameWithUsername()]);
- $form->freeze(['user_name']);
- if (api_is_drh()) {
- $interbreadcrumb[] = [
- 'url' => api_get_path(WEB_CODE_PATH).'mySpace/index.php',
- "name" => get_lang('MySpace'),
- ];
- if ($user->getStatus() == COURSEMANAGER) {
- $interbreadcrumb[] = [
- "url" => api_get_path(WEB_CODE_PATH).'mySpace/teachers.php',
- 'name' => get_lang('Teachers'),
- ];
- } else {
- $interbreadcrumb[] = [
- "url" => api_get_path(WEB_CODE_PATH).'mySpace/student.php',
- 'name' => get_lang('MyStudents'),
- ];
- }
- $interbreadcrumb[] = [
- 'url' => api_get_path(WEB_CODE_PATH).'mySpace/myStudents.php?student='.$userId,
- 'name' => $user->getCompleteName(),
- ];
- } else {
- $interbreadcrumb[] = [
- 'url' => api_get_path(WEB_CODE_PATH).'admin/index.php',
- 'name' => get_lang('PlatformAdmin'),
- ];
- $interbreadcrumb[] = [
- 'url' => api_get_path(WEB_CODE_PATH).'admin/user_list.php',
- 'name' => get_lang('UserList'),
- ];
- $interbreadcrumb[] = [
- 'url' => api_get_path(WEB_CODE_PATH).'admin/user_information.php?user_id='.$userId,
- 'name' => $user->getCompleteName(),
- ];
- }
- $url = api_get_path(WEB_CODE_PATH).'badge/assign.php?user='.$userId;
- $disableSelect = '';
- if ($disableList) {
- foreach ($disableList as $name) {
- //$disableSelect .= "$('#".$name."').prop('disabled', true);";
- //$disableSelect .= "$('#".$name."').selectpicker('refresh');";
- }
- }
- $htmlHeadXtra[] = '<script>
- $(function() {
- $("#skill").on("change", function() {
- $(location).attr("href", "'.$url.'&id="+$(this).val());
- });
- $(".sub_skill").on("change", function() {
- $(location).attr("href", "'.$url.'&id='.$skillIdFromGet.'¤t_value="+$(this).val()+"¤t="+$(this).attr("id")+"&sub_skill_list='.$subSkillListToString.',"+$(this).val());
- });
- '.$disableSelect.'
- });
- </script>';
- $template = new Template(get_lang('AddSkill'));
- $template->assign('content', $form->returnForm());
- $template->display_one_col_template();
|