diff options
| author | Marcus Eibrink-Lunzenauer <lunzenauer@elan-ev.de> | 2022-01-21 10:10:40 +0000 |
|---|---|---|
| committer | Marcus Eibrink-Lunzenauer <lunzenauer@elan-ev.de> | 2022-01-21 10:10:40 +0000 |
| commit | b6389b549adefbdbd5441e39b6514e7ed1f0e566 (patch) | |
| tree | 6f0cf3df05b357d9522d67a973678cf133c1e7c0 /lib/models/Courseware/Instance.php | |
| parent | 27d90b64023cea588932282b1290325d14c3d26d (diff) | |
Courseware-Fortschrittseite beschleunigen (SORMless edition)
Diffstat (limited to 'lib/models/Courseware/Instance.php')
| -rwxr-xr-x | lib/models/Courseware/Instance.php | 35 |
1 files changed, 28 insertions, 7 deletions
diff --git a/lib/models/Courseware/Instance.php b/lib/models/Courseware/Instance.php index cc9189c..c823a4e 100755 --- a/lib/models/Courseware/Instance.php +++ b/lib/models/Courseware/Instance.php @@ -37,12 +37,19 @@ class Instance $range->getConfiguration()->delete('COURSEWARE_SEQUENTIAL_PROGRESSION'); $range->getConfiguration()->delete('COURSEWARE_EDITING_PERMISSION'); - $last_element_configs = \ConfigValue::findBySQL('field = ? AND value LIKE ?', ['COURSEWARE_LAST_ELEMENT', '%'.$range->getRangeId().'%']); + $last_element_configs = \ConfigValue::findBySQL('field = ? AND value LIKE ?', [ + 'COURSEWARE_LAST_ELEMENT', + '%' . $range->getRangeId() . '%', + ]); foreach ($last_element_configs as $config) { $arr = json_decode($config->value, true); - $arr = array_filter($arr, function ($key) use ($range) { - return $key !== $range->id; - }, ARRAY_FILTER_USE_KEY); + $arr = array_filter( + $arr, + function ($key) use ($range) { + return $key !== $range->id; + }, + ARRAY_FILTER_USE_KEY + ); \UserConfig::get($config->range_id)->unsetValue('COURSEWARE_LAST_ELEMENT'); \UserConfig::get($config->range_id)->store('COURSEWARE_LAST_ELEMENT', $arr); } @@ -270,8 +277,23 @@ class Instance return $data; } - public function findAllBlocksGroupedByStructuralElementId(): iterable + /** + * Find all blocks of this instance and group them by their structural element's ID. + * You may specify your own `$formatter` instead of the default one which stores the blocks as instances of \Courseware\Block. + * + * @param ?callable(array $row): mixed $formatter Provide your own callable if you need something else instead of + * full-blown instances of \Courseware\Block. + * @return iterable all the (optionally formatted) blocks grouped by the IDs of the structural element containing + * that block. + */ + public function findAllBlocksGroupedByStructuralElementId(callable $formatter = null): iterable { + if (!$formatter) { + $formatter = function ($row) { + return \Courseware\Block::build($row, false); + }; + } + $sql = 'SELECT se.id AS structural_element_id, b.* FROM cw_structural_elements se JOIN cw_containers c ON se.id = c.structural_element_id @@ -286,11 +308,10 @@ class Instance $structuralElementId = $row['structural_element_id']; unset($row['structural_element_id']); - $block = \Courseware\Block::build($row, false); if (!isset($data[$structuralElementId])) { $data[$structuralElementId] = []; } - $data[$structuralElementId][] = $block; + $data[$structuralElementId][] = $formatter($row); } return $data; |
