From c7fec301bcbe4452fc2a1a92dda25be5701ee2cc Mon Sep 17 00:00:00 2001 From: Laurent David Date: Tue, 19 Nov 2024 10:39:07 +0100 Subject: [PATCH] MDL-83561 format_social: Limit visible sections in social * Only the first section is visible in social format, so we limit the visibility of the section to the first section. This impacts the move dialog. * Fix PHP unit tests --- course/format/social/lib.php | 16 ++++++++++++++++ course/format/tests/external/get_state_test.php | 14 +++++++++----- .../tests/output/local/state/state_test.php | 2 +- course/format/tests/stateactions_test.php | 12 +++++++++--- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/course/format/social/lib.php b/course/format/social/lib.php index c1d239b793a..f2f2778453e 100644 --- a/course/format/social/lib.php +++ b/course/format/social/lib.php @@ -170,4 +170,20 @@ class format_social extends core_courseformat\base { // Social ony uses one section. return 1; } + + /** + * Returns if a specific section is visible to the current user. + * + * Formats can override this method to implement any special section logic. + * Social format does not use any other sections than section 0 and + * used this method to hide all other sections from the Move section activity. + * + * @param section_info $section the section modinfo + * @return bool; + */ + #[\Override] + public function is_section_visible(section_info $section): bool { + $visible = parent::is_section_visible($section); + return $visible && $section->section == 0; + } } diff --git a/course/format/tests/external/get_state_test.php b/course/format/tests/external/get_state_test.php index b9e610e71a3..6e8eb93e02e 100644 --- a/course/format/tests/external/get_state_test.php +++ b/course/format/tests/external/get_state_test.php @@ -86,7 +86,7 @@ final class get_state_test extends \externallib_advanced_testcase { // Create a course. $numsections = 6; - $visiblesections = $numsections + 1; // Include topic 0. + $course = $this->getDataGenerator()->create_course(['numsections' => $numsections, 'format' => $format]); $hiddensections = [4, 6]; foreach ($hiddensections as $section) { @@ -99,16 +99,20 @@ final class get_state_test extends \externallib_advanced_testcase { if ($isadmin) { $this->setAdminUser(); } else { - if (!$canedit) { - // User won't see the hidden sections. Remove them from the total. - $visiblesections = $visiblesections - count($hiddensections); - } $user = $this->getDataGenerator()->create_user(); if ($role != 'unenroled') { $this->getDataGenerator()->enrol_user($user->id, $course->id, $role); } $this->setUser($user); } + $visiblesections = $numsections + 1; // We include topic 0. + if (!$canedit) { + // User won't see the hidden sections. Remove them from the total. + $visiblesections = $visiblesections - count($hiddensections); + } + if ($format == 'social') { + $visiblesections = 1; // But Social format has one section visible. + } // Social course format automatically creates a forum activity. if (course_get_format($course)->get_format() === 'social') { diff --git a/course/format/tests/output/local/state/state_test.php b/course/format/tests/output/local/state/state_test.php index 76ff35a42f2..a40532ee794 100644 --- a/course/format/tests/output/local/state/state_test.php +++ b/course/format/tests/output/local/state/state_test.php @@ -111,8 +111,8 @@ final class state_test extends \advanced_testcase { $sections = $modinfo->get_section_info_all(); foreach ($sections as $key => $section) { - $this->assertEquals($section->id, $result->course->sectionlist[$key]); if (!$issocialformat || $format == 'theunittest') { + $this->assertEquals($section->id, $result->course->sectionlist[$key]); if (!empty($section->uservisible)) { $sectionstate = new $sectionclass($courseformat, $section); $result->section[$key] = $sectionstate->export_for_template($renderer); diff --git a/course/format/tests/stateactions_test.php b/course/format/tests/stateactions_test.php index d0919ffc49d..afea511509e 100644 --- a/course/format/tests/stateactions_test.php +++ b/course/format/tests/stateactions_test.php @@ -318,10 +318,14 @@ final class stateactions_test extends \advanced_testcase { $expectedexception = ($format === 'singleactivity'); $cms = ['cm0', 'cm1', 'cm2', 'cm3']; + // All sections and cms that the user can access to. + $usersections = ['section0', 'section1', 'section2', 'section3']; + $studentcms = ['cm0']; if ($format === 'social') { $cms = ['initialcm0', 'cm0', 'cm1', 'cm2', 'cm3']; $studentcms = ['initialcm0', 'cm0']; + $usersections = ['section0']; // Social format only uses section 0 (for all users). } return [ @@ -335,7 +339,7 @@ final class stateactions_test extends \advanced_testcase { ], 'expectedresults' => [ 'course' => ['course'], - 'section' => ['section0', 'section1', 'section2', 'section3'], + 'section' => array_intersect($usersections, ['section0', 'section1', 'section2', 'section3']), 'cm' => $cms, ], 'expectedexception' => $expectedexception, @@ -349,7 +353,7 @@ final class stateactions_test extends \advanced_testcase { ], 'expectedresults' => [ 'course' => ['course'], - 'section' => ['section0', 'section1', 'section2', 'section3'], + 'section' => array_intersect($usersections, ['section0', 'section1', 'section2', 'section3']), 'cm' => $cms, ], 'expectedexception' => $expectedexception, @@ -363,7 +367,7 @@ final class stateactions_test extends \advanced_testcase { ], 'expectedresults' => [ 'course' => ['course'], - 'section' => ['section0', 'section1', 'section3'], + 'section' => array_intersect($usersections, ['section0', 'section1', 'section3']), 'cm' => $studentcms, ], 'expectedexception' => $expectedexception, @@ -392,6 +396,7 @@ final class stateactions_test extends \advanced_testcase { } if ($format === 'social') { $usercms = ['initialcm0', ...$usercms]; + $usersections = ['section0']; // Social format only uses section 0 (for all users). } return [ @@ -519,6 +524,7 @@ final class stateactions_test extends \advanced_testcase { } if ($format === 'social') { $usercms = ['initialcm0', ...$usercms]; + $usersections = ['section0']; // Social format only uses section 0 (for all users). } return [