Skip to content

Commit

Permalink
Skill: Remove legacy methods to translate title and short code - refs…
Browse files Browse the repository at this point in the history
… BT#21568
  • Loading branch information
AngelFQC committed Nov 12, 2024
1 parent 878cca9 commit 05f4de2
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 103 deletions.
134 changes: 51 additions & 83 deletions public/main/inc/lib/SkillModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public function get($id)
return [];
}

$skill = Database::getManager()->find(Skill::class, $id);

// @todo fix badges icons
//$path = api_get_path(WEB_UPLOAD_PATH).'badges/';
$path = '';
Expand Down Expand Up @@ -110,8 +112,8 @@ public function get($id)
$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;
}
Expand Down Expand Up @@ -257,35 +259,41 @@ public function getSkillInfo($id)
}

/**
* @param array $skill_list
* @param array<int, int> $skill_list
*
* @return array
* @return array<int, 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;
}

/**
Expand Down Expand Up @@ -346,8 +354,8 @@ public function getAllSkills(
$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;
Expand Down Expand Up @@ -665,6 +673,7 @@ public function edit(array $params)
*/
public function getUserSkills($userId, $getSkillData = false, $courseId = 0, $sessionId = 0)
{
$em = Database::getManager();
$userId = (int) $userId;
$courseId = (int) $courseId;
$sessionId = (int) $sessionId;
Expand All @@ -681,9 +690,6 @@ public function getUserSkills($userId, $getSkillData = false, $courseId = 0, $se

$sql = 'SELECT DISTINCT
s.id,
s.title,
s.icon,
s.asset_id,
u.id as issue,
u.acquired_skill_at,
u.course_id
Expand Down Expand Up @@ -815,19 +821,18 @@ public function getUserSkillsTable(int $userId, int $courseId = 0, int $sessionI
$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
);
$tableRow = [
'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']),
Expand Down Expand Up @@ -1434,6 +1439,8 @@ public function listAchievedByCourse($courseId)
return [];
}

$em = Database::getManager();

$list = [];

$sql = "SELECT
Expand All @@ -1445,7 +1452,6 @@ public function listAchievedByCourse($courseId)
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}
Expand All @@ -1459,7 +1465,8 @@ public function listAchievedByCourse($courseId)
$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;
}

Expand All @@ -1481,6 +1488,8 @@ public function listUsersWhoAchieved($skillId)
return [];
}

$em = Database::getManager();

$list = [];
$sql = "SELECT
course.id c_id,
Expand All @@ -1491,7 +1500,6 @@ public function listUsersWhoAchieved($skillId)
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}
Expand All @@ -1504,7 +1512,8 @@ public function listUsersWhoAchieved($skillId)

$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;
}

Expand Down Expand Up @@ -1706,31 +1715,24 @@ public static function hasAccessToUserSkill($currentUserId, $studentId)
*/
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'),
];
}
}
Expand All @@ -1739,40 +1741,6 @@ public function getStudentSkills($userId, $level = 0)
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
*
Expand Down
9 changes: 7 additions & 2 deletions public/main/social/my_skills_report.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/* For licensing terms, see /license.txt */

use Chamilo\CoreBundle\Entity\Skill;
use Chamilo\CoreBundle\Framework\Container;

/**
Expand All @@ -18,6 +19,8 @@
$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;
Expand Down Expand Up @@ -74,7 +77,7 @@
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
Expand All @@ -86,9 +89,11 @@
$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',
Expand Down
20 changes: 2 additions & 18 deletions src/CoreBundle/Entity/Skill.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,29 +136,13 @@ public function setTitle(string $title): self
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;
}

Expand Down
3 changes: 3 additions & 0 deletions src/CoreBundle/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,9 @@ public function hasSkill(Skill $skill): bool
return false;
}

/**
* @return Collection<int, SkillRelUser>
*/
public function getAchievedSkills(): Collection
{
return $this->achievedSkills;
Expand Down

0 comments on commit 05f4de2

Please sign in to comment.