def modules_completed():
    try:
        badge = Badge.objects.get(ref="modulecompleted")
    except Badge.DoesNotExist:
        print "Badge not found"
        return

    users = Tracker.objects.values("user_id").distinct()
    modules = Module.objects.all()
    for u in users:
        user = User.objects.get(pk=u["user_id"])
        # loop through the modules
        for m in modules:
            # check if the user has already been awarded for this module
            already_completed = AwardModule.objects.filter(award__user=user, module=m)
            if already_completed.count() == 0:
                if media_complete(user, m) and activities_complete(user, m) and quiz_complete(user, m):
                    print "%s badge awarded to %s" % (badge, user.username)
                    award = Award()
                    award.badge = badge
                    award.user = user
                    award.description = "Module completed: " + m.get_title()
                    award.save()

                    am = AwardModule()
                    am.module = m
                    am.award = award
                    am.module_version = m.version
                    am.save()
    return
def created_quizzes(num):
    try:
        badge = Badge.objects.get(ref="create" + str(num) + "quiz")
    except Badge.DoesNotExist:
        print "Badge not found"
        return

    # get all the ppl who've created more than 10 quizzes
    users = User.objects.annotate(total=Count("quiz")).filter(total__gt=num)
    for u in users:
        # find out how many awards this user already has
        no_awards = Award.objects.filter(badge=badge, user=u).count()
        if (no_awards * num) + num <= u.total:
            # how many awards to make?
            make_awards = int((u.total - (no_awards * num)) / num)
            for i in range(0, make_awards):
                a = Award()
                a.user = u
                a.badge = badge
                a.description = badge.description
                a.save()
                print a.description + " award made to " + u.username
    return