MDL-68169 user: Add roles filter
authorAndrew Nicols <andrew@nicols.co.uk>
Fri, 3 Apr 2020 07:00:42 +0000 (15:00 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Wed, 27 May 2020 02:49:43 +0000 (10:49 +0800)
Part of MDL-67743

user/classes/output/participants_filter.php

index 12dddd6..21c1ec0 100644 (file)
@@ -66,6 +66,10 @@ class participants_filter implements renderable, templatable {
             $filtertypes[] = $filtertype;
         }
 
+        if ($filtertype = $this->get_roles_filter()) {
+            $filtertypes[] = $filtertype;
+        }
+
         return $filtertypes;
     }
 
@@ -98,6 +102,35 @@ class participants_filter implements renderable, templatable {
         );
     }
 
+    /**
+     * Get data for the roles filter.
+     *
+     * @return stdClass|null
+     */
+    protected function get_roles_filter(): ?stdClass {
+        $roles = [];
+        $roles += [-1 => get_string('noroles', 'role')];
+        $roles += get_viewable_roles($this->context);
+
+        if (has_capability('moodle/role:assign', $this->context)) {
+            $roles += get_assignable_roles($this->context, ROLENAME_ALIAS);
+        }
+
+        return $this->get_filter_object(
+            'roles',
+            get_string('roles', 'core_role'),
+            false,
+            true,
+            null,
+            array_map(function($id, $title) {
+                return (object) [
+                    'value' => $id,
+                    'title' => $title,
+                ];
+            }, array_keys($roles), array_values($roles))
+        );
+    }
+
     /**
      * Export the renderer data in a mustache template friendly format.
      *