From d6c289c5e30572c0b2a84a33d06f18b30276dbea Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Fri, 18 Sep 2020 20:26:57 +0200 Subject: [PATCH] MDL-64657 course: Return hidden section names when configured --- course/externallib.php | 19 ++++++---- course/tests/externallib_test.php | 58 +++++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 8 deletions(-) diff --git a/course/externallib.php b/course/externallib.php index 5a896e7f2bf..54a1e0d9e50 100644 --- a/course/externallib.php +++ b/course/externallib.php @@ -167,7 +167,8 @@ class core_course_external extends external_api { //retrieve sections $modinfo = get_fast_modinfo($course); $sections = $modinfo->get_section_info_all(); - $coursenumsections = course_get_format($course)->get_last_section_number(); + $courseformat = course_get_format($course); + $coursenumsections = $courseformat->get_last_section_number(); $stealthmodules = array(); // Array to keep all the modules available but not visible in a course section/topic. $completioninfo = new completion_info($course); @@ -383,20 +384,26 @@ class core_course_external extends external_api { // We didn't this before to be able to retrieve stealth activities. foreach ($coursecontents as $sectionnumber => $sectioncontents) { $section = $sections[$sectionnumber]; - // Show the section if the user is permitted to access it, OR if it's not available - // but there is some available info text which explains the reason & should display. + // Show the section if the user is permitted to access it OR + // if it's not available but there is some available info text which explains the reason & should display OR + // the course is configured to show hidden sections name. $showsection = $section->uservisible || - ($section->visible && !$section->available && - !empty($section->availableinfo)); + ($section->visible && !$section->available && !empty($section->availableinfo)) || + (!$section->visible && empty($courseformat->get_course()->hiddensections)); if (!$showsection) { unset($coursecontents[$sectionnumber]); continue; } - // Remove modules information if the section is not visible for the user. + // Remove section and modules information if the section is not visible for the user. if (!$section->uservisible) { $coursecontents[$sectionnumber]['modules'] = array(); + // Remove summary information if the section is completely hidden only, + // even if the section is not user visible, the summary is always displayed among the availability information. + if (!$section->visible) { + $coursecontents[$sectionnumber]['summary'] = ''; + } } } diff --git a/course/tests/externallib_test.php b/course/tests/externallib_test.php index 57e61083be2..99693eb2a2d 100644 --- a/course/tests/externallib_test.php +++ b/course/tests/externallib_test.php @@ -921,7 +921,8 @@ class core_course_externallib_testcase extends externallib_advanced_testcase { $CFG->allowstealth = 1; // Allow stealth activities. $CFG->enablecompletion = true; - $course = self::getDataGenerator()->create_course(['numsections' => 4, 'enablecompletion' => 1]); + // Course with 4 sections (apart from the main section), with completion and not displaying hidden sections. + $course = self::getDataGenerator()->create_course(['numsections' => 4, 'enablecompletion' => 1, 'hiddensections' => 1]); $forumdescription = 'This is the forum description'; $forum = $this->getDataGenerator()->create_module('forum', @@ -1115,7 +1116,7 @@ class core_course_externallib_testcase extends externallib_advanced_testcase { $this->assertCount(1, $sections[1]['modules']); $this->assertCount(1, $sections[2]['modules']); $this->assertCount(0, $sections[3]['modules']); // No modules for the section with availability restrictions. - $this->assertCount(1, $sections[4]['modules']); // One stealh module. + $this->assertCount(1, $sections[4]['modules']); // One stealth module. $this->assertEquals(-1, $sections[4]['id']); } @@ -1422,6 +1423,59 @@ class core_course_externallib_testcase extends externallib_advanced_testcase { } } + /** + * Test get_course_contents when hidden sections are displayed. + */ + public function test_get_course_contents_hiddensections() { + global $DB; + $this->resetAfterTest(true); + + list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test(); + // Force returning hidden sections. + $course->hiddensections = 0; + update_course($course); + + $studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student')); + $user = self::getDataGenerator()->create_user(); + self::getDataGenerator()->enrol_user($user->id, $course->id, $studentroleid); + $this->setUser($user); + + $sections = core_course_external::get_course_contents($course->id, array()); + // We need to execute the return values cleaning process to simulate the web service server. + $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections); + + $this->assertCount(5, $sections); // All the sections, including the "not visible" one. + $this->assertCount(5, $sections[0]['modules']); + $this->assertCount(1, $sections[1]['modules']); + $this->assertCount(1, $sections[2]['modules']); + $this->assertCount(0, $sections[3]['modules']); // No modules for the section with availability restrictions. + $this->assertCount(0, $sections[4]['modules']); // No modules for the section hidden. + + $this->assertNotEmpty($sections[3]['availabilityinfo']); + $this->assertEquals(1, $sections[1]['section']); + $this->assertEquals(2, $sections[2]['section']); + $this->assertEquals(3, $sections[3]['section']); + // The module with the availability restriction met is returning contents. + $this->assertNotEmpty($sections[1]['modules'][0]['contents']); + // The module with the availability restriction not met is not returning contents. + $this->assertArrayNotHasKey('contents', $sections[2]['modules'][0]); + + // Now include flag for returning stealth information (fake section). + $sections = core_course_external::get_course_contents($course->id, + array(array("name" => "includestealthmodules", "value" => 1))); + // We need to execute the return values cleaning process to simulate the web service server. + $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections); + + $this->assertCount(6, $sections); // Include fake section with stealth activities. + $this->assertCount(5, $sections[0]['modules']); + $this->assertCount(1, $sections[1]['modules']); + $this->assertCount(1, $sections[2]['modules']); + $this->assertCount(0, $sections[3]['modules']); // No modules for the section with availability restrictions. + $this->assertCount(0, $sections[4]['modules']); // No modules for the section hidden. + $this->assertCount(1, $sections[5]['modules']); // One stealth module. + $this->assertEquals(-1, $sections[5]['id']); + } + /** * Test duplicate_course */