def courses_completed(): try: badge = Badge.objects.get(ref='coursecompleted') except Badge.DoesNotExist: print "Badge not found: coursecompleted" return users = Tracker.objects.values('user_id').distinct() courses = Course.objects.all() for u in users: user = User.objects.get(pk=u['user_id']) #loop through the courses for c in courses: # check if the user has already been awarded for this course already_completed = AwardCourse.objects.filter(award__user=user, course=c) if already_completed.count() == 0: if media_complete(user, c) and activities_complete( user, c) and quiz_complete(user, c): print "%s badge awarded to %s" % (badge, user.username) award = Award() award.badge = badge award.user = user award.description = "Course completed: " + c.get_title() award.save() am = AwardCourse() am.course = c am.award = award am.course_version = c.version am.save() return
def update_summary( self, last_tracker_pk=0, newest_tracker_pk=0, # range of tracker ids to process last_points_pk=0, newest_points_pk=0 # range of points ids to process ): first_tracker = (last_tracker_pk == 0) first_points = (last_points_pk == 0) self_trackers = Tracker.objects.filter(user=self.user, course=self.course, pk__gt=last_tracker_pk, pk__lte=newest_tracker_pk) activity_trackers = self_trackers.exclude( type=constants.STR_TRACKER_TYPE_DOWNLOAD) # Add the values that are directly obtained from the last pks self.total_activity = (0 if first_tracker else self.total_activity ) + activity_trackers.count() self.total_downloads = ( 0 if first_tracker else self.total_downloads) + self_trackers.filter( type=constants.STR_TRACKER_TYPE_DOWNLOAD).count() filters = { 'user': self.user, 'course': self.course, 'pk__gt': last_points_pk } if newest_points_pk > 0: filters['pk__lte'] = newest_points_pk new_points = Points.objects.filter(** filters) \ .aggregate(total=Sum('points'))['total'] if new_points: self.points = (0 if first_points else self.points) + new_points # Values that need to be recalculated (as the course digests may vary) self.pretest_score = Course.get_pre_test_score(self.course, self.user) self.quizzes_passed = Course.get_no_quizzes_completed( self.course, self.user) self.completed_activities = Course.get_activities_completed( self.course, self.user) self.media_viewed = Course.get_media_viewed(self.course, self.user) self.badges_achieved = Award.get_userawards(self.user, self.course) # Update the data in the database self.save() # update total_activity_current and total_activity_previous self.update_current_previous_activity()
def get_points(request): if not request.user.is_authenticated: return {'points': 0, 'badges': 0} else: points = Points.get_userscore(request.user) if points is None: points = 0 badges = Award.get_userawards(request.user) if badges is None: badges = 0 return {'points': points, 'badges': badges}
def get_points(request): if not request.user.is_authenticated(): return {'points': 0, 'badges':0 } else: points = Points.get_userscore(request.user) if points is None: points = 0 badges = Award.get_userawards(request.user) if badges is None: badges = 0 return {'points': points, 'badges':badges }
def courses_completed(): try: badge = Badge.objects.get(ref='coursecompleted') except Badge.DoesNotExist: print "Badge not found: coursecompleted" return users = Tracker.objects.values('user_id').distinct() courses = Course.objects.all() for u in users: user = User.objects.get(pk=u['user_id']) #loop through the courses for c in courses: # check if the user has already been awarded for this course already_completed = AwardCourse.objects.filter(award__user=user,course=c) if already_completed.count() == 0: if media_complete(user,c) and activities_complete(user,c) and quiz_complete(user,c): print "%s badge awarded to %s" % (badge, user.username) award = Award() award.badge = badge award.user = user award.description = "Course completed: " + c.get_title() award.save() am = AwardCourse() am.course = c am.award = award am.course_version = c.version am.save() return
def badge_award_all_activities(badge, hours): courses = Course.objects.filter(is_draft=False, is_archived=False) for c in courses: digests = Activity.objects.filter(section__course=c).values('digest').distinct() # get all the users who've added tracker for this course in last 'hours' if hours == 0: users = User.objects.filter(tracker__course=c).distinct() else: since = timezone.now() - datetime.timedelta(hours=int(hours)) users = User.objects.filter(tracker__course=c, tracker__submitted_date__gte=since).distinct() for u in users: if AwardCourse.objects.filter(award__user=u,course=c).count() == 0: user_completed = Tracker.objects.filter(user=u, course=c, completed=True, digest__in=digests).values('digest').distinct().count() if digests.count() == user_completed: print c.title print "-----------------------------" print digests.count() print u.username + " AWARD BADGE" award = Award() award.badge = badge award.user = u award.description = "Course completed: " + c.get_title() award.save() am = AwardCourse() am.course = c am.award = award am.course_version = c.version am.save()
def update_summary( self, last_tracker_pk=0, newest_tracker_pk=0, # range of tracker ids to process last_points_pk=0, newest_points_pk=0 # range of points ids to process ): first_tracker = (last_tracker_pk == 0) first_points = (last_points_pk == 0) t = time.time() self_trackers = Tracker.objects.filter(user=self.user, course=self.course, pk__gt=last_tracker_pk, pk__lte=newest_tracker_pk) ### Add the values that are directly obtained from the last pks self.total_activity = (0 if first_tracker else self.total_activity) + self_trackers.count() self.total_downloads = (0 if first_tracker else self.total_downloads) + self_trackers.filter( type='download').count() filters = { 'user': self.user, 'course': self.course, 'pk__gt': last_points_pk } if newest_points_pk > 0: filters['pk__lte'] = newest_points_pk new_points = Points.objects.filter(**filters).aggregate( total=Sum('points'))['total'] if new_points: self.points = (0 if first_points else self.points) + new_points ### Values that need to be recalculated (as the course digests may vary) self.pretest_score = Course.get_pre_test_score(self.course, self.user) self.quizzes_passed = Course.get_no_quizzes_completed( self.course, self.user) self.badges_achieved = Award.get_userawards(self.user, self.course) self.completed_activities = Course.get_activities_completed( self.course, self.user) self.media_viewed = Course.get_media_viewed(self.course, self.user) ### Update the data in the database self.save() elapsed_time = time.time() - t print('took %.2f seconds' % elapsed_time)
def badge_award_final_quiz(badge, hours): courses = Course.objects.filter(is_draft=False, is_archived=False) for c in courses: final_quiz_digest_activity = Activity.objects \ .filter(section__course=c, type=Activity.QUIZ) \ .order_by('-section__order', '-order')[:1] if final_quiz_digest_activity.count() != 1: continue final_quiz_digest = final_quiz_digest_activity[0].digest print(final_quiz_digest) print(c.title) # get all the users who've added tracker for this course in last # 'hours' if hours == 0: users = User.objects.filter(tracker__course=c) else: since = timezone.now() - datetime.timedelta(hours=int(hours)) users = User.objects.filter(tracker__course=c, tracker__submitted_date__gte=since) # exclude the users that already own this course award users = users.exclude(award__awardcourse__course=c).distinct() for u in users: user_completed = Tracker.objects \ .filter(user=u, course=c, completed=True, digest=final_quiz_digest) \ .values('digest') \ .distinct() \ .count() if user_completed > 0: print(c.title) print("-----------------------------") print(u.username + " AWARD BADGE") award = Award() award.badge = badge award.user = u award.description = STR_COURSE_COMPLETED + c.get_title() award.save() am = AwardCourse() am.course = c am.award = award am.course_version = c.version am.save()
def badge_award_final_quiz(badge, hours): courses = Course.objects.filter(is_draft=False, is_archived=False) for c in courses: final_quiz_digest_activity = Activity.objects.filter( section__course=c, type=Activity.QUIZ).order_by('-section__order', '-order')[:1] if final_quiz_digest_activity.count() != 1: continue final_quiz_digest = final_quiz_digest_activity[0].digest print final_quiz_digest print c.title print "hours: " + str(hours) # get all the users who've added tracker for this course in last 'hours' if hours == 0: users = User.objects.filter(tracker__course=c).distinct() print "hours2:" + str(hours) else: since = timezone.now() - datetime.timedelta(hours=int(hours)) users = User.objects.filter( tracker__course=c, tracker__submitted_date__gte=since).distinct() print "users:" print users for u in users: if AwardCourse.objects.filter(award__user=u, course=c).count() == 0: user_completed = Tracker.objects.filter( user=u, course=c, completed=True, digest=final_quiz_digest).values( 'digest').distinct().count() if user_completed > 0: print c.title print "-----------------------------" print u.username + " AWARD BADGE" award = Award() award.badge = badge award.user = u award.description = "Course completed: " + c.get_title() award.save() am = AwardCourse() am.course = c am.award = award am.course_version = c.version am.save()
def badge_award_all_activities(badge, hours): courses = Course.objects.filter(is_draft=False, is_archived=False).values_list('id') for c in courses: digests = Activity.objects.filter(section__course=c) \ .values_list('digest', flat=True).distinct() digests = list(digests) total_activities = len(digests) # get all the users who've added tracker for this course in last # 'hours' if hours == 0: users = User.objects.filter(tracker__course=c) else: since = timezone.now() - datetime.timedelta(hours=int(hours)) users = User.objects.filter(tracker__course=c, tracker__submitted_date__gte=since) # exclude the users that already own this course award users = users.exclude(award__awardcourse__course=c) \ .values_list('id').distinct() for u in users: user_completed = Tracker.objects.filter(user=u, course=c, completed=True, digest__in=digests) \ .values('digest').distinct().count() if user_completed >= total_activities: course_award = Course.objects.get(id=c[0]) user_awarded = User.objects.get(id=u[0]) print(course_award.title) print("-----------------------------") print(total_activities) print(user_awarded.username + " AWARD BADGE") award = Award() award.badge = badge award.user = user_awarded award.description = STR_COURSE_COMPLETED \ + course_award.get_title() award.save() am = AwardCourse() am.course = course_award am.award = award am.course_version = course_award.version am.save()
def courses_completed(hours): try: badge = Badge.objects.get(ref='coursecompleted') except Badge.DoesNotExist: print "Badge not found: coursecompleted" return print hours # create batch of course with all the digests for each course courses = Course.objects.filter(is_draft=False, is_archived=False) for c in courses: digests = Activity.objects.filter( section__course=c).values('digest').distinct() # get all the users who've added tracker for this course in last 'hours' if hours == 0: users = User.objects.filter(tracker__course=c).distinct() else: since = timezone.now() - datetime.timedelta(hours=int(hours)) users = User.objects.filter( tracker__course=c, tracker__submitted_date__gte=since).distinct() for u in users: user_completed = Tracker.objects.filter( user=u, course=c, completed=True, digest__in=digests).values('digest').distinct().count() if digests.count( ) == user_completed and AwardCourse.objects.filter( award__user=u, course=c).count() == 0: print c.title print "-----------------------------" print digests.count() print u.username + " AWARD BADGE" award = Award() award.badge = badge award.user = u award.description = "Course completed: " + c.get_title() award.save() am = AwardCourse() am.course = c am.award = award am.course_version = c.version am.save() return
def award_badge(self, course, user, badge): print(course.get_title()) print("-----------------------------") print(user.username + " AWARD BADGE") award = Award() award.badge = badge award.user = user award.description = self.STR_COURSE_COMPLETED \ + course.get_title() award.save() ac = AwardCourse() ac.course = course ac.award = award ac.course_version = course.version ac.save()
def badge_award_all_quizzes(badge, hours): courses = Course.objects.filter(is_draft=False, is_archived=False) for c in courses: digests = Activity.objects.filter( section__course=c, type=Activity.QUIZ).values('digest').distinct() # get all the users who've added tracker for this course in last 'hours' if hours == 0: users = User.objects.filter(tracker__course=c) else: since = timezone.now() - datetime.timedelta(hours=int(hours)) users = User.objects.filter(tracker__course=c, tracker__submitted_date__gte=since) # exclude the users that already own this course award users = users.exclude(award__awardcourse__course=c).distinct() for u in users: if AwardCourse.objects.filter(award__user=u, course=c).count() == 0: user_completed = Tracker.objects.filter( user=u, course=c, completed=True, type=Activity.QUIZ, digest__in=digests).values('digest').distinct().count() if digests.count() == user_completed: print(c.title) print("-----------------------------") print(digests.count()) print(u.username + " AWARD BADGE") award = Award() award.badge = badge award.user = u award.description = "Course completed: " + c.get_title() award.save() am = AwardCourse() am.course = c am.award = award am.course_version = c.version am.save()
def update_summary(self, last_tracker_pk=0, newest_tracker_pk=0, # range of tracker ids to process last_points_pk=0, newest_points_pk=0 # range of points ids to process ): first_tracker = (last_tracker_pk == 0) first_points = (last_points_pk == 0) t = time.time() self_trackers = Tracker.objects.filter(user=self.user, course=self.course, pk__gt=last_tracker_pk, pk__lte=newest_tracker_pk) ### Add the values that are directly obtained from the last pks self.total_activity = (0 if first_tracker else self.total_activity) + self_trackers.count() self.total_downloads = (0 if first_tracker else self.total_downloads) + self_trackers.filter(type='download').count() filters = { 'user': self.user, 'course': self.course, 'pk__gt': last_points_pk } if newest_points_pk > 0: filters['pk__lte'] = newest_points_pk new_points = Points.objects.filter( ** filters).aggregate(total=Sum('points'))['total'] if new_points: self.points = (0 if first_points else self.points) + new_points ### Values that need to be recalculated (as the course digests may vary) self.pretest_score = Course.get_pre_test_score(self.course, self.user) self.quizzes_passed = Course.get_no_quizzes_completed(self.course, self.user) self.badges_achieved = Award.get_userawards(self.user, self.course) self.completed_activities = Course.get_activities_completed(self.course, self.user) self.media_viewed = Course.get_media_viewed(self.course, self.user) ### Update the data in the database self.save() elapsed_time = time.time() - t print('took %.2f seconds' % elapsed_time)
def badge_award_final_quiz(badge, hours): courses = Course.objects.filter(is_draft=False, is_archived=False) for c in courses: final_quiz_digest_activity = Activity.objects.filter(section__course=c, type=Activity.QUIZ).order_by('-section__order','-order')[:1] if final_quiz_digest_activity.count() != 1: continue final_quiz_digest = final_quiz_digest_activity[0].digest print final_quiz_digest print c.title print "hours: " + str(hours) # get all the users who've added tracker for this course in last 'hours' if hours == 0: users = User.objects.filter(tracker__course=c).distinct() print "hours2:" + str(hours) else: since = timezone.now() - datetime.timedelta(hours=int(hours)) users = User.objects.filter(tracker__course=c, tracker__submitted_date__gte=since).distinct() print "users:" print users for u in users: if AwardCourse.objects.filter(award__user=u,course=c).count() == 0: user_completed = Tracker.objects.filter(user=u, course=c, completed=True, digest=final_quiz_digest).values('digest').distinct().count() if user_completed > 0: print c.title print "-----------------------------" print u.username + " AWARD BADGE" award = Award() award.badge = badge award.user = u award.description = "Course completed: " + c.get_title() award.save() am = AwardCourse() am.course = c am.award = award am.course_version = c.version am.save()
def created_quizzes(num): badge_ref = 'create' + str(num) + 'quiz' try: badge = Badge.objects.get(ref=badge_ref) except Badge.DoesNotExist: print "Badge not found: " + badge_ref 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
def courses_completed(hours): try: badge = Badge.objects.get(ref='coursecompleted') except Badge.DoesNotExist: print "Badge not found: coursecompleted" return print hours # create batch of course with all the digests for each course courses = Course.objects.filter(is_draft=False, is_archived=False) for c in courses: digests = Activity.objects.filter(section__course=c).values('digest').distinct() # get all the users who've added tracker for this course in last 'hours' if hours == 0: users = User.objects.filter(tracker__course=c).distinct() else: since = timezone.now() - datetime.timedelta(hours=int(hours)) users = User.objects.filter(tracker__course=c, tracker__submitted_date__gte=since).distinct() for u in users: user_completed = Tracker.objects.filter(user=u, course=c, completed=True, digest__in=digests).values('digest').distinct().count() if digests.count() == user_completed and AwardCourse.objects.filter(award__user=u,course=c).count() == 0: print c.title print "-----------------------------" print digests.count() print u.username + " AWARD BADGE" award = Award() award.badge = badge award.user = u award.description = "Course completed: " + c.get_title() award.save() am = AwardCourse() am.course = c am.award = award am.course_version = c.version am.save() return
def badge_award_all_activities(badge, hours): courses = Course.objects.filter(is_draft=False, is_archived=False).values_list('id') for c in courses: digests = Activity.objects.filter(section__course=c).values_list('digest', flat=True).distinct() digests = list(digests) total_activities = len(digests) # get all the users who've added tracker for this course in last 'hours' if hours == 0: users = User.objects.filter(tracker__course=c) else: since = timezone.now() - datetime.timedelta(hours=int(hours)) users = User.objects.filter(tracker__course=c, tracker__submitted_date__gte=since) # exclude the users that already own this course award users = users.exclude(award__awardcourse__course=c).values_list('id').distinct() for u in users: user_completed = Tracker.objects.filter(user=u, course=c, completed=True, digest__in=digests).values('digest').distinct().count() if user_completed >= total_activities: course_award = Course.objects.get(id=c[0]) user_awarded = User.objects.get(id=u[0]) print course_award.title print "-----------------------------" print total_activities print user_awarded.username + " AWARD BADGE" award = Award() award.badge = badge award.user = user_awarded award.description = "Course completed: " + course_award.get_title() award.save() am = AwardCourse() am.course = course_award am.award = award am.course_version = course_award.version am.save()
def created_quizzes(num): badge_ref = 'create'+str(num)+'quiz' try: badge = Badge.objects.get(ref=badge_ref) except Badge.DoesNotExist: print "Badge not found: " + badge_ref 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
def dehydrate_badges(self, bundle): badges = Award.get_userawards(bundle.request.user) return badges
def dehydrate_badges(self, bundle): badges = Award.get_userawards( User.objects.get(username__exact=bundle.data['username'])) return badges
def dehydrate_badges(self, bundle): badges = Award.get_userawards( User.objects.get(username=bundle.request.user.username)) return badges
def dehydrate_badges(self,bundle): badges = Award.get_userawards(bundle.request.user) return badges
def dehydrate_badges(self,bundle): badges = Award.get_userawards(User.objects.get(username__exact=bundle.data['username'])) return badges
def dehydrate_badges(self,bundle): badges = Award.get_userawards(User.objects.get(username=bundle.request.user.username)) return badges