Skip to content

Commit

Permalink
MDL-51130 tool_lp: Deleting frameworks should delete associated data
Browse files Browse the repository at this point in the history
  • Loading branch information
taboubi authored and Frederic Massart committed Apr 18, 2016
1 parent 85e45ff commit 90c7bda
Show file tree
Hide file tree
Showing 9 changed files with 446 additions and 5 deletions.
2 changes: 1 addition & 1 deletion admin/tool/lp/amd/build/frameworkactions.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions admin/tool/lp/amd/src/frameworkactions.js
Expand Up @@ -93,6 +93,25 @@ define(['jquery', 'core/templates', 'core/ajax', 'core/notification', 'core/str'
}
}
}]);
requests[0].done(function (success) {
if (success === false) {
var req = ajax.call([{
methodname: 'tool_lp_read_competency_framework',
args: { id: frameworkid }
}]);
req[0].done(function (framework) {
str.get_strings([
{ key: 'frameworkcannotbedeleted', component: 'tool_lp', param: framework.shortname },
{ key: 'cancel', component: 'moodle' }
]).done(function (strings) {
notification.alert(
null,
strings[0]
);
}).fail(notification.exception);
});
}
}).fail(notification.exception);
requests[1].done(reloadList).fail(notification.exception);
};

Expand Down
35 changes: 32 additions & 3 deletions admin/tool/lp/classes/api.php
Expand Up @@ -528,13 +528,42 @@ public static function duplicate_framework($id) {
* @return boolean
*/
public static function delete_framework($id) {
global $DB;
$framework = new competency_framework($id);
require_capability('tool/lp:competencymanage', $framework->get_context());

// Trigger a competency framework deleted event.
\tool_lp\event\competency_framework_deleted::create_from_framework($framework)->trigger();
$competenciesid = competency::get_ids_by_frameworkid($id);
if (!competency::can_all_be_deleted($competenciesid)) {
return false;
}
$transaction = $DB->start_delegated_transaction();
try {
if (!empty($competenciesid)) {
// Delete competencies.
competency::delete_by_frameworkid($id);

// Delete the related competencies.
related_competency::delete_multiple_relations($competenciesid);

// Delete the evidences for competencies.
user_evidence_competency::delete_by_competencyids($competenciesid);
}

// Create a competency framework deleted event.
$event = \tool_lp\event\competency_framework_deleted::create_from_framework($framework);
$result = $framework->delete();

return $framework->delete();
} catch (\Exception $e) {
$transaction->rollback($e);
}

// Commit the transaction.
$transaction->allow_commit();

// If all operations are successfull then trigger the delete event.
$event->trigger();

return $result;
}

/**
Expand Down
30 changes: 30 additions & 0 deletions admin/tool/lp/classes/competency.php
Expand Up @@ -754,6 +754,9 @@ protected static function build_tree($all, $parentid) {
* @return bool True if we can delete the competencies.
*/
public static function can_all_be_deleted($ids) {
if (empty($ids)) {
return true;
}
// Check if competency is used in template.
if (template_competency::has_records_for_competencies($ids)) {
return false;
Expand Down Expand Up @@ -807,4 +810,31 @@ public static function get_descendants_ids($competency) {
return $DB->get_fieldset_select(self::TABLE, 'id', $like, array('likepath' => $path));
}

/**
* Get competencyids by frameworkid.
*
* @param int $frameworkid The competency framework ID.
* @return array Array of competency ids.
*/
public static function get_ids_by_frameworkid($frameworkid) {
global $DB;

return $DB->get_fieldset_select(self::TABLE, 'id', 'competencyframeworkid = :frmid', array('frmid' => $frameworkid));
}

/**
* Delete competencies by framework ID.
*
* This method is reserved to core usage.
* This method does not trigger the after_delete event.
* This method does not delete related objects such as related competencies and evidences.
*
* @param int $id the framework ID
* @return bool Return true if delete was successful.
*/
public static function delete_by_frameworkid($id) {
global $DB;
return $DB->delete_records(self::TABLE, array('competencyframeworkid' => $id));
}

}
Expand Up @@ -86,7 +86,7 @@ public static function get_name() {
protected function init() {
$this->data['crud'] = 'd';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = competency_framework::TABLE;;
$this->data['objecttable'] = competency_framework::TABLE;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions admin/tool/lp/classes/related_competency.php
Expand Up @@ -196,6 +196,10 @@ public static function get_multiple_relations($competencyids) {
*/
public static function delete_multiple_relations($competencyids) {
global $DB;
if (empty($competencyids)) {
return true;
}

list($insql, $params) = $DB->get_in_or_equal($competencyids);
return $DB->delete_records_select(self::TABLE,
"competencyid $insql OR relatedcompetencyid $insql",
Expand Down
3 changes: 3 additions & 0 deletions admin/tool/lp/classes/user_evidence_competency.php
Expand Up @@ -139,6 +139,9 @@ public static function get_relation($userevidenceid, $competencyid) {
*/
public static function delete_by_competencyids($competencyids) {
global $DB;
if (empty($competencyids)) {
return true;
}
list($insql, $params) = $DB->get_in_or_equal($competencyids);
return $DB->delete_records_select(self::TABLE, "competencyid $insql", $params);
}
Expand Down
1 change: 1 addition & 0 deletions admin/tool/lp/lang/en/tool_lp.php
Expand Up @@ -119,6 +119,7 @@
$string['errorplanstatus'] = 'Learning plans \'{$a}\' status unknown';
$string['errorscalealreadyused'] = 'The scale cannot be changed, it is already being used.';
$string['errorscaleconfiguration'] = 'You must configure the scale by selecting default and proficient values.';
$string['frameworkcannotbedeleted'] = 'The competency framework \'{$a}\' can not be deleted';
$string['hidden'] = 'Hidden';
$string['hiddenhint'] = '(hidden)';
$string['idnumber'] = 'Id number';
Expand Down

0 comments on commit 90c7bda

Please sign in to comment.