MDL-51866 tool_lp: Framework scale cannot be changed once it is used

This commit is contained in:
Frederic Massart
2016-04-18 10:58:40 +08:00
parent 4ad3ac34f4
commit 2ec1b70bcd
5 changed files with 117 additions and 13 deletions
+43 -1
View File
@@ -62,6 +62,9 @@ class competency_framework extends persistent {
/** Taxonomy constant. */
const TAXONOMY_VALUE = 'value';
/** @var static The object before it was updated. */
protected $beforeupdate;
/**
* Get the context.
*
@@ -113,6 +116,21 @@ class competency_framework extends persistent {
);
}
/**
* Hook to execute before validate.
*
* @return void
*/
protected function before_validate() {
$this->beforeupdate = null;
// During update.
if ($this->get_id()) {
$this->beforeupdate = new competency_framework($this->get_id());
}
}
/**
* Return the scale.
*
@@ -165,6 +183,17 @@ class competency_framework extends persistent {
return $taxonomies;
}
/**
* Returns true when some competencies of the framework have user competencies.
*
* This is useful to determine if the framework, or part of it, should be locked down.
*
* @return boolean
*/
public function has_user_competencies() {
return user_competency::has_records_for_framework($this->get_id());
}
/**
* Convenience method to set taxonomies from an array or string.
*
@@ -236,10 +265,23 @@ class competency_framework extends persistent {
protected function validate_scaleid($value) {
global $DB;
// Always validate that the scale exists.
if (!$DB->record_exists_select('scale', 'id = :id', array('id' => $value))) {
return new lang_string('invalidscaleid', 'error');
}
// During update.
if ($this->get_id()) {
// Validate that we can only change the scale when it is not used yet.
if ($this->beforeupdate->get_scaleid() != $value) {
if ($this->beforeupdate->has_user_competencies()) {
return new lang_string('errorscalealreadyused', 'tool_lp');
}
}
}
return true;
}
@@ -262,7 +304,7 @@ class competency_framework extends persistent {
$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');
return new lang_string('errorscaleconfiguration', 'tool_lp');
}
// Walk through the array to find proficient and default values.