From 86f293fb27afb485492b1c1f389f2fe829f10330 Mon Sep 17 00:00:00 2001 From: Rajneel Totaram Date: Thu, 10 Jul 2025 12:28:48 +1200 Subject: [PATCH] MDL-66579 mod_book: Make chapter numbering consistent --- mod/book/locallib.php | 36 +++++++--- .../tests/behat/chapter_numbering.feature | 66 +++++++++++++++++++ 2 files changed, 93 insertions(+), 9 deletions(-) create mode 100644 mod/book/tests/behat/chapter_numbering.feature diff --git a/mod/book/locallib.php b/mod/book/locallib.php index 1627c3e16e6..ec1867720df 100644 --- a/mod/book/locallib.php +++ b/mod/book/locallib.php @@ -352,9 +352,6 @@ function book_get_toc($chapters, $chapter, $book, $cm, $edit) { $titleunescaped = trim(format_string($ch->title, true, array('context' => $context, 'escape' => false))); if (!$ch->hidden || ($ch->hidden && $viewhidden)) { if (!$ch->subchapter) { - $nch++; - $ns = 0; - if ($first) { $toc .= html_writer::start_tag('li'); } else { @@ -363,12 +360,20 @@ function book_get_toc($chapters, $chapter, $book, $cm, $edit) { $toc .= html_writer::start_tag('li'); } - if ($book->numbering == BOOK_NUM_NUMBERS) { - $title = "$nch. $title"; + // Don't show numbering for hidden chapters, so that numbering is consistent with what students see + // and the edit mode. + if (!$ch->hidden) { + $nch++; + $ns = 0; + if ($book->numbering == BOOK_NUM_NUMBERS) { + $title = "$nch. $title"; + } + } else { + if ($book->numbering == BOOK_NUM_NUMBERS) { + $title = "x. $title"; + } } } else { - $ns++; - if ($first) { $toc .= html_writer::start_tag('li'); $toc .= html_writer::start_tag('ul'); @@ -377,8 +382,21 @@ function book_get_toc($chapters, $chapter, $book, $cm, $edit) { $toc .= html_writer::start_tag('li'); } - if ($book->numbering == BOOK_NUM_NUMBERS) { - $title = "$nch.$ns. $title"; + // Don't show numbering for hidden subchapters, so that numbering is consistent with what students see + // and the edit mode. + if (!$ch->hidden) { + $ns++; + if ($book->numbering == BOOK_NUM_NUMBERS) { + $title = "$nch.$ns. $title"; + } + } else { + if ($book->numbering == BOOK_NUM_NUMBERS) { + if (empty($chapters[$ch->parent]->hidden)) { + $title = "$nch.x. $title"; + } else { + $title = "x.x. $title"; + } + } } } diff --git a/mod/book/tests/behat/chapter_numbering.feature b/mod/book/tests/behat/chapter_numbering.feature new file mode 100644 index 00000000000..48261474e7b --- /dev/null +++ b/mod/book/tests/behat/chapter_numbering.feature @@ -0,0 +1,66 @@ +@mod @mod_book +Feature: Book chapter numbering should be consistent for users + In order to correctly refer to book chapters + As a teacher or student + I should be able to see the same chapter numbering as other users + + Background: + Given the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + And the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | Teacher | 1 | teacher1@example.com | + | student1 | Student | One | student1@example.com | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | editingteacher | + | student1 | C1 | student | + And the following "activity" exists: + | course | C1 | + | activity | book | + | name | Test book | + And the following "mod_book > chapters" exist: + | book | title | content | pagenum |subchapter | hidden | + | Test book | Intro | Intro chapter | 1 | 0 | 0 | + | Test book | First chapter | First chapter | 2 | 0 | 0 | + | Test book | Sub chapter A | Sub chapter A | 3 | 1 | 0 | + | Test book | Sub chapter B | Sub chapter B | 4 | 1 | 1 | + | Test book | Sub chapter C | Sub chapter C | 5 | 1 | 0 | + | Test book | Second chapter | Second chapter | 6 | 0 | 1 | + | Test book | Sub chapter D | Sub chapter C | 7 | 1 | 1 | + | Test book | Third chapter | Third chapter | 8 | 0 | 0 | + + Scenario Outline: Chapter numbering for teachers is consistent in editing and view mode + Given I am on the "Course 1" course page logged in as teacher1 + And I turn editing mode + And I am on the "Test book" "book activity" page + # Check chapter numbering + When I follow "2.1. Sub chapter A" + Then I should see "2.1. Sub chapter A" in the ".book_content" "css_element" + And I follow "2.x. Sub chapter B" + And I should see "2.x. Sub chapter B" in the ".book_content" "css_element" + And I follow "2.2. Sub chapter C" + And I should see "2.2. Sub chapter C" in the ".book_content" "css_element" + And I follow "x. Second chapter" + And I should see "x. Second chapter" in the ".book_content" "css_element" + And I follow "x.x. Sub chapter D" + And I should see "x.x. Sub chapter D" in the ".book_content" "css_element" + And I follow "3. Third chapter" + And I should see "3. Third chapter" in the ".book_content" "css_element" + + Examples: + | editmode | + | on | + | off | + + Scenario: Chapter numbering for students is consistent with what teachers see + Given I am on the "Course 1" course page logged in as student1 + And I am on the "Test book" "book activity" page + # Check chapter numbering + When I follow "2.1. Sub chapter A" + Then I should see "2.1. Sub chapter A" in the ".book_content" "css_element" + And I follow "2.2. Sub chapter C" + And I should see "2.2. Sub chapter C" in the ".book_content" "css_element" + And I follow "3. Third chapter" + And I should see "3. Third chapter" in the ".book_content" "css_element"