mirror of
https://github.com/moodle/moodle.git
synced 2025-03-20 07:30:01 +01:00
MDL-76903 mod_book: Fix activity completion
* Check only visibles pages to set completion status. * Make sure we have consistent completion value in API and on the view page.
This commit is contained in:
parent
176a323b4e
commit
20b4efad5c
@ -91,7 +91,6 @@ class mod_book_external extends external_api {
|
||||
|
||||
$chapters = book_preload_chapters($book);
|
||||
$firstchapterid = 0;
|
||||
$lastchapterid = 0;
|
||||
|
||||
foreach ($chapters as $ch) {
|
||||
if ($ch->hidden) {
|
||||
@ -100,7 +99,6 @@ class mod_book_external extends external_api {
|
||||
if (!$firstchapterid) {
|
||||
$firstchapterid = $ch->id;
|
||||
}
|
||||
$lastchapterid = $ch->id;
|
||||
}
|
||||
|
||||
if (!$chapterid) {
|
||||
@ -126,8 +124,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();
|
||||
|
44
mod/book/classes/helper.php
Normal file
44
mod/book/classes/helper.php
Normal file
@ -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;
|
||||
}
|
||||
}
|
@ -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
|
||||
|
48
mod/book/tests/helper_test.php
Normal file
48
mod/book/tests/helper_test.php
Normal file
@ -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));
|
||||
}
|
||||
}
|
@ -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();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user