Refactor Skills code #2243
Move classes in independent files. Fix legacy skill classes from Skill to SkillModel to avoid conflicts with the entities.pull/3904/head
parent
d7468b6df7
commit
07f4947ca6
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,106 @@ |
||||
<?php |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
class SkillProfileModel extends Model |
||||
{ |
||||
public $columns = ['id', 'name', 'description']; |
||||
|
||||
public function __construct() |
||||
{ |
||||
$this->table = Database::get_main_table(TABLE_MAIN_SKILL_PROFILE); |
||||
$this->table_rel_profile = Database::get_main_table(TABLE_MAIN_SKILL_REL_PROFILE); |
||||
} |
||||
|
||||
/** |
||||
* @return array |
||||
*/ |
||||
public function getProfiles() |
||||
{ |
||||
$sql = "SELECT * FROM $this->table p |
||||
INNER JOIN $this->table_rel_profile sp |
||||
ON (p.id = sp.profile_id) "; |
||||
$result = Database::query($sql); |
||||
|
||||
return Database::store_result($result, 'ASSOC'); |
||||
} |
||||
|
||||
/** |
||||
* This function is for editing profile info from profile_id. |
||||
* |
||||
* @param int $profileId |
||||
* @param string $name |
||||
* @param string $description |
||||
* |
||||
* @return bool |
||||
*/ |
||||
public function updateProfileInfo($profileId, $name, $description) |
||||
{ |
||||
$profileId = (int) $profileId; |
||||
|
||||
if (empty($profileId)) { |
||||
return false; |
||||
} |
||||
|
||||
$name = Database::escape_string($name); |
||||
$description = Database::escape_string($description); |
||||
|
||||
$sql = "UPDATE $this->table SET |
||||
name = '$name', |
||||
description = '$description' |
||||
WHERE id = $profileId "; |
||||
Database::query($sql); |
||||
|
||||
return true; |
||||
} |
||||
|
||||
/** |
||||
* Call the save method of the parent class and the SkillRelProfile object. |
||||
* |
||||
* @param array $params |
||||
* @param bool $showQuery Whether to show the query in parent save() method |
||||
* |
||||
* @return mixed Profile ID or false if incomplete params |
||||
*/ |
||||
public function save($params, $showQuery = false) |
||||
{ |
||||
if (!empty($params)) { |
||||
$profile_id = parent::save($params, $showQuery); |
||||
if ($profile_id) { |
||||
$skill_rel_profile = new SkillRelProfileModel(); |
||||
if (isset($params['skills'])) { |
||||
foreach ($params['skills'] as $skill_id) { |
||||
$attributes = [ |
||||
'skill_id' => $skill_id, |
||||
'profile_id' => $profile_id, |
||||
]; |
||||
$skill_rel_profile->save($attributes); |
||||
} |
||||
} |
||||
|
||||
return $profile_id; |
||||
} |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* Delete a skill profile. |
||||
* |
||||
* @param int $id The skill profile id |
||||
* |
||||
* @return bool Whether delete a skill profile |
||||
*/ |
||||
public function delete($id) |
||||
{ |
||||
Database::delete( |
||||
$this->table_rel_profile, |
||||
[ |
||||
'profile_id' => $id, |
||||
] |
||||
); |
||||
|
||||
return parent::delete($id); |
||||
} |
||||
} |
@ -0,0 +1,138 @@ |
||||
<?php |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
class SkillRelGradebookModel extends Model |
||||
{ |
||||
public $columns = ['id', 'gradebook_id', 'skill_id']; |
||||
|
||||
public function __construct() |
||||
{ |
||||
$this->table = Database::get_main_table(TABLE_MAIN_SKILL_REL_GRADEBOOK); |
||||
} |
||||
|
||||
/** |
||||
* @param int $gradebookId |
||||
* @param int $skillId |
||||
* |
||||
* @return bool |
||||
*/ |
||||
public function existsGradeBookSkill($gradebookId, $skillId) |
||||
{ |
||||
$result = $this->find( |
||||
'all', |
||||
[ |
||||
'where' => [ |
||||
'gradebook_id = ? AND skill_id = ?' => [ |
||||
$gradebookId, |
||||
$skillId, |
||||
], |
||||
], |
||||
] |
||||
); |
||||
if (!empty($result)) { |
||||
return true; |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* Gets an element. |
||||
*/ |
||||
public function getSkillInfo($skill_id, $gradebookId) |
||||
{ |
||||
if (empty($skill_id)) { |
||||
return []; |
||||
} |
||||
$result = Database::select( |
||||
'*', |
||||
$this->table, |
||||
[ |
||||
'where' => [ |
||||
'skill_id = ? AND gradebook_id = ? ' => [ |
||||
$skill_id, |
||||
$gradebookId, |
||||
], |
||||
], |
||||
], |
||||
'first' |
||||
); |
||||
|
||||
return $result; |
||||
} |
||||
|
||||
/** |
||||
* @param int $skill_id |
||||
* @param array $gradebook_list |
||||
*/ |
||||
public function updateGradeBookListBySkill($skill_id, $gradebook_list) |
||||
{ |
||||
$original_gradebook_list = $this->find( |
||||
'all', |
||||
['where' => ['skill_id = ?' => [$skill_id]]] |
||||
); |
||||
$gradebooks_to_remove = []; |
||||
$gradebooks_to_add = []; |
||||
$original_gradebook_list_ids = []; |
||||
|
||||
if (!empty($original_gradebook_list)) { |
||||
foreach ($original_gradebook_list as $gradebook) { |
||||
if (!in_array($gradebook['gradebook_id'], $gradebook_list)) { |
||||
$gradebooks_to_remove[] = $gradebook['id']; |
||||
} |
||||
} |
||||
foreach ($original_gradebook_list as $gradebook_item) { |
||||
$original_gradebook_list_ids[] = $gradebook_item['gradebook_id']; |
||||
} |
||||
} |
||||
|
||||
if (!empty($gradebook_list)) { |
||||
foreach ($gradebook_list as $gradebook_id) { |
||||
if (!in_array($gradebook_id, $original_gradebook_list_ids)) { |
||||
$gradebooks_to_add[] = $gradebook_id; |
||||
} |
||||
} |
||||
} |
||||
|
||||
if (!empty($gradebooks_to_remove)) { |
||||
foreach ($gradebooks_to_remove as $id) { |
||||
$this->delete($id); |
||||
} |
||||
} |
||||
|
||||
if (!empty($gradebooks_to_add)) { |
||||
foreach ($gradebooks_to_add as $gradebook_id) { |
||||
$attributes = [ |
||||
'skill_id' => $skill_id, |
||||
'gradebook_id' => $gradebook_id, |
||||
]; |
||||
$this->save($attributes); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* @param array $params |
||||
* |
||||
* @return bool|void |
||||
*/ |
||||
public function updateBySkill($params) |
||||
{ |
||||
$skillInfo = $this->existsGradeBookSkill( |
||||
$params['gradebook_id'], |
||||
$params['skill_id'] |
||||
); |
||||
|
||||
if ($skillInfo) { |
||||
return; |
||||
} else { |
||||
$result = $this->save($params); |
||||
} |
||||
if ($result) { |
||||
return true; |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
} |
@ -0,0 +1,56 @@ |
||||
<?php |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
class SkillRelProfileModel extends Model |
||||
{ |
||||
public $columns = ['id', 'skill_id', 'profile_id']; |
||||
|
||||
/** |
||||
* Constructor. |
||||
*/ |
||||
public function __construct() |
||||
{ |
||||
$this->table = Database::get_main_table(TABLE_MAIN_SKILL_REL_PROFILE); |
||||
$this->tableProfile = Database::get_main_table(TABLE_MAIN_SKILL_PROFILE); |
||||
} |
||||
|
||||
/** |
||||
* @param int $profileId |
||||
* |
||||
* @return array |
||||
*/ |
||||
public function getSkillsByProfile($profileId) |
||||
{ |
||||
$profileId = (int) $profileId; |
||||
$skills = $this->get_all(['where' => ['profile_id = ? ' => $profileId]]); |
||||
$return = []; |
||||
if (!empty($skills)) { |
||||
foreach ($skills as $skill_data) { |
||||
$return[] = $skill_data['skill_id']; |
||||
} |
||||
} |
||||
|
||||
return $return; |
||||
} |
||||
|
||||
/** |
||||
* This function is for getting profile info from profile_id. |
||||
* |
||||
* @param int $profileId |
||||
* |
||||
* @return array |
||||
*/ |
||||
public function getProfileInfo($profileId) |
||||
{ |
||||
$profileId = (int) $profileId; |
||||
$sql = "SELECT * FROM $this->table p |
||||
INNER JOIN $this->tableProfile pr |
||||
ON (pr.id = p.profile_id) |
||||
WHERE p.profile_id = ".$profileId; |
||||
$result = Database::query($sql); |
||||
$profileData = Database::fetch_array($result, 'ASSOC'); |
||||
|
||||
return $profileData; |
||||
} |
||||
} |
@ -0,0 +1,192 @@ |
||||
<?php |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
class SkillRelSkillModel extends Model |
||||
{ |
||||
public $columns = ['skill_id', 'parent_id', 'relation_type', 'level']; |
||||
|
||||
/** |
||||
* Constructor. |
||||
*/ |
||||
public function __construct() |
||||
{ |
||||
$this->table = Database::get_main_table(TABLE_MAIN_SKILL_REL_SKILL); |
||||
$this->tableSkill = Database::get_main_table(TABLE_MAIN_SKILL); |
||||
} |
||||
|
||||
/** |
||||
* Gets an element. |
||||
* |
||||
* @param int $id |
||||
* |
||||
* @return array |
||||
*/ |
||||
public function getSkillInfo($id) |
||||
{ |
||||
$id = (int) $id; |
||||
|
||||
if (empty($id)) { |
||||
return []; |
||||
} |
||||
|
||||
$result = Database::select( |
||||
'*', |
||||
$this->table, |
||||
['where' => ['skill_id = ?' => $id]], |
||||
'first' |
||||
); |
||||
|
||||
return $result; |
||||
} |
||||
|
||||
/** |
||||
* @param int $skillId |
||||
* @param bool $add_child_info |
||||
* |
||||
* @return array |
||||
*/ |
||||
public function getSkillParents($skillId, $add_child_info = true) |
||||
{ |
||||
$skillId = (int) $skillId; |
||||
$sql = 'SELECT child.* FROM '.$this->table.' child |
||||
LEFT JOIN '.$this->table.' parent |
||||
ON child.parent_id = parent.skill_id |
||||
WHERE child.skill_id = '.$skillId.' '; |
||||
$result = Database::query($sql); |
||||
$skill = Database::store_result($result, 'ASSOC'); |
||||
$skill = isset($skill[0]) ? $skill[0] : null; |
||||
|
||||
$parents = []; |
||||
if (!empty($skill)) { |
||||
if (null != $skill['parent_id']) { |
||||
$parents = self::getSkillParents($skill['parent_id']); |
||||
} |
||||
if ($add_child_info) { |
||||
$parents[] = $skill; |
||||
} |
||||
} |
||||
|
||||
return $parents; |
||||
} |
||||
|
||||
/** |
||||
* @param int $skillId |
||||
* |
||||
* @return array |
||||
*/ |
||||
public function getDirectParents($skillId) |
||||
{ |
||||
$skillId = (int) $skillId; |
||||
$sql = 'SELECT parent_id as skill_id |
||||
FROM '.$this->table.' |
||||
WHERE skill_id = '.$skillId; |
||||
$result = Database::query($sql); |
||||
$skill = Database::store_result($result, 'ASSOC'); |
||||
$skill = isset($skill[0]) ? $skill[0] : null; |
||||
$parents = []; |
||||
if (!empty($skill)) { |
||||
$parents[] = $skill; |
||||
} |
||||
|
||||
return $parents; |
||||
} |
||||
|
||||
/** |
||||
* @param int $skill_id |
||||
* @param bool $load_user_data |
||||
* @param bool $user_id |
||||
* |
||||
* @return array |
||||
*/ |
||||
public function getChildren( |
||||
$skill_id, |
||||
$load_user_data = false, |
||||
$user_id = false, |
||||
$order = '' |
||||
) { |
||||
$skill_id = (int) $skill_id; |
||||
$sql = 'SELECT parent.* FROM '.$this->tableSkill.' skill |
||||
INNER JOIN '.$this->table.' parent |
||||
ON parent.id = skill.id |
||||
WHERE parent_id = '.$skill_id.' |
||||
ORDER BY skill.name ASC'; |
||||
$result = Database::query($sql); |
||||
$skills = Database::store_result($result, 'ASSOC'); |
||||
|
||||
$skill_obj = new SkillModel(); |
||||
$skill_rel_user = new SkillRelUserModel(); |
||||
|
||||
if ($load_user_data) { |
||||
$passed_skills = $skill_rel_user->getUserSkills($user_id); |
||||
$done_skills = []; |
||||
foreach ($passed_skills as $done_skill) { |
||||
$done_skills[] = $done_skill['skill_id']; |
||||
} |
||||
} |
||||
|
||||
if (!empty($skills)) { |
||||
foreach ($skills as &$skill) { |
||||
$skill['data'] = $skill_obj->get($skill['skill_id']); |
||||
if (isset($skill['data']) && !empty($skill['data'])) { |
||||
if (!empty($done_skills)) { |
||||
$skill['data']['passed'] = 0; |
||||
if (in_array($skill['skill_id'], $done_skills)) { |
||||
$skill['data']['passed'] = 1; |
||||
} |
||||
} |
||||
} else { |
||||
$skill = null; |
||||
} |
||||
} |
||||
} |
||||
|
||||
return $skills; |
||||
} |
||||
|
||||
/** |
||||
* @param array $params |
||||
* |
||||
* @return bool |
||||
*/ |
||||
public function updateBySkill($params) |
||||
{ |
||||
$result = Database::update( |
||||
$this->table, |
||||
$params, |
||||
['skill_id = ? ' => $params['skill_id']] |
||||
); |
||||
if ($result) { |
||||
return true; |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* @param int $skill_id |
||||
* @param int $parent_id |
||||
* |
||||
* @return bool |
||||
*/ |
||||
public function relationExists($skill_id, $parent_id) |
||||
{ |
||||
$result = $this->find( |
||||
'all', |
||||
[ |
||||
'where' => [ |
||||
'skill_id = ? AND parent_id = ?' => [ |
||||
$skill_id, |
||||
$parent_id, |
||||
], |
||||
], |
||||
] |
||||
); |
||||
|
||||
if (!empty($result)) { |
||||
return true; |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
} |
@ -0,0 +1,152 @@ |
||||
<?php |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
use Chamilo\CoreBundle\Entity\SkillRelUser; |
||||
|
||||
class SkillRelUserModel extends Model |
||||
{ |
||||
public $columns = [ |
||||
'id', |
||||
'user_id', |
||||
'skill_id', |
||||
'acquired_skill_at', |
||||
'assigned_by', |
||||
'course_id', |
||||
'session_id', |
||||
]; |
||||
|
||||
public function __construct() |
||||
{ |
||||
$this->table = Database::get_main_table(TABLE_MAIN_SKILL_REL_USER); |
||||
} |
||||
|
||||
/** |
||||
* @param array $skill_list |
||||
* |
||||
* @return array |
||||
*/ |
||||
public function getUserBySkills($skill_list) |
||||
{ |
||||
$users = []; |
||||
if (!empty($skill_list)) { |
||||
$skill_list = array_map('intval', $skill_list); |
||||
$skill_list = implode("', '", $skill_list); |
||||
|
||||
$sql = "SELECT user_id FROM {$this->table} |
||||
WHERE skill_id IN ('$skill_list') "; |
||||
|
||||
$result = Database::query($sql); |
||||
$users = Database::store_result($result, 'ASSOC'); |
||||
} |
||||
|
||||
return $users; |
||||
} |
||||
|
||||
/** |
||||
* Get the achieved skills for the user. |
||||
* |
||||
* @param int $userId |
||||
* @param int $courseId Optional. The course id |
||||
* @param int $sessionId Optional. The session id |
||||
* |
||||
* @return array The skill list. Otherwise return false |
||||
*/ |
||||
public function getUserSkills($userId, $courseId = 0, $sessionId = 0) |
||||
{ |
||||
if (empty($userId)) { |
||||
return []; |
||||
} |
||||
|
||||
$courseId = (int) $courseId; |
||||
$sessionId = $sessionId ? (int) $sessionId : null; |
||||
$whereConditions = [ |
||||
'user_id = ? ' => (int) $userId, |
||||
]; |
||||
|
||||
if ($courseId > 0) { |
||||
$whereConditions['AND course_id = ? '] = $courseId; |
||||
$whereConditions['AND session_id = ?'] = $sessionId; |
||||
} |
||||
|
||||
$result = Database::select( |
||||
'skill_id', |
||||
$this->table, |
||||
[ |
||||
'where' => $whereConditions, |
||||
], |
||||
'all' |
||||
); |
||||
|
||||
return $result; |
||||
} |
||||
|
||||
/** |
||||
* Get the relation data between user and skill. |
||||
* |
||||
* @param int $userId The user id |
||||
* @param int $skillId The skill id |
||||
* @param int $courseId Optional. The course id |
||||
* @param int $sessionId Optional. The session id |
||||
* |
||||
* @return array The relation data. Otherwise return false |
||||
*/ |
||||
public function getByUserAndSkill($userId, $skillId, $courseId = 0, $sessionId = 0) |
||||
{ |
||||
$sql = "SELECT * FROM {$this->table} WHERE user_id = %d AND skill_id = %d "; |
||||
|
||||
if ($courseId > 0) { |
||||
$sql .= "AND course_id = %d ".api_get_session_condition($sessionId, true); |
||||
} |
||||
|
||||
$sql = sprintf( |
||||
$sql, |
||||
$userId, |
||||
$skillId, |
||||
$courseId |
||||
); |
||||
|
||||
$result = Database::query($sql); |
||||
|
||||
return Database::fetch_assoc($result); |
||||
} |
||||
|
||||
/** |
||||
* Get the URL for the issue. |
||||
* |
||||
* @return string |
||||
*/ |
||||
public static function getIssueUrl(SkillRelUser $skillIssue) |
||||
{ |
||||
return api_get_path(WEB_PATH)."badge/{$skillIssue->getId()}"; |
||||
} |
||||
|
||||
/** |
||||
* Get the URL for the All issues page. |
||||
* |
||||
* @return string |
||||
*/ |
||||
public static function getIssueUrlAll(SkillRelUser $skillIssue) |
||||
{ |
||||
return api_get_path(WEB_PATH)."skill/{$skillIssue->getSkill()->getId()}/user/{$skillIssue->getUser()->getId()}"; |
||||
} |
||||
|
||||
/** |
||||
* Get the URL for the assertion. |
||||
* |
||||
* @return string |
||||
*/ |
||||
public static function getAssertionUrl(SkillRelUser $skillIssue) |
||||
{ |
||||
$url = api_get_path(WEB_CODE_PATH).'badge/assertion.php?'; |
||||
|
||||
$url .= http_build_query([ |
||||
'user' => $skillIssue->getUser()->getId(), |
||||
'skill' => $skillIssue->getSkill()->getId(), |
||||
'course' => $skillIssue->getCourse() ? $skillIssue->getCourse()->getId() : 0, |
||||
'session' => $skillIssue->getSession() ? $skillIssue->getSession()->getId() : 0, |
||||
]); |
||||
|
||||
return $url; |
||||
} |
||||
} |
Loading…
Reference in new issue