Skip to content

Commit

Permalink
Merge branch 'MDL-76903-402' of https://github.com/laurentdavid/moodle
Browse files Browse the repository at this point in the history
…into MOODLE_402_STABLE
  • Loading branch information
junpataleta committed May 15, 2023
2 parents 04b35cb + 717c74a commit 9ac38f5
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 7 deletions.
5 changes: 1 addition & 4 deletions mod/book/classes/external.php
Expand Up @@ -94,7 +94,6 @@ public static function view_book($bookid, $chapterid = 0) {

$chapters = book_preload_chapters($book);
$firstchapterid = 0;
$lastchapterid = 0;

foreach ($chapters as $ch) {
if ($ch->hidden) {
Expand All @@ -103,7 +102,6 @@ public static function view_book($bookid, $chapterid = 0) {
if (!$firstchapterid) {
$firstchapterid = $ch->id;
}
$lastchapterid = $ch->id;
}

if (!$chapterid) {
Expand All @@ -129,8 +127,7 @@ public static function view_book($bookid, $chapterid = 0) {
}

// 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();
Expand Down
44 changes: 44 additions & 0 deletions mod/book/classes/helper.php
@@ -0,0 +1,44 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_book;

/**
* Book helper
*
* @package mod_book
* @copyright 2023 Laurent David <laurent.david@moodle.com>
* @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;
}
}
29 changes: 29 additions & 0 deletions mod/book/tests/behat/book_activity_completion.feature
Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions mod/book/tests/helper_test.php
@@ -0,0 +1,48 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_book;
/**
* Helper test class
*
* @package mod_book
* @copyright 2023 Laurent David <laurent.david@moodle.com>
* @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));
}
}
4 changes: 1 addition & 3 deletions mod/book/view.php
Expand Up @@ -114,9 +114,7 @@
}
// 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();

Expand Down

0 comments on commit 9ac38f5

Please sign in to comment.