## Script (Python) "cpsdocument_edit"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=
##
# $Id: student_edit.py 486 2006-09-06 10:09:39Z joachim $
"""
return Info about the current Student
"""
#from Products.ZCTextIndex.QueryParser import ParseError,QueryError
request = context.REQUEST
form = request.form
fget = form.get
info = {}
wf = context.portal_workflow
catalog = context.portal_catalog
#student_wf_states = wf.waeup_student_wf.states.keys()
#info['wf_states'] = student_wf_states
#info['wf_states'][0] = " ----- "
lt = context.portal_layouts
pr = context.portal_registration
path_info = request.get('PATH_INFO').split('/')
is_manager = context.isManager
validate = request.has_key("cpsdocument_edit_button")
items = []
default = {'search_mode': 'name',
        'review_state': 'all',
        'search_string': ''
        }

rend,psm,ds = lt.renderLayout(layout_id= 'student_search',
                      schema_id= 'student_search',
                      context=context,
                      mapping=validate and request,
                      ob=default,
                      layout_mode='edit',
                      formaction="searchStudents"
                      )
if psm == '':
    return context.students_manager_view(rendered = rend,
                             psm = psm,
                             #psm = "%s, %s" % (psm,ds),
                             students = items,
                             is_manager = is_manager,
                             )
what = ds.get('search_mode')
state = ds.get('review_state')
st = term = ds.get('search_string')
err = False
with_review = state != "all"
if not term and not with_review:
    psm = "You must specify a search string when searching 'all states'."
    err = True
elif '*' in term:
    psm = "you cannot use the '*' alone"
    err = True
if err:
    return context.students_manager_view(rendered = rend,
                             psm = psm,
                             #psm = "%s, %s" % (psm,ds),
                             students = items,
                             is_manager = is_manager,
                             )
with_review = state != "all"
items = []
res = []
portal_type_query = {'query':['Student','StudentApplication','StudentPersonal']}
st_queries = ('jamb_reg_no','matric_no','name')
onlyreview = with_review and not term
if onlyreview:
    res = catalog(portal_type='Student',
                  review_state=state)
elif what == "student_id":
    if with_review:
        res = catalog(portal_type='Student',
                      id = term.strip(),
                      review_state=state)
    else:
        res = catalog(portal_type='Student',
                      id = term.strip())
elif what in st_queries:
    if what == "jamb_reg_no":
        pt = 'StudentApplication'
        st = "%s*" % term.strip().lower()
    elif what == "matric_no":
        pt = 'StudentClearance'
        st = "%s*" % term.strip().lower()
    elif what == "name":
        pt = portal_type_query
        st = "%s*" % term.strip()
    try:
        res = catalog(portal_type=pt,SearchableText=st)
    except:
        return context.students_manager_view(rendered = rend,
                             psm = 'Search string "%s" not allowed.' % term,
                             #psm = "%s, %s" % (psm,ds),
                             students = items,
                             is_manager = is_manager,
                             )
students = []
if res:
    for r in res:
        if r.portal_type in ("StudentApplication","StudentPersonal"):
            student = r.getObject().aq_parent
##            pp = '/'.join(r.getPath().split('/')[:-1])
##            student_cat = catalog(pathindex=pp,portal_type='Student')[0]
##            student_rs = student_cat.review_state
##            student = student_cat.getObject()
        else:
            student = r.getObject()
        student_rs = wf.getInfoFor(student,'review_state','keiner')
        if (with_review and student_rs != state) or student in items:
            continue
        items.append(student)
if items:
    for item in items:
        students.append(context.getStudentInfo(item))
    return context.students_manager_view(rendered = rend,
                             psm = "%d matching Students found" % len(items),
                             students = students,
                             is_manager = is_manager,
                             )
return context.students_manager_view(rendered = rend,
                             psm = """Your search for "%s" in %s with state %s failed.""" % (st,what,state),
                             students = students,
                             is_manager = is_manager,
                             )
