def retrieve(self, request, pk): """ Get the difficult questions for a particular quiz. """ group_id = request.GET.get("group_id", None) queryset = ExamAttemptLog.objects.filter(examlog__exam=pk) if group_id is not None: queryset = HierarchyRelationsFilter(queryset).filter_by_hierarchy( ancestor_collection=group_id, target_user=F("user") ) collection_id = group_id else: collection_id = Exam.objects.get(pk=pk).collection_id data = queryset.values("item", "content_id").annotate(correct=Sum("correct")) # Instead of inferring the totals from the number of logs, use the total # number of people who took the exam as our guide, as people who started the exam # but did not attempt the question are still important. total = ( HierarchyRelationsFilter(ExamLog.objects.filter(exam_id=pk)) .filter_by_hierarchy( ancestor_collection=collection_id, target_user=F("user") ) .count() ) for datum in data: datum["total"] = total return Response(data)
def retrieve(self, request, pk): """ Get the difficult questions for a particular quiz. """ group_id = request.GET.get("group_id", None) # Only return logs when the learner has submitted the Quiz OR # the coach has deactivated the Quiz. Do not return logs when Quiz is still # in-progress. queryset = ExamAttemptLog.objects.filter( Q(examlog__closed=True) | Q(examlog__exam__active=False), examlog__exam=pk) if group_id is not None: queryset = HierarchyRelationsFilter(queryset).filter_by_hierarchy( ancestor_collection=group_id, target_user=F("user")) collection_id = group_id else: collection_id = Exam.objects.get(pk=pk).collection_id data = queryset.values("item", "content_id").annotate(correct=Sum("correct")) # Instead of inferring the totals from the number of logs, use the total # number of people who submitted (if quiz is active) or started the exam # (if quiz is inactive) as our guide, as people who started the exam # but did not attempt the question are still important. total = (HierarchyRelationsFilter( ExamLog.objects.filter(Q(closed=True) | Q(exam__active=False), exam_id=pk)).filter_by_hierarchy( ancestor_collection=collection_id, target_user=F("user")).count()) for datum in data: datum["total"] = total return Response(data)
def retrieve(self, request, pk): """ Get the difficult questions for a particular exercise. pk maps to the content_id of the exercise in question. """ classroom_id = request.GET.get("classroom_id", None) group_id = request.GET.get("group_id", None) lesson_id = request.GET.get("lesson_id", None) queryset = AttemptLog.objects.filter(masterylog__summarylog__content_id=pk) if lesson_id is not None: collection_ids = Lesson.objects.get( id=lesson_id ).lesson_assignments.values_list("collection_id", flat=True) if group_id is not None: if ( group_id not in collection_ids and classroom_id not in collection_ids ): # In the special case that the group is not in the lesson assignments # nor the containing classroom, just return an empty queryset. queryset = AttemptLog.objects.none() else: # Only filter by all the collections in the lesson if we are not also # filtering by a specific group. Otherwise the group should be sufficient. base_queryset = queryset # Set starting queryset to null, then OR. queryset = AttemptLog.objects.none() for collection_id in collection_ids: queryset |= HierarchyRelationsFilter( base_queryset ).filter_by_hierarchy( ancestor_collection=collection_id, target_user=F("user") ) queryset = queryset.distinct() if group_id is not None: collection_id = group_id or classroom_id queryset = HierarchyRelationsFilter(queryset).filter_by_hierarchy( ancestor_collection=collection_id, target_user=F("user") ) data = ( queryset.values("item") .annotate(total=Count("correct")) .annotate(correct=Sum("correct")) ) return Response(data)