## Script (Python) "getAccoHallInfo"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=student=None
##title=
##
# $Id: getAccoHallInfo.py 1269 2007-01-11 17:06:34Z joachim $
"""
return Info about a Accommodation Hall
"""

def cmp_bed(a,b):
    if a['bed'] > b['bed']:
        return 1
    return -1

request = context.REQUEST
#from Products.zdb import set_trace;set_trace()

wf = context.portal_workflow
path_info = request.get('PATH_INFO').split('/')
mtool = context.portal_membership
pa = context.portal_accommodation
info = {}
#dep_id = request.get('PATH_TRANSLATED').split('/')[-2]
dep_id = context.aq_parent.getId()
info['action'] = "%s" % context.absolute_url()
info['choosen_ids'] = request.get('ids',[])
info['doc'] = context.getContent()
bed_types = pa.uniqueValuesFor('bed_type')
bt_list = []
bt_names =  context.getBedTypeNames()
reserved = pa.uniqueValuesFor('student')
for bt in bed_types:
    total = len(pa(bed_type=bt,hall=context.getId()))
    if total <= 1:
        continue
    free = len(pa.searchResults({'bed_type': bt,
                                 'student':reserved,
                                 'hall': context.getId()}))
    bt_list.append({'name': bt_names[bt],
                    'total': total,
                    'reserved': free,
                    })
info['bed_types'] = bt_list
res_list = []
students_url = "%s/campus/students" % (context.portal_url())
if reserved:
    for st in reserved:
        if st:
            res = pa(hall=context.getId(),student=st)
            if not res:
                continue
            d = {}
            d['student'] = st
            d['student_url'] ='%s/%s' % (students_url,st)
            bed = context.formatBed(res[0].bed).split('/')
            d['bed'] = " / ".join(bed[1:])
            erg = context.students_catalog(id = st)
            if erg:
                sd = context.getFormattedStudentEntry(erg[0])
                d.update(sd)
            res_list.append(d)
res_list.sort(cmp_bed)
info['reserved'] = res_list

return info
