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 Oct 28, 2023
1 parent 8473240 commit 2868a3f
Showing 1 changed file with 53 additions and 6 deletions.
59 changes: 53 additions & 6 deletions classes/repository/template_category_repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,21 @@ class template_category_repository {
public function get_all() : array {
global $DB;
$results = [];
$dbcategories = $DB->get_records("verbalfeedback_t_category");

foreach ($dbcategories as $dbocategory) {
$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;
Expand Down Expand Up @@ -168,6 +174,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;

$dboheaders = [];
$rs = $DB->get_recordset('verbalfeedback_local_string', ['type' => localized_string_type::TEMPLATE_CATEGORY_HEADER]);
foreach ($rs as $row) {
if (!isset($dboheaders[$row->foreignkey])) {
$dboheaders[$row->foreignkey] = [];
}
$dboheaders[$row->foreignkey][] = $row;
}
$rs->close();
return $dboheaders;
}

/**
* Gets the parametrized criteria of a category
*
Expand All @@ -185,4 +212,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_records('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;
}
}

0 comments on commit 2868a3f

Please sign in to comment.