def cohort_leaderboard_view(request, cohort_id): cohort = can_view_cohort(request, cohort_id) # get leaderboard lb = cohort.get_leaderboard(0) paginator = Paginator(lb, 25) # Show 25 contacts per page # Make sure page request is an int. If not, deliver first page. try: page = int(request.GET.get('page', '1')) except ValueError: page = 1 # If page request (9999) is out of range, deliver last page of results. try: leaderboard = paginator.page(page) except (EmptyPage, InvalidPage): leaderboard = paginator.page(paginator.num_pages) return render(request, 'cohort/leaderboard.html', { 'cohort': cohort, 'page': leaderboard })
def cohort_view(request, cohort_id): cohort = can_view_cohort(request, cohort_id) start_date = timezone.now() - datetime.timedelta( days=constants.ACTIVITY_GRAPH_DEFAULT_NO_DAYS) end_date = timezone.now() # get student activity students = User.objects.filter(participant__role=Participant.STUDENT, participant__cohort=cohort) trackers = Tracker.objects \ .filter(course__coursecohort__cohort=cohort, user__is_staff=False, user__in=students) student_activity = filter_trackers(trackers, start_date, end_date) # get leaderboard leaderboard = cohort.get_leaderboard( constants.LEADERBOARD_HOMEPAGE_RESULTS_PER_PAGE) return render( request, 'cohort/activity.html', { 'cohort': cohort, 'activity_graph_data': student_activity, 'leaderboard': leaderboard, })
def cohort_view(request, cohort_id): cohort = can_view_cohort(request, cohort_id) start_date = timezone.now() - datetime.timedelta(days=31) end_date = timezone.now() # get student activity student_activity = [] no_days = (end_date - start_date).days + 1 students = User.objects.filter(participant__role=Participant.STUDENT, participant__cohort=cohort) trackers = Tracker.objects \ .filter(course__coursecohort__cohort=cohort, user__is_staff=False, user__in=students, tracker_date__gte=start_date, tracker_date__lte=end_date) \ .annotate(day=TruncDay('tracker_date'), month=TruncMonth('tracker_date'), year=TruncYear('tracker_date')) \ .values('day') \ .annotate(count=Count('id')) for i in range(0, no_days, +1): temp = start_date + datetime.timedelta(days=i) temp_date = temp.date().strftime(STR_DATE_FORMAT) count = next((dct['count'] for dct in trackers if dct['day'].strftime(STR_DATE_FORMAT) == temp_date), 0) student_activity.append([temp_date, count]) # get leaderboard leaderboard = cohort.get_leaderboard(10) return render( request, 'cohort/activity.html', { 'cohort': cohort, 'activity_graph_data': student_activity, 'leaderboard': leaderboard, })
def cohort_course_view(request, cohort_id, course_id): cohort = can_view_cohort(request, cohort_id) try: course = Course.objects.get(pk=course_id, coursecohort__cohort=cohort) except Course.DoesNotExist: raise Http404() start_date = timezone.now() - datetime.timedelta( days=constants.ACTIVITY_GRAPH_DEFAULT_NO_DAYS) end_date = timezone.now() users = User.objects.filter(participant__role=Participant.STUDENT, participant__cohort=cohort).order_by( 'first_name', 'last_name') trackers = Tracker.objects.filter(course=course, user__is_staff=False, user__in=users) student_activity = filter_trackers(trackers, start_date, end_date) students = [] media_count = course.get_no_media() for user in users: course_stats = UserCourseSummary.objects.filter(user=user, course=course_id) if course_stats: course_stats = course_stats[0] if course_stats.pretest_score: pretest_score = course_stats.pretest_score else: pretest_score = 0 data = { 'user': user, 'user_display': str(user), 'no_quizzes_completed': course_stats.quizzes_passed, 'pretest_score': pretest_score, 'no_activities_completed': course_stats.completed_activities, 'no_points': course_stats.points, 'no_badges': course_stats.badges_achieved, 'no_media_viewed': course_stats.media_viewed } else: # The user has no activity registered data = { 'user': user, 'user_display': str(user), 'no_quizzes_completed': 0, 'pretest_score': 0, 'no_activities_completed': 0, 'no_points': 0, 'no_badges': 0, 'no_media_viewed': 0 } students.append(data) order_options = [ 'user_display', 'no_quizzes_completed', 'pretest_score', 'no_activities_completed', 'no_points', 'no_badges', 'no_media_viewed' ] default_order = 'pretest_score' ordering = request.GET.get('order_by', default_order) inverse_order = ordering.startswith('-') if inverse_order: ordering = ordering[1:] if ordering not in order_options: ordering = default_order inverse_order = False students.sort(key=operator.itemgetter(ordering), reverse=inverse_order) return render( request, 'cohort/course-activity.html', { 'course': course, 'cohort': cohort, 'course_media_count': media_count, 'activity_graph_data': student_activity, 'page_ordering': ('-' if inverse_order else '') + ordering, 'students': students })
def test_func(self): return can_view_cohort(self.request, self.kwargs['pk'])
def cohort_course_view(request, cohort_id, course_id): cohort = can_view_cohort(request, cohort_id) try: course = Course.objects.get(pk=course_id, coursecohort__cohort=cohort) except Course.DoesNotExist: raise Http404() start_date = timezone.now() - datetime.timedelta(days=31) end_date = timezone.now() student_activity = [] no_days = (end_date - start_date).days + 1 users = User.objects.filter(participant__role=Participant.STUDENT, participant__cohort=cohort).order_by( 'first_name', 'last_name') trackers = Tracker.objects.filter(course=course, user__is_staff=False, user__in=users, tracker_date__gte=start_date, tracker_date__lte=end_date) \ .annotate(day=TruncDay('tracker_date'), month=TruncMonth('tracker_date'), year=TruncYear('tracker_date')) \ .values('day') \ .annotate(count=Count('id')) for i in range(0, no_days, +1): temp = start_date + datetime.timedelta(days=i) temp_date = temp.date().strftime(STR_DATE_FORMAT) count = next((dct['count'] for dct in trackers if dct['day'].strftime(STR_DATE_FORMAT) == temp_date), 0) student_activity.append([temp.strftime(STR_DATE_FORMAT), count]) students = [] media_count = course.get_no_media() for user in users: course_stats = UserCourseSummary.objects.filter(user=user, course=course_id) if course_stats: course_stats = course_stats[0] if course_stats.pretest_score: pretest_score = course_stats.pretest_score else: pretest_score = 0 data = { 'user': user, 'user_display': str(user), 'no_quizzes_completed': course_stats.quizzes_passed, 'pretest_score': pretest_score, 'no_activities_completed': course_stats.completed_activities, 'no_points': course_stats.points, 'no_badges': course_stats.badges_achieved, 'no_media_viewed': course_stats.media_viewed } else: # The user has no activity registered data = { 'user': user, 'user_display': str(user), 'no_quizzes_completed': 0, 'pretest_score': 0, 'no_activities_completed': 0, 'no_points': 0, 'no_badges': 0, 'no_media_viewed': 0 } students.append(data) order_options = [ 'user_display', 'no_quizzes_completed', 'pretest_score', 'no_activities_completed', 'no_points', 'no_badges', 'no_media_viewed' ] default_order = 'pretest_score' ordering = request.GET.get('order_by', default_order) inverse_order = ordering.startswith('-') if inverse_order: ordering = ordering[1:] if ordering not in order_options: ordering = default_order inverse_order = False students.sort(key=operator.itemgetter(ordering), reverse=inverse_order) return render( request, 'cohort/course-activity.html', { 'course': course, 'cohort': cohort, 'course_media_count': media_count, 'activity_graph_data': student_activity, 'page_ordering': ('-' if inverse_order else '') + ordering, 'students': students })