From 481c57cabe2624008af52f6ddcf2ee6553d643ad Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Fri, 6 Nov 2020 15:15:45 +0100 Subject: [PATCH] MDL-70139 core_badges: fix invalid request when sending to backpack Having mixed $data in badge exporters is causing some issues. As all these exporters are using $data as an object, $data can be converted to object in the constructor, to avoid errors and get the expected behaviour always. --- badges/classes/external/assertion_exporter.php | 14 ++++++++++++++ badges/classes/external/badgeclass_exporter.php | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/badges/classes/external/assertion_exporter.php b/badges/classes/external/assertion_exporter.php index 06b523803e3..78e86b06462 100644 --- a/badges/classes/external/assertion_exporter.php +++ b/badges/classes/external/assertion_exporter.php @@ -39,6 +39,20 @@ use stdClass; */ class assertion_exporter extends exporter { + /** + * Constructor - saves the persistent object, and the related objects. + * + * @param mixed $data - Either an stdClass or an array of values. + * @param array $related - An optional list of pre-loaded objects related to this object. + */ + public function __construct($data, $related = array()) { + // Having mixed $data is causing some issues. As this class is treating $data as an object everywhere, it can be converted + // to object at this point, to avoid errors and get the expected behaviour always. + // $data is an array when this class is a request exporter in backpack_api_mapping, but it is an object when this is + // used as a response exporter. + parent::__construct((object) $data, $related); + } + /** * Map from a request response data to the internal structure. * diff --git a/badges/classes/external/badgeclass_exporter.php b/badges/classes/external/badgeclass_exporter.php index 4d4880fbad1..9aee9568256 100644 --- a/badges/classes/external/badgeclass_exporter.php +++ b/badges/classes/external/badgeclass_exporter.php @@ -45,6 +45,12 @@ class badgeclass_exporter extends exporter { * @param array $related - An optional list of pre-loaded objects related to this object. */ public function __construct($data, $related = array()) { + // Having mixed $data is causing some issues. As this class is treating $data as an object everywhere, it can be converted + // to object at this point, to avoid errors and get the expected behaviour always. + // $data is an array when this class is a request exporter in backpack_api_mapping, but it is an object when this is + // used as a response exporter. + $data = (object) $data; + $pick = $this->pick_related(); foreach ($pick as $one) { $isarray = false;