Skip to content

Commit

Permalink
MDL-63876 badges: Competency deletion
Browse files Browse the repository at this point in the history
Do not delete a competency if it is a criteria for a badge.
  • Loading branch information
Damyon Wiese committed Mar 29, 2019
1 parent 6bdaf20 commit f44557d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
18 changes: 18 additions & 0 deletions badges/criteria/award_criteria_competency.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,22 @@ public function get_completed_criteria_sql() {
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);
}
}
9 changes: 9 additions & 0 deletions competency/classes/competency.php
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,8 @@ protected static function build_tree($all, $parentid) {
* @return bool True if we can delete the competencies.
*/
public static function can_all_be_deleted($ids) {
global $CFG;

if (empty($ids)) {
return true;
}
Expand All @@ -792,6 +794,13 @@ public static function can_all_be_deleted($ids) {
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;
}

Expand Down
20 changes: 20 additions & 0 deletions lib/badgeslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit f44557d

Please sign in to comment.