From 3baf704ddb2071d02adfd84d0844a6b66af862cb Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Fri, 1 Apr 2016 15:16:26 +0800 Subject: [PATCH] MDL-53655 tool_lp: Allow closures in default values of persistents --- .../lp/classes/course_competency_settings.php | 4 +++- admin/tool/lp/classes/persistent.php | 24 +++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/admin/tool/lp/classes/course_competency_settings.php b/admin/tool/lp/classes/course_competency_settings.php index 32f25b2bcd8..729902075b5 100644 --- a/admin/tool/lp/classes/course_competency_settings.php +++ b/admin/tool/lp/classes/course_competency_settings.php @@ -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'); + } ), ); } diff --git a/admin/tool/lp/classes/persistent.php b/admin/tool/lp/classes/persistent.php index 4fae7c34da1..159beb2f127 100644 --- a/admin/tool/lp/classes/persistent.php +++ b/admin/tool/lp/classes/persistent.php @@ -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; } /** -- 2.43.0