MDL-68169 user: Add enrolment instance filter
authorAndrew Nicols <andrew@nicols.co.uk>
Fri, 3 Apr 2020 09:13:38 +0000 (17:13 +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 1aa3333..f0c7585 100644 (file)
@@ -75,6 +75,10 @@ class participants_filter implements renderable, templatable {
             $filtertypes[] = $filtertype;
         }
 
+        if ($filtertype = $this->get_enrolments_filter()) {
+            $filtertypes[] = $filtertype;
+        }
+
         if ($filtertype = $this->get_groups_filter()) {
             $filtertypes[] = $filtertype;
         }
@@ -140,6 +144,43 @@ class participants_filter implements renderable, templatable {
         );
     }
 
+    /**
+     * Get data for the roles filter.
+     *
+     * @return stdClass|null
+     */
+    protected function get_enrolments_filter(): ?stdClass {
+        if (!has_capability('moodle/course:enrolreview', $this->context)) {
+            return null;
+        }
+
+        if ($this->course->id == SITEID) {
+            // No enrolment methods for the site.
+            return null;
+        }
+
+        $instances = enrol_get_instances($this->course->id, true);
+        $plugins = enrol_get_plugins(false);
+
+        return $this->get_filter_object(
+            'enrolments',
+            get_string('enrolmentinstances', 'core_enrol'),
+            false,
+            true,
+            null,
+            array_filter(array_map(function($instance) use ($plugins): ?stdClass {
+                if (!array_key_exists($instance->enrol, $plugins)) {
+                    return null;
+                }
+
+                return (object) [
+                    'value' => $instance->id,
+                    'title' => $plugins[$instance->enrol]->get_instance_name($instance),
+                ];
+            }, array_values($instances)))
+        );
+    }
+
     /**
      * Get data for the groups filter.
      *