Skip to content

Commit

Permalink
Skills : Validate user skills by subcategory in gradebook - refs BT#1…
Browse files Browse the repository at this point in the history
…9823
  • Loading branch information
cfasanando committed Mar 23, 2022
1 parent 7c9d54d commit 4661eb7
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 3 deletions.
15 changes: 14 additions & 1 deletion main/gradebook/gradebook_edit_cat.php
Expand Up @@ -9,7 +9,7 @@
api_block_anonymous_users();
GradebookUtils::block_students();
$edit_cat = isset($_REQUEST['editcat']) ? (int) $_REQUEST['editcat'] : 0;

$enableGradeSubCategorySkills = (true === api_get_configuration_value('gradebook_enable_subcategory_skills_independant_assignement'));
$catedit = Category::load($edit_cat);
$form = new CatForm(
CatForm::TYPE_EDIT,
Expand Down Expand Up @@ -62,6 +62,13 @@

if ($values['hid_parent_id'] == 0) {
$cat->set_certificate_min_score($values['certif_min_score']);
} else {
if ($enableGradeSubCategorySkills) {
$allowSkillsBySubCategory = $cat->getAllowSkillBySubCategory($cat->get_parent_id());
if ($allowSkillsBySubCategory) {
$cat->set_certificate_min_score($values['certif_min_score']);
}
}
}

$visible = 1;
Expand All @@ -75,6 +82,12 @@
} else {
$cat->setIsRequirement(false);
}

if ($enableGradeSubCategorySkills) {
$allowSkillsBySubCategory = isset($values['allow_skills_by_subcategory']);
$cat->updateAllowSkillBySubCategory($allowSkillsBySubCategory);
}

$cat->save();
header('Location: '.Category::getUrl().'editcat=&selectcat='.$cat->get_parent_id());
exit;
Expand Down
39 changes: 38 additions & 1 deletion main/gradebook/lib/be/category.class.php
Expand Up @@ -713,6 +713,39 @@ public function save()
);
}

/**
* Update value to allow user skills by subcategory passed.
*
* @param $value
*/
public function updateAllowSkillBySubCategory($value)
{
$table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
$value = (int) $value;
$upd = 'UPDATE '.$table.' SET allow_skills_by_subcategory = '.$value.' WHERE id = '.intval($this->id);
Database::query($upd);
}

/**
* Get the value to Allow skill by subcategory.
*
* @return bool
*/
public function getAllowSkillBySubCategory($parentId = null)
{
$id = (int) $this->id;
if (isset($parentId)) {
$id = (int) $parentId;
}

$table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
$sql = 'SELECT allow_skills_by_subcategory FROM '.$table.' WHERE id = '.$id;
$rs = Database::query($sql);
$value = (bool) Database::result($rs, 0, 0);

return $value;
}

/**
* Update link weights see #5168.
*
Expand Down Expand Up @@ -2127,14 +2160,18 @@ public static function generateUserCertificate(
$userHasSkills = false;
if ($skillToolEnabled) {
$skill = new Skill();
$objSkillRelUser = new SkillRelUser();

// It cleans the previous results to generate the new user skills
$objSkillRelUser->deleteUserSkill($user_id, $courseId, $sessionId);

$skill->addSkillToUser(
$user_id,
$category,
$courseId,
$sessionId
);

$objSkillRelUser = new SkillRelUser();
$userSkills = $objSkillRelUser->getUserSkills(
$user_id,
$courseId,
Expand Down
40 changes: 40 additions & 0 deletions main/gradebook/lib/fe/catform.class.php
Expand Up @@ -292,6 +292,19 @@ private function build_basic_form()
true,
['maxlength' => '5']
);

if (true === api_get_configuration_value('gradebook_enable_subcategory_skills_independant_assignement')) {
// It allows the acquisition of competencies independently in the subcategories
$allowSkillsBySubCategory = $this->addCheckBox(
'allow_skills_by_subcategory',
[
null,
get_lang('ItAllowsTheAcquisitionOfSkillsBySubCategories'),
],
get_lang('AllowsSkillsBySubCategories')
);
$allowSkillsBySubCategory->setChecked($this->category_object->getAllowSkillBySubCategory());
}
} else {
$questionWeighting = $value;
$defaultCertification = api_number_format($this->category_object->getCertificateMinScore(), 2);
Expand Down Expand Up @@ -330,6 +343,33 @@ private function build_basic_form()
0
);
} else {
// It enables minimun score to get the skills independant assigment
if (true === api_get_configuration_value('gradebook_enable_subcategory_skills_independant_assignement')) {
$allowSkillsBySubCategory = $this->category_object->getAllowSkillBySubCategory($this->category_object->get_parent_id());
if ($allowSkillsBySubCategory) {
$this->addText(
'certif_min_score',
get_lang('SkillMinScore'),
true,
['maxlength' => '5']
);
$this->addRule(
'certif_min_score',
get_lang('OnlyNumbers'),
'numeric'
);
$this->addRule(
'certif_min_score',
get_lang('NegativeValue'),
'compare',
'>=',
'server',
false,
false,
0
);
}
}
$this->addElement('checkbox', 'visible', null, get_lang('Visible'));
}

Expand Down
64 changes: 63 additions & 1 deletion main/inc/lib/skill.lib.php
Expand Up @@ -624,6 +624,25 @@ public function getByUserAndSkill($userId, $skillId, $courseId = 0, $sessionId =
return Database::fetch_assoc($result);
}

/**
* Delete a user skill by course.
*
* @param int $userId
* @param int $courseId
* @param int $sessionId
*/
public function deleteUserSkill($userId, $courseId, $sessionId = 0)
{
$whereSession = ($sessionId ? " AND session_id = $sessionId" : " AND session_id IS NULL");
$sql = "DELETE FROM {$this->table}
WHERE
user_id = $userId AND
course_id = $courseId
$whereSession";

Database::query($sql);
}

/**
* Get the URL for the issue.
*
Expand Down Expand Up @@ -1171,10 +1190,18 @@ public function addSkillToUser(
$category->get_course_code(),
$category->get_session_id()
);
$scoreSubCategories = $this->getSubCategoryResultScore($category, $userId);
if (!empty($subCategories)) {
/** @var Category $subCategory */
foreach ($subCategories as $subCategory) {
$this->addSkillToUser($userId, $subCategory, $courseId, $sessionId);
$scoreChecked = true;
if (!empty($scoreSubCategories[$subCategory->get_id()])) {
$resultScore = $scoreSubCategories[$subCategory->get_id()];
$scoreChecked = ($resultScore['user_score'] >= $resultScore['min_score']);
}
if ($scoreChecked) {
$this->addSkillToUser($userId, $subCategory, $courseId, $sessionId);
}
}
}
}
Expand Down Expand Up @@ -1208,6 +1235,41 @@ public function addSkillToUser(
return true;
}

/**
* Get the results of user in a subCategory.
*
* @param $category
* @param $userId
*
* @return array
*/
public function getSubCategoryResultScore($category, $userId)
{
$scoreSubCategories = [];
if (true === api_get_configuration_value('gradebook_enable_subcategory_skills_independant_assignement')) {
$subCategories = $category->get_subcategories(
$userId,
$category->get_course_code(),
$category->get_session_id()
);
$alleval = $category->get_evaluations($userId, false, $category->get_course_code(),
$category->get_session_id());
$alllink = $category->get_links($userId, true, $category->get_course_code(), $category->get_session_id());
$datagen = new GradebookDataGenerator($subCategories, $alleval, $alllink);
$gradeResult = $datagen->get_data();
foreach ($gradeResult as $data) {
/** @var AbstractLink $item */
$item = $data[0];
if ('Category' === get_class($item)) {
$scoreSubCategories[$item->get_id()]['min_score'] = $item->getCertificateMinScore();
$scoreSubCategories[$item->get_id()]['user_score'] = round($data['result_score'][0]);
}
}
}

return $scoreSubCategories;
}

/* Deletes a skill */
public function delete($skill_id)
{
Expand Down
5 changes: 5 additions & 0 deletions main/install/configuration.dist.php
Expand Up @@ -2156,6 +2156,11 @@
// User extra fields to be check on user edition to generate a specific process if it was modified
//$_configuration['user_edition_extra_field_to_check'] = 'ExtrafieldLabel';

// Enable skills in subcategory to work independant on assignement
// Require DB changes:
// ALTER TABLE gradebook_category ADD allow_skills_by_subcategory tinyint(1) NOT NULL DEFAULT '1';
//$_configuration['gradebook_enable_subcategory_skills_independant_assignement'] = false;

// KEEP THIS AT THE END
// -------- Custom DB changes
// Add user activation by confirmation email
Expand Down

0 comments on commit 4661eb7

Please sign in to comment.