def create_section_form(request):
    try:
        canvas_course_id = request.LTI['custom_canvas_course_id']
        course_instance_id = request.LTI['lis_course_offering_sourcedid']
        if course_instance_id == '' or course_instance_id is None:
            logger.error(
                'CID unavailable for course %s' % canvas_course_id
            )
            return render(request, 'manage_sections/error.html', status=500)
        sis_enrollment_section_list = []  # Sections fed from SIS
        section_list = []  # Sections not fed from SIS
        canvas_sections = canvas_api_helper_sections.get_sections(canvas_course_id)
        if not canvas_sections:
            logger.error(
                'No sections found for Canvas course %s' % canvas_course_id
            )
            return render(request, 'manage_sections/error.html', status=500)
        for section in canvas_sections:

            section['enrollment_count'] = len(_filter_student_view_enrollments(section['enrollments']))
            sis_section_id = section.get('sis_section_id')
            if sis_section_id == course_instance_id or sis_section_id == "ci:%s" % course_instance_id:
                # this matches the current course instance id and placed first on the list
                sis_enrollment_section_list.insert(0, section)
            elif is_enrollment_section(sis_section_id) or is_credit_status_section(sis_section_id):
                sis_enrollment_section_list.append(section)
            else:
                if is_sis_section(sis_section_id):
                    section['registrar_section_flag'] = True
                section_list.append(section)

        # case insensitive sort the sections in alpha order
        section_list = sorted(section_list, key=lambda x: x[u'name'].lower())
        
        return render(request, 'manage_sections/create_section_form.html', {
            'sections': section_list,
            'sisenrollmentsections': sis_enrollment_section_list
        })

    except Exception:
        logger.exception('Exception in create_section_form')
        return render(request, 'manage_sections/error.html', status=500)
Пример #2
0
def get_sections(canvas_course_id):
    return canvas_api_helper_sections.get_sections(canvas_course_id)
Пример #3
0
def get_sections(canvas_course_id):
    return canvas_api_helper_sections.get_sections(canvas_course_id)
Пример #4
0
def create_section_form(request):
    try:
        canvas_course_id = request.LTI['custom_canvas_course_id']
        course_instance_id = request.LTI['lis_course_offering_sourcedid']
        if course_instance_id == '' or course_instance_id is None:
            logger.error(
                'CID unavailable for course %s' % canvas_course_id
            )
            return render(request, 'manage_sections/error.html', status=500)
        sis_enrollment_section_list = []  # Sections fed from SIS
        section_list = []  # Sections not fed from SIS

        # fetch total_students_size for the course
        kwargs = {}
        kwargs['include'] = 'total_students'
        start = time.time()
        course = canvas_api_helper_courses.get_course(canvas_course_id, **kwargs)
        logger.debug('Total time for get_course is ={} for course {}'.format(time.time() - start, canvas_course_id))
        total_students_size = 0
        if course:
            if 'total_students' not in course:
                logger.debug('total_students not in cached object, flushing and refetching object..')
                # this is probably  a cached object without the count. Clear the cache object and re-fetch the course
                canvas_api_helper_courses.delete_cache(canvas_course_id=canvas_course_id)
                course = canvas_api_helper_courses.get_course(canvas_course_id, **kwargs)

            if course and 'total_students' in course:
                total_students_size = course['total_students']

        logger.debug('total_students_size={}'.format(total_students_size))


        # If the size > 300, due to performnace issues for larger courses,  do not fetch enrollments
        if total_students_size > 300:
            canvas_sections = canvas_api_helper_sections.get_sections(canvas_course_id, fetch_enrollments=False)
        else:
            canvas_sections = canvas_api_helper_sections.get_sections(canvas_course_id, fetch_enrollments=True)
        if not canvas_sections:
            logger.error(
                'No sections found for Canvas course %s' % canvas_course_id
            )
            return render(request, 'manage_sections/error.html', status=500)
        for section in canvas_sections:
            if 'enrollments' in section:
                section['enrollment_count'] = len(_filter_student_view_enrollments(section['enrollments']))
            else:
                section['enrollment_count'] = 'n/a'
            sis_section_id = section.get('sis_section_id')
            if sis_section_id == course_instance_id or sis_section_id == "ci:%s" % course_instance_id:
                # this matches the current course instance id and placed first on the list
                sis_enrollment_section_list.insert(0, section)
            elif is_enrollment_section(sis_section_id) or is_credit_status_section(sis_section_id):
                sis_enrollment_section_list.append(section)
            else:
                if is_sis_section(sis_section_id):
                    section['registrar_section_flag'] = True
                section_list.append(section)

        # case insensitive sort the sections in alpha order
        section_list = sorted(section_list, key=lambda x: x['name'].lower())

        return render(request, 'manage_sections/create_section_form.html', {
            'sections': section_list,
            'sisenrollmentsections': sis_enrollment_section_list
        })

    except Exception:
        logger.exception('Exception in create_section_form')
        return render(request, 'manage_sections/error.html', status=500)