From 10bf349e70407b833ced7d70ce5db3a370c4cd64 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Mon, 21 Mar 2016 13:21:57 +0800 Subject: [PATCH] MDL-53568 tool_lp: Fix getting proficiency of grade not in scaleconfig --- .../tool/lp/classes/competency_framework.php | 2 +- admin/tool/lp/tests/api_test.php | 6 +---- admin/tool/lp/tests/generator/lib.php | 24 ++++++++++++------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/admin/tool/lp/classes/competency_framework.php b/admin/tool/lp/classes/competency_framework.php index d665558e39d..f9a41c8e21f 100644 --- a/admin/tool/lp/classes/competency_framework.php +++ b/admin/tool/lp/classes/competency_framework.php @@ -397,7 +397,7 @@ class competency_framework extends persistent { } } - throw new coding_exception('Grade not found in configuration.'); + return 0; } /** diff --git a/admin/tool/lp/tests/api_test.php b/admin/tool/lp/tests/api_test.php index b22cd597aea..63fb817274e 100644 --- a/admin/tool/lp/tests/api_test.php +++ b/admin/tool/lp/tests/api_test.php @@ -2014,12 +2014,10 @@ class tool_lp_api_testcase extends advanced_testcase { $u1ctx = context_user::instance($u1->id); $scale = $dg->create_scale(array('scale' => 'A,B,C,D')); $scaleconfig = array(array('scaleid' => $scale->id)); - $scaleconfig[] = array('name' => 'A', 'id' => 1, 'scaledefault' => 0, 'proficient' => 0); $scaleconfig[] = array('name' => 'B', 'id' => 2, 'scaledefault' => 1, 'proficient' => 0); $scaleconfig[] = array('name' => 'C', 'id' => 3, 'scaledefault' => 0, 'proficient' => 1); $scaleconfig[] = array('name' => 'D', 'id' => 4, 'scaledefault' => 0, 'proficient' => 1); $c2scaleconfig = array(array('scaleid' => $scale->id)); - $c2scaleconfig[] = array('name' => 'A', 'id' => 1, 'scaledefault' => 0, 'proficient' => 0); $c2scaleconfig[] = array('name' => 'B', 'id' => 2, 'scaledefault' => 0, 'proficient' => 1); $c2scaleconfig[] = array('name' => 'C', 'id' => 3, 'scaledefault' => 0, 'proficient' => 0); $c2scaleconfig[] = array('name' => 'D', 'id' => 4, 'scaledefault' => 1, 'proficient' => 1); @@ -2785,9 +2783,7 @@ class tool_lp_api_testcase extends advanced_testcase { $scaleconfiguration1 = '[{"scaleid":"1"},{"name":"value1","id":1,"scaledefault":1,"proficient":0},' . '{"name":"value2","id":2,"scaledefault":0,"proficient":1}]'; $scaleconfiguration2 = '[{"scaleid":"2"},{"name":"value3","id":1,"scaledefault":1,"proficient":0},' - . '{"name":"value4","id":2,"scaledefault":0,"proficient":1},' - . '{"name":"value5","id":3,"scaledefault":0,"proficient":0},' - . '{"name":"value6","id":4,"scaledefault":0,"proficient":0}]'; + . '{"name":"value4","id":2,"scaledefault":0,"proficient":1}]'; // Create a framework with scale configuration1. $frm = array( diff --git a/admin/tool/lp/tests/generator/lib.php b/admin/tool/lp/tests/generator/lib.php index 3d0f0064bee..ee5902cd8ee 100644 --- a/admin/tool/lp/tests/generator/lib.php +++ b/admin/tool/lp/tests/generator/lib.php @@ -162,15 +162,21 @@ class tool_lp_generator extends component_generator_base { if (count($values) < 2) { throw new coding_exception('Please provide the scale configuration for one-item scales.'); } - $scaleconfig = array(array('scaleid' => $record->scaleid)); - foreach ($values as $key => $value) { - $scaleconfig[] = array( - 'name' => $value['name'], - 'id' => $value['id'], - 'scaledefault' => $key == count($values) - 2 ? 1 : 0, // Second to last is default. - 'proficient' => $key >= count($values) - 2 ? 1 : 0, // Second to last and last are proficient. - ); - } + $scaleconfig = array(); + // Last item is proficient. + $item = array_pop($values); + array_unshift($scaleconfig, array( + 'id' => $item['id'], + 'proficient' => 1 + )); + // Second-last item is default and proficient. + $item = array_pop($values); + array_unshift($scaleconfig, array( + 'id' => $item['id'], + 'scaledefault' => 1, + 'proficient' => 1 + )); + array_unshift($scaleconfig, array('scaleid' => $record->scaleid)); $record->scaleconfiguration = json_encode($scaleconfig); } if (is_array($record->scaleconfiguration) || is_object($record->scaleconfiguration)) {