git2063 sm:MDL-57292-master> git d diff --git a/admin/tool/lpimportcsv/classes/framework_importer.php b/admin/tool/lpimportcsv/classes/framework_importer.php index 796bd97..f0369f1 100644 --- a/admin/tool/lpimportcsv/classes/framework_importer.php +++ b/admin/tool/lpimportcsv/classes/framework_importer.php @@ -269,6 +269,14 @@ class framework_importer { $this->progress = $progress; $this->progress->start_progress('Theoretical max', count($this->flat)); + // Map all parent idnumbers to the index they're stored in. + // This makes searching for children much more efficient. + $this->flatparents = array_map(function($key) { + if (property_exists($key, 'parentidnumber')) { + return $key->parentidnumber; + } + return ''; + }, $this->flat); + // Build a tree from this flat list. $this->add_children($this->framework, ''); @@ -282,11 +291,13 @@ class framework_importer { * @param string $parentidnumber Add this competency to the parent with this idnumber. */ public function add_children(& $node, $parentidnumber) { - foreach ($this->flat as $competency) { - if ($competency->parentidnumber == $parentidnumber) { - $node->children[] = $competency; - $this->add_children($competency, $competency->idnumber); - } + // The flat parents are stored as a mapping of index => parent value. + // We use the search feature of array_keys() to fetch all competencies within that parent. + $parents = array_keys($this->flatparents, $parentidnumber); + foreach ($parents as $flatindex) { + $competency = $this->flat[$flatindex]; + $node->children[] = $competency; + $this->add_children($competency, $competency->idnumber); } }