## Script (Python) "getNewStudentStatistics"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=
##
# $Id: getStudentStatistics.py 1277 2007-01-11 21:11:37Z joachim $
"""
return Student Statistics
"""
try:
    from Products.zdb import set_trace
except:
    def set_trace():
        pass
import logging
logger = logging.getLogger('Skins.getNewStudentStatistics')

try:
    from Products.AdvancedQuery import Eq, Between, Le,In, Ge,MatchRegexp
    aq_portal = context.portal_catalog.evalAdvancedQuery
    aq_students = context.students_catalog.evalAdvancedQuery
except:
    evalAdvancedQuery = None

def intersect(m,n):
    return [i for i in m if i in n]
    
logger.info('%s invoked statistics' % context.portal_membership.getAuthenticatedMember())
if not context.isStaff():
    return l
total_dict = {}

total_dict['id'] = "All Faculties"

##freshquery = Eq('level','100') | (Eq('level','200') & Eq('entry_mode','DE'))
##total_new = aq_students(freshquery)
##total_new = float(len(total_new))
newquery = Eq('portal_type','StudentApplication') & MatchRegexp('SearchableText',r'^6*')
total_res = aq_portal(newquery)
new_ids = [r.getPath().split('/')[-2] for r in total_res]
total_new = float(len(total_res))
total_dict['new'] = "%.0f" % total_new
special_states =  ('admitted',
                   'clearance_pin_entered',
                   'clearance_requested',
                   'cleared_and_validated',
                   'objection_raised',
                   )
state_count = 0 
state_ids = {}
for state in special_states:
    res = context.portal_catalog(review_state = state)
    ids = [r.getId for r in res]
    state_ids[state] = ids 
    count = len(res)
    state_count += count
    total_dict[state] = count
    total_dict['%s_percent' % state] =  "%.0f" % round(count*100/total_new)
total_dict['rest'] = "%.0f" % (total_new - state_count)
total_dict['rest_percent'] = "%.0f" % round((total_new - state_count)*100/total_new)
fac_res = {}
#fac_res['total'] = total_dict
faculties = context.portal_catalog(portal_type="Faculty")
for f in faculties:
    dict = {}
    dict['id'] = f.getId
    dict['title'] = f.Title
    sid_in_faculty = [s.id for s in context.students_catalog(faculty=f.getId)]
    new_in_faculty = intersect(new_ids,sid_in_faculty)
    dict['new'] = rest = len(new_in_faculty)
    for state in special_states:
        state_count = len(intersect(state_ids[state], new_in_faculty))
        rest -= state_count
        dict[state] = state_count
    dict['rest'] = rest
    fac_res[f.getId] = dict
#set_trace()    
##for sid,review_state in new_ids:
##    scat_res = context.students_catalog(id = sid)
##    if not scat_res:
##        continue
##    student = scat_res[0]
##    faculty = student.faculty
##    if not fac_res.has_key(faculty):
##        continue
##    if review_state in special_states:
##        fac_res[faculty][review_state] = fac_res[faculty][review_state] + 1
##    else:
##        fac_res[faculty]['rest'] = fac_res[faculty]['rest'] + 1
l = []
l.append(total_dict)
fac_ids = [f.getId for f in faculties]
fac_ids.sort()
check_dict = {}
check_dict['id'] = 'Check'
check_dict['new'] = 0
check_dict['new_percent'] = 0
check_dict['rest'] = 0
check_dict['rest_percent'] = 0
for state in special_states:
    check_dict[state] = 0
    check_dict["%s_percent" % state] = 0
    
for f in fac_ids:
    total_fac = fac_res[f]['new']
    check_dict['new'] = check_dict['new'] + total_fac
    check_dict['rest'] = check_dict['rest'] + fac_res[f]['rest']
    for state in special_states:
        check_dict[state] = check_dict[state] + fac_res[f][state]
        if total_fac != 0:
            fac_res[f]['%s_percent' % state] = "%.0f" % round(fac_res[f][state]*100/total_fac)
        else:
            fac_res[f]['%s_percent' % state] = "%.0f" % 0.0
    if total_fac != 0:
        fac_res[f]['rest_percent'] = "%.0f" % round(fac_res[f]['rest']*100/total_fac)
    else:
        fac_res[f]['rest_percent'] = "%.0f" % 0.0
    l.append(fac_res[f])
l.append(check_dict)    

return l

