MDL-70815 completion: Cast custom data to array

Since cm_info::customdata can be of any type, we need to cast it to an
array first before checking for custom completion rules. Otherwise,
an exception can be thrown (e.g. customdata has been set as an stdClass)
This commit is contained in:
Jun Pataleta 2021-03-10 22:28:05 +08:00 committed by Sara Arjona
parent 5a7c629f7c
commit fc238f329d
2 changed files with 6 additions and 3 deletions

View File

@ -96,8 +96,9 @@ abstract class activity_custom_completion {
public function get_available_custom_rules(): array {
$rules = static::get_defined_custom_rules();
$availablerules = [];
$customdata = (array)$this->cm->customdata;
foreach ($rules as $rule) {
$customrule = $this->cm->customdata['customcompletionrules'][$rule] ?? false;
$customrule = $customdata['customcompletionrules'][$rule] ?? false;
if (!empty($customrule)) {
$availablerules[] = $rule;
}

View File

@ -1107,8 +1107,10 @@ class completion_info {
// Custom activity module completion data.
// Cast custom data to array before checking for custom completion rules.
$customdata = (array)$cm->customdata;
// Return early if the plugin does not define custom completion rules.
if (empty($cm->customdata['customcompletionrules'])) {
if (empty($customdata['customcompletionrules'])) {
return [];
}
@ -1120,7 +1122,7 @@ class completion_info {
/** @var activity_custom_completion $customcmcompletion */
$customcmcompletion = new $cmcompletionclass($cm, $userid);
foreach ($cm->customdata['customcompletionrules'] as $rule => $enabled) {
foreach ($customdata['customcompletionrules'] as $rule => $enabled) {
if (!$enabled) {
// Skip inactive completion rules.
continue;