From 840bcd88104b17b79b808a4abb329c441f079383 Mon Sep 17 00:00:00 2001 From: Michael Hawkins Date: Fri, 18 Sep 2020 18:52:13 +0800 Subject: [PATCH] MDL-69559 course: Add course setting and cap to control course downloads --- course/edit_form.php | 21 ++++ .../behat/course_download_content.feature | 47 ++++++++ ...ourse_download_content_permissions.feature | 112 ++++++++++++++++++ lang/en/moodle.php | 1 + lang/en/role.php | 1 + lib/db/access.php | 10 ++ lib/db/install.xml | 5 +- lib/db/upgrade.php | 14 +++ version.php | 2 +- 9 files changed, 210 insertions(+), 3 deletions(-) create mode 100644 course/tests/behat/course_download_content.feature create mode 100644 course/tests/behat/course_download_content_permissions.feature diff --git a/course/edit_form.php b/course/edit_form.php index 8a289cc7258..9c674744e25 100644 --- a/course/edit_form.php +++ b/course/edit_form.php @@ -120,6 +120,27 @@ class course_edit_form extends moodleform { $mform->setConstant('visible', $courseconfig->visible); } } + + // Download course content. + if ($CFG->downloadcoursecontentallowed) { + $downloadchoices = [ + DOWNLOAD_COURSE_CONTENT_DISABLED => get_string('no'), + DOWNLOAD_COURSE_CONTENT_ENABLED => get_string('yes'), + ]; + $sitedefaultstring = $downloadchoices[$courseconfig->downloadcontentsitedefault]; + $downloadchoices[DOWNLOAD_COURSE_CONTENT_SITE_DEFAULT] = get_string('sitedefaultspecified', '', $sitedefaultstring); + $downloadselectdefault = $courseconfig->downloadcontent ?? DOWNLOAD_COURSE_CONTENT_SITE_DEFAULT; + + $mform->addElement('select', 'downloadcontent', get_string('enabledownloadcoursecontent', 'course'), $downloadchoices); + $mform->addHelpButton('downloadcontent', 'downloadcoursecontent', 'course'); + $mform->setDefault('downloadcontent', $downloadselectdefault); + + if (!has_capability('moodle/course:configuredownloadcontent', $coursecontext)) { + $mform->hardFreeze('downloadcontent'); + $mform->setConstant('downloadcontent', $downloadselectdefault); + } + } + $mform->addElement('date_time_selector', 'startdate', get_string('startdate')); $mform->addHelpButton('startdate', 'startdate'); $date = (new DateTime())->setTimestamp(usergetmidnight(time())); diff --git a/course/tests/behat/course_download_content.feature b/course/tests/behat/course_download_content.feature new file mode 100644 index 00000000000..8b2e3b887e7 --- /dev/null +++ b/course/tests/behat/course_download_content.feature @@ -0,0 +1,47 @@ +@core @core_course +Feature: Course content can be downloaded + In order to retain a backup offline copy of course activity/resource data + As a user + I can download a course's content + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | Teacher | 1 | teacher1@example.com | + | student1 | Student | 1 | student1@example.com | + And the following "courses" exist: + | fullname | shortname | + | Hockey 101 | C1 | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | editingteacher | + | student1 | C1 | student | + And I log in as "admin" + And I navigate to "Courses > Courses > Download course content" in site administration + And I set the following fields to these values: + | Download course content feature available | 1 | + And I press "Save changes" + And I navigate to "Courses > Courses > Course default settings" in site administration + And I set the field "Enable download course content" to "Yes" + And I press "Save changes" + And I log out + + @javascript + Scenario: A student can download course content when the feature is enabled in their course + Given I log in as "student1" + When I am on "Hockey 101" course homepage + And "Download course content" "button" should exist + And I press "Download course content" + Then I should see "You are about to download a zip file" + # Without the ability to check the downloaded file, the absence of an exception being thrown here is considered a success. + And I click on "Download" "button" in the "Download course content" "dialogue" + + @javascript + Scenario: A teacher can download course content when the feature is enabled in their course + Given I log in as "teacher1" + When I am on "Hockey 101" course homepage + And "Download course content" "link" should exist in current page administration + And I navigate to "Download course content" in current page administration + Then I should see "You are about to download a zip file" + # Without the ability to check the downloaded file, the absence of an exception being thrown here is considered a success. + And I click on "Download" "button" in the "Download course content" "dialogue" diff --git a/course/tests/behat/course_download_content_permissions.feature b/course/tests/behat/course_download_content_permissions.feature new file mode 100644 index 00000000000..e204b538f9e --- /dev/null +++ b/course/tests/behat/course_download_content_permissions.feature @@ -0,0 +1,112 @@ +@core @core_course +Feature: Access to downloading course content can be controlled + In order to allow or restrict access to download course content + As a trusted user + I can control access to the download course content feature + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | Teacher | 1 | teacher1@example.com | + | student1 | Student | 1 | student1@example.com | + And the following "courses" exist: + | fullname | shortname | + | Hockey 101 | C1 | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | editingteacher | + | student1 | C1 | student | + And I log in as "admin" + And the following config values are set as admin: + | downloadcoursecontentallowed | 1 | + And I log out + +Scenario: Site admins can remove the download course content feature + Given I log in as "admin" + And I am on "Hockey 101" course homepage + And I navigate to "Edit settings" in current page administration + And I set the field "Enable download course content" to "Yes" + And I press "Save and display" + And "Download course content" "link" should exist in current page administration + When the following config values are set as admin: + | downloadcoursecontentallowed | 0 | + And I am on "Hockey 101" course homepage + Then "Download course content" "link" should not exist in current page administration + And I navigate to "Edit settings" in current page administration + And I should not see "Enable download course content" + +Scenario: Site admins can set the default value for whether download course content is enabled in courses + Given I log in as "admin" + And I am on "Hockey 101" course homepage + And "Download course content" "link" should not exist in current page administration + When I navigate to "Courses > Courses > Course default settings" in site administration + And I set the field "Enable download course content" to "Yes" + And I press "Save changes" + And I am on "Hockey 101" course homepage + Then "Download course content" "link" should exist in current page administration + +Scenario: A teacher can enable and disable the download course content feature when it is available + Given I log in as "teacher1" + When I am on "Hockey 101" course homepage + And "Download course content" "link" should not exist in current page administration + And I navigate to "Edit settings" in current page administration + And I should see "Enable download course content" + And I set the field "Enable download course content" to "Yes" + And I press "Save and display" + Then "Download course content" "link" should exist in current page administration + And I navigate to "Edit settings" in current page administration + And I set the field "Enable download course content" to "No" + And I press "Save and display" + And "Download course content" "link" should not exist in current page administration + +Scenario: Teachers require a capability to access the download course content feature or modify its availability in a course + Given I log in as "admin" + And I navigate to "Courses > Courses > Course default settings" in site administration + And I set the field "Enable download course content" to "Yes" + And I press "Save changes" + And I log out + # Check teacher can see download option and enable dropdown. + And I log in as "teacher1" + And I am on "Hockey 101" course homepage + And "Download course content" "link" should exist in current page administration + And I navigate to "Edit settings" in current page administration + And "Enable download course content" "select" should exist + And I log out + # Remove teacher's capabilities for download course content. + And I log in as "admin" + And I set the following system permissions of "Teacher" role: + | capability | permission | + | moodle/course:downloadcoursecontent | Prohibit | + | moodle/course:configuredownloadcontent | Prohibit | + And I log out + # Check teacher can no longer see download option, and that enable value is visible, but dropdown no longer available. + When I log in as "teacher1" + And I am on "Hockey 101" course homepage + Then "Download course content" "link" should not exist in current page administration + And I navigate to "Edit settings" in current page administration + And I should see "Enable download course content" + And I should see "Site default (Yes)" + And "Enable download course content" "select" should not exist + +Scenario: Students require a capability to access the download course content feature in a course + Given I log in as "teacher1" + And I am on "Hockey 101" course homepage + And I navigate to "Edit settings" in current page administration + And I set the field "Enable download course content" to "Yes" + And I press "Save and display" + And I log out + # Check student can see download button. + And I log in as "student1" + And I am on "Hockey 101" course homepage + And "Download course content" "button" should exist + And I log out + And I log in as "admin" + # Remove student's capability for download course content. + When I set the following system permissions of "Student" role: + | capability | permission | + | moodle/course:downloadcoursecontent | Prohibit | + And I log out + # Check student can no longer see download button. + And I log in as "student1" + And I am on "Hockey 101" course homepage + Then "Download course content" "link" should not exist in current page administration diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 3fe5a858aee..05702bb9d83 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -1930,6 +1930,7 @@ $string['since'] = 'Since'; $string['sincelast'] = 'since last login'; $string['site'] = 'Site'; $string['sitedefault'] = 'Site default'; +$string['sitedefaultspecified'] = 'Site default ({$a})'; $string['siteerrors'] = 'Site errors'; $string['sitefiles'] = 'Site files'; $string['sitefilesused'] = 'Site files used in this course'; diff --git a/lang/en/role.php b/lang/en/role.php index f6251637bc3..e8841271307 100644 --- a/lang/en/role.php +++ b/lang/en/role.php @@ -172,6 +172,7 @@ $string['course:changelockedcustomfields'] = 'Change locked custom fields'; $string['course:changeshortname'] = 'Change course short name'; $string['course:changesummary'] = 'Change course summary'; $string['course:configurecustomfields'] = 'Configure custom fields'; +$string['course:configuredownloadcontent'] = 'Configure download course content'; $string['course:downloadcoursecontent'] = 'Download course content'; $string['course:enrolconfig'] = 'Configure enrol instances in courses'; $string['course:enrolreview'] = 'Review course enrolments'; diff --git a/lib/db/access.php b/lib/db/access.php index 403746a490e..74861f2cb36 100644 --- a/lib/db/access.php +++ b/lib/db/access.php @@ -2588,4 +2588,14 @@ $capabilities = array( 'manager' => CAP_ALLOW ) ], + + // Allow users to configure download course content functionality within a course, if the feature is available. + 'moodle/course:configuredownloadcontent' => [ + 'captype' => 'write', + 'contextlevel' => CONTEXT_COURSE, + 'archetypes' => array( + 'editingteacher' => CAP_ALLOW, + 'manager' => CAP_ALLOW + ) + ], ); diff --git a/lib/db/install.xml b/lib/db/install.xml index 478146184cc..391c79979ae 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1,5 +1,5 @@ - @@ -89,6 +89,7 @@ + @@ -4322,4 +4323,4 @@ - + \ No newline at end of file diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index b9c5d342359..4f3fc7d44f0 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -2774,6 +2774,7 @@ function xmldb_main_upgrade($oldversion) { // Add example field. $field = new xmldb_field('example', XMLDB_TYPE_TEXT, null, null, null, null, null, 'tutorial'); + if (!$dbman->field_exists($table, $field)) { $dbman->add_field($table, $field); } @@ -2862,5 +2863,18 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2021052500.30); } + if ($oldversion < 2021052500.32) { + // Define field downloadcontent to be added to course. + $table = new xmldb_table('course'); + $field = new xmldb_field('downloadcontent', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'visibleold'); + + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Main savepoint reached. + upgrade_main_savepoint(true, 2021052500.32); + } + return true; } diff --git a/version.php b/version.php index 40a8acb9c7c..a1508dc11ec 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2021052500.31; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2021052500.32; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. $release = '4.0dev (Build: 20201023)'; // Human-friendly version name