## Script (Python) "deactivate_students"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=REQUEST
##title=
##
# $Id: deactivate_students.py 2729 2007-11-21 14:32:20Z joachim $
"""
remove a list of students
"""
try:
    from Products.zdb import set_trace
except:
    def set_trace():
        pass

import logging
logger = logging.getLogger('Skins.deactivate_students')
from Products.AdvancedQuery import Eq, Between, Le,In
#aq_portal = context.portal_catalog_real.evalAdvancedQuery
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 = []
psm = ''
deleted_list = []
if delete:
    if not ids:
        psm = "Nothing selected!"
    else:
        for student_id in ids:
            student = getattr(students_folder,student_id,None)
            student_state = context.portal_workflow.getInfoFor(student,'review_state',None)
            if student is not None:
                for object in student.objectValues():
                    if context.portal_workflow.getInfoFor(object,'review_state',None) in ('open','created'):
                        context.portal_workflow.doActionFor(object,'close')
                try:
                    context.portal_workflow.doActionFor(student,'deactivate')
                except:
                    logger.info("%s failed to deactivate student %s in state %s" % (member,student_id,student_state))
                    deleted_list.append(student_id + ' (failed)')
                    continue
                logger.info("%s deactivated student %s" % (member,student_id))
                deleted_list.append(student_id)
            else:
                logger.info("Student object %s not found" % (student_id))
                continue
        psm = "Deactivated: %s" % ' '.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'] = False
            else:
                student['found'] = True
        else:
            student['found'] = False
            student['id'] = student_id
            student['jamb_reg_no'] = ""
            student['name'] = "not found"
            student['review_state'] = ""
            student['matric_no'] = ''
        student['show_checkbox'] = not student['deleted'] and student['found']
        students.append(student)

allowed = students_folder.isSectionOfficer()

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