MDL-52477 tool_lp: Minor adjustments when duplicating competency rules

This commit is contained in:
Frederic Massart
2016-04-18 10:58:48 +08:00
parent 4c4a8d4177
commit 4b7784ee49
3 changed files with 14 additions and 15 deletions
+3 -1
View File
@@ -2584,7 +2584,9 @@ class api {
$competency->set_ruleoutcome($node->competency->get_ruleoutcome());
$competency->update();
} catch (\Exception $e) {
debugging('Error occured when migrating rules');
debugging('Could not migrate competency rule from: ' . $oldcompid . ' to: ' . $competency->get_id() . '.' .
' Exception: ' . $e->getMessage(), DEBUG_DEVELOPER);
$competency->reset_rule();
}
}
+7 -5
View File
@@ -99,12 +99,14 @@ abstract class competency_rule {
}
/**
* Migrate rule config when duplicate competency based on mapping competencies ids.
* Migrate rule config from one set of competencies to another.
*
* @param string $config the config rule of a competency
* @param array $mappings array that match the old competency ids with the new competencies object
*
* @return string
* Exceptions should be thrown when the migration can not be performed.
*
* @param string $config Original config rule of a competency.
* @param array $mappings Array that matches the original competency IDs with the new competencies objects.
* @return string New configuration.
* @throws Exception
*/
public static function migrate_config($config, $mappings) {
return $config;
@@ -25,6 +25,7 @@
namespace tool_lp;
defined('MOODLE_INTERNAL') || die();
use coding_exception;
use lang_string;
@@ -199,30 +200,24 @@ class competency_rule_points extends competency_rule {
/**
* Migrate rule config when duplicate competency based on mapping competencies ids.
* An exception can be thrown if the competency id is not found in the matchids.
* An exception can be thrown if the json config can not be decoded.
*
* @param string $config the config rule of a competency
* @param array $mappings array that match the old competency ids with the new competencies
*
* @return string
*/
public static function migrate_config($config, $mappings) {
$ruleconfig = json_decode($config, true);
if ($ruleconfig) {
if (is_array($ruleconfig)) {
foreach ($ruleconfig['competencies'] as $key => $rulecomp) {
$rulecmpid = $rulecomp['id'];
if (array_key_exists($rulecmpid, $mappings)) {
$ruleconfig['competencies'][$key]['id'] = $mappings[$rulecmpid]->get_id();
} else {
// Debugging message and throw exception when there is no match found.
debugging('Migrate rule config, the competency id is not found: ' . $rulecmpid);
throw new coding_exception("the competency id is not found in the matchids.");
throw new coding_exception("The competency id is not found in the matchids.");
}
}
} else {
debugging('Error decoding json config:' . $config);
throw new coding_exception("invalid json config rule.");
throw new coding_exception("Invalid JSON config rule.");
}
return json_encode($ruleconfig);