Exemplo n.º 1
0
def discussion_countries(request, course_id='all'):
    if api.views.is_cached(request):
        return api.views.api_cacherender(request)
    courses = {}
    if course_id == 'all':
        courses = api.views.get_all_courses()
    else:
        course = api.views.get_course(course_id)
        if course is None:
            return api.views.api_render(request, {'error': 'Unknown course code'}, status.HTTP_404_NOT_FOUND)
        courses[course_id] = course
    #print courses
    DiscussionForum.connect_to_mongo()
    data = {}
    data['country_dict'] = {}
    data['country_enrol'] = {}
    data['post_total'] = 0
    #data['user_total'] = 0
    for course in courses.values():
        df_map = DiscussionForum.post_user_country(course)
        for country, num in df_map['country_dict'].items():
            data['post_total'] += num
            if country in data['country_dict']:
                data['country_dict'][country] += num
            else:
                data['country_dict'][country] = num

        #remove duplicate of user?
        for item in df_map['country_enrol']:
            #data['user_total'] += item['userSum']
            if item['_id'] in data['country_enrol']:
                data['country_enrol'][item['_id']] += item['userSum']
            else:
                data['country_enrol'][item['_id']] = item['userSum']

    data['country_post'] = []
    data['post_max'] = 0
    for country, num in data['country_dict'].items():
        post_percentage = round(num * 100 / float(data['post_total']), 2)
        data['country_post'].append([country, num, post_percentage])
        if post_percentage > data['post_max']:
            data['post_max'] = post_percentage

    data['country_post_enrol'] = []
    data['post_enrol_max'] = 0
    for country, userSum in data['country_enrol'].items():
        if country in data['country_dict']:
            pe_percentage = round(data['country_dict'][country] * 100 / float(userSum), 2)
            post_enrol = [country, data['country_dict'][country], userSum, pe_percentage]
            if pe_percentage > data['post_enrol_max']:
                data['post_enrol_max'] = pe_percentage
        else:
            post_enrol = [country, 0, userSum, 0]
        data['country_post_enrol'].append(post_enrol)

    return api.views.api_render(request, data, status.HTTP_200_OK)
Exemplo n.º 2
0
def discussion_countries(request, course_id='all'):
    if api.views.is_cached(request):
        return api.views.api_cacherender(request)
    courses = {}
    if course_id == 'all' or course_id == 'allcourses':
        courses = api.views.get_all_courses()
    else:
        course = api.views.get_course(course_id)
        if course is None:
            return api.views.api_render(request,
                                        {'error': 'Unknown course code'},
                                        status.HTTP_404_NOT_FOUND)
        courses[course_id] = course
    #print courses
    DiscussionForum.connect_to_mongo()
    data = {}
    data['country_dict'] = {}
    data['country_enrol'] = {}
    data['post_total'] = 0
    #data['user_total'] = 0
    for course in courses.values():
        df_map = DiscussionForum.post_user_country(course)
        for country, num in df_map['country_dict'].items():
            data['post_total'] += num
            if country in data['country_dict']:
                data['country_dict'][country] += num
            else:
                data['country_dict'][country] = num

        #remove duplicate of user?
        for item in df_map['country_enrol']:
            #data['user_total'] += item['userSum']
            if item['_id'] in data['country_enrol']:
                data['country_enrol'][item['_id']] += item['userSum']
            else:
                data['country_enrol'][item['_id']] = item['userSum']

    data['country_post'] = []
    data['post_max'] = 0
    for country, num in data['country_dict'].items():
        post_percentage = round(num * 100 / float(data['post_total']), 2)
        data['country_post'].append([country, num, post_percentage])
        if post_percentage > data['post_max']:
            data['post_max'] = post_percentage

    data['country_post_enrol'] = []
    data['post_enrol_max'] = 0
    for country, userSum in data['country_enrol'].items():
        if country in data['country_dict']:
            pe_percentage = round(
                data['country_dict'][country] * 100 / float(userSum), 2)
            post_enrol = [
                country, data['country_dict'][country], userSum, pe_percentage
            ]
            if pe_percentage > data['post_enrol_max']:
                data['post_enrol_max'] = pe_percentage
        else:
            post_enrol = [country, 0, userSum, 0]
        data['country_post_enrol'].append(post_enrol)

    return api.views.api_render(request, data, status.HTTP_200_OK)