From d3bfae8fcc060405bb983b5e536a8b9264646002 Mon Sep 17 00:00:00 2001
From: Andrew Nicols <andrew@nicols.co.uk>
Date: Thu, 11 Jun 2020 12:13:09 +0800
Subject: [PATCH] MDL-69017 user: Show all links should work for students

---
 user/index.php                                | 37 +++++++++----------
 .../behat/filter_participants_showall.feature | 33 +++++++++++------
 2 files changed, 39 insertions(+), 31 deletions(-)

diff --git a/user/index.php b/user/index.php
index f3addab941c..1570374f10c 100644
--- a/user/index.php
+++ b/user/index.php
@@ -171,19 +171,17 @@ $participanttable->out($perpage, true);
 $participanttablehtml = ob_get_contents();
 ob_end_clean();
 
-if ($bulkoperations) {
-    echo html_writer::start_tag('form', [
-        'action' => 'action_redir.php',
-        'method' => 'post',
-        'id' => 'participantsform',
-        'data-course-id' => $course->id,
-        'data-table-unique-id' => $participanttable->uniqueid,
-        'data-table-default-per-page' => ($perpage < DEFAULT_PAGE_SIZE) ? $perpage : DEFAULT_PAGE_SIZE,
-    ]);
-    echo '<div>';
-    echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
-    echo '<input type="hidden" name="returnto" value="'.s($PAGE->url->out(false)).'" />';
-}
+echo html_writer::start_tag('form', [
+    'action' => 'action_redir.php',
+    'method' => 'post',
+    'id' => 'participantsform',
+    'data-course-id' => $course->id,
+    'data-table-unique-id' => $participanttable->uniqueid,
+    'data-table-default-per-page' => ($perpage < DEFAULT_PAGE_SIZE) ? $perpage : DEFAULT_PAGE_SIZE,
+]);
+echo '<div>';
+echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
+echo '<input type="hidden" name="returnto" value="'.s($PAGE->url->out(false)).'" />';
 
 echo html_writer::tag(
     'p',
@@ -229,6 +227,10 @@ echo $OUTPUT->container(html_writer::link(
     ]
 ), [], 'showall');
 
+$bulkoptions = (object) [
+    'uniqueid' => $participanttable->uniqueid,
+];
+
 if ($bulkoperations) {
     echo '<br /><div class="buttons"><div class="form-inline">';
 
@@ -310,15 +312,12 @@ if ($bulkoperations) {
     echo '<input type="hidden" name="id" value="' . $course->id . '" />';
     echo '<div class="d-none" data-region="state-help-icon">' . $OUTPUT->help_icon('publishstate', 'notes') . '</div>';
     echo '</div></div></div>';
-    echo '</form>';
 
-    $options = (object) [
-        'uniqueid' => $participanttable->uniqueid,
-        'noteStateNames' => note_get_state_names(),
-    ];
-    $PAGE->requires->js_call_amd('core_user/participants', 'init', [$options]);
+    $bulkoptions->noteStateNames = note_get_state_names();
 }
+echo '</form>';
 
+$PAGE->requires->js_call_amd('core_user/participants', 'init', [$bulkoptions]);
 echo '</div>';  // Userlist.
 
 $enrolrenderer = $PAGE->get_renderer('core_enrol');
diff --git a/user/tests/behat/filter_participants_showall.feature b/user/tests/behat/filter_participants_showall.feature
index 997122262f6..51c35c06c03 100644
--- a/user/tests/behat/filter_participants_showall.feature
+++ b/user/tests/behat/filter_participants_showall.feature
@@ -1,4 +1,5 @@
 @core @core_user
+
 Feature: Course participants can be filtered to display all the users
   In order to filter the list of course participants
   As a user
@@ -6,9 +7,9 @@ Feature: Course participants can be filtered to display all the users
 
   Background:
     Given the following "courses" exist:
-      | fullname | shortname | groupmode |
-      | Course 1 | C1        |     1     |
-      | Course 2 | C2        |     0     |
+      | fullname | shortname |
+      | Course 1 | C1        |
+      | Course 2 | C2        |
     And the following "users" exist:
       | username  | firstname | lastname | email                 |
       | student1  | Student   | 1        | student1@example.com  |
@@ -67,15 +68,6 @@ Feature: Course participants can be filtered to display all the users
       | student3  | C2     | student        |    0   |               |
       | teacher1  | C1     | editingteacher |    0   |               |
       | teacher1  | C2     | editingteacher |    0   |               |
-    And the following "groups" exist:
-      | name    | course | idnumber |
-      | Group 1 | C1     | G1       |
-      | Group 2 | C1     | G2       |
-    And the following "group members" exist:
-      | user     | group |
-      | student2 | G1    |
-      | student2 | G2    |
-      | student3 | G2    |
 
   @javascript
   Scenario: Show all users in a course that match a single filter value
@@ -95,6 +87,23 @@ Feature: Course participants can be filtered to display all the users
     And I should see "Show 20 per page"
     And I should not see "Show all 24"
 
+  @javascript
+  Scenario: Show all users as a student
+    Given I log in as "student1"
+    And I am on "Course 1" course homepage
+    And I navigate to course participants
+    And I set the field "Match" in the "Filter 1" "fieldset" to "All"
+    And I set the field "type" in the "Filter 1" "fieldset" to "Roles"
+    And I click on ".form-autocomplete-downarrow" "css_element" in the "Filter 1" "fieldset"
+    And I click on "Student" "list_item"
+    When I click on "Apply filters" "button"
+    Then I should see "24 participants found"
+    And I should see "Show all 24"
+    And I should not see "Show 20 per page"
+    And I click on "Show all 24" "link"
+    And I should see "Show 20 per page"
+    And I should not see "Show all 24"
+
   @javascript
   Scenario: Apply one value for more than one filter and show all matching users
     Given I log in as "teacher1"