MDL-51442 tool_lp: Validate the frameworks' scale ID

This commit is contained in:
Frederic Massart
2016-04-18 10:58:36 +08:00
parent 31582c1638
commit bf61cd0cf3
3 changed files with 108 additions and 102 deletions
+77 -67
View File
@@ -193,6 +193,83 @@ class competency_framework extends persistent {
return true;
}
/**
* Validate the id number.
*
* @param string $value The id number.
* @return bool|lang_string
*/
public function validate_idnumber($value) {
global $DB;
$params = array(
'id' => $this->get('id'),
'idnumber' => $value,
);
if ($DB->record_exists_select(self::TABLE, 'idnumber = :idnumber AND id <> :id', $params)) {
return new lang_string('idnumbertaken', 'error');
}
return true;
}
/**
* Validate the scale ID.
*
* @param string $value The scale ID.
* @return bool|lang_string
*/
protected function validate_scaleid($value) {
global $DB;
if (!$DB->record_exists_select('scale', 'id = :id', array('id' => $value))) {
return new lang_string('invalidscaleid', 'error');
}
return true;
}
/**
* Validate the scale configuration.
*
* @param string $value The scale configuration.
* @return bool|lang_string
*/
public function validate_scaleconfiguration($value) {
global $DB;
$scaledefaultselected = false;
$proficientselected = false;
$scaleconfigurations = json_decode($value);
if (is_array($scaleconfigurations)) {
// The first element of the array contains the scale ID.
$scaleinfo = array_shift($scaleconfigurations);
if (empty($scaleinfo) || !isset($scaleinfo->scaleid) || $scaleinfo->scaleid != $this->get('scaleid')) {
// This should never happen.
return new lang_string('invaliddata', 'error');
}
// Walk through the array to find proficient and default values.
foreach ($scaleconfigurations as $scaleconfiguration) {
if (isset($scaleconfiguration->scaledefault) && $scaleconfiguration->scaledefault) {
$scaledefaultselected = true;
}
if (isset($scaleconfiguration->proficient) && $scaleconfiguration->proficient) {
$proficientselected = true;
}
}
}
if (!$scaledefaultselected || !$proficientselected) {
return new lang_string('errorscaleconfiguration', 'tool_lp');
}
return true;
}
/**
* Validate taxonomies.
*
@@ -266,71 +343,4 @@ class competency_framework extends persistent {
return $list;
}
/**
* Validate the scale configuration.
*
* @param string $value The scale configuration.
* @return bool|lang_string
*/
public function validate_scaleconfiguration($value) {
global $DB;
$scaleidvalid = false;
$scaledefaultselected = false;
$proficientselected = false;
if (!empty($value)) {
$scaleconfigurations = json_decode($value);
if (is_array($scaleconfigurations)) {
foreach ($scaleconfigurations as $scaleconfiguration) {
// First element must be a valid scaleid.
if (isset($scaleconfiguration->scaleid)) {
$params = array(
'id' => $scaleconfiguration->scaleid,
);
if ($DB->record_exists_select('scale', 'id = :id', $params)) {
$scaleidvalid = true;
}
}
if (!$scaleidvalid || ($scaledefaultselected && $proficientselected)) {
break;
}
if (isset($scaleconfiguration->scaledefault) && $scaleconfiguration->scaledefault) {
$scaledefaultselected = true;
}
if (isset($scaleconfiguration->proficient) && $scaleconfiguration->proficient) {
$proficientselected = true;
}
}
}
}
if (!$scaleidvalid || !$scaledefaultselected || !$proficientselected) {
return new lang_string('errorscaleconfiguration', 'tool_lp');
}
return true;
}
/**
* Validate the id number.
*
* @param string $value The id number.
* @return bool|lang_string
*/
public function validate_idnumber($value) {
global $DB;
$params = array(
'id' => $this->get('id'),
'idnumber' => $value,
);
if ($DB->record_exists_select(self::TABLE, 'idnumber = :idnumber AND id <> :id', $params)) {
return new lang_string('idnumbertaken', 'error');
}
return true;
}
}
+25 -27
View File
@@ -188,37 +188,35 @@ class tool_lp_api_testcase extends advanced_testcase {
public function test_list_frameworks() {
$this->resetAfterTest(true);
$this->setAdminUser();
$lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
// Create a list of frameworks.
$params1 = array(
'shortname' => 'shortname_a',
'idnumber' => 'idnumber_c',
'description' => 'description',
'descriptionformat' => FORMAT_HTML,
'visible' => true,
'contextid' => context_system::instance()->id
);
$framework1 = api::create_framework((object) $params1);
$framework1 = $lpg->create_framework(array(
'shortname' => 'shortname_a',
'idnumber' => 'idnumber_c',
'description' => 'description',
'descriptionformat' => FORMAT_HTML,
'visible' => true,
'contextid' => context_system::instance()->id
));
$params2 = array(
'shortname' => 'shortname_b',
'idnumber' => 'idnumber_a',
'description' => 'description',
'descriptionformat' => FORMAT_HTML,
'visible' => true,
'contextid' => context_system::instance()->id
);
$framework2 = api::create_framework((object) $params2);
$framework2 = $lpg->create_framework(array(
'shortname' => 'shortname_b',
'idnumber' => 'idnumber_a',
'description' => 'description',
'descriptionformat' => FORMAT_HTML,
'visible' => true,
'contextid' => context_system::instance()->id
));
$params3 = array(
'shortname' => 'shortname_c',
'idnumber' => 'idnumber_b',
'description' => 'description',
'descriptionformat' => FORMAT_HTML,
'visible' => true,
'contextid' => context_system::instance()->id
);
$framework3 = api::create_framework((object) $params3);
$framework3 = $lpg->create_framework(array(
'shortname' => 'shortname_c',
'idnumber' => 'idnumber_b',
'description' => 'description',
'descriptionformat' => FORMAT_HTML,
'visible' => true,
'contextid' => context_system::instance()->id
));
// Get frameworks list order by shortname desc.
$result = api::list_frameworks('shortname', 'DESC', null, 3, context_system::instance());
+6 -8
View File
@@ -1246,6 +1246,7 @@ class tool_lp_external_testcase extends externallib_advanced_testcase {
public function test_remove_competency_from_template() {
$this->setUser($this->creator);
$lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
$syscontext = context_system::instance();
@@ -1255,20 +1256,17 @@ class tool_lp_external_testcase extends externallib_advanced_testcase {
$template = (object) external_api::clean_returnvalue(external::create_template_returns(), $template);
// Create a competency.
$framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1,
$this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id));
$framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework);
$competency = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0);
$competency = (object) external_api::clean_returnvalue(external::create_competency_returns(), $competency);
$framework = $lpg->create_framework();
$competency = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
// Add the competency.
external::add_competency_to_template($template->id, $competency->id);
external::add_competency_to_template($template->id, $competency->get_id());
// Check that it was added.
$this->assertEquals(1, external::count_competencies_in_template($template->id));
// Check that we can remove the competency.
external::remove_competency_from_template($template->id, $competency->id);
external::remove_competency_from_template($template->id, $competency->get_id());
// Check that it was removed.
$this->assertEquals(0, external::count_competencies_in_template($template->id));
@@ -1279,7 +1277,7 @@ class tool_lp_external_testcase extends externallib_advanced_testcase {
// Check we can not remove the competency now.
try {
external::add_competency_to_template($template->id, $competency->id);
external::add_competency_to_template($template->id, $competency->get_id());
$this->fail('Exception expected due to not permissions to manage template competencies');
} catch (moodle_exception $e) {
$this->assertEquals('nopermissions', $e->errorcode);