From 4294acc5abbe347c137ebff6e0fe4d73f446875d Mon Sep 17 00:00:00 2001 From: Philipp Memmel Date: Fri, 16 Sep 2022 17:42:17 +0200 Subject: [PATCH 1/2] MDL-75771 core_courseformat: Fix capability for edit controls course_format\base:show_editor now by default uses 'moodle/course:manageactivities', but also accepts other capabilities as param --- course/format/classes/base.php | 9 +++++++-- .../format/classes/output/local/content/addsection.php | 2 +- course/format/classes/output/local/content/cm/title.php | 5 +---- .../classes/output/local/content/frontpagesection.php | 4 +--- course/format/upgrade.txt | 3 ++- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/course/format/classes/base.php b/course/format/classes/base.php index c8b1318a440..66b1e5f09f5 100644 --- a/course/format/classes/base.php +++ b/course/format/classes/base.php @@ -1340,13 +1340,18 @@ abstract class base { /** * return true if the course editor must be displayed. * + * @param array|null $capabilities array of capabilities a user needs to have to see edit controls in general. + * If null or not specified, the user needs to have 'moodle/course:manageactivities'. * @return bool true if edit controls must be displayed */ - public function show_editor(): bool { + public function show_editor(?array $capabilities = ['moodle/course:manageactivities']): bool { global $PAGE; $course = $this->get_course(); $coursecontext = context_course::instance($course->id); - return $PAGE->user_is_editing() && has_capability('moodle/course:update', $coursecontext); + if ($capabilities === null) { + $capabilities = ['moodle/course:manageactivities']; + } + return $PAGE->user_is_editing() && has_all_capabilities($capabilities, $coursecontext); } /** diff --git a/course/format/classes/output/local/content/addsection.php b/course/format/classes/output/local/content/addsection.php index 801a6bc90d0..6309a3b5e43 100644 --- a/course/format/classes/output/local/content/addsection.php +++ b/course/format/classes/output/local/content/addsection.php @@ -63,7 +63,7 @@ class addsection implements named_templatable, renderable { public function export_for_template(\renderer_base $output): stdClass { // If no editor must be displayed, just return an empty structure. - if (!$this->format->show_editor()) { + if (!$this->format->show_editor(['moodle/course:update'])) { return new stdClass(); } diff --git a/course/format/classes/output/local/content/cm/title.php b/course/format/classes/output/local/content/cm/title.php index 779ed39c861..e0948a12a4a 100644 --- a/course/format/classes/output/local/content/cm/title.php +++ b/course/format/classes/output/local/content/cm/title.php @@ -90,10 +90,7 @@ class title extends inplace_editable implements named_templatable, renderable { $this->displayoptions = $this->load_display_options($displayoptions); if ($editable === null) { - $editable = $format->show_editor() && has_capability( - 'moodle/course:manageactivities', - $mod->context - ); + $editable = $format->show_editor(); } $this->editable = $editable; diff --git a/course/format/classes/output/local/content/frontpagesection.php b/course/format/classes/output/local/content/frontpagesection.php index b689053175a..50a4b47e243 100644 --- a/course/format/classes/output/local/content/frontpagesection.php +++ b/course/format/classes/output/local/content/frontpagesection.php @@ -79,8 +79,6 @@ class frontpagesection implements named_templatable, renderable { global $USER; $format = $this->format; - $course = $format->get_course(); - $context = context_course::instance($course->id); $section = $this->section; $sectionoutput = new $this->sectionclass($format, $section); @@ -94,7 +92,7 @@ class frontpagesection implements named_templatable, renderable { 'sections' => [$sectionoutput->export_for_template($output)], ]; - if ($format->show_editor() && has_capability('moodle/course:update', $context)) { + if ($format->show_editor(['moodle/course:update'])) { $data->showsettings = true; $data->settingsurl = new moodle_url('/course/editsection.php', ['id' => $section->id]); } diff --git a/course/format/upgrade.txt b/course/format/upgrade.txt index 428258c4981..38220c87823 100644 --- a/course/format/upgrade.txt +++ b/course/format/upgrade.txt @@ -7,7 +7,8 @@ Overview of this plugin type at http://docs.moodle.org/dev/Course_formats the deprecated methods add_section_delete() and add_cm_delete(). * The 4th param of the core_courseformat\output\local\content\cm\cmname construct is now deprecated. The page edition is now detected using the course_format\base:show_editor method as the rest of the - core_courseformat outputs. + core_courseformat outputs. It defaults to checking the capability 'moodle/course:manageactivities'. If different + capabilities are needed to check, an array of capabilities can be passed as an optional parameter to the function. === 4.0 === * New core_courseformat\base::uses_course_index() to define whether the course format uses course index or not. From b4c292166a40d503418f3ae9ea809d9fc46db2fd Mon Sep 17 00:00:00 2001 From: Philipp Memmel Date: Thu, 27 Oct 2022 22:33:02 +0200 Subject: [PATCH 2/2] MDL-75771 core_courseformat: Add behat test for show_editor function --- .../behat/course_manageactivities.feature | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 course/format/tests/behat/course_manageactivities.feature diff --git a/course/format/tests/behat/course_manageactivities.feature b/course/format/tests/behat/course_manageactivities.feature new file mode 100644 index 00000000000..d9245a9c844 --- /dev/null +++ b/course/format/tests/behat/course_manageactivities.feature @@ -0,0 +1,77 @@ +@core @core_courseformat @show_editor +Feature: Verify edit utils availability. + In order to edit the course activities + As a student with capability 'moodle/course:manageactivities' + I need to be able to use the edit utils. + + Background: + Given the following "course" exists: + | fullname | Course 1 | + | shortname | C1 | + | category | 0 | + | numsections | 3 | + And the following "activities" exist: + | activity | name | intro | course | idnumber | section | + | assign | Activity sample 1 | Test assignment description | C1 | sample1 | 1 | + And the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | Teacher | 1 | teacher1@example.com | + | student1 | Student | 1 | student1@example.com | + | author1 | Author | 1 | author1@example.com | + And the following "roles" exist: + | shortname | name | archetype | + | author | Author | student | + And the following "permission overrides" exist: + | capability | permission | role | contextlevel | reference | + | moodle/course:manageactivities | Allow | author | Course | C1 | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | editingteacher | + | author1 | C1 | author | + | student1 | C1 | student | + + @javascript + Scenario: Edit tools should be available to teachers. + Given I log in as "teacher1" + 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 open "Activity sample 1" actions menu + And I should see "Edit settings" + And ".section_action_menu" "css_element" should exist in the "Topic 1" "section" + And I click on ".section_action_menu" "css_element" in the "Topic 1" "section" + And I should see "Edit topic" + + @javascript + Scenario: Edit mode should not be available to students. + Given I log in as "student1" + When I am on "Course 1" course homepage + Then I should not see "Edit mode" + + @javascript + Scenario: Edit tools should be available to students with the capability 'moodle/course:manageactivities', + but should not be allowed to add and edit sections without having 'moodle/course:update' + 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 should not see "Add topic" + And I open "Activity sample 1" actions menu + And I should see "Edit settings" + And ".section_action_menu" "css_element" should not exist in the "Topic 1" "section" + + @javascript + Scenario: Section adding should be available to students if they also have the capability 'moodle/course:update'. + Given the following "permission overrides" exist: + | capability | permission | role | contextlevel | reference | + | moodle/course:update | Allow | author | Course | C1 | + 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 should see "Add topic" + And I open "Activity sample 1" actions menu + And I should see "Edit settings" + And ".section_action_menu" "css_element" should exist in the "Topic 1" "section" + And I click on ".section_action_menu" "css_element" in the "Topic 1" "section" + And I should see "Edit topic"