diff --git a/public/main/inc/lib/SkillModel.php b/public/main/inc/lib/SkillModel.php index 3c5553dcb8..37ecb3777e 100644 --- a/public/main/inc/lib/SkillModel.php +++ b/public/main/inc/lib/SkillModel.php @@ -80,6 +80,8 @@ class SkillModel extends Model return []; } + $skill = Database::getManager()->find(Skill::class, $id); + // @todo fix badges icons //$path = api_get_path(WEB_UPLOAD_PATH).'badges/'; $path = ''; @@ -110,8 +112,8 @@ class SkillModel extends Model $result['img_mini'] = Display::img($iconBig, $result['title'], ['width' => ICON_SIZE_MEDIUM]); $result['img_big'] = Display::img($iconBig, $result['name']); $result['img_small'] = Display::img($iconSmall, $result['name']);*/ - $result['title'] = self::translateName($result['title']); - $result['short_code'] = self::translateCode($result['short_code']); + $result['title'] = $skill->getTitle(); + $result['short_code'] = $skill->getShortCode(); return $result; } @@ -257,35 +259,41 @@ class SkillModel extends Model } /** - * @param array $skill_list + * @param array $skill_list * - * @return array + * @return array */ - public function getSkillsInfo($skill_list) + public function getSkillsInfo(array $skill_list): array { + $skillRepo = Container::getSkillRepository(); $skill_list = array_map('intval', $skill_list); - $skill_list = implode("', '", $skill_list); - $sql = "SELECT * FROM {$this->table} - WHERE id IN ('$skill_list') "; - - $result = Database::query($sql); - $skills = Database::store_result($result, 'ASSOC'); + $skills = $skillRepo->findBy(['id' => $skill_list]); + $skillsInfo = []; - foreach ($skills as &$skill) { - if (!$skill['icon']) { - continue; + /** @var Skill $skill */ + foreach ($skills as $skill) { + if (!$skill->getIcon()) { + continue; } - $skill['icon_small'] = sprintf( - 'badges/%s-small.png', - sha1($skill['title']) - ); - $skill['title'] = self::translateName($skill['title']); - $skill['short_code'] = self::translateCode($skill['short_code']); + $skillsInfo[] = [ + 'id' => $skill->getId(), + 'title' => $skill->getTitle(), + 'description' => $skill->getDescription(), + 'access_url_id' => $skill->getAccessUrlId(), + 'updated_at' => $skill->getUpdatedAt()->format('Y-m-d H:i:s'), + 'short_code' => $skill->getShortCode(), + 'icon' => $skill->getIcon(), + 'criteria' => $skill->getCriteria(), + 'status' => $skill->getStatus(), + 'asset_id' => (string) $skill->getAsset()?->getId(), + 'profile_id' => $skill->getProfile()?->getId(), + 'icons_small' => sprintf('badges/%s-small.png', sha1($skill['title'])), + ]; } - return $skills; + return $skillsInfo; } /** @@ -346,8 +354,8 @@ class SkillModel extends Model $row['asset'] = $assetRepo->getAssetUrl($skill->getAsset()); } - $row['title'] = self::translateName($skill->getTitle()); - $row['short_code'] = self::translateCode($skill->getShortCode()); + $row['title'] = $skill->getTitle(); + $row['short_code'] = $skill->getShortCode(); $skillRelSkill = new SkillRelSkillModel(); $parents = $skillRelSkill->getSkillParents($skillId); $row['level'] = count($parents) - 1; @@ -665,6 +673,7 @@ class SkillModel extends Model */ public function getUserSkills($userId, $getSkillData = false, $courseId = 0, $sessionId = 0) { + $em = Database::getManager(); $userId = (int) $userId; $courseId = (int) $courseId; $sessionId = (int) $sessionId; @@ -681,9 +690,6 @@ class SkillModel extends Model $sql = 'SELECT DISTINCT s.id, - s.title, - s.icon, - s.asset_id, u.id as issue, u.acquired_skill_at, u.course_id @@ -815,11 +821,10 @@ class SkillModel extends Model $asset = $assetRepo->find($resultData['asset_id']); } - $skillTitle = self::translateName($resultData['title']); $image = $asset ? $assetRepo->getAssetUrl($asset) : '/img/icons/32/badges-default.png'; $badgeImage = Display::img( $image, - $skillTitle, + $resultData['title'], ['width' => '40'], false ); @@ -827,7 +832,7 @@ class SkillModel extends Model 'skill_id' => $resultData['id'], 'asset_id' => $resultData['asset_id'], 'skill_badge' => $badgeImage, - 'skill_title' => $skillTitle, + 'skill_title' => $resultData['title'], 'short_code' => $resultData['short_code'], 'skill_url' => $resultData['url'], 'achieved_at' => api_get_local_time($resultData['acquired_skill_at']), @@ -1434,6 +1439,8 @@ class SkillModel extends Model return []; } + $em = Database::getManager(); + $list = []; $sql = "SELECT @@ -1445,7 +1452,6 @@ class SkillModel extends Model user.firstname, user.username, skill.id skill_id, - skill.title skill_title, sru.acquired_skill_at FROM {$this->table_skill_rel_user} AS sru INNER JOIN {$this->table_course} @@ -1459,7 +1465,8 @@ class SkillModel extends Model $result = Database::query($sql); while ($row = Database::fetch_assoc($result)) { - $row['skill_title'] = self::translateName($row['skill_title']); + $skill = $em->find(Skill::class, $row['skill_id']); + $row['skill_title'] = $skill->getTitle(); $list[] = $row; } @@ -1481,6 +1488,8 @@ class SkillModel extends Model return []; } + $em = Database::getManager(); + $list = []; $sql = "SELECT course.id c_id, @@ -1491,7 +1500,6 @@ class SkillModel extends Model user.firstname, user.username, skill.id skill_id, - skill.title skill_title, sru.acquired_skill_at FROM {$this->table_skill_rel_user} AS sru INNER JOIN {$this->table_course} @@ -1504,7 +1512,8 @@ class SkillModel extends Model $result = Database::query($sql); while ($row = Database::fetch_assoc($result)) { - $row['skill_title'] = self::translateName($row['skill_title']); + $skill = $em->find(Skill::class, $row['skill_id']); + $row['skill_title'] = $skill->getTitle(); $list[] = $row; } @@ -1706,31 +1715,24 @@ class SkillModel extends Model */ public function getStudentSkills($userId, $level = 0) { - $userId = (int) $userId; - - $sql = "SELECT s.id, s.title, sru.acquired_skill_at - FROM {$this->table} s - INNER JOIN {$this->table_skill_rel_user} sru - ON s.id = sru.skill_id - WHERE sru.user_id = $userId"; + $achievedSkills = api_get_user_entity($userId)->getAchievedSkills(); + $skills = []; - $query = Database::query($sql); - $result = Database::store_result($query, 'ASSOC'); + foreach ($achievedSkills as $achievedSkill) { + $skill = $achievedSkill->getSkill(); - $skills = []; - foreach ($result as $item) { if (empty($level)) { $skills[] = [ - 'title' => self::translateName($item['title']), - 'acquired_skill_at' => $item['acquired_skill_at'], + 'title' => $skill->getTitle(), + 'acquired_skill_at' => $achievedSkill->getAcquiredSkillAt()->format('Y-m-d H:i:s'), ]; } else { - $parents = self::get_parents($item['id']); + $parents = self::get_parents($skill->getId()); // +2 because it takes into account the root if (count($parents) == $level + 1) { $skills[] = [ - 'title' => self::translateName($item['title']), - 'acquired_skill_at' => $item['acquired_skill_at'], + 'title' => $skill->getTitle(), + 'acquired_skill_at' => $achievedSkill->getAcquiredSkillAt()->format('Y-m-d H:i:s'), ]; } } @@ -1739,40 +1741,6 @@ class SkillModel extends Model return $skills; } - /** - * @param string $name - * - * @return string - */ - public static function translateName($name) - { - $variable = ChamiloApi::getLanguageVar($name, 'Skill'); - $translation = get_lang($variable); - if ($variable != $translation) { - return $translation; - } - return $name; - } - - /** - * @param string $code - * - * @return mixed|string - */ - public static function translateCode($code) - { - if (empty($code)) { - return ''; - } - - $variable = ChamiloApi::getLanguageVar($code, 'SkillCode'); - $translation = get_lang($variable); - if ($variable != $translation) { - return $translation; - } - return $code; - } - /** * @param array $skillInfo * diff --git a/public/main/social/my_skills_report.php b/public/main/social/my_skills_report.php index 1244917d7f..2a5976aea2 100644 --- a/public/main/social/my_skills_report.php +++ b/public/main/social/my_skills_report.php @@ -2,6 +2,7 @@ /* For licensing terms, see /license.txt */ +use Chamilo\CoreBundle\Entity\Skill; use Chamilo\CoreBundle\Framework\Container; /** @@ -18,6 +19,8 @@ $isStudent = api_is_student(); $isStudentBoss = api_is_student_boss(); $isDRH = api_is_drh(); +$em = Database::getManager(); + if (!$isStudent && !$isStudentBoss && !$isDRH) { header('Location: '.api_get_path(WEB_CODE_PATH).'social/skills_wheel.php'); exit; @@ -74,7 +77,7 @@ if ($isStudent) { if ($frmStudents->validate()) { $selectedStudent = (int) $frmStudents->exportValue('student'); - $sql = "SELECT s.title, sru.acquired_skill_at, c.title, c.directory, c.id as course_id + $sql = "SELECT s.id AS skill_id9i, sru.acquired_skill_at, c.title, c.directory, c.id as course_id FROM $skillTable s INNER JOIN $skillRelUserTable sru ON s.id = sru.skill_id @@ -86,9 +89,11 @@ if ($isStudent) { $result = Database::query($sql); while ($resultData = Database::fetch_assoc($result)) { + $skill = $em->find(Skill::class, $resultData['skill_id']); + $tableRow = [ 'complete_name' => $followedStudents[$selectedStudent]['completeName'], - 'skill_name' => SkillModel::translateName($resultData['title']), + 'skill_name' => $skill->getTitle(), 'achieved_at' => api_format_date($resultData['acquired_skill_at'], DATE_FORMAT_NUMBER), 'course_image' => Display::return_icon( 'course.png', diff --git a/src/CoreBundle/Entity/Skill.php b/src/CoreBundle/Entity/Skill.php index 97cf450d20..dd8a814bfa 100644 --- a/src/CoreBundle/Entity/Skill.php +++ b/src/CoreBundle/Entity/Skill.php @@ -136,29 +136,13 @@ class Skill implements Stringable, Translatable return $this; } - public function getTitle($translated = true): string + public function getTitle(): string { - if ($translated) { - $variable = ChamiloApi::getLanguageVar($this->title, 'Skill'); - $translation = get_lang($variable); - if ($variable != $translation) { - return $translation; - } - } - return $this->title; } - public function getShortCode($translated = true): string + public function getShortCode(): string { - if ($translated) { - $variable = ChamiloApi::getLanguageVar($this->shortCode, 'SkillCode'); - $translation = get_lang($variable); - if ($variable != $translation) { - return $translation; - } - } - return $this->shortCode; } diff --git a/src/CoreBundle/Entity/User.php b/src/CoreBundle/Entity/User.php index 5d27cae73a..5bc7cca348 100644 --- a/src/CoreBundle/Entity/User.php +++ b/src/CoreBundle/Entity/User.php @@ -1426,6 +1426,9 @@ class User implements UserInterface, EquatableInterface, ResourceInterface, Reso return false; } + /** + * @return Collection + */ public function getAchievedSkills(): Collection { return $this->achievedSkills;