Skip to content

Commit

Permalink
MDL-51442 tool_lp: Framework scales configuration must be mandatory
Browse files Browse the repository at this point in the history
  • Loading branch information
gauts authored and Frederic Massart committed Apr 18, 2016
1 parent 8cb101a commit 31582c1
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 19 deletions.
75 changes: 69 additions & 6 deletions admin/tool/lp/classes/competency_framework.php
Expand Up @@ -95,15 +95,11 @@ protected static function define_properties() {
'type' => PARAM_BOOL,
'default' => 1
),
// TODO MDL-51442 make this mandatory.
'scaleid' => array(
'type' => PARAM_INT,
'default' => 0
'type' => PARAM_INT
),
// TODO MDL-51442 make this mandatory.
'scaleconfiguration' => array(
'type' => PARAM_RAW,
'default' => ''
'type' => PARAM_RAW
),
'contextid' => array(
'type' => PARAM_INT
Expand Down Expand Up @@ -270,4 +266,71 @@ public static function get_taxonomies_list() {
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;
}

}
2 changes: 0 additions & 2 deletions admin/tool/lp/classes/external.php
Expand Up @@ -312,7 +312,6 @@ public static function create_competency_framework($shortname, $idnumber, $descr

$context = self::get_context_from_params($params['context']);
self::validate_context($context);
// TODO MDL-51506 Implement validation of scale configuration.

unset($params['context']);
$params['contextid'] = $context->id;
Expand Down Expand Up @@ -539,7 +538,6 @@ public static function update_competency_framework($id,
'visible' => $visible
));

// TODO MDL-51506 Implement validation of scale configuration.
$params = (object) $params;

return api::update_framework($params);
Expand Down
25 changes: 14 additions & 11 deletions admin/tool/lp/classes/form/competency_framework.php
Expand Up @@ -133,17 +133,20 @@ public function get_data() {
* @return array
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);

// Add field validation check for duplicate idnumber.
$framework = new \tool_lp\competency_framework();
$params = array(
'id' => $data['id'],
'idnumber' => $data['idnumber'],
);
$exists = $framework->get_records_select('idnumber = :idnumber AND id <> :id', $params, '', 'id', 0, 1);
if ($exists) {
$errors['idnumber'] = get_string('idnumbertaken', 'error');
$context = $this->_customdata['context'];

$data = $this->get_submitted_data();
unset($data->submitbutton);
$data->descriptionformat = $data->description['format'];
$data->description = $data->description['text'];
$data->contextid = $context->id;
$data->taxonomies = implode(',', $data->taxonomies);

$framework = new \tool_lp\competency_framework(0, $data);
$errors = $framework->get_errors();
if (isset($errors['scaleconfiguration'])) {
$errors['scaleid'] = $errors['scaleconfiguration'];
unset($errors['scaleconfiguration']);
}

return $errors;
Expand Down
1 change: 1 addition & 0 deletions admin/tool/lp/lang/en/tool_lp.php
Expand Up @@ -64,6 +64,7 @@
tool/lp:planmanage or tool/lp:planmanageown capabilities.';
$string['errornocompetency'] = '{$a} competency can not be found';
$string['errorplanstatus'] = 'Learning plans \'{$a}\' status unknown';
$string['errorscaleconfiguration'] = 'You must configure the scale by selecting default and proficient values.';
$string['hidden'] = 'Hidden';
$string['hiddenhint'] = '(hidden)';
$string['idnumber'] = 'Id number';
Expand Down
6 changes: 6 additions & 0 deletions admin/tool/lp/tests/externallib_test.php
Expand Up @@ -127,6 +127,12 @@ protected function setUp() {
$this->user = $user;
$this->catuser = $catuser;
$this->category = $category;

$this->getDataGenerator()->create_scale(array("id" => "1", "scale" => "value1, value2"));
$this->getDataGenerator()->create_scale(array("id" => "2", "scale" => "value3, value4"));
$this->getDataGenerator()->create_scale(array("id" => "3", "scale" => "value5, value6"));
$this->getDataGenerator()->create_scale(array("id" => "4", "scale" => "value7, value8"));

$this->scaleconfiguration1 = '[{"scaleid":"1"},{"name":"value1","id":1,"scaledefault":1,"proficient":0},' .
'{"name":"value2","id":2,"scaledefault":0,"proficient":1}]';
$this->scaleconfiguration2 = '[{"scaleid":"2"},{"name":"value3","id":1,"scaledefault":1,"proficient":0},' .
Expand Down

0 comments on commit 31582c1

Please sign in to comment.