MDL-53655 tool_lp: Allow closures in default values of persistents
This commit is contained in:
@@ -51,7 +51,9 @@ class course_competency_settings extends persistent {
|
||||
),
|
||||
'pushratingstouserplans' => array(
|
||||
'type' => PARAM_BOOL,
|
||||
'default' => get_config('tool_lp', 'pushcourseratingstouserplans')
|
||||
'default' => function() {
|
||||
return get_config('tool_lp', 'pushcourseratingstouserplans');
|
||||
}
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -140,7 +140,14 @@ abstract class persistent {
|
||||
*
|
||||
* Each property MUST be listed here.
|
||||
*
|
||||
* Example:
|
||||
* The result of this method is cached internally for the whole request.
|
||||
*
|
||||
* The 'default' value can be a Closure when its value may change during a single request.
|
||||
* For example if the default value is based on a $CFG property, then it should be wrapped in a closure
|
||||
* to avoid running into scenarios where the true value of $CFG is not reflected in the definition.
|
||||
* Do not abuse closures as they obviously add some overhead.
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* array(
|
||||
* 'property_name' => array(
|
||||
@@ -152,6 +159,15 @@ abstract class persistent {
|
||||
* )
|
||||
* )
|
||||
*
|
||||
* array(
|
||||
* 'dynamic_property_name' => array(
|
||||
* 'default' => function() {
|
||||
* return $CFG->something;
|
||||
* },
|
||||
* 'type' => PARAM_INT,
|
||||
* )
|
||||
* )
|
||||
*
|
||||
* @return array Where keys are the property names.
|
||||
*/
|
||||
protected static function define_properties() {
|
||||
@@ -254,7 +270,11 @@ abstract class persistent {
|
||||
if (!isset($properties[$property]['default'])) {
|
||||
return null;
|
||||
}
|
||||
return $properties[$property]['default'];
|
||||
$value = $properties[$property]['default'];
|
||||
if ($value instanceof \Closure) {
|
||||
return $value();
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user