From bfb4e5bbb74b05616e9a588c216ad1425f3b9619 Mon Sep 17 00:00:00 2001 From: gthomas2 Date: Mon, 9 Oct 2023 20:24:24 +0100 Subject: [PATCH 1/3] BFH-1: Improve verbal feedback questionnaire performance by reducing database calls --- classes/repository/instance_repository.php | 153 ++++++++++++++++----- 1 file changed, 122 insertions(+), 31 deletions(-) diff --git a/classes/repository/instance_repository.php b/classes/repository/instance_repository.php index 4ea75c2..c496d34 100644 --- a/classes/repository/instance_repository.php +++ b/classes/repository/instance_repository.php @@ -30,6 +30,7 @@ use Exception; use mod_verbalfeedback\model\instance; +use mod_verbalfeedback\model\instance_category; use mod_verbalfeedback\model\instance_status; use mod_verbalfeedback\repository\model\db_instance; use mod_verbalfeedback\repository\model\db_instance_category; @@ -52,50 +53,45 @@ class instance_repository { */ public static function get_by_id(int $id) : instance { global $DB; + + // Return cached $byid instance if available and if we are not running a php unit test. + static $byid = []; + if (!PHPUNIT_TEST && isset($byid[$id])) { + return $byid[$id]; + } + $dboinstance = $DB->get_record(tables::INSTANCE_TABLE, ["id" => $id]); $instance = db_instance::to_instance($dboinstance); $dbocategories = $DB->get_records(tables::INSTANCE_CATEGORY_TABLE, ["instanceid" => $id]); - $sortedstrings = []; - $dboheaders = $DB->get_records(tables::LOCALIZED_STRING_TABLE); - foreach ($dboheaders as $dboheader) { - $dbo_obj = new db_localized_string; - $dbo_obj->id = $dboheader->id; - $dbo_obj->languageid = $dboheader->languageid; - $dbo_obj->string = $dboheader->string; - $dbo_obj->type = $dboheader->type; - $dbo_obj->foreignkey = $dboheader->foreignkey; - - $sortedstrings[$dbo_obj->type][$dbo_obj->foreignkey][$dbo_obj->languageid] = $dbo_obj; - } + $criteriabycatid = self::get_criteria_by_category_for_instance_id($id); + $subratingsbycritid = self::get_subratings_by_criterion_for_instance($id); foreach ($dbocategories as $dbocategory) { $category = db_instance_category::to_instance_category($dbocategory); // Load category headers. - $dbolocalizedstrings = $sortedstrings[localized_string_type::INSTANCE_CATEGORY_HEADER][$category->get_id()] ?? []; + $dbolocalizedstrings = self::get_strings(localized_string_type::INSTANCE_CATEGORY_HEADER, $category->get_id()); foreach ($dbolocalizedstrings as $dbo) { $header = db_localized_string::to_localized_string($dbo); $category->add_header($header); } // Load category criteria. - $dbocriteria = $DB->get_records(tables::INSTANCE_CRITERION_TABLE, ["categoryid" => $category->get_id()]); - foreach ($dbocriteria as $dbocriterion) { - $criterion = db_instance_criterion::to_instance_criterion($dbocriterion); + $criteria = $criteriabycatid[$category->get_id()]; + foreach ($criteria as $criterion) { // Load criterion description. - $dbodescriptions = $sortedstrings[localized_string_type::INSTANCE_CRITERION][$criterion->get_id()] ?? []; + $dbodescriptions = self::get_strings(localized_string_type::INSTANCE_CRITERION, $criterion->get_id()); foreach ($dbodescriptions as $dbodescription) { $description = db_localized_string::to_localized_string($dbodescription); $criterion->add_description($description); } // Load criterion subratings. - $dbosubratings = $DB->get_records(tables::INSTANCE_SUBRATING_TABLE, ["criterionid" => $criterion->get_id()]); - foreach ($dbosubratings as $dbosubrating) { - $subrating = db_instance_subrating::to_subrating($dbosubrating); + $subratings = $subratingsbycritid[$criterion->get_id()]; + foreach ($subratings as $subrating) { foreach ([ // Load subrating titles (1 title per language). localized_string_type::INSTANCE_SUBRATING_TITLE => 'titles', @@ -110,7 +106,7 @@ public static function get_by_id(int $id) : instance { // Load subrating very positive texts. localized_string_type::INSTANCE_SUBRATING_VERY_POSITIVE => 'verypositives', ] as $type => $attribute) { - $dbotitles = $sortedstrings[$type][$subrating->get_id()] ?? []; + $dbotitles = self::get_strings($type, $subrating->get_id()); foreach ($dbotitles as $dbotitle) { $item = db_localized_string::to_localized_string($dbotitle); $subrating->{$attribute}[] = $item; @@ -118,40 +114,35 @@ public static function get_by_id(int $id) : instance { } // Load subrating descriptions. - $dbosubratingdescriptions = $DB->get_records(tables::LOCALIZED_STRING_TABLE, - ["type" => localized_string_type::INSTANCE_SUBRATING_DESCRIPTION, "foreignkey" => $subrating->get_id()]); + $dbosubratingdescriptions = self::get_strings(localized_string_type::INSTANCE_SUBRATING_DESCRIPTION, $subrating->get_id()); foreach ($dbosubratingdescriptions as $dbosubratingdescription) { $subratingdescription = db_localized_string::to_localized_string($dbosubratingdescription); $subrating->add_description($subratingdescription); } // Load subrating very negative texts. - $dboverynegatives = $DB->get_records(tables::LOCALIZED_STRING_TABLE, - ["type" => localized_string_type::INSTANCE_SUBRATING_VERY_NEGATIVE, "foreignkey" => $subrating->get_id()]); + $dboverynegatives = self::get_strings(localized_string_type::INSTANCE_SUBRATING_VERY_NEGATIVE, $subrating->get_id()); foreach ($dboverynegatives as $dboverynegative) { $verynegative = db_localized_string::to_localized_string($dboverynegative); $subrating->add_verynegative($verynegative); } // Load subrating negative texts. - $dbonegatives = $DB->get_records(tables::LOCALIZED_STRING_TABLE, - ["type" => localized_string_type::INSTANCE_SUBRATING_NEGATIVE, "foreignkey" => $subrating->get_id()]); + $dbonegatives = self::get_strings(localized_string_type::INSTANCE_SUBRATING_NEGATIVE, $subrating->get_id()); foreach ($dbonegatives as $dbonegative) { $negative = db_localized_string::to_localized_string($dbonegative); $subrating->add_negative($negative); } // Load subrating positive texts. - $dbopositives = $DB->get_records(tables::LOCALIZED_STRING_TABLE, - ["type" => localized_string_type::INSTANCE_SUBRATING_POSITIVE, "foreignkey" => $subrating->get_id()]); + $dbopositives = self::get_strings(localized_string_type::INSTANCE_SUBRATING_POSITIVE, $subrating->get_id()); foreach ($dbopositives as $dbopositive) { $positive = db_localized_string::to_localized_string($dbopositive); $subrating->add_positive($positive); } // Load subrating very positive texts. - $dboverypositives = $DB->get_records(tables::LOCALIZED_STRING_TABLE, - ["type" => localized_string_type::INSTANCE_SUBRATING_VERY_POSITIVE, "foreignkey" => $subrating->get_id()]); + $dboverypositives = self::get_strings(localized_string_type::INSTANCE_SUBRATING_VERY_POSITIVE, $subrating->get_id()); foreach ($dboverypositives as $dboverypositive) { $verypositive = db_localized_string::to_localized_string($dboverypositive); $subrating->add_verypositive($verypositive); @@ -162,9 +153,109 @@ public static function get_by_id(int $id) : instance { } $instance->add_category($category); } + + // Add to cache. + $byid[$id] = $instance; return $instance; } + private static function get_strings(string $type, int $subratingid, bool $throwonerror = false): array { + global $DB; + + static $sortedstrings = null; + + if ($sortedstrings === null && !PHPUNIT_TEST) { + $sortedstrings = []; + $rs = $DB->get_recordset(tables::LOCALIZED_STRING_TABLE); + foreach ($rs as $dboheader) { + $dbo_obj = new db_localized_string; + $dbo_obj->id = $dboheader->id; + $dbo_obj->languageid = $dboheader->languageid; + $dbo_obj->string = $dboheader->string; + $dbo_obj->type = $dboheader->type; + $dbo_obj->foreignkey = $dboheader->foreignkey; + + $sortedstrings[$dbo_obj->type][$dbo_obj->foreignkey][$dbo_obj->languageid] = $dbo_obj; + } + $rs->close(); + } + + if (!isset($sortedstrings[$type])) { + if ($throwonerror) { + throw new \coding_exception("Invalid type .$type"); + } + return []; + } + if (!isset($sortedstrings[$type][$subratingid])) { + if ($throwonerror) { + throw new \coding_exception("Invalid subratingid $subratingid for type $type"); + } + return []; + } + + return $sortedstrings[$type][$subratingid]; + } + + /** + * Gets all criteria hashed by category id for a specific instance. + * @param int $id - instance id + * @return array + * @throws \dml_exception + */ + private static function get_criteria_by_category_for_instance_id(int $id): array { + global $DB; + + $crittab = tables::INSTANCE_CRITERION_TABLE; + $cattab = tables::INSTANCE_CATEGORY_TABLE; + + $sql = "SELECT crit.* + FROM {{$crittab}} crit + JOIN {{$cattab}} cat + ON crit.categoryid = cat.id + WHERE cat.instanceid = ?"; + + $bycat = []; + $rs = $DB->get_recordset_sql($sql, [$id]); + foreach ($rs as $dbocriterion) { + if (!isset($bycat[$dbocriterion->categoryid])) { + $bycat[$dbocriterion->categoryid] = []; + } + $bycat[$dbocriterion->categoryid][$dbocriterion->id] = db_instance_criterion::to_instance_criterion($dbocriterion); + } + $rs->close(); + return $bycat; + } + + /** + * Get subratings hashed by criterion id for an instance. + * @param int $id - instance id + * @return array + * @throws \dml_exception + */ + private static function get_subratings_by_criterion_for_instance(int $id): array { + global $DB; + $crittab = tables::INSTANCE_CRITERION_TABLE; + $cattab = tables::INSTANCE_CATEGORY_TABLE; + $srattab = tables::INSTANCE_SUBRATING_TABLE; + $sql = "SELECT srat.* + FROM {{$srattab}} srat + JOIN {{$crittab}} crit + ON srat.criterionid = crit.id + JOIN {{$cattab}} mvic + ON crit.categoryid = mvic.id + WHERE mvic.instanceid = ?"; + $rs = $DB->get_recordset_sql($sql, [$id]); + $bycrit = []; + foreach ($rs as $dbosubrating) { + if (!isset($bycrit[$dbosubrating->criterionid])) { + $bycrit[$dbosubrating->criterionid] = []; + } + $bycrit[$dbosubrating->criterionid][$dbosubrating->id] = db_instance_subrating::to_subrating($dbosubrating); + } + $rs->close(); + return $bycrit; + } + /** * Save the given instance * @param instance $instance The instance object. From c1b5603d4e51f15a0eda77082d5e3ffd90116a5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luca=20B=C3=B6sch?= Date: Mon, 9 Oct 2023 22:29:11 +0200 Subject: [PATCH 2/3] Github Moodle Plugin CI Actions updated. --- .github/workflows/moodle-plugin-ci.yml | 90 +++++++++++--------------- 1 file changed, 38 insertions(+), 52 deletions(-) diff --git a/.github/workflows/moodle-plugin-ci.yml b/.github/workflows/moodle-plugin-ci.yml index bd55c73..2733a1c 100644 --- a/.github/workflows/moodle-plugin-ci.yml +++ b/.github/workflows/moodle-plugin-ci.yml @@ -8,7 +8,7 @@ jobs: services: postgres: - image: postgres:12 + image: postgres:13 env: POSTGRES_USER: 'postgres' POSTGRES_HOST_AUTH_METHOD: 'trust' @@ -16,7 +16,7 @@ jobs: - 5432:5432 options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 3 mariadb: - image: mariadb:10.5 + image: mariadb:10.6 env: MYSQL_USER: 'root' MYSQL_ALLOW_EMPTY_PASSWORD: "true" @@ -28,6 +28,42 @@ jobs: fail-fast: false matrix: include: + - php: 8.2 + moodle-branch: MOODLE_403_STABLE + database: pgsql + - php: 8.2 + moodle-branch: MOODLE_403_STABLE + database: mariadb + - php: 8.1 + moodle-branch: MOODLE_403_STABLE + database: pgsql + - php: 8.1 + moodle-branch: MOODLE_403_STABLE + database: mariadb + - php: 8.0 + moodle-branch: MOODLE_403_STABLE + database: pgsql + - php: 8.0 + moodle-branch: MOODLE_403_STABLE + database: mariadb + - php: 8.2 + moodle-branch: MOODLE_402_STABLE + database: pgsql + - php: 8.2 + moodle-branch: MOODLE_402_STABLE + database: mariadb + - php: 8.1 + moodle-branch: MOODLE_402_STABLE + database: pgsql + - php: 8.1 + moodle-branch: MOODLE_402_STABLE + database: mariadb + - php: 8.0 + moodle-branch: MOODLE_402_STABLE + database: pgsql + - php: 8.0 + moodle-branch: MOODLE_402_STABLE + database: mariadb - php: 8.1 moodle-branch: MOODLE_401_STABLE database: pgsql @@ -58,56 +94,6 @@ jobs: - php: 7.4 moodle-branch: MOODLE_400_STABLE database: mariadb - - php: 7.4 - moodle-branch: MOODLE_311_STABLE - database: pgsql - - php: 7.4 - moodle-branch: MOODLE_311_STABLE - database: mariadb - - php: 7.4 - moodle-branch: MOODLE_310_STABLE - database: pgsql - - php: 7.4 - moodle-branch: MOODLE_310_STABLE - database: mariadb - - php: 7.4 - moodle-branch: MOODLE_39_STABLE - database: pgsql - - php: 7.4 - moodle-branch: MOODLE_39_STABLE - database: mariadb - - - php: 7.3 - moodle-branch: MOODLE_311_STABLE - database: pgsql - - php: 7.3 - moodle-branch: MOODLE_311_STABLE - database: mariadb - - php: 7.3 - moodle-branch: MOODLE_310_STABLE - database: pgsql - - php: 7.3 - moodle-branch: MOODLE_310_STABLE - database: mariadb - - php: 7.3 - moodle-branch: MOODLE_39_STABLE - database: pgsql - - php: 7.3 - moodle-branch: MOODLE_39_STABLE - database: mariadb - - - php: 7.2 - moodle-branch: MOODLE_310_STABLE - database: pgsql - - php: 7.2 - moodle-branch: MOODLE_310_STABLE - database: mariadb - - php: 7.2 - moodle-branch: MOODLE_39_STABLE - database: pgsql - - php: 7.2 - moodle-branch: MOODLE_39_STABLE - database: mariadb steps: - name: Check out repository code From f2bfecc87a8bf4fee93b4ac36f77488461405e44 Mon Sep 17 00:00:00 2001 From: gthomas2 Date: Sat, 28 Oct 2023 10:38:42 +0100 Subject: [PATCH 3/3] BFH-1: Additional performance improvements (on saving mod instance) --- classes/repository/instance_repository.php | 6 +- .../template_category_repository.php | 83 +++++++++++++---- .../template_criterion_repository.php | 89 +++++++++++++++---- 3 files changed, 137 insertions(+), 41 deletions(-) diff --git a/classes/repository/instance_repository.php b/classes/repository/instance_repository.php index c496d34..3374a36 100644 --- a/classes/repository/instance_repository.php +++ b/classes/repository/instance_repository.php @@ -26,8 +26,6 @@ defined('MOODLE_INTERNAL') || die(); -raise_memory_limit(MEMORY_HUGE); - use Exception; use mod_verbalfeedback\model\instance; use mod_verbalfeedback\model\instance_category; @@ -56,7 +54,7 @@ public static function get_by_id(int $id) : instance { // Return cached $byid instance if available and if we are not running a php unit test. static $byid = []; - if (!PHPUNIT_TEST && isset($byid[$id])) { + if (isset($byid[$id]) && !PHPUNIT_TEST) { return $byid[$id]; } @@ -164,7 +162,7 @@ private static function get_strings(string $type, int $subratingid, bool $throwo static $sortedstrings = null; - if ($sortedstrings === null && !PHPUNIT_TEST) { + if ($sortedstrings === null || PHPUNIT_TEST) { $sortedstrings = []; $rs = $DB->get_recordset(tables::LOCALIZED_STRING_TABLE); foreach ($rs as $dboheader) { diff --git a/classes/repository/template_category_repository.php b/classes/repository/template_category_repository.php index 2d5be12..df3bd4b 100644 --- a/classes/repository/template_category_repository.php +++ b/classes/repository/template_category_repository.php @@ -25,6 +25,7 @@ namespace mod_verbalfeedback\repository; use Exception; +use mod_verbalfeedback\model\localized_string; use mod_verbalfeedback\model\template\template_category; use mod_verbalfeedback\repository\model\db_localized_string; use mod_verbalfeedback\repository\model\db_parametrized_criterion; @@ -43,19 +44,31 @@ class template_category_repository { */ public function get_all() : array { global $DB; - $results = []; - $dbcategories = $DB->get_records("verbalfeedback_t_category"); - foreach ($dbcategories as $dbocategory) { + static $results = null; + if ($results !== null && !PHPUNIT_TEST) { + return $results; + } + + $results = []; + $rs = $DB->get_recordset("verbalfeedback_t_category"); + $templatecategories = []; + foreach ($rs as $dbocategory) { $templatecategory = db_template_category::to_template_category($dbocategory); + $templatecategories[$templatecategory->get_id()] = $templatecategory; + } + $rs->close(); - $headers = $this->get_headers($templatecategory->get_id()); - $templatecategory->set_headers($headers); + $headersbycatids = $this->get_headers_by_category_ids(); + $parametrizedcriteriabycatid = $this->get_parameterized_criteria_by_categoryid(); - $parametrizedcriteria = $this->get_parametrized_criteria($templatecategory->get_id()); + foreach ($templatecategories as $catid => $templatecategory) { + $headers = $headersbycatids[$catid]; + $templatecategory->set_headers($headers); + $parametrizedcriteria = $parametrizedcriteriabycatid[$catid]; $templatecategory->set_template_criteria($parametrizedcriteria); - $results[] = $templatecategory; + $results[$catid] = $templatecategory; } return $results; } @@ -66,18 +79,9 @@ public function get_all() : array { * @param int $id The category template id. * @return template_category|null The template categories. */ - public function get_by_id(int $id) : template_category { - global $DB; - $dbo = $DB->get_record('verbalfeedback_t_category', ['id' => $id]); - $templatecategory = db_template_category::to_template_category($dbo); - - $headers = $this->get_headers($templatecategory->get_id()); - $templatecategory->set_headers($headers); - - $parametrizedcriteria = $this->get_parametrized_criteria($templatecategory->get_id()); - $templatecategory->set_template_criteria($parametrizedcriteria); - - return $templatecategory; + public function get_by_id(int $id) : ?template_category { + $all = $this->get_all(); + return $all[$id] ?? null; } /** @@ -168,6 +172,27 @@ private function get_headers($foreignkey) : array { return $headers; } + /** + * Get all category headers hashed by category id. + * + * @return array + * @throws \dml_exception + */ + private function get_headers_by_category_ids() : array { + global $DB; + + $headers = []; + $rs = $DB->get_recordset('verbalfeedback_local_string', ['type' => localized_string_type::TEMPLATE_CATEGORY_HEADER]); + foreach ($rs as $row) { + if (!isset($dboheaders[$row->foreignkey])) { + $headers[$row->foreignkey] = []; + } + $headers[$row->foreignkey][] = new localized_string($row->languageid, $row->id, $row->string); + } + $rs->close(); + return $headers; + } + /** * Gets the parametrized criteria of a category * @@ -185,4 +210,24 @@ private function get_parametrized_criteria($categoryid) : array { } return $parametrizedcriteria; } + + /** + * Get all parameterized criteria hashed by category id + * @return array The paramterized criteria in an array hashed by category id + * @throws \dml_exception + */ + private function get_parameterized_criteria_by_categoryid() : array { + global $DB; + + $critbycatid = []; + $rs = $DB->get_recordset('verbalfeedback_t_param_crit'); + foreach ($rs as $row) { + if (!isset($critbycatid[$row->categoryid])) { + $critbycatid[$row->categoryid] = []; + } + $critbycatid[$row->categoryid][] = db_parametrized_criterion::to_parametrized_criterion($row); + } + $rs->close(); + return $critbycatid; + } } diff --git a/classes/repository/template_criterion_repository.php b/classes/repository/template_criterion_repository.php index fb46670..8411449 100644 --- a/classes/repository/template_criterion_repository.php +++ b/classes/repository/template_criterion_repository.php @@ -25,6 +25,7 @@ namespace mod_verbalfeedback\repository; use Exception; +use mod_verbalfeedback\model\localized_string; use mod_verbalfeedback\model\subrating; use mod_verbalfeedback\model\template\parametrized_template_criterion; use mod_verbalfeedback\model\template\template_criterion; @@ -47,9 +48,9 @@ public function get_all() : array { global $DB; $results = []; - $dbocriteria = $DB->get_records(tables::TEMPLATE_CRITERION_TABLE); + $rs = $DB->get_recordset(tables::TEMPLATE_CRITERION_TABLE); - foreach ($dbocriteria as $dbocriterion) { + foreach ($rs as $dbocriterion) { $templatecriterion = self::dbo_to_template_criterion($dbocriterion); $criteriondescriptions = $this->get_localized_strings($templatecriterion->get_id(), @@ -61,26 +62,46 @@ public function get_all() : array { $results[] = $templatecriterion; } + + $rs->close(); return $results; } + /** + * Get all template criterion hashed by id. + * @return array + * @throws \dml_exception + */ + private function get_all_template_criterion(): array { + global $DB; + $result = []; + $rs = $DB->get_recordset(tables::TEMPLATE_CRITERION_TABLE); + foreach ($rs as $row) { + $templatecriterion = db_template_criterion::to_template_criterion($row); + $criteriondescriptions = $this->get_localized_strings($templatecriterion->get_id(), + localized_string_type::TEMPLATE_CRITERION); + $templatecriterion->set_descriptions($criteriondescriptions); + $subratings = $this->get_subratings_by_criterion_id($templatecriterion->get_id()); + $templatecriterion->set_subratings($subratings); + $result[$row->id] = $templatecriterion; + } + $rs->close(); + return $result; + } + /** * Gets the criteria template for the given id * @param int $id The criteria template id. * @return template_criterion|null The template criterion. */ - public function get_by_id(int $id) : template_criterion { - global $DB; - $dbo = $DB->get_record(tables::TEMPLATE_CRITERION_TABLE, ['id' => $id]); - $templatecriterion = db_template_criterion::to_template_criterion($dbo); + public function get_by_id(int $id) : ?template_criterion { + static $all = null; - $criteriondescriptions = $this->get_localized_strings($templatecriterion->get_id(), - localized_string_type::TEMPLATE_CRITERION); - $templatecriterion->set_descriptions($criteriondescriptions); + if ($all === null || PHPUNIT_TEST) { + $all = $this->get_all_template_criterion(); + } - $subratings = $this->get_subratings_by_criterion_id($templatecriterion->get_id()); - $templatecriterion->set_subratings($subratings); - return $templatecriterion; + return $all[$id] ?? null; } /** @@ -262,6 +283,39 @@ public static function dbo_to_template_criterion($dbo) { return $templatecriteria; } + /** + * Get all localized strings for a type, hashed by foreignkey, cached in memory for speed. + * @TODO - evaluate this for memory usage. + * @param string $type + * @return array + * @throws \dml_exception + */ + private function get_all_localized_strings_for_type(string $type) : array { + global $DB; + + static $strings = []; + + if (isset($strings[$type]) && !PHPUNIT_TEST) { + // We already have this cached, so return it. + return $strings[$type]; + } + + $strings[$type] = []; + + $rs = $DB->get_recordset(tables::LOCALIZED_STRING_TABLE, ['type' => $type]); + foreach ($rs as $row) { + $type = $row->type; + $key = $row->foreignkey; + if (!isset($strings[$type][$key])) { + $strings[$type][$key] = []; + } + $strings[$type][$key][$row->id] = db_localized_string::to_localized_string($row); + } + $rs->close(); + + return $strings[$type]; + } + /** * Get localized strings for a criterion * @@ -271,17 +325,16 @@ public static function dbo_to_template_criterion($dbo) { * @throws \dml_exception */ private function get_localized_strings(int $foreignkey, string $type) : array { - global $DB; if (!localized_string_type::exists($type)) { throw new \Exception("Unknown localized string type."); } - $dbolocalizedstrings = $DB->get_records(tables::LOCALIZED_STRING_TABLE, ['foreignkey' => $foreignkey, - 'type' => $type]); - $localizedstrings = []; - foreach ($dbolocalizedstrings as $dbo) { - $localizedstrings[] = db_localized_string::to_localized_string($dbo); + $allstrings = $this->get_all_localized_strings_for_type($type); + $localizedstrings = $allstrings[$foreignkey] ?? null; + if (!$localizedstrings) { + throw new coding_exception("Couldn't find localized strings by type '$type' and foreignkey '$foreignkey'"); } + return $localizedstrings; }