From 14450ae72ef9d8c2bd06ed99bc6a0001f9c974f3 Mon Sep 17 00:00:00 2001
From: Kathrin Osswald <kathrin.osswald@uni-ulm.de>
Date: Thu, 6 Aug 2020 14:33:43 +0200
Subject: [PATCH] Added setting to be able to show a hint for a unrestricted
 self enrolment in a visible course.

---
 CHANGES.md                                    |  2 +
 README.md                                     |  4 +
 classes/output/core_renderer.php              | 43 ++++++++++
 db/access.php                                 | 39 +++++++++
 lang/en/theme_boost_campus.php                |  9 ++
 settings.php                                  | 11 +++
 ...oost_campus_course_layout_settings.feature | 85 +++++++++++++++++++
 version.php                                   |  2 +-
 8 files changed, 194 insertions(+), 1 deletion(-)
 create mode 100644 db/access.php

diff --git a/CHANGES.md b/CHANGES.md
index 2f53531..3694a00 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -6,6 +6,8 @@ Changes
 
 ### Unreleased
 
+* 2020-08-11 - Added setting to be able to show a hint for a unrestricted self enrolment in a visible course.
+               NOTE: Capability theme/boost_campus:viewhintcourseselfenrol introduced.
 * 2020-08-07 - Added rule to show description border for all users for restricted activities.
 * 2020-08-07 - Added check for empty string to regular expression for time controlled info banner settings.
 * 2020-08-05 - Added settings to be able to show a banner with information on selected pages.
diff --git a/README.md b/README.md
index 485cea4..30c4b6b 100644
--- a/README.md
+++ b/README.md
@@ -139,6 +139,10 @@ With this setting a hint will appear in the course header as long as the visibil
 
 With this setting a hint will appear in the course header when a user is accessing it with the guest access feature. If the course provides an active self enrolment, a link to that page is also presented to the user.
 
+##### Show hint for unrestricted self enrolment
+
+With this setting a hint will appear in the course header when the course is visible and a unrestricted (no enrolment key or end date is set) self enrolment is active.
+
 #### Course settings
 
 ##### In course settings menu
diff --git a/classes/output/core_renderer.php b/classes/output/core_renderer.php
index 5b1a0ff..477e6bf 100644
--- a/classes/output/core_renderer.php
+++ b/classes/output/core_renderer.php
@@ -26,6 +26,7 @@
 namespace theme_boost_campus\output;
 
 use coding_exception;
+use core\plugininfo\enrol;
 use html_writer;
 use tabobject;
 use tabtree;
@@ -250,6 +251,48 @@ class core_renderer extends \core_renderer {
         }
         // MODIFICATION END.
 
+        // MODIFICATION START:
+        // If the setting showhintcourseselfenrol is set, a hint for users is shown that the course has an unrestricted self
+        // enrolment. This hint is only shown if the course is visible, the self enrolment is visible and if the user has the
+        // capability "theme/boost_campus:viewhintcourseselfenrol".
+        if (get_config('theme_boost_campus', 'showhintcourseselfenrol') == 'yes'
+                && has_capability('theme/boost_campus:viewhintcourseselfenrol', \context_course::instance($COURSE->id))
+                && $this->page->has_set_url()
+                && $this->page->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)
+                && $COURSE->visible == true) {
+            // 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)) {
+                    if (empty($instance->name)) {
+                        $selfenrolinstances[$instance->id] = get_string('pluginname', 'enrol_self') .
+                                " (" . get_string('defaultcoursestudent', 'core') . ")";
+                    } else {
+                        $selfenrolinstances[$instance->id] = $instance->name;
+                    }
+                }
+            }
+
+            if (!empty($selfenrolinstances)) {
+                // Give out a hint for each unrestricted active self enrolment in the course.
+                foreach ($selfenrolinstances as $selfenrolinstanceid => $selfenrolinstancename) {
+                    $html .= html_writer::start_tag('div', array('class' => 'course-selfenrol-infobox alert alert-info'));
+                    $html .= html_writer::tag('i', null, array('class' => 'fa fa-sign-in fa-3x fa-pull-left'));
+                    $html .= get_string('showhintcourseselfenrol', 'theme_boost_campus',
+                            array('name' => $selfenrolinstancename));
+                    // Only show the link to edit the specific self enrolment if the user has the capability to config self enrolments.
+                    if (has_capability('enrol/self:config', \context_course::instance($COURSE->id))) {
+                        $url = new moodle_url('/enrol/editinstance.php', array('courseid' => $COURSE->id,
+                                                                               'id' => $selfenrolinstanceid, 'type' => 'self'));
+                        $html .= html_writer::tag('div', get_string('showhintcourseselfenrollink',
+                                'theme_boost_campus', array('url' => $url->out())));
+                    }
+                    $html .= html_writer::end_tag('div');
+                }
+            }
+        }
+        // MODIFICATION END.
+
         // MODIFICATION START.
         // Only use this if setting 'showswitchedroleincourse' is active.
         if (get_config('theme_boost_campus', 'showswitchedroleincourse') === 'yes') {
diff --git a/db/access.php b/db/access.php
new file mode 100644
index 0000000..e78d994
--- /dev/null
+++ b/db/access.php
@@ -0,0 +1,39 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Theme Boost Campus - Capability definitions.
+ *
+ * @package    theme_boost_campus
+ * @copyright  2020 Kathrin Osswald, Ulm University <kathrin.osswald@uni-ulm.de>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+$capabilities = array(
+
+    // Ability to see a hint for unrestricted self enrolment in a visible course.
+        'theme/boost_campus:viewhintcourseselfenrol' => array(
+                'captype' => 'read',
+                'contextlevel' => CONTEXT_COURSE,
+                'archetypes' => array(
+                        'teacher' => CAP_ALLOW,
+                        'editingteacher' => CAP_ALLOW,
+                        'manager' => CAP_ALLOW
+                )
+        )
+);
diff --git a/lang/en/theme_boost_campus.php b/lang/en/theme_boost_campus.php
index d7f33eb..a7c161f 100644
--- a/lang/en/theme_boost_campus.php
+++ b/lang/en/theme_boost_campus.php
@@ -89,6 +89,9 @@ $string['showhintcoursehiddensetting_desc'] = 'With this setting a hint will app
 // ... Show hint for guest access.
 $string['showhintcoursguestaccesssetting'] = 'Show hint for guest access';
 $string['showhintcourseguestaccesssetting_desc'] = 'With this setting a hint will appear in the course header when a user is accessing it with the guest access feature. If the course provides an active self enrolment, a link to that page is also presented to the user.';
+// ... Show hint for unrestricted self enrolment.
+$string['showhintcourseselfenrolsetting'] = 'Show hint for unrestricted self enrolment';
+$string['showhintcourseselfenrolsetting_desc'] = 'With this setting a hint will appear in the course header when the course is visible and a unrestricted (no enrolment key or end date is set) self enrolment is active.';
 // ...Course settings.
 $string['coursesettingsheadingsetting'] = 'Course settings';
 // ...Show course settings within the course.
@@ -296,6 +299,9 @@ $string['showhintcourseguestaccessgeneral'] = 'You are currently viewing this co
 $string['showhintcourseguestaccesslink'] = 'To have full access to the course, you can <a href="{$a->url}">self enrol into this course</a>.';
 $string['showhintcoursehiddengeneral'] = 'This course is currently <strong>hidden</strong>. Only enrolled teachers can access this course when hidden.';
 $string['showhintcoursehiddensettingslink'] = 'You can change the visibility in the <a href="{$a->url}">course settings</a>.';
+$string['showhintcourseselfenrol'] = 'This course is currently visible and an <strong>unrestricted self enrolment</strong> is active: <strong>"{$a->name}"</strong>. <br/>
+This means, that neither an enrolment key nor a self enrolment end date is set.';
+$string['showhintcourseselfenrollink'] = 'If you don\'t want that any Moodle user can enrol into this course freely, please restrict the settings for this self enrolment instance in the <a href="{$a->url}">enrolment settings</a>.';
 $string['switchroleto'] = 'Switch role to';
 $string['yes_close'] = "Yes, close!";
 
@@ -303,3 +309,6 @@ $string['yes_close'] = "Yes, close!";
 $string['privacy:metadata:preference:infobanner_dismissed'] = 'The user preference for the status if the perpetual info banner has been dismissed.';
 $string['privacy:metadata:request:infobanner_dismissed_yes'] = 'Perpetual info banner has been dismissed.';
 $string['privacy:metadata:request:infobanner_dismissed_no'] = 'Perpetual info banner has not been dismissed.';
+
+// CAPABILITIES.
+$string['boost_campus:viewhintcourseselfenrol'] = 'To be able to see a hint for unrestricted self enrolment in a visible course.';
diff --git a/settings.php b/settings.php
index 4f22779..37f9016 100644
--- a/settings.php
+++ b/settings.php
@@ -295,6 +295,17 @@ if ($ADMIN->fulltree) {
     $setting->set_updatedcallback('theme_reset_all_caches');
     $page->add($setting);
 
+    // Setting to display a hint that the active course has a unrestricted self enrolment.
+    $name = 'theme_boost_campus/showhintcourseselfenrol';
+    $title = get_string('showhintcourseselfenrolsetting', 'theme_boost_campus', null, true);
+    $description = get_string('showhintcourseselfenrolsetting_desc', 'theme_boost_campus', null, true);
+    $setting = new admin_setting_configcheckbox($name, $title, $description, 'no', 'yes', 'no'); // Overriding default values
+    // yes = 1 and no = 0 because of the use of empty() in theme_boost_campus_get_pre_scss() (lib.php).
+    // Default 0 value would not write the variable to scss that could cause the scss to crash if used in that file.
+    // See MDL-58376.
+    $setting->set_updatedcallback('theme_reset_all_caches');
+    $page->add($setting);
+
     // Settings title for grouping course settings related aspects together. We don't need a description here.
     $name = 'theme_boost_campus/coursesettingsheading';
     $title = get_string('coursesettingsheadingsetting', 'theme_boost_campus', null, true);
diff --git a/tests/behat/theme_boost_campus_course_layout_settings.feature b/tests/behat/theme_boost_campus_course_layout_settings.feature
index 2cbc973..613c923 100644
--- a/tests/behat/theme_boost_campus_course_layout_settings.feature
+++ b/tests/behat/theme_boost_campus_course_layout_settings.feature
@@ -140,6 +140,91 @@ Feature: Configuring the theme_boost_campus plugin for the "Course Layout settin
     Then I should not see "You are currently viewing this course as Guest."
     And ".course-guestaccess-infobox" "css_element" should not exist
 
+  Scenario: Enable "Show hint for unrestricted self enrolment"
+    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
+    And I log out
+    When I log in as "student1"
+    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 hides when password or end date 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 | 1 |
+    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_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 click on "enrolment settings" "link" in the ".course-selfenrol-infobox" "css_element"
+    And I set the following fields to these values:
+        | 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 add more than one self enrolment instance"
+    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 add "Self enrolment" enrolment method with:
+      | Custom instance name | Custom self enrolment |
+    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
+    And I should see "This course is currently visible and an unrestricted self enrolment is active: \"Custom self enrolment\"."
+    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:
+      | 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 I should see "This course is currently visible and an unrestricted self enrolment is active: \"Custom self enrolment\"."
+    And ".course-selfenrol-infobox" "css_element" should exist
+
   @javascript
   Scenario: Enable "In course settings menu"
     Given the following config values are set as admin:
diff --git a/version.php b/version.php
index 5743c80..5274540 100644
--- a/version.php
+++ b/version.php
@@ -25,7 +25,7 @@
 defined('MOODLE_INTERNAL') || die();
 
 $plugin->component = 'theme_boost_campus';
-$plugin->version = 2020080500;
+$plugin->version = 2020081200;
 $plugin->release = 'v3.8-r2';
 $plugin->requires = 2019111804;
 $plugin->maturity = MATURITY_STABLE;
-- 
GitLab