## 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('Student.Statistics')

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 returning statistics"' % context.portal_membership.getAuthenticatedMember())
l = []
if not context.isStaff():
    return l
dep = {}
sum = 0
dep['id'] = "All Faculties"

#from Products.zdb import set_trace;set_trace()

ret_res = context.portal_catalog(review_state ='returning')
ret_ids = [r.getId for r in ret_res]
ret = len(ret_res)
dep['total'] = ret

l.append(dep)
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

    ret = len([s for s in stud_ids if s in ret_ids])
    dep['returned'] = ret
    sum += ret

    if ret == 0:
        continue

    l.append(dep)

l[0]['sum'] = sum

return l

