Skip to content

Commit

Permalink
MDL-83056 core_course: Disable subsection add link when max reached
Browse files Browse the repository at this point in the history
* Fix behat test to changes in the Add Activity or resource button
  • Loading branch information
laurentdavid committed Jan 13, 2025
1 parent e16db63 commit e507e5e
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 20 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions course/format/amd/src/local/content/section/cmitem.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ export default class extends DndCmItem {
CARD: `[data-region='activity-card']`,
DRAGICON: `.editing_move`,
INPLACEEDITABLE: `[data-itemtype="activityname"] > [data-inplaceeditablelink]`,
ADDSECTIONREGIONLINK: `a[data-region='section-addsection']`,
};
// Most classes will be loaded later by DndCmItem.
this.classes = {
LOCKED: 'editinprogress',
HIDE: 'd-none',
SELECTED: 'selected',
DISABLED: `disabled`,
};
// We need our id to watch specific events.
this.id = this.element.dataset.id;
Expand All @@ -72,6 +74,7 @@ export default class extends DndCmItem {
{watch: `cm[${this.id}]:deleted`, handler: this.unregister},
{watch: `cm[${this.id}]:updated`, handler: this._refreshCm},
{watch: `bulk:updated`, handler: this._refreshBulk},
{watch: `course.sectionlist:updated`, handler: this._refreshAddActivityButton},
];
}

Expand Down Expand Up @@ -181,4 +184,15 @@ export default class extends DndCmItem {
}
return bulk.selection.includes(this.id);
}
/**
* Check the Add activity or resource button and disable some options if needed.
*
* @param {Object} detail the update details.
* @param {Object} detail.state the state object.
*/
_refreshAddActivityButton({state}) {
const canAddSection = state.course.numsections < state.course.maxsections;
const addSectionLink = this.getElement(this.selectors.ADDSECTIONREGIONLINK);
addSectionLink.classList.toggle(this.classes.DISABLED, !canAddSection);
}
}
2 changes: 0 additions & 2 deletions course/format/tests/behat/course_courseindex.feature
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,7 @@ Feature: Course index depending on role
And I log in as "teacher1"
And I am on "Course 1" course homepage with editing mode on
Then I should see "Section 1" in the "courseindex-content" "region"
And ".disabled" "css_element" should exist in the "[data-action='addSection']" "css_element"
And I should see "You have reached the maximum number of sections allowed for a course."
And I delete section "4"
And I click on "Delete" "button" in the ".modal" "css_element"
And ".disabled" "css_element" should not exist in the "[data-action='addSection']" "css_element"
And I should not see "You have reached the maximum number of sections allowed for a course."
6 changes: 4 additions & 2 deletions course/format/tests/behat/course_manageactivities.feature
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ Feature: Verify edit utils availability
Given I log in as "author1"
When I am on "Course 1" course homepage
And I turn editing mode on
Then I should see "Add an activity or resource"
And I click on add content dropdown action
Then "Activity or resource" "core_course > Add content dropdown action" should be visible
But I should not see "Add section"
And I open "Activity sample 1" actions menu
And I should see "Edit settings"
Expand All @@ -72,7 +73,8 @@ Feature: Verify edit utils availability
And I log in as "author1"
When I am on "Course 1" course homepage
And I turn editing mode on
Then I should see "Add an activity or resource"
And I click on add content dropdown action
Then "Activity or resource" "core_course > Add content dropdown action" should be visible
And I should see "Add section"
And I open "Activity sample 1" actions menu
And I should see "Edit settings"
Expand Down
10 changes: 5 additions & 5 deletions course/tests/behat/activity_chooser.feature
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Feature: Display and choose from the available activities in course
# Confirm show summary also works for weekly format course
And I am on "C2" course homepage with editing mode on
And I click on "Add content" "button" in the "13 January - 19 January" "section"
And I click on "Activity or resource" "button" in the "13 January - 19 January" "section"
And I click on "Activity or resource" "core_course > Add content dropdown action"
And I click on "Information about the Assignment activity" "button" in the "Add an activity or resource" "dialogue"
And I should see "Assignment" in the "help" "core_course > Activity chooser screen"
And I should see "The assignment activity module enables a teacher to communicate tasks, collect work and provide grades and feedback."
Expand All @@ -67,7 +67,7 @@ Feature: Display and choose from the available activities in course
# Confirm hide summary also works for weekly format course
And I am on "C2" course homepage with editing mode on
And I click on "Add content" "button" in the "13 January - 19 January" "section"
And I click on "Activity or resource" "button" in the "13 January - 19 January" "section"
And I click on "Activity or resource" "core_course > Add content dropdown action"
And I click on "Information about the Assignment activity" "button" in the "Add an activity or resource" "dialogue"
And I click on "Back" "button" in the "help" "core_course > Activity chooser screen"
And "modules" "core_course > Activity chooser screen" should be visible
Expand Down Expand Up @@ -107,7 +107,7 @@ Feature: Display and choose from the available activities in course
And I should see "Starred" in the "Add an activity or resource" "dialogue"
And I click on "Close" "button" in the "Add an activity or resource" "dialogue"
And I click on "Add content" "button" in the "New section" "section"
And I click on "Activity or resource" "button" in the "New section" "section"
And I click on "Activity or resource" "core_course > Add content dropdown action"
And I click on "Starred" "link" in the "Add an activity or resource" "dialogue"
Then I should see "Forum" in the "favourites" "core_course > Activity chooser tab"

Expand Down Expand Up @@ -279,7 +279,7 @@ Feature: Display and choose from the available activities in course
Scenario: Teacher can navigate through activity chooser in Weekly format course
Given I am on "C2" course homepage with editing mode on
And I click on "Add content" "button" in the "13 January - 19 January" "section"
When I click on "Activity or resource" "button" in the "13 January - 19 January" "section"
When I click on "Activity or resource" "core_course > Add content dropdown action"
Then I should see "All" in the "Add an activity or resource" "dialogue"
And I press the tab key
And I press the tab key
Expand Down Expand Up @@ -313,7 +313,7 @@ Feature: Display and choose from the available activities in course
# Confirm the same behaviour for weekly format course
And I am on "C2" course homepage with editing mode on
And I click on "Add content" "button" in the "13 January - 19 January" "section"
And I click on "Activity or resource" "button" in the "13 January - 19 January" "section"
And I click on "Activity or resource" "core_course > Add content dropdown action"
And I should see "All" in the "Add an activity or resource" "dialogue"
And I click on "Information about the Assignment activity" "button" in the "Add an activity or resource" "dialogue"
# Confirm more help link exists
Expand Down
25 changes: 25 additions & 0 deletions course/tests/behat/behat_course.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public static function get_partial_named_selectors(): array {
'initials bar',
[".//*[contains(concat(' ', @class, ' '), ' initialbar ')]//span[contains(., %locator%)]/parent::div"]
),
new behat_component_named_selector(
'Add content dropdown action',
["//*[contains(@class, 'dropdown-menu')][contains(@class, 'show')]//*[contains(., %locator%)]"]
),
];
}

Expand All @@ -86,6 +90,27 @@ public static function get_named_replacements(): array {
];
}

/**
* Click on the first add content dropdown action.
*
* Examples:
* Given I click on add content dropdown action
* Given I click on add content dropdown action in the ".my-wrapper" "css_element"
*
* @Given /^I click on add content dropdown action(?: in the "([^"]+)" "([^"]+)")?$/
*/
public function i_click_on_add_content_dropdown_action(?string $element = null, ?string $selectortype = 'css_element') {
$buttonxpath = ["//*[@data-action='open-addingcontent']", "xpath_element"];
if ($element) {
$this->execute('behat_general::i_click_on_in_the', [$element, $selectortype, ...$buttonxpath]);
} else {
$this->execute(
'behat_general::i_click_on',
$buttonxpath
);
}
}

/**
* Creates a new course with the provided table data matching course settings names with the desired values.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ class before_activitychooserbutton_exported_handler {
public static function callback(before_activitychooserbutton_exported $hook): void {
/** @var section_info $section */
$section = $hook->get_section();

if (!permission::can_add_subsection($section)) {
return;
}

$format = course_get_format($section->course);
$attributes = [
'class' => 'dropdown-item',
'class' => 'dropdown-item' . ($format->is_max_sections_reached() ? ' disabled' : ''),
'data-action' => 'addModule',
'data-modname' => 'subsection',
'data-sectionnum' => $section->sectionnum,
'data-region' => 'section-addsection',
];
if ($hook->get_cm()) {
$attributes['data-beforemod'] = $hook->get_cm()->id;
Expand Down
9 changes: 5 additions & 4 deletions mod/subsection/classes/permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@
*/
class permission {
/**
* Whether given user can add a subsection in a section
* Whether given user can add a subsection in a section.
*
* Here we check if we can add a subsection in a section and the delegated section is enabled.
* It means that this will only check things that are not supposed to change except through a new page load.
* Typically checking that we reach max_sections is not done here.
*
* @param section_info $section the course section
* @param int|null $userid User ID to check, or the current user if omitted
Expand All @@ -49,9 +53,6 @@ public static function can_add_subsection(section_info $section, ?int $userid =
return false;
}
$format = course_get_format($section->course);
if ($format->is_max_sections_reached()) {
return false;
}
if (!$format->supports_components()) {
return false;
}
Expand Down
24 changes: 22 additions & 2 deletions mod/subsection/tests/behat/subsection_limit.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,29 @@ Feature: Teacher can only add subsection when certain conditions are met
| maxsections | 10 | moodlecourse |
And I log in as "teacher1"
And I am on "Course 1" course homepage with editing mode on
And I click on "Add content" "button" in the "Section 1" "section"
And I click on "Subsection" "link" in the ".dropdown-menu.show" "css_element"
And I click on add content dropdown action
And I click on "Subsection" "core_course > Add content dropdown action"
When the following config values are set as admin:
| maxsections | 4 | moodlecourse |
And I am on "Course 1" course homepage
And I should see "You have reached the maximum number of sections allowed for a course."

@javascript
Scenario: The add subsection link is disabled when maxsections is reached
Given the following config values are set as admin:
| maxsections | 6 | moodlecourse |
And I log in as "teacher1"
And I am on "Course 1" course homepage with editing mode on
And I click on add content dropdown action
And the "class" attribute of "Subsection" "core_course > Add content dropdown action" should not contain "disabled"
And I click on "Subsection" "core_course > Add content dropdown action"
And I click on add content dropdown action
And the "class" attribute of "Subsection" "core_course > Add content dropdown action" should contain "disabled"
And I should see "You have reached the maximum number of sections allowed for a course."

@javascript
Scenario: When subsection is disabled, we should not see the add subsection link
Given I disable "subsection" "mod" plugin
And I log in as "teacher1"
And I am on "Course 1" course homepage with editing mode on
And I should see "Add an activity or resource" in the "General" "section"

0 comments on commit e507e5e

Please sign in to comment.