Skip to content

Commit

Permalink
Merge pull request #40 from ScottVerbeek/upstream-fixes-and-issue-776…
Browse files Browse the repository at this point in the history
…-merge-branch

776 bug in mod book chapter viewed
  • Loading branch information
rhell4 authored Nov 29, 2023
2 parents 0dded03 + cdbc647 commit a9a0302
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
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

0 comments on commit a9a0302

Please sign in to comment.