def handle(self, *args, **options):

        course_id = options.get('course_id')
        course_ids = []

        if course_id:
            course_ids.append(course_id)
        else:
            course_ids = StudentGradebook.objects.filter(
                grade_summary='').values_list('course_id', flat=True)

        for course_id in course_ids:
            course_key = CourseKey.from_string(course_id)
            users = CourseEnrollment.objects.users_enrolled_in(course_key)
            course = modulestore().get_course(course_key, depth=None)
            if course:
                # For each user...
                for user in users:
                    request = RequestMockWithoutMiddleware().get('/')
                    request.user = user
                    grade_data = grades.grade(user, course)
                    grade = grade_data['percent']
                    grading_policy = course.grading_policy
                    proforma_grade = grades.calculate_proforma_grade(
                        grade_data, grading_policy)
                    progress_summary = grades.progress_summary(user, course)
                    try:
                        gradebook_entry = StudentGradebook.objects.get(
                            user=user, course_id=course.id)
                        if not gradebook_entry.grade_summary:
                            gradebook_entry.grade = grade
                            gradebook_entry.proforma_grade = proforma_grade
                            gradebook_entry.progress_summary = json.dumps(
                                progress_summary, cls=EdxJSONEncoder)
                            gradebook_entry.grade_summary = json.dumps(
                                grade_data, cls=EdxJSONEncoder)
                            gradebook_entry.grading_policy = json.dumps(
                                grading_policy, cls=EdxJSONEncoder)
                            gradebook_entry.save()
                    except StudentGradebook.DoesNotExist:
                        pass

                    log_msg = 'Gradebook entry created -- Course: {}, User: {}  (grade: {}, proforma_grade: {})'.format(
                        course.id, user.id, grade, proforma_grade)
                    log.info(log_msg)
def _generate_user_gradebook(course_key, user):
    """
    Recalculates the specified user's gradebook entry
    """
    # import is local to avoid recursive import
    from courseware.courses import get_course
    course_descriptor = get_course(course_key, depth=None)
    grading_policy = course_descriptor.grading_policy
    request = RequestMockWithoutMiddleware().get('/')
    request.user = user
    request.course_descriptor = course_descriptor
    progress_summary = progress_summary_wrapped(request, course_id)
    log.info(progress_summary)
    grade_summary = grades.grade(user, course_descriptor)
    grade = grade_summary['percent']
    proforma_grade = grades.calculate_proforma_grade(grade_summary,
                                                     grading_policy)

    try:
        gradebook_entry = StudentGradebook.objects.get(user=user,
                                                       course_id=course_key)
        if gradebook_entry.grade != grade:
            gradebook_entry.grade = grade
            gradebook_entry.proforma_grade = proforma_grade
            gradebook_entry.progress_summary = json.dumps(progress_summary,
                                                          cls=EdxJSONEncoder)
            gradebook_entry.grade_summary = json.dumps(grade_summary,
                                                       cls=EdxJSONEncoder)
            gradebook_entry.grading_policy = json.dumps(grading_policy,
                                                        cls=EdxJSONEncoder)
            gradebook_entry.save()
    except StudentGradebook.DoesNotExist:
        StudentGradebook.objects.create(
            user=user,
            course_id=course_key,
            grade=grade,
            proforma_grade=proforma_grade,
            progress_summary=json.dumps(progress_summary, cls=EdxJSONEncoder),
            grade_summary=json.dumps(grade_summary, cls=EdxJSONEncoder),
            grading_policy=json.dumps(grading_policy, cls=EdxJSONEncoder))
Exemple #3
0
def on_score_changed(sender, **kwargs):
    """
    Listens for a 'score_changed' signal and when observed
    recalculates the specified user's gradebook entry
    """
    from courseware.views import get_course
    user = kwargs['user']
    course_key = kwargs['course_key']
    course_descriptor = get_course(course_key, depth=None)
    request = RequestMockWithoutMiddleware().get('/')
    request.user = user
    progress_summary = grades.progress_summary(user, request, course_descriptor, locators_as_strings=True)
    grade_summary = grades.grade(user, request, course_descriptor)
    grading_policy = course_descriptor.grading_policy
    grade = grade_summary['percent']
    proforma_grade = grades.calculate_proforma_grade(grade_summary, grading_policy)

    try:
        gradebook_entry = StudentGradebook.objects.get(user=user, course_id=course_key)
        if gradebook_entry.grade != grade:
            gradebook_entry.grade = grade
            gradebook_entry.proforma_grade = proforma_grade
            gradebook_entry.progress_summary = json.dumps(progress_summary, cls=EdxJSONEncoder)
            gradebook_entry.grade_summary = json.dumps(grade_summary, cls=EdxJSONEncoder)
            gradebook_entry.grading_policy = json.dumps(grading_policy, cls=EdxJSONEncoder)
            gradebook_entry.save()
    except StudentGradebook.DoesNotExist:
        StudentGradebook.objects.create(
            user=user,
            course_id=course_key,
            grade=grade,
            proforma_grade=proforma_grade,
            progress_summary=json.dumps(progress_summary, cls=EdxJSONEncoder),
            grade_summary=json.dumps(grade_summary, cls=EdxJSONEncoder),
            grading_policy=json.dumps(grading_policy, cls=EdxJSONEncoder)
        )
    def handle(self, *args, **options):
        help = "Command to create or update gradebook entries"
        option_list = BaseCommand.option_list + (
            make_option(
                "-c",
                "--course_ids",
                dest="course_ids",
                help="List of courses for which to generate grades",
                metavar="first/course/id,second/course/id"
            ),
            make_option(
                "-u",
                "--user_ids",
                dest="user_ids",
                help="List of users for which to generate grades",
                metavar="1234,2468,3579"
            ),
        )

        course_ids = options.get('course_ids')
        user_ids = options.get('user_ids')

        # Get the list of courses from the system
        courses = modulestore().get_courses()

        # If one or more courses were specified by the caller, just use those ones...
        if course_ids is not None:
            filtered_courses = []
            for course in courses:
                if unicode(course.id) in course_ids.split(','):
                    filtered_courses.append(course)
            courses = filtered_courses

        for course in courses:
            users = CourseEnrollment.objects.users_enrolled_in(course.id)
            # If one or more users were specified by the caller, just use those ones...
            if user_ids is not None:
                filtered_users = []
                for user in users:
                    if str(user.id) in user_ids.split(','):
                        filtered_users.append(user)
                users = filtered_users

            # For each user...
            for user in users:
                request = RequestMockWithoutMiddleware().get('/')
                request.user = user
                grade_data = grades.grade(user, request, course)
                grade = grade_data['percent']
                grading_policy = course.grading_policy
                proforma_grade = grades.calculate_proforma_grade(grade_data, grading_policy)
                progress_summary = grades.progress_summary(user, request, course)
                try:
                    gradebook_entry = StudentGradebook.objects.get(user=user, course_id=course.id)
                    if (gradebook_entry.grade != grade or
                            gradebook_entry.proforma_grade != proforma_grade or
                            gradebook_entry.progress_summary != progress_summary or
                            gradebook_entry.grade_summary != grade_data or
                            gradebook_entry.grading_policy != grading_policy):
                        gradebook_entry.grade = grade
                        gradebook_entry.proforma_grade = proforma_grade
                        gradebook_entry.progress_summary = json.dumps(progress_summary, cls=EdxJSONEncoder)
                        gradebook_entry.grade_summary = json.dumps(grade_data, cls=EdxJSONEncoder)
                        gradebook_entry.grading_policy = json.dumps(grading_policy, cls=EdxJSONEncoder)
                        gradebook_entry.save()
                except StudentGradebook.DoesNotExist:
                    StudentGradebook.objects.create(
                        user=user,
                        course_id=course.id,
                        grade=grade,
                        proforma_grade=proforma_grade,
                        progress_summary=json.dumps(progress_summary, cls=EdxJSONEncoder),
                        grade_summary=json.dumps(grade_data, cls=EdxJSONEncoder),
                        grading_policy=json.dumps(grading_policy, cls=EdxJSONEncoder)
                    )
                log_msg = 'Gradebook entry created -- Course: {}, User: {}  (grade: {}, proforma_grade: {})'.format(course.id, user.id, grade, proforma_grade)
                print log_msg
                log.info(log_msg)