From 7abf78fc41aab7e019125d3cbf40dba24886a5a4 Mon Sep 17 00:00:00 2001
From: Simey Lameze <simey@moodle.com>
Date: Wed, 3 Jul 2019 09:23:31 +0800
Subject: [PATCH 1/4] MDL-65958 calendar: fix adminseesall logic in the event
 form

---
 calendar/classes/local/event/forms/eventtype.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/calendar/classes/local/event/forms/eventtype.php b/calendar/classes/local/event/forms/eventtype.php
index b3d582694f1..d615a50804b 100644
--- a/calendar/classes/local/event/forms/eventtype.php
+++ b/calendar/classes/local/event/forms/eventtype.php
@@ -98,7 +98,7 @@ trait eventtype {
             $mform->hideIf('categoryid', 'eventtype', 'noteq', 'category');
         }
 
-        $showall = $CFG->calendar_adminseesall && !has_capability('moodle/calendar:manageentries', \context_system::instance());
+        $showall = is_siteadmin() && !empty($CFG->calendar_adminseesall);
         if (!empty($eventtypes['course'])) {
             $mform->addElement('course', 'courseid', get_string('course'), ['limittoenrolled' => !$showall]);
             $mform->hideIf('courseid', 'eventtype', 'noteq', 'course');

From 47177cd6bc39f9523e0b2b1c7c04f095e48b8dcc Mon Sep 17 00:00:00 2001
From: Simey Lameze <simey@moodle.com>
Date: Wed, 3 Jul 2019 11:57:15 +0800
Subject: [PATCH 2/4] MDL-65958 calendar: add behat to test
 calendar_adminseesall behaviour

---
 calendar/tests/behat/calendar.feature | 28 +++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/calendar/tests/behat/calendar.feature b/calendar/tests/behat/calendar.feature
index 8dc6f3681a0..f1ed7e698c7 100644
--- a/calendar/tests/behat/calendar.feature
+++ b/calendar/tests/behat/calendar.feature
@@ -14,6 +14,8 @@ Feature: Perform basic calendar functionality
     And the following "courses" exist:
       | fullname | shortname | format |
       | Course 1 | C1 | topics |
+      | Course 2 | C2 | topics |
+      | Course 3 | C3 | topics |
     And the following "course enrolments" exist:
       | user | course | role |
       | student1 | C1 | student |
@@ -188,3 +190,29 @@ Feature: Perform basic calendar functionality
     And I follow "This month"
     When I click on "New event" "button"
     Then the field "Type of event" matches value "Course"
+
+  @javascript
+  Scenario: Admin can only see all courses if calendar_adminseesall setting is enabled.
+    Given I log in as "admin"
+    And I am on "Course 1" course homepage
+    And I enrol "admin" user as "Teacher"
+    And I am viewing site calendar
+    And I click on "New event" "button"
+    And I set the field "Type of event" to "Course"
+    When I open the autocomplete suggestions list
+    Then I should see "Course 1" in the ".form-autocomplete-suggestions" "css_element"
+    And I should not see "Course 2" in the ".form-autocomplete-suggestions" "css_element"
+    And I should not see "Course 3" in the ".form-autocomplete-suggestions" "css_element"
+    And I click on "Close" "button"
+    And I am on site homepage
+    And I navigate to "Appearance > Calendar" in site administration
+    And I set the field "Admins see all" to "1"
+    And I press "Save changes"
+    And I am viewing site calendar
+    And I click on "New event" "button"
+    And I set the field "Type of event" to "Course"
+    When I open the autocomplete suggestions list
+    Then I should see "Course 1" in the ".form-autocomplete-suggestions" "css_element"
+    And I should see "Course 2" in the ".form-autocomplete-suggestions" "css_element"
+    And I should see "Course 3" in the ".form-autocomplete-suggestions" "css_element"
+

From 26d6404315f8e4fcdd777d99d53f099ff9f9b604 Mon Sep 17 00:00:00 2001
From: Simey Lameze <simey@moodle.com>
Date: Wed, 3 Jul 2019 13:19:46 +0800
Subject: [PATCH 3/4] MDL-65958 calendar: students should not see course event
 type

---
 calendar/lib.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/calendar/lib.php b/calendar/lib.php
index 4ffa4e9ca06..b744caf2f3e 100644
--- a/calendar/lib.php
+++ b/calendar/lib.php
@@ -3691,7 +3691,7 @@ function calendar_get_allowed_event_types(int $courseid = null) {
 
         $types['user'] = has_capability('moodle/calendar:manageownentries', $context);
 
-        if (has_capability('moodle/calendar:manageentries', $context) || !empty($CFG->calendar_adminseesall)) {
+        if (has_capability('moodle/calendar:manageentries', $context)) {
             $types['course'] = true;
 
             $types['group'] = (!empty($groups) && has_capability('moodle/site:accessallgroups', $context))

From da6ed259996f75e09ba2d22a2e85ed6dac76d70d Mon Sep 17 00:00:00 2001
From: Simey Lameze <simey@moodle.com>
Date: Wed, 3 Jul 2019 13:21:54 +0800
Subject: [PATCH 4/4] MDL-65958 calendar: cover students default event type
 visibility

---
 calendar/tests/behat/calendar.feature | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/calendar/tests/behat/calendar.feature b/calendar/tests/behat/calendar.feature
index f1ed7e698c7..5f1ffaf5043 100644
--- a/calendar/tests/behat/calendar.feature
+++ b/calendar/tests/behat/calendar.feature
@@ -216,3 +216,25 @@ Feature: Perform basic calendar functionality
     And I should see "Course 2" in the ".form-autocomplete-suggestions" "css_element"
     And I should see "Course 3" in the ".form-autocomplete-suggestions" "css_element"
 
+  @javascript
+  Scenario: Students can only see user event type by default.
+    Given I log in as "student1"
+    And I am viewing site calendar
+    When I click on "New event" "button"
+    Then I should see "User" in the "div#fitem_id_staticeventtype" "css_element"
+    And I am on "Course 1" course homepage
+    And I follow "This month"
+    When I click on "New event" "button"
+    Then I should see "User" in the "div#fitem_id_staticeventtype" "css_element"
+    And I click on "Close" "button"
+    And I log out
+    Given I log in as "admin"
+    And I navigate to "Appearance > Calendar" in site administration
+    And I set the field "Admins see all" to "1"
+    And I press "Save changes"
+    And I log out
+    Given I log in as "student1"
+    And I am on "Course 1" course homepage
+    And I follow "This month"
+    When I click on "New event" "button"
+    Then I should see "User" in the "div#fitem_id_staticeventtype" "css_element"