def out(line):
    with open(output + "/account_stats.txt", "a") as f:
        f.write(str(line) + "\n")


out(datetime.datetime.today())

has = 0
cannot = 0
last_week = 0
last_month = 0
oneweekago = datetime.datetime.today() - datetime.timedelta(days=7)
onemonthago = datetime.datetime.today() - datetime.timedelta(days=30)
dnet = MoodleDBConnection()
raw = dnet.call_sql("select lastaccess,lastlogin,idnumber from ssismdl_user where idnumber like '%P' and deleted=0")
total = len(raw)

for line in raw:
    lastaccess, lastlogin, idnumber = line
    lastaccess_date = convert(lastaccess)
    lastlogin_date = convert(lastlogin)
    if lastaccess and not lastlogin:
        cannot += 1
    if lastlogin:
        has += 1
    if lastlogin_date > oneweekago:
        last_week += 1
    if lastlogin_date > onemonthago:
        last_month += 1
def name_to_email(long_name):

    try:
        where = long_name.index(')')
    except ValueError:
        where = -1
    where += 1
    long_name = long_name[where:].strip().lower()
    return re.sub('[^a-z]', '', long_name)
    

if __name__ == "__main__":

    students = Tree()
    
    db = MoodleDBConnection()
    sf = NS()

    results = db.get_all_users_activity_enrollments()
    sf.domain = 'student.ssis-suzhou.net'
    sf.AT = '@'

    from collections import defaultdict
    postfix = defaultdict(list)
    activities = defaultdict(list)
    homerooms = defaultdict( lambda : defaultdict(list) )

    # PARSE RESULTS
    for result in results:
        activity_name, student_key = result
        student = students.get_student(student_key)