MDL-73549 Course: My course page menu improvement

- Introduce core_course_category::get_nearest_editable_subcategory()
 - This function will return the first creatable/manageable category
for current user
 - With this new function, we can fix the issue that the users with
course management or creation permision at category level cannot see
the manage menu on My courses page
This commit is contained in:
Huong Nguyen 2022-03-02 13:16:18 +07:00
parent 1d99ba19a2
commit 481cfdc3f0
6 changed files with 251 additions and 9 deletions

View file

@ -63,12 +63,18 @@ $PAGE->theme->addblockposition = BLOCK_ADDBLOCK_POSITION_CUSTOM;
// Add course management if the user has the capabilities for it.
$coursecat = core_course_category::user_top();
if ($coursecat->can_create_course() || $coursecat->has_manage_capability()) {
$data = [
'newcourseurl' => new moodle_url('/course/edit.php', ['category' => $coursecat->id]),
'manageurl' => new moodle_url('/course/management.php', ['categoryid' => $coursecat->id]),
];
$PAGE->add_header_action($OUTPUT->render_from_template('my/dropdown', $data));
$coursemanagemenu = [];
if ($category = core_course_category::get_nearest_editable_subcategory($coursecat, ['create'])) {
// The user has the capability to create course.
$coursemanagemenu['newcourseurl'] = new moodle_url('/course/edit.php', ['category' => $category->id]);
}
if ($category = core_course_category::get_nearest_editable_subcategory($coursecat, ['manage'])) {
// The user has the capability to manage the course category.
$coursemanagemenu['manageurl'] = new moodle_url('/course/management.php', ['categoryid' => $category->id]);
}
if (!empty($coursemanagemenu)) {
// Render the course management menu.
$PAGE->add_header_action($OUTPUT->render_from_template('my/dropdown', $coursemanagemenu));
}
echo $OUTPUT->header();

View file

@ -31,7 +31,11 @@
<i class="fa fa-ellipsis-v text-dark py-2" aria-hidden="true"></i>
</a>
<div class="dropdown-menu dropdown-menu-right">
<a class="dropdown-item" href="{{newcourseurl}}">{{#str}}newcourse, core{{/str}}</a>
<a class="dropdown-item" href="{{manageurl}}">{{#str}}managecourses, core{{/str}}</a>
{{#newcourseurl}}
<a class="dropdown-item" href="{{newcourseurl}}">{{#str}}newcourse, core{{/str}}</a>
{{/newcourseurl}}
{{#manageurl}}
<a class="dropdown-item" href="{{manageurl}}">{{#str}}managecourses, core{{/str}}</a>
{{/manageurl}}
</div>
</div>

View file

@ -1,6 +1,20 @@
@core @core_my
Feature: Run tests over my courses.
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| user1 | User | 1 | user1@example.com |
And the following "categories" exist:
| name | category | idnumber |
| CatA | 0 | cata |
And the following "roles" exist:
| shortname | name | archetype |
| role1 | Role 1 | |
And the following "system role assigns" exist:
| user | role | contextlevel | reference |
| user1 | role1 | Category | CatA |
Scenario: Admin can add new courses or manage them from my courses
Given I am on the "My courses" page logged in as "admin"
And I click on "Course management options" "link"
@ -11,3 +25,57 @@ Feature: Run tests over my courses.
And I click on "Course management options" "link"
And I click on "Manage courses" "link"
And I should see "Manage course categories and courses"
Scenario: User without creating a course and managing category permissions cannot see any link
Given I am on the "My courses" page logged in as "user1"
Then "Course management options" "link" should not exist
@javascript
Scenario: User with creating a course permission can see the Create course link only
Given the following "permission overrides" exist:
| capability | permission | role | contextlevel | reference |
| moodle/course:create | Allow | role1 | Category | cata |
When I am on the "My courses" page logged in as "user1"
Then "Course management options" "link" should exist
And I click on "Course management options" "link"
And I should see "New course"
And I should not see "Manage courses"
And I click on "New course" "link"
And I wait to be redirected
And I should see "Add a new course"
And "CatA" "autocomplete_selection" should exist
@javascript
Scenario: User with managing a category permission can see the Manage course link only
Given the following "permission overrides" exist:
| capability | permission | role | contextlevel | reference |
| moodle/category:manage | Allow | role1 | Category | cata |
When I am on the "My courses" page logged in as "user1"
Then "Course management options" "link" should exist
And I click on "Course management options" "link"
And I should not see "New course"
And I should see "Manage courses"
And I click on "Manage courses" "link"
And I wait to be redirected
And I should see "Manage course categories and courses"
@javascript
Scenario: User with both creating a course and managing a category permission can see both links
Given the following "permission overrides" exist:
| capability | permission | role | contextlevel | reference |
| moodle/course:create | Allow | role1 | Category | cata |
| moodle/category:manage | Allow | role1 | Category | cata |
When I am on the "My courses" page logged in as "user1"
Then "Course management options" "link" should exist
And I click on "Course management options" "link"
And I should see "New course"
And I should see "Manage courses"
And I click on "New course" "link"
And I wait to be redirected
And I should see "Add a new course"
And "CatA" "autocomplete_selection" should exist
And I am on the "My courses" page
And I click on "Course management options" "link"
And I click on "Manage courses" "link"
And I wait to be redirected
And I should see "Manage course categories and courses"