Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for availability in hidden sections #131

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions classes/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,34 @@ public static function set_visibility(array $modules, bool $visible, bool $visib
}

foreach ($modules as $cm) {
// Convert boolean to integer.
$visibleint = $visible ? 1 : 0;
$visibleonpageint = $visibleonpage ? 1 : 0;

// Stealth activities, if enabled, are stored differently in visible and hidden sections.
// So we still need to do a little bit of extra work here.
if ($visible && !$visibleonpage) {
// We want to set the visibility to 'available, but hidden', but have to respect the global config and
// the course format config.
// We want to have an available but hidden activity.
// First we need to make sure stealth activities are enabled.
if (empty($CFG->allowstealth)) {
// We silently ignore this course module it must not be set to 'available, but not visible on course page'.
continue;
}
}

// We here also cover the case of a hidden section. In this case moodle only uses the attribute 'visible' to determine,
// if a course module is completely hidden ('visible' => 0) or 'available, but not visible on course page'
// ('visible' => 1). The attribute 'visibleonpage' is being ignored, so we can pass it along anyway.
// Because of this in case of a hidden section both actions ('show' and 'make available') lead to the same result:
// 'available, but not visible on course page'.
// In a hidden section, show and make available are the same thing.
// In a visible section, show has a visibleonpage of 1 and stealth has a visibleonpage of 0.
// Gather more information about the module and where it is located.
$modinfo = get_fast_modinfo($cm->course);
$format = course_get_format($modinfo->get_course());
$cm = $modinfo->get_cm($cm->id);

// Modules in hidden sections cannot by definition be visible on the course page.
$allowstealth = $format->allow_stealth_module_visibility($cm, $cm->get_section_info());
// Only modules in visible sections need a visibleonpage of 0.
$visibleonpageint = $allowstealth ? 0 : 1;
}

$visibleint = $visible ? 1 : 0;
$visibleonpageint = $visibleonpage ? 1 : 0;
// Set visibility.
if (set_coursemodule_visible($cm->id, $visibleint, $visibleonpageint)) {
course_module_updated::create_from_cm(get_coursemodule_from_id(false, $cm->id))->trigger();
}
Expand Down
28 changes: 25 additions & 3 deletions tests/behat/actions.feature
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Feature: Check if all the different type of actions of the mass actions block wo
| label | TC | 3 | Test Activity3 | Test Activity3 | 2 |
| page | TC | 4 | Test Activity4 | Test page description4 | 4 |
| assign | TC | 5 | Test Activity5 | Test page description5 | 4 |
And the following "blocks" exist:
| blockname | contextlevel | reference | pagetypepattern | defaultregion |
| recent_activity | Course | TC | course-view-* | side-pre |
When I log in as "teacher1"
And I am on "Test course" course homepage with editing mode on
And I add the "Mass Actions" block
Expand All @@ -42,15 +45,34 @@ Feature: Check if all the different type of actions of the mass actions block wo
And "Test Activity4" activity should be visible
When I click on "Enable bulk editing" "button"
And I click on "Test Activity1" "checkbox"
And I click on "Test Activity4" "checkbox"
And I click on "Make available" "button" in the "Mass Actions" "block"
Then I should see "Available but not shown on course page" in the "Test Activity1" "activity"
When I hide section "4"
And I click on "Enable bulk editing" "button"
And I click on "Test Activity4" "checkbox"
And I click on "Test Activity5" "checkbox"
And I click on "Make available" "button" in the "Mass Actions" "block"
And I should see "Available but not shown on course page" in the "Test Activity4" "activity"
And I should see "Available but not shown on course page" in the "Test Activity5" "activity"
When I click on "Enable bulk editing" "button"
And I click on "Test Activity5" "checkbox"
And I click on "Hide" "button" in the "Mass Actions" "block"
And "Test Activity5" activity should be hidden
And I log out
When I log in as "student1"
And I am on "Test course" course homepage
Then I should not see "Test Activity1"
And I should not see "Test Activity4"
Then "Test Activity1" activity should be hidden
And I should see "Test Activity1" in the "Recent activity" "block"
When I click on "Test Activity1" "link" in the "Recent activity" "block"
And I should see "Test Activity1"
When I click on "TC" "link"
And "Test Activity4" activity should be hidden
And I should see "Test Activity4" in the "Recent activity" "block"
When I click on "Test Activity4" "link" in the "Recent activity" "block"
And I should see "Test Activity4"
When I click on "TC" "link"
And "Test Activity5" activity should be hidden
And I should not see "Test Activity5" in the "Recent activity" "block"

@javascript
Scenario: Check if mass action 'move to section' works
Expand Down
26 changes: 14 additions & 12 deletions tests/massaction_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,40 +384,42 @@ public function test_mass_hide_unhide_modules(): void {
$CFG->allowstealth = 1;
$moduleid = reset($selectedmoduleids);
$module = get_fast_modinfo($this->course)->get_cm($moduleid);
$this->assertEquals(1, $module->visible);
$this->assertEquals(1, $module->visibleoncoursepage);
$this->assertEquals(1, $module->visible); // Visible in a visible section.
$this->assertEquals(1, $module->visibleoncoursepage); // Visible on course page in a visible section.

// Hide section.
set_section_visible($this->course->id, $module->sectionnum, 0);
$module = get_fast_modinfo($this->course)->get_cm($moduleid);
// After section has been hidden the course module should also be hidden.
$this->assertEquals(0, $module->visible);
$this->assertEquals(1, $module->visibleoncoursepage);

// In case the section is hidden, moodle only knows 2 states only depending on the attribute 'visible':
// 'visible' => 1 means that module is 'available, but not visible on course page',
// 'visible' => 0 means that module is completely hidden.
$this->assertEquals(0, $module->visible); // Hidden in a hidden section.
// Hidden on module in a hidden section: visibleoncoursepage can be 1 or 0.
// Makes no difference so we don't need to test it.

// Try to set it visible.
// Set module in a hidden section to be visible (defaults to available).
actions::set_visibility([$module], true);
$module = get_fast_modinfo($this->course)->get_cm($moduleid);
// The section is still hidden, so instead it should be set to 'available, but not visible on course page'.
$this->assertEquals(1, $module->visible);
$this->assertEquals(1, $module->visibleoncoursepage);
$this->assertEquals(1, $module->visibleoncoursepage); // Seems strange but this is how it works.

// Set module in a hidden section to be hidden.
actions::set_visibility([$module], false);
$module = get_fast_modinfo($this->course)->get_cm($moduleid);
// We make sure the module is completely hidden again.
$this->assertEquals(0, $module->visible);
$this->assertEquals(1, $module->visibleoncoursepage);
// Hidden on module in a hidden section: visibleoncoursepage can be 1 or 0.
// Makes no difference so we don't need to test it.

// Now we use the 'make available' feature for setting it to 'available, but not visible on course page'.
// If modules in hidden sections have visible = 1 and visibleoncoursepage = 0, it will show an available tag on
// the course page, but when editing the module it will show the Availability as "Hide on the course page".
actions::set_visibility([$module], true, false);
$module = get_fast_modinfo($this->course)->get_cm($moduleid);
$this->assertEquals(1, $module->visible);
$this->assertEquals(1, $module->visibleoncoursepage);
$this->assertEquals(1, $module->visibleoncoursepage); // Seems strange but this is how it works.

// Just to doublecheck that a visible section behaves differently.
// Available modules in visible sections are not visible on course page.
set_section_visible($this->course->id, $module->sectionnum, 1);
actions::set_visibility([$module], true, false);
$module = get_fast_modinfo($this->course)->get_cm($moduleid);
Expand Down