From 6bcea231240c97bd87928a7995943bae055033cc Mon Sep 17 00:00:00 2001 From: Amaia Anabitarte Date: Tue, 3 Mar 2026 18:09:43 +0100 Subject: [PATCH] MDL-87571 course: Previous of first activity --- .../route/controller/course_navigation.php | 6 +- .../controller/course_navigation_test.php | 65 ++++++++++++++++++- 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/public/course/classes/route/controller/course_navigation.php b/public/course/classes/route/controller/course_navigation.php index 606108f13c3..a835a3e5f62 100644 --- a/public/course/classes/route/controller/course_navigation.php +++ b/public/course/classes/route/controller/course_navigation.php @@ -129,7 +129,7 @@ class course_navigation { return $this->redirect($response, $prevcm->get_url()); } } - return $this->page_not_found($request, $response); + return $this->redirect_to_course($response, $cm->get_course()->id); } /** @@ -201,6 +201,10 @@ class course_navigation { string $direction = 'next', ): ?ResponseInterface { if ($direction === 'previous') { + if ($currentsection->sectionnum == 0) { + // Going to previous on the first section. + return $this->redirect_to_course($response, $modinfo->get_course()->id); + } $section = $modinfo->get_section_info($currentsection->sectionnum); } else { $section = $modinfo->get_section_info($currentsection->sectionnum + 1); diff --git a/public/course/tests/route/controller/course_navigation_test.php b/public/course/tests/route/controller/course_navigation_test.php index e805baa9262..fa86c369049 100644 --- a/public/course/tests/route/controller/course_navigation_test.php +++ b/public/course/tests/route/controller/course_navigation_test.php @@ -396,6 +396,48 @@ final class course_navigation_test extends route_testcase { 'id' => 'cm1', // Students cannot see stealth modules in the course page. ], ]; + yield 'Hidden first module (teacher)' => [ + 'cmsdef' => [ + ['name' => 'cm1', 'options' => ['visible' => false]], + ['name' => 'cm2'], + ], + 'current' => 'cm2', + 'expected' => [ + 'id' => 'cm1', // Students cannot see stealth modules in the course page. + ], + 'role' => 'teacher', + ]; + yield 'Hidden first module (student)' => [ + 'cmsdef' => [ + ['name' => 'cm1', 'options' => ['visible' => false]], + ['name' => 'cm2'], + ], + 'current' => 'cm2', + 'expected' => [ + 'type' => 'course', // Students cannot see hidden modules. + ], + ]; + yield 'Stealth first module (teacher)' => [ + 'cmsdef' => [ + ['name' => 'cm1', 'options' => ['visibleoncoursepage' => false]], + ['name' => 'cm2'], + ], + 'current' => 'cm2', + 'expected' => [ + 'id' => 'cm1', // Students cannot see stealth modules in the course page. + ], + 'role' => 'teacher', + ]; + yield 'Stealth first module (student)' => [ + 'cmsdef' => [ + ['name' => 'cm1', 'options' => ['visibleoncoursepage' => false]], + ['name' => 'cm2'], + ], + 'current' => 'cm2', + 'expected' => [ + 'type' => 'course', // Students cannot see stealth modules in the course page. + ], + ]; yield 'First activity of a course (student)' => [ 'cmsdef' => [ ['name' => 'cm1'], @@ -403,8 +445,17 @@ final class course_navigation_test extends route_testcase { ], 'current' => 'cm1', 'expected' => [ - 'type' => 'section', - 'id' => '0', + 'type' => 'course', + ], + ]; + yield 'With previous module being a subsection in the first section (student)' => [ + 'cmsdef' => [ + ['name' => 'subsection1', 'type' => 'subsection', 'options' => ['section' => 0]], + ['name' => 'cm1', 'options' => ['section' => 'subsection1']], + ], + 'current' => 'cm1', + 'expected' => [ + 'type' => 'course', ], ]; yield 'With previous module being a subsection (student)' => [ @@ -452,6 +503,16 @@ final class course_navigation_test extends route_testcase { 'id' => 'cm1', ], ]; + yield 'With first module without url in the first section (student)' => [ + 'cmsdef' => [ + ['name' => 'cm1', 'type' => 'label'], + ['name' => 'cm2', 'options' => ['section' => 0]], + ], + 'current' => 'cm2', + 'expected' => [ + 'type' => 'course', + ], + ]; yield 'With first module without url (student)' => [ 'cmsdef' => [ ['name' => 'cm1', 'type' => 'label'],