# Script (Python) "getStudyLevelInfo"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=student=None
##title=
##
# $Id: getStudyLevelInfo.py 913 2006-11-21 10:49:45Z henrik $
"""
return Info about the Studylevel
"""
def calculateGPA():
    """calculate the gpa"""
    sum = 0
    course_count = 0
    for sc in context.objectValues():
        result = sc.getContent()
        if not result.grade:
            continue
        res = context.portal_catalog({'meta_type': 'Course',
                                      'id': sc.aq_parent.id})
        if len(res) < 1:
            continue
        course = res[0].getObject().getContent()
    if course_count:
        return sum/course_count
    return 0.0

request = context.REQUEST

wf = context.portal_workflow
mtool = context.portal_membership
path_info = request.get('PATH_INFO').split('/')
try:
    i = int(path_info[-1])
    p = 0
except:
    p = 1
info = {}
pt = request.get('PATH_TRANSLATED').split('/')

student_id = pt[-(3+p)]
level_id = pt[-(1+p)]
info['action'] = "%s" % context.absolute_url()
info['choosen_ids'] = request.get('ids',[])
info['doc'] = context.getContent()
study_course = context.aq_parent.getContent()
cert_id = study_course.study_course
brain = context.portal_catalog(meta_type="Student",id = student_id)[-1]
cp = brain.getPath()
info['container_path'] = cp
info['cert_id'] = cert_id

res = context.portal_catalog(meta_type="StudentCourseResult",
                             container_path="%s/study_course/%s" % (cp,level_id))
first = []
second = []
sum = 0
course_count = 0
for r in res:
    row = {}
    ro = r.getObject()
    rd = ro.getContent()
    row['id'] = ro.getId()
    row['credits'] = rd.credits
    if row['credits'] and rd.grade:
        credits = int(rd.credits)
        sum += credits * ['F','E','D','C','B','A'].index(rd.grade)
        course_count += credits
    row['sum'] = sum
    row['count'] = course_count
    row['title'] = rd.title
    #row['core'] = rd.core_or_elective
    row['semester'] = rd.semester
    row['score'] = rd.score
    row['grade'] = rd.grade
    row['weight'] = rd.weight
    row['url'] = ro.absolute_url()
    row['review_state'] = wf.getInfoFor(ro,'review_state','None')
    row['is_editable'] = mtool.checkPermission('Modify portal content', ro)
    if rd.semester == 1:
        first.append(row)
    else:
        second.append(row)
first.sort()
second.sort()
gpaf = 0.0
if course_count:
    gpaf = (float(sum)/course_count)
gpa = "%4.2f" % gpaf
info['doc'].edit(mapping={'gpa': gpaf})
info['first'] = first
info['second'] = second
return info
