Skip to content

Commit

Permalink
BFH-1: Additional performance improvements (on saving mod instance)
Browse files Browse the repository at this point in the history
  • Loading branch information
gthomas2 committed Nov 2, 2023
1 parent 8473240 commit 65029e2
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 39 deletions.
2 changes: 0 additions & 2 deletions classes/repository/instance_repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
83 changes: 64 additions & 19 deletions classes/repository/template_category_repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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
*
Expand All @@ -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;
}
}
85 changes: 67 additions & 18 deletions classes/repository/template_criterion_repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(),
Expand All @@ -61,26 +62,41 @@ public function get_all() : array {

$results[] = $templatecriterion;
}

$rs->close();
return $results;
}

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;
}

/**
Expand Down Expand Up @@ -262,6 +278,40 @@ 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<localized_string[]>
* @throws \dml_exception
*/
private function get_all_localized_strings_for_type(string $type) : array {
global $DB;

static $strings = null;

if (!PHPUNIT_TEST && ($strings[$type] ?? null)) {
// We already have this cached, so return it.
return $strings[$type];
}

$strings = $strings ?? [];
$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
*
Expand All @@ -271,17 +321,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;
}

Expand Down

0 comments on commit 65029e2

Please sign in to comment.