From 952748e882f75ba89d1f2b91d6260a813d0edc3a Mon Sep 17 00:00:00 2001
From: Alexander Bias <alexander.bias@uni-ulm.de>
Date: Sat, 13 Mar 2021 21:42:41 +0100
Subject: [PATCH] Bugfix: Unrestricted self enrolment hint did not respect
 existing but disabled enrolment instances.

---
 CHANGES.md                                    |   4 +
 classes/output/core_renderer.php              |   6 +-
 ...oost_campus_course_layout_settings.feature | 110 +++++++++++++++++-
 3 files changed, 113 insertions(+), 7 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index e40d656..930f3d9 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -4,6 +4,10 @@ moodle-theme_boost_campus
 Changes
 -------
 
+### Unreleased
+
+* 2021-03-13 - Bugfix: Unrestricted self enrolment hint did not respect existing but disabled enrolment instances.
+
 ### v3.10-r3
 
 * 2021-02-05 - Improvement: Add missing catch branch in Back-to-top button JS
diff --git a/classes/output/core_renderer.php b/classes/output/core_renderer.php
index 43229ad..179b159 100644
--- a/classes/output/core_renderer.php
+++ b/classes/output/core_renderer.php
@@ -257,7 +257,11 @@ class core_renderer extends \core_renderer {
             // Get the active enrol instances for this course.
             $enrolinstances = enrol_get_instances($COURSE->id, true);
             foreach ($enrolinstances as $instance) {
-                if ($instance->enrol == 'self' && empty($instance->password) && empty($instance->enrolenddate)) {
+                // Check if unrestricted self enrolment is possible.
+                $now = (new \DateTime("now", \core_date::get_server_timezone_object()))->getTimestamp();
+                if ($instance->enrol == 'self' && empty($instance->password) && $instance->customint6 == 1 &&
+                        (empty($instance->enrolenddate) || $instance->enrolenddate > $now) &&
+                        (empty($instance->enrolstartdate) || $instance->enrolstartdate < $now)) {
                     if (empty($instance->name)) {
                         $selfenrolinstances[$instance->id] = get_string('pluginname', 'enrol_self') .
                                 " (" . get_string('defaultcoursestudent', 'core') . ")";
diff --git a/tests/behat/theme_boost_campus_course_layout_settings.feature b/tests/behat/theme_boost_campus_course_layout_settings.feature
index 4cf8a57..18a3b92 100644
--- a/tests/behat/theme_boost_campus_course_layout_settings.feature
+++ b/tests/behat/theme_boost_campus_course_layout_settings.feature
@@ -138,7 +138,7 @@ Feature: Configuring the theme_boost_campus plugin for the "Course Layout settin
     Then I should not see "This course is currently visible and an unrestricted self enrolment is active: \"Self enrolment (Student)\""
     And ".course-selfenrol-infobox" "css_element" should not exist
 
-  Scenario: Enable "Show hint for unrestricted self enrolment and check that it hides when password or end date is set"
+  Scenario: Enable "Show hint for unrestricted self enrolment" and check that it is hidden when new enrolments are disabled
     Given the following config values are set as admin:
       | config                  | value | plugin             |
       | showhintcourseselfenrol | yes   | theme_boost_campus |
@@ -153,27 +153,125 @@ Feature: Configuring the theme_boost_campus plugin for the "Course Layout settin
     And ".course-selfenrol-infobox" "css_element" should exist
     When I click on "enrolment settings" "link" in the ".course-selfenrol-infobox" "css_element"
     And I set the following fields to these values:
-      | id_enrolenddate_enabled | 1 |
+      | Allow new enrolments | 0 |
     And I press "Save changes"
     And I am on "Course 1" course homepage
     Then I should not see "This course is currently visible and an unrestricted self enrolment is active: \"Self enrolment (Student)\""
     And ".course-selfenrol-infobox" "css_element" should not exist
-    When I navigate to "Users > Enrolment methods" in current page administration
-    And I click on "Edit" "link" in the "Self enrolment (Student)" "table_row"
+
+  Scenario: Enable "Show hint for unrestricted self enrolment" and check that it is hidden when a password is set
+    Given the following config values are set as admin:
+      | config                  | value | plugin             |
+      | showhintcourseselfenrol | yes   | theme_boost_campus |
+    When I log in as "teacher1"
+    And I am on "Course 1" course homepage
+    Then I should not see "This course is currently visible and an unrestricted self enrolment is active:"
+    And ".course-selfenrol-infobox" "css_element" should not exist
+    And I navigate to "Users > Enrolment methods" in current page administration
+    When I click on "Enable" "link" in the "Self enrolment (Student)" "table_row"
+    And I am on "Course 1" course homepage
+    Then I should see "This course is currently visible and an unrestricted self enrolment is active: \"Self enrolment (Student)\"."
+    And ".course-selfenrol-infobox" "css_element" should exist
+    When I click on "enrolment settings" "link" in the ".course-selfenrol-infobox" "css_element"
     And I set the following fields to these values:
-        | id_enrolenddate_enabled | 0 |
+      | Enrolment key | 1234 |
     And I press "Save changes"
     And I am on "Course 1" course homepage
+    Then I should not see "This course is currently visible and an unrestricted self enrolment is active: \"Self enrolment (Student)\""
+    And ".course-selfenrol-infobox" "css_element" should not exist
+
+  Scenario: Enable "Show hint for unrestricted self enrolment and check that it is hidden when appropriate start and / or end dates are set"
+    Given the following config values are set as admin:
+      | config                  | value | plugin             |
+      | showhintcourseselfenrol | yes   | theme_boost_campus |
+    When I log in as "teacher1"
+    And I am on "Course 1" course homepage
+    Then I should not see "This course is currently visible and an unrestricted self enrolment is active:"
+    And ".course-selfenrol-infobox" "css_element" should not exist
+    And I navigate to "Users > Enrolment methods" in current page administration
+    When I click on "Enable" "link" in the "Self enrolment (Student)" "table_row"
+    And I am on "Course 1" course homepage
     Then I should see "This course is currently visible and an unrestricted self enrolment is active: \"Self enrolment (Student)\"."
     And ".course-selfenrol-infobox" "css_element" should exist
     When I click on "enrolment settings" "link" in the ".course-selfenrol-infobox" "css_element"
     And I set the following fields to these values:
-        | Enrolment key | 1234 |
+      | id_enrolstartdate_enabled | 0             |
+      | id_enrolenddate_enabled   | 1             |
+      | id_enrolenddate           | ##yesterday## |
     And I press "Save changes"
     And I am on "Course 1" course homepage
     Then I should not see "This course is currently visible and an unrestricted self enrolment is active: \"Self enrolment (Student)\""
     And ".course-selfenrol-infobox" "css_element" should not exist
 
+    When I navigate to "Users > Enrolment methods" in current page administration
+    And I click on "Edit" "link" in the "Self enrolment (Student)" "table_row"
+    And I set the following fields to these values:
+      | id_enrolstartdate_enabled | 0            |
+      | id_enrolenddate_enabled   | 1            |
+      | id_enrolenddate           | ##tomorrow## |
+    And I press "Save changes"
+    And I am on "Course 1" course homepage
+    Then I should see "This course is currently visible and an unrestricted self enrolment is active: \"Self enrolment (Student)\"."
+    And ".course-selfenrol-infobox" "css_element" should exist
+
+    When I navigate to "Users > Enrolment methods" in current page administration
+    And I click on "Edit" "link" in the "Self enrolment (Student)" "table_row"
+    And I set the following fields to these values:
+      | id_enrolstartdate_enabled | 1             |
+      | id_enrolstartdate         | ##yesterday## |
+      | id_enrolenddate_enabled   | 0             |
+    And I press "Save changes"
+    And I am on "Course 1" course homepage
+    Then I should see "This course is currently visible and an unrestricted self enrolment is active: \"Self enrolment (Student)\"."
+    And ".course-selfenrol-infobox" "css_element" should exist
+
+    When I navigate to "Users > Enrolment methods" in current page administration
+    And I click on "Edit" "link" in the "Self enrolment (Student)" "table_row"
+    And I set the following fields to these values:
+      | id_enrolstartdate_enabled | 1            |
+      | id_enrolstartdate         | ##tomorrow## |
+      | id_enrolenddate_enabled   | 0            |
+    And I press "Save changes"
+    And I am on "Course 1" course homepage
+    Then I should not see "This course is currently visible and an unrestricted self enrolment is active: \"Self enrolment (Student)\"."
+    And ".course-selfenrol-infobox" "css_element" should not exist
+
+    When I navigate to "Users > Enrolment methods" in current page administration
+    And I click on "Edit" "link" in the "Self enrolment (Student)" "table_row"
+    And I set the following fields to these values:
+      | id_enrolstartdate_enabled | 1                     |
+      | id_enrolstartdate         | ##Monday next week##  |
+      | id_enrolenddate_enabled   | 1                     |
+      | id_enrolenddate           | ##Tuesday next week## |
+    And I press "Save changes"
+    And I am on "Course 1" course homepage
+    Then I should not see "This course is currently visible and an unrestricted self enrolment is active: \"Self enrolment (Student)\"."
+    And ".course-selfenrol-infobox" "css_element" should not exist
+
+    When I navigate to "Users > Enrolment methods" in current page administration
+    And I click on "Edit" "link" in the "Self enrolment (Student)" "table_row"
+    And I set the following fields to these values:
+      | id_enrolstartdate_enabled | 1             |
+      | id_enrolstartdate         | ##yesterday## |
+      | id_enrolenddate_enabled   | 1             |
+      | id_enrolenddate           | ##tomorrow##  |
+    And I press "Save changes"
+    And I am on "Course 1" course homepage
+    Then I should see "This course is currently visible and an unrestricted self enrolment is active: \"Self enrolment (Student)\"."
+    And ".course-selfenrol-infobox" "css_element" should exist
+
+    When I navigate to "Users > Enrolment methods" in current page administration
+    And I click on "Edit" "link" in the "Self enrolment (Student)" "table_row"
+    And I set the following fields to these values:
+      | id_enrolstartdate_enabled | 1              |
+      | id_enrolstartdate         | ##3 days ago## |
+      | id_enrolenddate_enabled   | 1              |
+      | id_enrolenddate           | ##2 days ago## |
+    And I press "Save changes"
+    And I am on "Course 1" course homepage
+    Then I should not see "This course is currently visible and an unrestricted self enrolment is active: \"Self enrolment (Student)\"."
+    And ".course-selfenrol-infobox" "css_element" should not exist
+
   Scenario: Enable "Show hint for unrestricted self enrolment and add more than one self enrolment instance"
     Given the following config values are set as admin:
       | config                  | value | plugin             |
-- 
GitLab