Skip to content

Commit

Permalink
Merge branch 'MDL-64657-master' of git://github.com/jleyva/moodle int…
Browse files Browse the repository at this point in the history
…o master
  • Loading branch information
sarjona committed Oct 8, 2020
2 parents e5a9a92 + d6c289c commit f25dba2
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
19 changes: 13 additions & 6 deletions course/externallib.php
Expand Up @@ -167,7 +167,8 @@ public static function get_course_contents($courseid, $options = array()) {
//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);
Expand Down Expand Up @@ -384,20 +385,26 @@ public static function get_course_contents($courseid, $options = array()) {
// 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'] = '';
}
}
}

Expand Down
58 changes: 56 additions & 2 deletions course/tests/externallib_test.php
Expand Up @@ -1015,7 +1015,8 @@ private function prepare_get_course_contents_test() {

$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',
Expand Down Expand Up @@ -1211,7 +1212,7 @@ public function test_get_course_contents_student() {
$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']);
}

Expand Down Expand Up @@ -1518,6 +1519,59 @@ public function test_get_course_contents_contentsinfo() {
}
}

/**
* 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
*/
Expand Down

0 comments on commit f25dba2

Please sign in to comment.