<commit_before>
# instructions:
#   ---on open edx server
#       cd /edx/app/edxapp/edx-platform
#       source ../edxapp_env
#       python manage.py lms --settings=aws_appsembler shell  
#   
#       inside the django shell, execute this script
#       
#  ---after script has executed
#  scp academy.metalogix.com:/tmp/gradeOutputFull.csv ./gradeOutputFull20160211.csv
#  
#
from xmodule.modulestore.django import modulestore
from django.contrib.auth.models import User
from instructor.utils import DummyRequest
from instructor.views.legacy import get_student_grade_summary_data

import csv 

from django.contrib.auth.models import User
random_user = User.objects.all()[0]

fp = open('/tmp/gradeOutputFull.csv','w')
writer = csv.writer(fp, dialect='excel', quotechar='"', quoting=csv.QUOTE_ALL)
request = DummyRequest()
request.user = random_user

mongo_courses = modulestore().get_courses()

writer.writerow(['#course_id','user_id','username','full_name','email','location','city','country','course_access_group','registered','is_active','last_login','final_score'])
for course in mongo_courses:
#location, city, country; registered, is_active, last_login
#course_id = 'course-v1:Metalogix+EO301+2015'
#course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)
#course = get_course_by_id(course_key)
    get_raw_scores = False
    datatable = get_student_grade_summary_data(request, course, get_raw_scores=get_raw_scores)
    for d in datatable['data']:
        user_id = d[0]
        u = User.objects.get(id=user_id)
        cag = u.courseaccessgroup_set.all()
        if not cag:
            cag = ''
        else:
            cag = cag[0]
        try: 
            loc = u.profile.location
        except Exception:
            loc = ''
        try: 
            p = u.profile   
            city = str(p.city)
            country = str(p.country)
            full_name = str(p.name)
        except Exception: #profile doesn't exist or city isn't ascii
            city = ''
            country = ''
            full_name = ''
        # try:
        #     course_access_group = user.courseaccessgroup_set.all()[0].name #assume there's at least one group and get it
        #     p = user.profile
        # except:
        #     course_access_group = 'None'
        output_data = [course.id, 
                        str(u.id), 
                        u.username, 
                        full_name, 
                        u.email,
                        loc,
                        city,
                        country,
                        str(cag), 
                        str(u.date_joined),
                        u.is_active,
                        str(u.last_login),
                        d[len(d)-1]
                    ]
        encoded_row = [unicode(s).encode('utf-8') for s in output_data]
        #writer.writerow(output_data)
        writer.writerow(encoded_row)

fp.close()

<commit_msg>Add a Course Name field to the Metalogix Grade Report script.<commit_after>
# instructions:
#   ---on open edx server
#       cd /edx/app/edxapp/edx-platform
#       source ../edxapp_env
#       python manage.py lms --settings=aws_appsembler shell  
#   
#       inside the django shell, execute this script
#       
#  ---after script has executed
#  scp academy.metalogix.com:/tmp/gradeOutputFull.csv ./gradeOutputFull20160211.csv
#  
#
from xmodule.modulestore.django import modulestore
from django.contrib.auth.models import User
from instructor.utils import DummyRequest
from instructor.views.legacy import get_student_grade_summary_data

import csv 

from django.contrib.auth.models import User
random_user = User.objects.all()[0]

fp = open('/tmp/gradeOutputFull.csv','w')
writer = csv.writer(fp, dialect='excel', quotechar='"', quoting=csv.QUOTE_ALL)
request = DummyRequest()
request.user = random_user

mongo_courses = modulestore().get_courses()

writer.writerow(['#course_id', 'course_name','user_id','username','full_name','email','location','city','country','course_access_group','registered','is_active','last_login','final_score'])
for course in mongo_courses:
#location, city, country; registered, is_active, last_login
#course_id = 'course-v1:Metalogix+EO301+2015'
#course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)
#course = get_course_by_id(course_key)
    course_name = course.display_name
    get_raw_scores = False
    datatable = get_student_grade_summary_data(request, course, get_raw_scores=get_raw_scores)
    for d in datatable['data']:
        user_id = d[0]
        u = User.objects.get(id=user_id)
        cag = u.courseaccessgroup_set.all()
        if not cag:
            cag = ''
        else:
            cag = cag[0]
        try: 
            loc = u.profile.location
        except Exception:
            loc = ''
        try: 
            p = u.profile   
            city = str(p.city)
            country = str(p.country)
            full_name = str(p.name)
        except Exception: #profile doesn't exist or city isn't ascii
            city = ''
            country = ''
            full_name = ''
        # try:
        #     course_access_group = user.courseaccessgroup_set.all()[0].name #assume there's at least one group and get it
        #     p = user.profile
        # except:
        #     course_access_group = 'None'
        output_data = [course.id,
                       course_name,
                        str(u.id), 
                        u.username, 
                        full_name, 
                        u.email,
                        loc,
                        city,
                        country,
                        str(cag), 
                        str(u.date_joined),
                        u.is_active,
                        str(u.last_login),
                        d[len(d)-1]
                    ]
        encoded_row = [unicode(s).encode('utf-8') for s in output_data]
        #writer.writerow(output_data)
        writer.writerow(encoded_row)

fp.close()


{'fraction_non_alphanumeric': 0.10635818908122503, 'fraction_numerical': 0.007323568575233023, 'mean_word_length': 2.0317860746720484, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2271157', 'n_tokens_mistral': 1863, 'n_tokens_neox': 1761, 'n_words': 360}