Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions src/transformer/events/mod_book/chapter_viewed.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,40 @@ function chapter_viewed(array $config, \stdClass $event) {
]
];

if ($chapter->subchapter != '0') {
$parentchapter = $repo->read_record_by_id('book_chapters', $chapter->subchapter);
$statement['context']['contextActivities']['parent'] = [
utils\get_activity\book_chapter($config, $course, $parentchapter, $event->contextinstanceid)
];
// Is parent chapter?
if ($chapter->subchapter == '0') {
return [$statement];
}

$parentchapters = $repo->read_records('book_chapters', [
'bookid' => $chapter->bookid,
'subchapter' => 0,
]);

// Could not find parent chapter?
if (empty($parentchapters)) {
return [$statement];
}

// Sort the parentchapters by pagenumber ids.
usort($parentchapters, function ($a, $b) {
return $a->pagenum - $b->pagenum;
});

// As a default, the first element in the array becomes parent chapter.
$parentchapter = reset($parentchapters);

foreach ($parentchapters as $posparent) {
// Stop the loop when page number surpasses the subchapter.
if ($posparent->pagenum > $chapter->pagenum) {
break;
}
$parentchapter = $posparent;
}

$statement['context']['contextActivities']['parent'] = [
utils\get_activity\book_chapter($config, $course, $parentchapter, $event->contextinstanceid)
];

return [$statement];
}
13 changes: 11 additions & 2 deletions src/transformer/repos/TestRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,20 @@ public function read_records(string $type, array $query) {
$matchingrecords = [];

foreach ($records as $record) {
$conditionfailed = false;

foreach ($query as $key => $value) {
if ($record->$key === $value) {
$matchingrecords[] = (object) $record;
if ($record->$key !== $value) {
$conditionfailed = true;
break;
}
}

if ($conditionfailed) {
continue;
}

$matchingrecords[] = (object) $record;
}

return $matchingrecords;
Expand Down