MDL-63876 badges: Competency deletion

Do not delete a competency if it is a criteria for a badge.
This commit is contained in:
Damyon Wiese 2019-03-29 09:48:10 +08:00
parent 6bdaf20462
commit f44557d045
3 changed files with 47 additions and 0 deletions

View File

@ -262,4 +262,22 @@ class award_criteria_competency extends award_criteria {
public static function is_enabled() {
return \core_competency\api::is_enabled();
}
/**
* Check if any badge has records for competencies.
*
* @param array $competencyids Array of competencies ids.
* @return boolean Return true if competencies were found in any badge.
*/
public static function has_records_for_competencies($competencyids) {
global $DB;
list($insql, $params) = $DB->get_in_or_equal($competencyids, SQL_PARAMS_NAMED);
$sql = "SELECT DISTINCT bc.badgeid
FROM {badge_criteria} bc
JOIN {badge_criteria_param} bcp ON bc.id = bcp.critid
WHERE bc.criteriatype = :criteriatype AND value $insql";
$params['criteriatype'] = BADGE_CRITERIA_TYPE_COMPETENCY;
return self::record_exists_sql($sql, $params);
}
}

View File

@ -769,6 +769,8 @@ class competency extends persistent {
* @return bool True if we can delete the competencies.
*/
public static function can_all_be_deleted($ids) {
global $CFG;
if (empty($ids)) {
return true;
}
@ -792,6 +794,13 @@ class competency extends persistent {
if (user_competency_plan::has_records_for_competencies($ids)) {
return false;
}
require_once($CFG->libdir . '/badgeslib.php');
// Check if competency is used in a badge.
if (badge_award_criteria_competency_has_records_for_competencies($ids)) {
return false;
}
return true;
}

View File

@ -1591,3 +1591,23 @@ function badges_list_criteria($enabled = true) {
}
return $types;
}
/**
* Check if any badge has records for competencies.
*
* @param array $competencyids Array of competencies ids.
* @return boolean Return true if competencies were found in any badge.
*/
function badge_award_criteria_competency_has_records_for_competencies($competencyids) {
global $DB;
list($insql, $params) = $DB->get_in_or_equal($competencyids, SQL_PARAMS_NAMED);
$sql = "SELECT DISTINCT bc.badgeid
FROM {badge_criteria} bc
JOIN {badge_criteria_param} bcp ON bc.id = bcp.critid
WHERE bc.criteriatype = :criteriatype AND bcp.value $insql";
$params['criteriatype'] = BADGE_CRITERIA_TYPE_COMPETENCY;
return $DB->record_exists_sql($sql, $params);
}