diff --git a/mod/book/locallib.php b/mod/book/locallib.php index 3005c70798b..545a1d962d0 100644 --- a/mod/book/locallib.php +++ b/mod/book/locallib.php @@ -168,7 +168,7 @@ function book_get_chapter_title($chid, $chapters, $book, $context) { * @param stdClass $chapter The current chapter * @param stdClass $book The book * @param stdClass $cm The course module - * @param bool $edit Whether the user is editing + * @param bool|null $edit Whether the user is editing */ function book_add_fake_block($chapters, $chapter, $book, $cm, $edit = null) { global $PAGE, $USER; @@ -185,7 +185,7 @@ function book_add_fake_block($chapters, $chapter, $book, $cm, $edit = null) { } } - $toc = book_get_toc($chapters, $chapter, $book, $cm, $edit, 0); + $toc = book_get_toc($chapters, $chapter, $book, $cm, $edit); $bc = new block_contents(); $bc->title = get_string('toc', 'mod_book'); @@ -215,6 +215,7 @@ function book_get_toc($chapters, $chapter, $book, $cm, $edit) { $first = 1; $context = context_module::instance($cm->id); + $viewhidden = has_capability('mod/book:viewhiddenchapters', $context); switch ($book->numbering) { case BOOK_NUM_NONE: @@ -231,7 +232,7 @@ function book_get_toc($chapters, $chapter, $book, $cm, $edit) { break; } - if ($edit) { // Teacher's TOC + if ($edit) { // Editing on (Teacher's TOC). $toc .= html_writer::start_tag('ul'); $i = 0; foreach ($chapters as $ch) { @@ -351,12 +352,12 @@ function book_get_toc($chapters, $chapter, $book, $cm, $edit) { $toc .= html_writer::end_tag('li'); $toc .= html_writer::end_tag('ul'); - } else { // Normal students view + } else { // Editing off. Normal students, teachers view. $toc .= html_writer::start_tag('ul'); foreach ($chapters as $ch) { $title = trim(format_string($ch->title, true, array('context'=>$context))); $titleunescaped = trim(format_string($ch->title, true, array('context' => $context, 'escape' => false))); - if (!$ch->hidden) { + if (!$ch->hidden || ($ch->hidden && $viewhidden)) { if (!$ch->subchapter) { $nch++; $ns = 0; @@ -387,12 +388,15 @@ function book_get_toc($chapters, $chapter, $book, $cm, $edit) { $title = "$nch.$ns. $title"; } } + + $cssclass = ($ch->hidden && $viewhidden) ? 'dimmed_text' : ''; + if ($ch->id == $chapter->id) { - $toc .= html_writer::tag('strong', $title); + $toc .= html_writer::tag('strong', $title, array('class' => $cssclass)); } else { $toc .= html_writer::link(new moodle_url('view.php', array('id' => $cm->id, 'chapterid' => $ch->id)), - $title, array('title' => s($titleunescaped))); + $title, array('title' => s($titleunescaped), 'class' => $cssclass)); } if (!$ch->subchapter) { diff --git a/mod/book/tests/behat/show_hide_chapters.feature b/mod/book/tests/behat/show_hide_chapters.feature index c84ab8feda1..f235bccc319 100644 --- a/mod/book/tests/behat/show_hide_chapters.feature +++ b/mod/book/tests/behat/show_hide_chapters.feature @@ -11,9 +11,11 @@ Feature: Book activity chapter visibility management And the following "users" exist: | username | firstname | lastname | email | | teacher1 | Teacher | 1 | teacher1@example.com | + | student1 | Student | 2 | student1@example.com | And the following "course enrolments" exist: | user | course | role | | teacher1 | C1 | editingteacher | + | student1 | C1 | student | And I log in as "teacher1" And I am on "Course 1" course homepage with editing mode on And I add a "Book" to section "1" and I fill the form with: @@ -54,8 +56,8 @@ Feature: Book activity chapter visibility management And I turn editing mode off And I am on "Course 1" course homepage And I follow "Test book" - Then I should not see "Second chapter" in the "Table of contents" "block" - And I should not see "Third chapter" in the "Table of contents" "block" + Then the "class" attribute of "a[title='Second chapter']" "css_element" should contain "dimmed_text" + And the "class" attribute of "a[title='Third chapter']" "css_element" should contain "dimmed_text" And I follow "Next" And I should see "Fourth chapter" in the ".book_content" "css_element" And I follow "Exit book" @@ -72,3 +74,9 @@ Feature: Book activity chapter visibility management And I follow "Next" And I should see "Fourth chapter" in the ".book_content" "css_element" And I follow "Exit book" + And I log out + And I log in as "student1" + And I am on "Course 1" course homepage + And I follow "Test book" + And I should not see "Second chapter" in the "Table of contents" "block" + And I should not see "Third chapter" in the "Table of contents" "block"