## Script (Python) "remove_students"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=REQUEST
##title=
##
# $Id: remove_students.py 2400 2007-10-19 15:36:36Z henrik $
"""
remove a list of students
"""
try:
    from Products.zdb import set_trace
except:
    def set_trace():
        pass

import logging
logger = logging.getLogger('Skins.remove_students')
from Products.AdvancedQuery import Eq, Between, Le,In
aq_students = context.students_catalog.evalAdvancedQuery
students_folder = context.portal_url.getPortalObject().campus.students
stdir = context.portal_directories.students
mtool = context.portal_membership
member = mtool.getAuthenticatedMember()
rget = REQUEST.form.get
info = {}
info['member'] = member
info['is_so'] = context.isSectionOfficer()
ids = info['choosen_ids'] = rget('ids',[])
search_list = rget('search_string','')
delete = rget('delete',None)
info['search_string'] = search_list
student_ids = search_list.split()
students = []
#set_trace()
psm = ''
deleted_list = []
if delete:
    if not ids:
        psm = "Nothing selected!"
    else:
        for student_id in ids:
            logger.info("%s started to remove student record %s" % (member,student_id))
            if hasattr(students_folder,student_id):
                students_folder.manage_delObjects((student_id),)
                logger.info("%s successfully removed student object %s" % (member,student_id))
            else:
                logger.info("Student object %s not found" % (student_id))
                continue
            deleted_list.append(student_id)
            # remove from directory
            if hasattr(stdir, student_id):
                stdir.deleteEntry(student_id)
                logger.info("%s successfully deleted student directory entry %s" % (member,student_id))
            else:
                logger.info("Student directory entry %s not found" % (student_id))
                continue
            context.waeup_tool.removePictureFolder(student_id)
        psm = "%s removed" % ' '.join(deleted_list)

if student_ids:
    brains = aq_students(In('id',student_ids) | In('matric_no', student_ids))
    brains_dict = {}
    if brains:
        for b in brains:
            if b.id in student_ids:
                brains_dict[b.id] = b
            elif b.matric_no in student_ids:
                brains_dict[b.matric_no] = b
    for student_id in student_ids:
        student = {}
        student['deleted'] = delete and student_id in ids
        if student_id in brains_dict.keys():
            brain = brains_dict[student_id]
            student['id'] = brain.id
            student['review_state'] = brain.review_state
            student['jamb_reg_no'] = brain.jamb_reg_no
            student['name'] = brain.name
            student['matric_no'] = brain.matric_no
            if brain.review_state == "deactivated":
                student['found'] = True
            else:
                student['found'] = False
        else:
            student['found'] = False
            student['id'] = student_id
            student['jamb_reg_no'] = ""
            student['name'] = "not found"
            student['matric_no'] = ''
            student['review_state'] = ''
        student['show_checkbox'] = not student['deleted'] and student['found']
        students.append(student)

allowed = students_folder.isSectionOfficer()

return context.remove_students_form(info = info,
                                    students = students,
                                    allowed = allowed,
                                    psm = psm,
                                    )
