#-*- mode: python; mode: fold -*-
# $Id: Students.py 398 2006-08-21 14:29:27Z joachim $
from Globals import InitializeClass
from AccessControl import ClassSecurityInfo
from AccessControl.SecurityManagement import newSecurityManager

from Products.CMFCore.utils import UniqueObject, getToolByName
from Products.CMFCore.permissions import View
from Products.CMFCore.permissions import ModifyPortalContent
from Products.CPSCore.CPSBase import CPSBase_adder, CPSBaseFolder
#from Products.CPSCore.CPSBase import CPSBaseDocument as BaseDocument
from Products.CPSDocument.CPSDocument import CPSDocument
from Products.CPSCore.CPSBase import CPSBaseBTreeFolder as BaseBTreeFolder
from Products.CPSCore.CPSMembershipTool import CPSUnrestrictedUser
from Products.WAeUP_SRP.Academics import makeCertificateCode
import logging
import csv,re
import Globals
p_home = Globals.package_home(globals())
i_home = Globals.INSTANCE_HOME
MAX_TRANS = 1000

class StudentsFolder(CPSDocument): ###(
    """
    WAeUP container for the various WAeUP containers data
    """
    meta_type = 'StudentsFolder'
    portal_type = meta_type
    security = ClassSecurityInfo()

    security.declareProtected(ModifyPortalContent,"loadFullTimeStudentsFromCSV")###(
    def loadFullTimeStudentsFromCSV(self):
        """load Fulltime Studentdata from CSV values"""
        #return
        import transaction
        tr_count = 0
        name = 'short_full_time'
        no_import = False
        if not no_import:
            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
            no_import.write('"MatricNo","EntryRegNo","CurrentSession","StudentLevel","fullname","FirstName","MiddleName","Lastname","FormerSurname","Sex","Nationality","State","LGA","PermanentAddress","PermanentAddressCity","CampusAddress","PhoneNumber","Emailaddress","Mode","CourseMajor","Faculty","Dept"\n')
        logger = logging.getLogger('%s_import' % name)
        logger.info('Start loading from %s.csv' % name)
        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
        try:
            students = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
        except:
            logger.error('Error reading %s.csv' % name)
            return
        l = self.portal_catalog({'meta_type': "Student"})
        studs = {}
        for s in l:
            studs[s.id] = s.getObject()
        l = self.portal_catalog({'meta_type': "Certificate"})
        certs = {}
        students_added = []
        for c in l:
            certs[c.id] = c.getObject()
        for student in students:
            logger.info('processing "%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
            sid = student.get('MatricNo')
            if sid == "":
                em = 'Empty MatricNo\n'
                logger.info(em)
                no_import.write(em)
                no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
                continue
            certcode = makeCertificateCode(student.get('CourseMajor'))
##            if sid in students_added:
##                em = 'Student with ID %(MatricNo)s %(fullname)s already exists in this import\n' % student
##                logger.info(em)
##                no_import.write(em)
##                no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
##                continue
            s = studs.get(sid,None)
            if certcode not in certs.keys():
                em = 'Certificate with ID %s %s not found\n' % (certcode, student.get('CourseMajor'))
                logger.info(em)
                no_import.write(em)
                no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
                continue
            level = student.get('StudentLevel')
            try: 
                int(level)
            except:
                em = 'Student with ID %(MatricNo)s StudentLevel is empty\n' % student
                logger.info(em)
                no_import.write(em)
                no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
                continue
            if s is None:
                #self.log('Creating Faculty %(id)s = %(Title)s' % faculty)
                logger.info('Creating Student with ID %(MatricNo)s %(fullname)s' % student)
                students_folder.invokeFactory('Student', sid)
                s = getattr(self,sid)
                students_added.append(sid)
                studs[sid] = s
                s.invokeFactory('StudentPersonal','personal')
                sp = s.personal
                d = {'Title': 'Personal Data'}
                d['matricel_no'] = student.get('MatricNo')
                d['entry_reg_no'] = student.get('EntryRegNo')
                d['fullname'] = student.get('fullname')
                d['firstname'] = student.get('FirstName')
                d['middlename'] = student.get('MiddleName')
                d['lastname'] = student.get('Lastname')
                d['former_surname'] = student.get('FormerSurname')
                d['sex'] = student.get('Sex') == 'F'
                d['nationality'] = student.get('Nationality')
                d['state'] = student.get('State')
                d['lga'] = student.get('LGA')
                d['street'] = student.get('PermanentAddress')
                d['city'] = student.get('PermanentAddressCity')
                d['campus_address'] = student.get('CampusAddress')
                d['phone'] = student.get('PhoneNumber')
                d['email'] = student.get('Emailaddress')
                sp.getContent().edit(mapping=d)
                #
                # Study Course
                #
                s.invokeFactory('StudentStudyCourse','study_course')
                sc = s.study_course
                d = {}
                d['matricel_no'] = student.get('MatricNo')
                d['entry_reg_no'] = student.get('EntryRegNo')
                d['faculty'] = student.get('Faculty')
                d['department'] = student.get('Dept')
                d['course_major'] = certcode
                css = student.get('CurrentSession') or '2004-2005'
                cs = int(css.split('-')[0]) - 2000
                cl = int(student.get('StudentLevel'))/100
                d['entry_session'] = "200%s" % (cs - cl)
                sc.getContent().edit(mapping=d)
                #
                # Level
                #
                l = getattr(sc,level,None)
                if l is None:
                    #self.log('Creating Department %(DeptCode)s = %(Description)s' % dep)
                    logger.info('Creating Level %(StudentLevel)s for %(fullname)s' % student)
                    sc.invokeFactory('StudyLevel', level)
                    l = getattr(sc, level)
                    certificate = certs[certcode]
                    cert_level = getattr(certificate,level,None)
                    if cert_level is None:
                        logger.info('Level %(level)s not in %(certcode)s' % vars())
                    l.getContent().edit(mapping={'Title': "Level %s" % level})
                    l.invokeFactory('Semester','first')
                    l.invokeFactory('Semester','second')
                    first_s = getattr(l,'first')
                    first_s.getContent().edit(mapping={'Title': 'First Semester'})
                    second_s = getattr(l,'second')
                    second_s.getContent().edit(mapping={'Title': 'Second Semester'})
            else:
                em = 'Student with ID %(MatricNo)s %(fullname)s already exists\n' % student
                logger.info(em)
                no_import.write(em)
                no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
            if tr_count > MAX_TRANS:
                #transaction.commit()
                #em = '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Transaction commited\n' % student
                logger.info(em)
                tr_count = 0
            tr_count += 1
        return self.students.academics_contents()
    ###)

    security.declareProtected(ModifyPortalContent,"loadFullTimeStudentsResultsFromCSV") ###(
    def loadFullTimeStudentsResultsFromCSV(self):
        """load Fulltime Studentdata from CSV values"""
        #return
        import transaction
        tr_count = 0
        name = 'short_full_time_results_2004_2005'
        no_import = False
        if not no_import:
            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
            no_import.write('"Matnumber","CosCode","Ansbook","CosStuatus","Session","Mat_Cos","Score","CarryLevel","Grade","Weight","Semster","Verdict","Level","id","GPA"\n')
        logger = logging.getLogger('%s_import' % name)
        logger.info('Start loading from %s.csv' % name)
        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
        try:
            results = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
        except:
            logger.error('Error reading %s.csv' % name)
            return
        l = self.portal_catalog({'meta_type': "Student"})
        students = {}
        for s in l:
            students[s.id] = s.getObject()
        l = self.portal_catalog({'meta_type': "Course"})
        courses = {}
        for c in l:
            courses[c.id] = c.getObject()
        for result in results:
            sid = result.get('Matnumber')
            if sid not in students.keys():                
                em = 'Student with ID %(Matnumber)s not found\n' % result
                logger.info(em)
                no_import.write(em)
                no_import.write('"%(Matnumber)s","%(CosCode)s","%(Ansbook)s","%(CosStuatus)s","%(Session)s","%(Mat_Cos)s","%(Score)s","%(CarryLevel)s","%(Grade)s","%(Weight)s","%(Semster)s","%(Verdict)s","%(Level)s","%(id)s","%(GPA)s"\n' % result)
                continue
            course = result.get('CosCode')
            if course not in courses.keys():
                em = 'Course with ID %(CosCode)s not found\n' % result
                logger.info(em)
                no_import.write(em)
                no_import.write('"%(Matnumber)s","%(CosCode)s","%(Ansbook)s","%(CosStuatus)s","%(Session)s","%(Mat_Cos)s","%(Score)s","%(CarryLevel)s","%(Grade)s","%(Weight)s","%(Semster)s","%(Verdict)s","%(Level)s","%(id)s","%(GPA)s"\n' % result)
                continue
            level = result.get('Level')
            try: 
                int(level)
            except:
                em = 'Result for result with ID %(Matnumber)s Course %(CosCode)s Level is empty\n' % result
                logger.info(em)
                no_import.write(em)
                no_import.write('"%(Matnumber)s","%(CosCode)s","%(Ansbook)s","%(CosStuatus)s","%(Session)s","%(Mat_Cos)s","%(Score)s","%(CarryLevel)s","%(Grade)s","%(Weight)s","%(Semster)s","%(Verdict)s","%(Level)s","%(id)s","%(GPA)s"\n' % result)
                continue
            sf = students.get(sid)
            sc = getattr(sf,'study_course')
            l = getattr(sc,level,None)
            if l is None:
                #self.log('Creating Department %(DeptCode)s = %(Description)s' % dep)
                logger.info('Creating Level %(Level)s for %(Matnumber)s' % result)
                sc.invokeFactory('StudyLevel', level)
                l = getattr(sc, level)
                l.getContent().edit(mapping={'Title': "Level %s" % level})
                l.invokeFactory('Semester','first')
                l.invokeFactory('Semester','second')
                first_s = getattr(l,'first')
                first_s.getContent().edit(mapping={'Title': 'First Semester'})
                second_s = getattr(l,'second')
                second_s.getContent().edit(mapping={'Title': 'Second Semester'})
            snr = result.get('Semester')
            semester = getattr(l,'first')
            if snr == "2":
                semester = getattr(l,'second')
            logger.info('Creating CourseTicket %(CourseTicket)s in Level %(Level)s for %(Matnumber)s' % result)
            semester.invokeFactory('CourseTicket',course)
            ct = getattr(semester,course)
            d = {}
            d['ansbook'] = result.get('Ansbook')
            d['status'] = result.get('CosStuatus')
            d['score'] = result.get('Score')
            d['carry_level'] = result.get('CarryLevel')
            d['grade'] = result.get('Grade')
            d['weight'] = result.get('Weight')
            d['verdict'] = result.get('Verdict')
            d['import_id'] = result.get('id')
            gpa = result.get('GPA').replace(',','.')
            d['gpa'] = float(gpa) 
            ct.getContent().edit(mapping = d)
            if tr_count > MAX_TRANS:
                transaction.commit()
                tr_count = 0
            tr_count += 1
        return self.students.academics_contents()
            
###)

            
    security.declareProtected(View,"Title")
    def Title(self):
        """compose title"""
        return "Student Section"

InitializeClass(StudentsFolder)

def addStudentsFolder(container, id, REQUEST=None, **kw):
    """Add a Student."""
    ob = StudentsFolder(id, **kw)
    return CPSBase_adder(container, ob, REQUEST=REQUEST)
###)

class Student(CPSDocument): ###(
    """
    WAeUP Student container for the various student data
    """
    meta_type = 'Student'
    portal_type = meta_type
    security = ClassSecurityInfo()

    security.declareProtected(View,"Title")
    def Title(self):
        """compose title"""
        reg_nr = self.getId()[1:]
        data = getattr(self,'personal',None)
        if data is None:
            data = getattr(self,'jamb',None)
        if data:
            content = data.getContent()
            return "%s %s" % (content.firstname,content.lastname)
        return self.title

    def Description(self):
        """compose description"""
        data = getattr(self,'PERSONAL',None)
        if data is None:
            return "none"
        if data:
            content = data.getContent()
            return "%s" % (content.description)
        return self.description

    security.declareProtected(View,"setScratchCardData")
    def setScratchCardData(self,ident,ds):
        """set this data """
        dict = {'%s_sc_pin' % ident : ds.get('sc_pin'),
                '%s_sc_id' % ident : ds.get('sc_id'),
                '%s_sc_value' % ident : ds.get('sc_value'),
                '%s_date' % ident : ds.get('sc_date'),
                }

        old_user = self.portal_membership.getAuthenticatedMember()
        if self.portal_membership.isAnonymousUser():
            tmp_user = CPSUnrestrictedUser('s%(jamb_id)s' % ds, '',
                                       ['StudentManager'], '')
            tmp_user = tmp_user.__of__(self.acl_users)
            newSecurityManager(None, tmp_user)
        #print str(dict)
        self.edit(mapping=dict)
        newSecurityManager(None, old_user)

    security.declareProtected(View,"memberIsOwner")
    def memberIsOwner(self):
        """is the current user the owner"""
        member = self.portal_membership.getAuthenticatedMember()
        #print member, self.getId(),self.aq_parent.getId()
        if self.aq_parent.getId() == str(member):
            return True
        return False

    security.declareProtected(View,"accommodationIsBooked")
    def accommodationIsBooked(self):
        """is the accommodation booked"""
        if self.accommodation_sc_pin != '':
            return True
        return False

    security.declareProtected(View,"accommodationIsPayed")
    def accommodationIsPayed(self):
        """is the accommodation payed"""
        if self.hostel_fee_sc_pin != '':
            return True
        return False

    security.declareProtected(View,"isRegisteredForCurrentLevel")
    def isRegisteredForCurrentLevel(self):
        """is the student registered for the current level"""
        for l in  self.aq_parent.objectValues():
            if l.portal_type == 'StudyLevel':
                return True
        return False

InitializeClass(Student)

def addStudent(container, id, REQUEST=None, **kw):
    """Add a Student."""
    ob = Student(id, **kw)
    return CPSBase_adder(container, ob, REQUEST=REQUEST)

###)

class StudentPersonal(CPSDocument): ###(
    """
    WAeUP Student container for the various student data
    """
    meta_type = 'StudentPersonal'
    portal_type = meta_type
    security = ClassSecurityInfo()

    security.declareProtected(View,"Title")
    def Title(self):
        """compose title"""
        content = self.getContent()
        #return "Personal Data for %s %s" % (content.firstname,content.lastname)
        return "Personal Data"


InitializeClass(StudentPersonal)

def addStudentPersonal(container, id, REQUEST=None, **kw):
    """Add a Students personal data."""
    ob = StudentPersonal(id, **kw)
    return CPSBase_adder(container, ob, REQUEST=REQUEST)

###)

class StudentStudyCourse(CPSDocument): ###(
    """
    WAeUP Student container for the various student data
    """
    meta_type = 'StudentStudyCourse'
    portal_type = meta_type
    security = ClassSecurityInfo()

    security.declareProtected(View,"Title")
    def Title(self):
        """compose title"""
        content = self.getContent()
        return "Course Major"


InitializeClass(StudentStudyCourse)

def addStudentStudyCourse(container, id, REQUEST=None, **kw):
    """Add a Students personal data."""
    ob = StudentStudyCourse(id, **kw)
    return CPSBase_adder(container, ob, REQUEST=REQUEST)

###)

class StudentEligibility(CPSDocument): ###(
    """
    WAeUP Student container for the various student data
    """
    meta_type = 'StudentEligibility'
    portal_type = meta_type
    security = ClassSecurityInfo()

    security.declareProtected(View,"Title")
    def Title(self):
        """compose title"""
        return "Eligibility Data"


InitializeClass(StudentEligibility)

def addStudentEligibility(container, id, REQUEST=None, **kw):
    """Add a Students eligibility data."""
    ob = StudentEligibility(id, **kw)
    return CPSBase_adder(container, ob, REQUEST=REQUEST)

###)

class StudentDocuments(CPSDocument): ###(
    """
    WAeUP Student container for the various student data
    """
    meta_type = 'StudentDocuments'
    portal_type = meta_type
    security = ClassSecurityInfo()

    security.declareProtected(View,"Title")
    def Title(self):
        """compose title"""
        content = self.getContent()
        return "Scanned Documents"


InitializeClass(StudentDocuments)

def addStudentDocuments(container, id, REQUEST=None, **kw):
    """Add a Students documents"""
    ob = StudentDocuments(id, **kw)
    return CPSBase_adder(container, ob, REQUEST=REQUEST)

###)

class Jamb(CPSDocument): ###(
    """
    WAeUP Jamb containing the courses and students
    """
    meta_type = 'Jamb'
    portal_type = meta_type
    security = ClassSecurityInfo()

    security.declareProtected(View,"Title")
    def Title(self):
        """compose title"""
        content = self.getContent()
        return "JAMB Data for %s %s" % (content.firstname,content.lastname)

    security.declareProtected(View,"setOwnership")
    def setOwnership(self,member_id):
        """set ownership"""
        pm = getattr(self,'portal_membership')
        member = pm.getMemberById(member_id)
        self.changeOwnership(member)

InitializeClass(Jamb)

def addJamb(container, id, REQUEST=None, **kw):
    """Add a Jamb."""
    ob = Jamb(id, **kw)
    return CPSBase_adder(container, ob, REQUEST=REQUEST)

###)

class StudyLevel(CPSDocument): ###(
    """
    WAeUP StudyLevel containing the courses and students
    """
    meta_type = 'StudyLevel'
    portal_type = meta_type
    security = ClassSecurityInfo()

InitializeClass(StudyLevel)

def addStudyLevel(container, id, REQUEST=None, **kw):
    """Add a StudyLevel."""
    ob = StudyLevel(id, **kw)
    return CPSBase_adder(container, ob, REQUEST=REQUEST)

###)

class Semester(CPSDocument): ###(
    """
    WAeUP Semester containing the courses and students
    """
    meta_type = 'Semester'
    portal_type = meta_type
    security = ClassSecurityInfo()

InitializeClass(Semester)

def addSemester(container, id, REQUEST=None, **kw):
    """Add a Semester."""
    ob = Semester(id, **kw)
    return CPSBase_adder(container, ob, REQUEST=REQUEST)

###)

