MDL-73355 mod_book: Update the zero state view

Updates the zero state view (when there is no content yet) in
the book activity and also fixes the highlighting of the active
item in the secondary navigation menu in the 'Add new chapter'
page.
This commit is contained in:
Mihail Geshoski 2022-02-08 00:13:13 +08:00
parent cb802c269d
commit cbf74374c4
4 changed files with 73 additions and 65 deletions

View File

@ -132,6 +132,7 @@ if ($mform->is_cancelled()) {
// Otherwise fill and print the form.
$PAGE->set_title($book->name);
$PAGE->set_heading($course->fullname);
$PAGE->set_secondary_active_tab('modulepage');
if ($chapters = book_preload_chapters($book)) {
book_add_fake_block($chapters, $chapter, $book, $cm);

View File

@ -8,12 +8,14 @@ Feature: In a book, create chapters and sub chapters
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
And the following "courses" exist:
| fullname | shortname | format |
| Course 1 | C1 | topics |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
And the following "activities" exist:
| activity | name | intro | course | section |
| book | Test book | A book about dreams! | C1 | 1 |
@ -27,6 +29,7 @@ Feature: In a book, create chapters and sub chapters
| Chapter title | Dummy first chapter |
| Content | Dream is the start of a journey |
And I press "Save changes"
And I should not see "No content has been added to this book yet."
And I should see "1. Dummy first chapter" in the "Table of contents" "block"
And I click on "Add new chapter after \"Dummy first chapter\"" "link" in the "Table of contents" "block"
And I should see "Dummy first chapter"
@ -78,3 +81,8 @@ Feature: In a book, create chapters and sub chapters
And "Delete chapter \"1. Dummy first chapter\"" "link" should not exist in the "Table of contents" "block"
And "Hide chapter \"1. Dummy first chapter\"" "link" should not exist in the "Table of contents" "block"
And "Add new chapter after \"Dummy first chapter\"" "link" should not exist in the "Table of contents" "block"
Scenario: When chapters are not created yet, students can see a notification in the book activity
Given I am on the "Test book" "book activity" page logged in as student1
Then I should see "No content has been added to this book yet." in the ".alert-info" "css_element"
And I should not see "Table of contents"

View File

@ -8,12 +8,14 @@ Feature: Display the book description in the book and optionally in the course
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
And the following "courses" exist:
| fullname | shortname | format |
| Course 1 | C1 | topics |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
And the following "activities" exist:
| activity | name | intro | course | idnumber | section |
| book | Test book | A book about dreams! | C1 | book1 | 1 |
@ -52,3 +54,13 @@ Feature: Display the book description in the book and optionally in the course
And I press "Save and return to course"
When I am on "Course 1" course homepage
Then I should not see "A book about dreams!"
@javascript
Scenario: Description is displayed in the book for students when there are no chapters added yet
Given I am on "Course 1" course homepage with editing mode on
And I am on the "Test book" "book activity" page
And I click on "Delete chapter \"1. Dummy first chapter\"" "link" in the "Table of contents" "block"
And I click on "Yes" "button" in the "Confirmation" "dialogue"
And I log out
And I am on the "Test book" "book activity" page logged in as student1
Then I should see "A book about dreams!"

View File

@ -91,78 +91,65 @@ if ($chapterid == '0') { // Go to first chapter if no given.
}
}
$courseurl = new moodle_url('/course/view.php', array('id' => $course->id));
// No content in the book.
if (!$chapterid) {
$PAGE->set_url('/mod/book/view.php', array('id' => $id));
notice(get_string('nocontent', 'mod_book'), $courseurl->out(false));
// Prepare header.
$pagetitle = $book->name;
if ($chapter = $DB->get_record('book_chapters', ['id' => $chapterid, 'bookid' => $book->id])) {
$pagetitle .= ": {$chapter->title}";
}
// Chapter doesnt exist or it is hidden for students
if ((!$chapter = $DB->get_record('book_chapters', array('id' => $chapterid, 'bookid' => $book->id))) or ($chapter->hidden and !$viewhidden)) {
print_error('errorchapter', 'mod_book', $courseurl);
}
$PAGE->set_url('/mod/book/view.php', array('id'=>$id, 'chapterid'=>$chapterid));
// Unset all page parameters.
unset($id);
unset($bid);
unset($chapterid);
// Read standard strings.
$strbooks = get_string('modulenameplural', 'mod_book');
$strbook = get_string('modulename', 'mod_book');
$strtoc = get_string('toc', 'mod_book');
// prepare header
$pagetitle = $book->name . ": " . $chapter->title;
$PAGE->set_title($pagetitle);
$PAGE->set_heading($course->fullname);
$PAGE->add_body_class('limitedwidth');
book_add_fake_block($chapters, $chapter, $book, $cm, $edit);
$renderer = $PAGE->get_renderer('mod_book');
$actionmenu = new \mod_book\output\main_action_menu($cm->id, $chapters, $chapter, $book);
$renderedmenu = $renderer->render($actionmenu);
// 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 display HTML code
// =====================================================
echo $OUTPUT->header();
echo $renderedmenu;
// The chapter itself.
$hidden = $chapter->hidden ? ' dimmed_text' : null;
echo $OUTPUT->box_start('generalbox book_content' . $hidden);
if (!$book->customtitles) {
if (!$chapter->subchapter) {
$currtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
echo $OUTPUT->heading($currtitle, 3);
} else {
$currtitle = book_get_chapter_title($chapters[$chapter->id]->parent, $chapters, $book, $context);
$currsubtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
echo $OUTPUT->heading($currtitle, 3);
echo $OUTPUT->heading($currsubtitle, 4);
// No content in the book.
if (!$chapterid) {
$PAGE->set_url('/mod/book/view.php', array('id' => $id));
echo $OUTPUT->header();
echo $OUTPUT->notification(get_string('nocontent', 'mod_book'), 'info', false);
} else {
$PAGE->set_url('/mod/book/view.php', ['id' => $id, 'chapterid' => $chapterid]);
// The chapter doesnt exist or it is hidden for students.
if (!$chapter or ($chapter->hidden and !$viewhidden)) {
$courseurl = new moodle_url('/course/view.php', ['id' => $course->id]);
throw new moodle_exception('errorchapter', 'mod_book', $courseurl);
}
}
$chaptertext = file_rewrite_pluginfile_urls($chapter->content, 'pluginfile.php', $context->id, 'mod_book', 'chapter', $chapter->id);
echo format_text($chaptertext, $chapter->contentformat, array('noclean'=>true, 'overflowdiv'=>true, 'context'=>$context));
// 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);
echo $OUTPUT->box_end();
echo $OUTPUT->header();
if (core_tag_tag::is_enabled('mod_book', 'book_chapters')) {
echo $OUTPUT->tag_list(core_tag_tag::get_item_tags('mod_book', 'book_chapters', $chapter->id), null, 'book-tags');
$renderer = $PAGE->get_renderer('mod_book');
$actionmenu = new \mod_book\output\main_action_menu($cm->id, $chapters, $chapter, $book);
$renderedmenu = $renderer->render($actionmenu);
echo $renderedmenu;
// The chapter itself.
$hidden = $chapter->hidden ? ' dimmed_text' : null;
echo $OUTPUT->box_start('generalbox book_content' . $hidden);
if (!$book->customtitles) {
if (!$chapter->subchapter) {
$currtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
echo $OUTPUT->heading($currtitle, 3);
} else {
$currtitle = book_get_chapter_title($chapters[$chapter->id]->parent, $chapters, $book, $context);
$currsubtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
echo $OUTPUT->heading($currtitle, 3);
echo $OUTPUT->heading($currsubtitle, 4);
}
}
$chaptertext = file_rewrite_pluginfile_urls($chapter->content, 'pluginfile.php', $context->id, 'mod_book',
'chapter', $chapter->id);
echo format_text($chaptertext, $chapter->contentformat, ['noclean' => true, 'overflowdiv' => true,
'context' => $context]);
echo $OUTPUT->box_end();
if (core_tag_tag::is_enabled('mod_book', 'book_chapters')) {
echo $OUTPUT->tag_list(core_tag_tag::get_item_tags('mod_book', 'book_chapters', $chapter->id), null, 'book-tags');
}
echo $renderedmenu;
}
echo $renderedmenu;
echo $OUTPUT->footer();