diff --git a/mod/book/classes/external.php b/mod/book/classes/external.php index ee22e0ed42a..c1740dc6f5b 100644 --- a/mod/book/classes/external.php +++ b/mod/book/classes/external.php @@ -94,7 +94,6 @@ class mod_book_external extends external_api { $chapters = book_preload_chapters($book); $firstchapterid = 0; - $lastchapterid = 0; foreach ($chapters as $ch) { if ($ch->hidden) { @@ -103,7 +102,6 @@ class mod_book_external extends external_api { if (!$firstchapterid) { $firstchapterid = $ch->id; } - $lastchapterid = $ch->id; } if (!$chapterid) { @@ -129,8 +127,7 @@ class mod_book_external extends external_api { } // Trigger the chapter viewed event. - $islastchapter = ($chapter->id == $lastchapterid) ? true : false; - book_view($book, $chapter, $islastchapter, $course, $cm, $context); + book_view($book, $chapter, \mod_book\helper::is_last_visible_chapter($chapterid, $chapters), $course, $cm, $context); } $result = array(); diff --git a/mod/book/classes/helper.php b/mod/book/classes/helper.php new file mode 100644 index 00000000000..b5192513f10 --- /dev/null +++ b/mod/book/classes/helper.php @@ -0,0 +1,44 @@ +. +namespace mod_book; + +/** + * Book helper + * + * @package mod_book + * @copyright 2023 Laurent David + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class helper { + /** + * Check if we are on the last visible chapter of the book. + * + * @param int $chapterid + * @param array $chapters chapter list provided by book_preload_chapters + * @see book_preload_chapters + * @return bool + */ + public static function is_last_visible_chapter(int $chapterid, array $chapters): bool { + $lastchapterid = 0; + foreach ($chapters as $ch) { + if ($ch->hidden) { + continue; + } + $lastchapterid = $ch->id; + } + return $chapterid == $lastchapterid; + } +} diff --git a/mod/book/tests/behat/book_activity_completion.feature b/mod/book/tests/behat/book_activity_completion.feature index 8722ab480cd..01c521aa9da 100644 --- a/mod/book/tests/behat/book_activity_completion.feature +++ b/mod/book/tests/behat/book_activity_completion.feature @@ -35,6 +35,35 @@ Feature: View activity completion information in the book activity When I am on the "Music history" "book activity" page logged in as student1 Then the "View" completion condition of "Music history" is displayed as "done" + Scenario: View automatic completion items with last section hidden + Given the following "activity" exists: + | activity | book | + | course | C1 | + | idnumber | arth1 | + | name | Art history | + | completion | 2 | + | completionview | 1 | + And the following "mod_book > chapters" exist: + | book | title | content | pagenum | subchapter | hidden | + | Art history | First chapter | First chapter | 1 | 0 | 0 | + | Art history | Second chapter | Second chapter | 2 | 0 | 0 | + | Art history | Sub chapter 1 | Sub chapter | 3 | 1 | 0 | + | Art history | Sub chapter 2 | Sub chapter | 4 | 1 | 0 | + | Art history | Sub chapter 3 | Sub chapter | 5 | 1 | 1 | + When I am on the "Art history" "book activity" page logged in as student1 + And I should see "First chapter" + And the "View" completion condition of "Art history" is displayed as "todo" + And I follow "Next" + And I should see "Second chapter" + And the "View" completion condition of "Art history" is displayed as "todo" + And I follow "Next" + And I should see "Sub chapter 1" + And the "View" completion condition of "Art history" is displayed as "todo" + And I follow "Next" + And I should see "Sub chapter 2" + And I should not see "Next" + Then the "View" completion condition of "Art history" is displayed as "done" + @javascript Scenario: Use manual completion Given I am on the "Music history" "book activity editing" page logged in as teacher1 diff --git a/mod/book/tests/helper_test.php b/mod/book/tests/helper_test.php new file mode 100644 index 00000000000..ac392dfef84 --- /dev/null +++ b/mod/book/tests/helper_test.php @@ -0,0 +1,48 @@ +. +namespace mod_book; +/** + * Helper test class + * + * @package mod_book + * @copyright 2023 Laurent David + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class helper_test extends \advanced_testcase { + + /** + * Test view_book + * @covers \mod_book\helper::is_last_visible_chapter + */ + public function test_is_last_chapter() { + $this->resetAfterTest(true); + + $this->setAdminUser(); + // Setup test data. + $course = $this->getDataGenerator()->create_course(); + $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id)); + $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book'); + $firstchapter = + $bookgenerator->create_chapter(array('bookid' => $book->id, 'pagenum' => 1)); // Create a first chapter to check that + // viewing the last chapter is enough for completing the activity. + $chapterhidden = $bookgenerator->create_chapter(array('bookid' => $book->id, 'hidden' => 1, 'pagenum' => 2)); + $lastchapter = $bookgenerator->create_chapter(array('bookid' => $book->id, 'pagenum' => 3)); + $chapters = book_preload_chapters($book); + $this->assertFalse(helper::is_last_visible_chapter($firstchapter->id, $chapters)); + $this->assertFalse(helper::is_last_visible_chapter($chapterhidden->id, $chapters)); + $this->assertTrue(helper::is_last_visible_chapter($lastchapter->id, $chapters)); + } +} diff --git a/mod/book/view.php b/mod/book/view.php index 872031e578b..25fb5ca0139 100644 --- a/mod/book/view.php +++ b/mod/book/view.php @@ -114,9 +114,7 @@ if (!$chapterid) { } // Add the Book TOC block. book_add_fake_block($chapters, $chapter, $book, $cm, $edit); - // We need to discover if this is the last chapter to mark activity as completed. - $islastchapter = $chapter->pagenum + 1 > count($chapters); - book_view($book, $chapter, $islastchapter, $course, $cm, $context); + book_view($book, $chapter, \mod_book\helper::is_last_visible_chapter($chapter->id, $chapters), $course, $cm, $context); echo $OUTPUT->header();