Repository: carpentries/amy

Issue:
Reporting completion percentages for instructor training
Please create a management command that reports:
- instructor training event slug
- total # of attendees
- # of attendees who got their instructor badge from that event
- # of attendees who have an instructor badge (from that event or any other)

for each instructor training event, so that we can see completion rates (both from the event and eventually).



Discussion:
@gvwilson when do you need it? For February or March?

@gvwilson also which instructor badge are we talking here? Will you only train SWC people, or DC too?

I'm doing this by hand right now, so next week would be a big help.

Good question - they're all mixed together, so "any instructor badge".
Thanks,
Greg

@gvwilson I'll try to work on this tomorrow.

Also, if in the future someone else may be doing DC instructor training, I'd include a code that looks for a badge of the type of Tag assigned to the specific training. For example:
1. SWC-tagged event: look for `swc-instructor`-badged people
2. SWC- and DC-tagged event: look for either `swc-instructor` or `dc-instructor`-badged people.

Hm - I don't understand what you have in mind looking for badged people...

That's for calculating number of attendees who received their badge for that event, or for any other event.


Resolving patch:
diff --git a/workshops/management/commands/trainings_completion_rates.py b/workshops/management/commands/trainings_completion_rates.py
new file mode 100644
--- /dev/null
+++ b/workshops/management/commands/trainings_completion_rates.py
@@ -0,0 +1,53 @@
+from django.core.management.base import BaseCommand
+from django.template.loader import get_template
+
+from workshops.models import Badge, Event, Tag, Person, Role
+
+
+class Command(BaseCommand):
+    help = 'Report completion rates for new training participants.'
+
+    learner = Role.objects.get(name='learner')
+    dc_badge = Badge.objects.get(name='dc-instructor')
+    swc_badge = Badge.objects.get(name='swc-instructor')
+    dc_tag = Tag.objects.get(name='DC')
+    swc_tag = Tag.objects.get(name='SWC')
+
+    def trainings(self):
+        """Create list of trainings."""
+        return Event.objects.filter(tags=Tag.objects.get(name='TTT')) \
+                            .prefetch_related('tags', 'task_set') \
+                            .order_by('-start')
+
+    def badge_type(self, tags):
+        """Return badge of the same type as event tags.
+
+        If no SWC or DC tag is present, SWC badge is assumed."""
+        if self.swc_tag in tags:
+            return self.swc_badge
+        elif self.dc_tag in tags:
+            return self.dc_badge
+        else:
+            return self.swc_badge
+
+    def learners(self, event):
+        """Return list of learners at specific training event."""
+        return Person.objects.filter(task__event=event,
+                                     task__role=self.learner)
+
+    def handle(self, *args, **options):
+        for training in self.trainings():
+            badge = self.badge_type(training.tags.all())
+            learners = self.learners(training)
+            context = {
+                'training': training,
+                'badge': badge,
+                'learners': learners,
+                'completed': learners.filter(badges=badge,
+                                             award__event=training),
+                'completed_other': learners.filter(badges=badge)
+                                           .exclude(award__event=training),
+                'no_badge': learners.exclude(badges=badge),
+            }
+            tmplt = get_template('reports/training_completion_rates.txt')
+            self.stdout.write(tmplt.render(context=context))


Test patch: