Esempio n. 1
0
    def get(self, request, course_id):
        """
        Gets a course progress status.

        Args:
            request (Request): Django request object.
            course_id (string): URI element specifying the course location.

        Return:
            A JSON serialized representation of the requesting user's current grade status.
        """

        course = self._get_course(course_id, request.user, 'load')
        if isinstance(course, Response):
            # Returns a 404 if course_id is invalid, or request.user is not enrolled in the course
            return course

        grade_user = self._get_effective_user(request, course)
        if isinstance(grade_user, Response):
            # Returns a 403 if the request.user can't access grades for the requested user,
            # or a 404 if the requested user does not exist.
            return grade_user

        prep_course_for_grading(course, request)
        course_grade = CourseGradeFactory().create(grade_user, course)
        return Response([{
            'username': grade_user.username,
            'course_key': course_id,
            'passed': course_grade.passed,
            'percent': course_grade.percent,
            'letter_grade': course_grade.letter_grade,
        }])
Esempio n. 2
0
def ccx_gradebook(request, course, ccx=None):
    """
    Show the gradebook for this CCX.
    """
    if not ccx:
        raise Http404

    ccx_key = CCXLocator.from_course_locator(course.id, ccx.id)
    with ccx_course(ccx_key) as course:
        prep_course_for_grading(course, request)
        student_info, page = get_grade_book_page(request,
                                                 course,
                                                 course_key=ccx_key)

        return render_to_response(
            'courseware/gradebook.html', {
                'page':
                page,
                'page_url':
                reverse('ccx_gradebook', kwargs={'course_id': ccx_key}),
                'students':
                student_info,
                'course':
                course,
                'course_id':
                course.id,
                'staff_access':
                request.user.is_staff,
                'ordered_grades':
                sorted(course.grade_cutoffs.items(),
                       key=lambda i: i[1],
                       reverse=True),
            })
Esempio n. 3
0
def ccx_gradebook(request, course, ccx=None):
    """
    Show the gradebook for this CCX.
    """
    if not ccx:
        raise Http404

    ccx_key = CCXLocator.from_course_locator(course.id, unicode(ccx.id))
    with ccx_course(ccx_key) as course:
        prep_course_for_grading(course, request)
        student_info, page = get_grade_book_page(
            request, course, course_key=ccx_key)

        return render_to_response(
            'courseware/gradebook.html', {
                'page':
                page,
                'page_url':
                reverse('ccx_gradebook', kwargs={'course_id': ccx_key}),
                'students':
                student_info,
                'course':
                course,
                'course_id':
                course.id,
                'staff_access':
                request.user.is_staff,
                'ordered_grades':
                sorted(
                    course.grade_cutoffs.items(),
                    key=lambda i: i[1],
                    reverse=True),
            })
Esempio n. 4
0
    def get(self, request, course_id):
        """
        Gets a course progress status.

        Args:
            request (Request): Django request object.
            course_id (string): URI element specifying the course location.

        Return:
            A JSON serialized representation of the requesting user's current grade status.
        """

        course = self._get_course(course_id, request.user, 'load')
        if isinstance(course, Response):
            # Returns a 404 if course_id is invalid, or request.user is not enrolled in the course
            return course

        grade_user = self._get_effective_user(request, course)
        if isinstance(grade_user, Response):
            # Returns a 403 if the request.user can't access grades for the requested user,
            # or a 404 if the requested user does not exist.
            return grade_user

        prep_course_for_grading(course, request)
        course_grade = CourseGradeFactory().create(grade_user, course)
        return Response([{
            'username': grade_user.username,
            'course_key': course_id,
            'passed': course_grade.passed,
            'percent': course_grade.percent,
            'letter_grade': course_grade.letter_grade,
        }])
Esempio n. 5
0
def ccx_grades_csv(request, course, ccx=None):
    """
    Download grades as CSV.
    """
    if not ccx:
        raise Http404

    ccx_key = CCXLocator.from_course_locator(course.id, ccx.id)
    with ccx_course(ccx_key) as course:
        prep_course_for_grading(course, request)

        enrolled_students = User.objects.filter(
            courseenrollment__course_id=ccx_key,
            courseenrollment__is_active=1).order_by('username').select_related(
                "profile")
        grades = iterate_grades_for(course, enrolled_students)

        header = None
        rows = []
        for student, gradeset, __ in grades:
            if gradeset:
                # We were able to successfully grade this student for this
                # course.
                if not header:
                    # Encode the header row in utf-8 encoding in case there are
                    # unicode characters
                    header = [
                        section['label'].encode('utf-8')
                        for section in gradeset[u'section_breakdown']
                    ]
                    rows.append(["id", "email", "username", "grade"] + header)

                percents = {
                    section['label']: section.get('percent', 0.0)
                    for section in gradeset[u'section_breakdown']
                    if 'label' in section
                }

                row_percents = [percents.get(label, 0.0) for label in header]
                rows.append([
                    student.id, student.email, student.username,
                    gradeset['percent']
                ] + row_percents)

        buf = StringIO()
        writer = csv.writer(buf)
        for row in rows:
            writer.writerow(row)

        response = HttpResponse(buf.getvalue(), content_type='text/csv')
        response['Content-Disposition'] = 'attachment'

        return response
Esempio n. 6
0
def ccx_grades_csv(request, course, ccx=None):
    """
    Download grades as CSV.
    """
    if not ccx:
        raise Http404

    ccx_key = CCXLocator.from_course_locator(course.id, unicode(ccx.id))
    with ccx_course(ccx_key) as course:
        prep_course_for_grading(course, request)

        enrolled_students = User.objects.filter(
            courseenrollment__course_id=ccx_key,
            courseenrollment__is_active=1).order_by('username').select_related(
                "profile")
        grades = CourseGradeFactory().iter(course, enrolled_students)

        header = None
        rows = []
        for student, course_grade, __ in grades:
            if course_grade:
                # We were able to successfully grade this student for this
                # course.
                if not header:
                    # Encode the header row in utf-8 encoding in case there are
                    # unicode characters
                    header = [
                        section['label'].encode('utf-8') for section in
                        course_grade.summary[u'section_breakdown']
                    ]
                    rows.append(["id", "email", "username", "grade"] + header)

                percents = {
                    section['label']: section.get('percent', 0.0)
                    for section in course_grade.summary[u'section_breakdown']
                    if 'label' in section
                }

                row_percents = [percents.get(label, 0.0) for label in header]
                rows.append([
                    student.id, student.email, student.username, course_grade.
                    percent
                ] + row_percents)

        buf = StringIO()
        writer = csv.writer(buf)
        for row in rows:
            writer.writerow(row)

        response = HttpResponse(buf.getvalue(), content_type='text/csv')
        response['Content-Disposition'] = 'attachment'

        return response
Esempio n. 7
0
def ccx_grades_csv(request, course, ccx=None):
    """
    Download grades as CSV.
    """
    if not ccx:
        raise Http404

    ccx_key = CCXLocator.from_course_locator(course.id, ccx.id)
    with ccx_course(ccx_key) as course:
        prep_course_for_grading(course, request)

        enrolled_students = (
            User.objects.filter(courseenrollment__course_id=ccx_key, courseenrollment__is_active=1)
            .order_by("username")
            .select_related("profile")
        )
        grades = iterate_grades_for(course, enrolled_students)

        header = None
        rows = []
        for student, gradeset, __ in grades:
            if gradeset:
                # We were able to successfully grade this student for this
                # course.
                if not header:
                    # Encode the header row in utf-8 encoding in case there are
                    # unicode characters
                    header = [section["label"].encode("utf-8") for section in gradeset[u"section_breakdown"]]
                    rows.append(["id", "email", "username", "grade"] + header)

                percents = {
                    section["label"]: section.get("percent", 0.0)
                    for section in gradeset[u"section_breakdown"]
                    if "label" in section
                }

                row_percents = [percents.get(label, 0.0) for label in header]
                rows.append([student.id, student.email, student.username, gradeset["percent"]] + row_percents)

        buf = StringIO()
        writer = csv.writer(buf)
        for row in rows:
            writer.writerow(row)

        response = HttpResponse(buf.getvalue(), content_type="text/csv")
        response["Content-Disposition"] = "attachment"

        return response
Esempio n. 8
0
    def get(self, request, course_id):
        """
        Gets a course progress status.

        Args:
            request (Request): Django request object.
            course_id (string): URI element specifying the course location.

        Return:
            A JSON serialized representation of the requesting user's current grade status.
        """
        username = request.GET.get('username')

        # only the student can access her own grade status info
        if request.user.username != username:
            log.info('User %s tried to access the grade for user %s.',
                     request.user.username, username)
            return self.make_error_response(
                status_code=status.HTTP_404_NOT_FOUND,
                developer_message=
                'The user requested does not match the logged in user.',
                error_code='user_mismatch')

        course = self._get_course(course_id, request.user, 'load')
        if isinstance(course, Response):
            return course

        prep_course_for_grading(course, request)
        course_grade = CourseGradeFactory(request.user).create(course)
        if not course_grade.has_access_to_course:
            # This means the student didn't have access to the course
            log.info('User %s not allowed to access grade for course %s',
                     request.user.username, username)
            return self.make_error_response(
                status_code=status.HTTP_403_FORBIDDEN,
                developer_message=
                'The user does not have access to the course.',
                error_code='user_does_not_have_access_to_course')

        return Response([{
            'username': username,
            'course_key': course_id,
            'passed': course_grade.passed,
            'percent': course_grade.percent,
            'letter_grade': course_grade.letter_grade,
        }])
Esempio n. 9
0
    def get(self, request, course_id):
        """
        Gets a course progress status.

        Args:
            request (Request): Django request object.
            course_id (string): URI element specifying the course location.

        Return:
            A JSON serialized representation of the requesting user's current grade status.
        """
        username = request.GET.get('username')

        # only the student can access her own grade status info
        if request.user.username != username:
            log.info(
                'User %s tried to access the grade for user %s.',
                request.user.username,
                username
            )
            return self.make_error_response(
                status_code=status.HTTP_404_NOT_FOUND,
                developer_message='The user requested does not match the logged in user.',
                error_code='user_mismatch'
            )

        course = self._get_course(course_id, request.user, 'load')
        if isinstance(course, Response):
            return course

        prep_course_for_grading(course, request)
        course_grade = CourseGradeFactory(request.user).create(course)

        return Response([{
            'username': username,
            'course_key': course_id,
            'passed': course_grade.passed,
            'percent': course_grade.percent,
            'letter_grade': course_grade.letter_grade,
        }])
Esempio n. 10
0
def ccx_gradebook(request, course, ccx=None):
    """
    Show the gradebook for this CCX.
    """
    if not ccx:
        raise Http404

    ccx_key = CCXLocator.from_course_locator(course.id, ccx.id)
    with ccx_course(ccx_key) as course:
        prep_course_for_grading(course, request)
        student_info, page = get_grade_book_page(request, course, course_key=ccx_key)

        return render_to_response(
            "courseware/gradebook.html",
            {
                "page": page,
                "page_url": reverse("ccx_gradebook", kwargs={"course_id": ccx_key}),
                "students": student_info,
                "course": course,
                "course_id": course.id,
                "staff_access": request.user.is_staff,
                "ordered_grades": sorted(course.grade_cutoffs.items(), key=lambda i: i[1], reverse=True),
            },
        )
Esempio n. 11
0
    def get(self, request, course_id):
        """
        Gets a course progress status.

        Args:
            request (Request): Django request object.
            course_id (string): URI element specifying the course location.

        Return:
            A JSON serialized representation of the requesting user's current grade status.
        """
        username = request.GET.get('username')

        # only the student can access her own grade status info
        if request.user.username != username:
            log.info('User %s tried to access the grade for user %s.',
                     request.user.username, username)
            return self.make_error_response(
                status_code=status.HTTP_404_NOT_FOUND,
                developer_message=
                'The user requested does not match the logged in user.',
                error_code='user_mismatch')

        # build the course key
        try:
            course_key = CourseKey.from_string(course_id)
        except InvalidKeyError:
            return self.make_error_response(
                status_code=status.HTTP_404_NOT_FOUND,
                developer_message='The provided course key cannot be parsed.',
                error_code='invalid_course_key')
        # load the course
        try:
            course = courses.get_course_with_access(request.user,
                                                    'load',
                                                    course_key,
                                                    depth=None,
                                                    check_if_enrolled=True)
        except Http404:
            log.info('Course with ID "%s" not found', course_id)
            return self.make_error_response(
                status_code=status.HTTP_404_NOT_FOUND,
                developer_message='The user, the course or both do not exist.',
                error_code='user_or_course_does_not_exist')
        prep_course_for_grading(course, request)
        course_grade = CourseGradeFactory(request.user).create(course)
        if not course_grade.has_access_to_course:
            # This means the student didn't have access to the course
            log.info('User %s not allowed to access grade for course %s',
                     request.user.username, username)
            return self.make_error_response(
                status_code=status.HTTP_403_FORBIDDEN,
                developer_message=
                'The user does not have access to the course.',
                error_code='user_does_not_have_access_to_course')

        return Response([{
            'username': username,
            'course_key': course_id,
            'passed': course_grade.passed,
            'percent': course_grade.percent,
            'letter_grade': course_grade.letter_grade,
        }])
Esempio n. 12
0
    def get(self, request, course_id):
        """
        Gets a course progress status.

        Args:
            request (Request): Django request object.
            course_id (string): URI element specifying the course location.

        Return:
            A JSON serialized representation of the requesting user's current grade status.
        """
        username = request.GET.get('username')

        # only the student can access her own grade status info
        if request.user.username != username:
            log.info(
                'User %s tried to access the grade for user %s.',
                request.user.username,
                username
            )
            return self.make_error_response(
                status_code=status.HTTP_404_NOT_FOUND,
                developer_message='The user requested does not match the logged in user.',
                error_code='user_mismatch'
            )

        # build the course key
        try:
            course_key = CourseKey.from_string(course_id)
        except InvalidKeyError:
            return self.make_error_response(
                status_code=status.HTTP_404_NOT_FOUND,
                developer_message='The provided course key cannot be parsed.',
                error_code='invalid_course_key'
            )
        # load the course
        try:
            course = courses.get_course_with_access(
                request.user,
                'load',
                course_key,
                depth=None,
                check_if_enrolled=True
            )
        except Http404:
            log.info('Course with ID "%s" not found', course_id)
            return self.make_error_response(
                status_code=status.HTTP_404_NOT_FOUND,
                developer_message='The user, the course or both do not exist.',
                error_code='user_or_course_does_not_exist'
            )
        prep_course_for_grading(course, request)
        course_grade = CourseGradeFactory(request.user).create(course)
        if not course_grade.has_access_to_course:
            # This means the student didn't have access to the course
            log.info('User %s not allowed to access grade for course %s', request.user.username, username)
            return self.make_error_response(
                status_code=status.HTTP_403_FORBIDDEN,
                developer_message='The user does not have access to the course.',
                error_code='user_does_not_have_access_to_course'
            )

        return Response([{
            'username': username,
            'course_key': course_id,
            'passed': course_grade.passed,
            'percent': course_grade.percent,
            'letter_grade': course_grade.letter_grade,
        }])