## Script (Python) "getRetStudentStatistics"
##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
"""
import logging
logger = logging.getLogger('Skins.getRetStudentStatistics')

try:
    from Products.AdvancedQuery import Eq, Between, Le,In, Ge
    aq_portal = context.portal_catalog.evalAdvancedQuery
    aq_students = context.students_catalog.evalAdvancedQuery
except:
    evalAdvancedQuery = None
logger.info('%s invoked statistics' % context.portal_membership.getAuthenticatedMember())
l = []
if not context.isStaff():
    return l
dep = {}
dep['id'] = "All Faculties"


ret_res1 = context.portal_catalog(review_state = 'returning')
ret_ids1 = [r.getId for r in ret_res1]
total_ret = len(ret_ids1)

ret_res2 = context.portal_catalog(review_state = ('school_fee_paid','courses_registered','courses_validated',))
ret_ids2 = [r.getId for r in ret_res2]

retquery = Ge('level','300') | (Eq('level','200') & Eq('entry_mode','UME'))
ret_res3 = aq_students(retquery)
ret_ids3 = [r.id for r in ret_res3]

l.append(dep)
sum_ret = 0
sum_sfp = 0
fs = context.portal_catalog(portal_type="Faculty")
for fid in [f.getId for f in fs]:
    dep = {}
    dep['id'] = fid
    fquery = Eq('faculty',fid)
    stud_res = aq_students(fquery)
    stud_ids = [r.id for r in stud_res]
    total = float(len(stud_res))
    if total == 0:
        continue
    sfp = len([s for s in stud_ids if s in ret_ids2 and s in ret_ids3])
    dep['sfp'] = sfp
    ret = len([s for s in stud_ids if s in ret_ids1])
    dep['ret'] = ret
    sum_ret += ret
    sum_sfp += sfp

    l.append(dep)

l[0]['ret'] = sum_ret
l[0]['sfp'] = sum_sfp
l[0]['total_ret'] = total_ret + sum_sfp
l[0]['total_li'] = sum_ret + sum_sfp


return l

