##parameters=REQUEST, certid=None
# $Id: refresh_certificate_info.py 1094 2006-12-19 12:11:44Z henrik $
"""
refresh the certificate info in the students_catalog
"""
request = context.REQUEST
if not certid:
    return "Certificate not specified"

course = request.get('certid')
res = context.portal_catalog(portal_type='Certificate',id = course)
if not res:
    return "Certificate %s not found"
c_brain = res[0]
c_path = c_brain.getPath().split('/')
faculty = c_path[-4]
department = c_path[-3]
s_cat = context.students_catalog
s_res = s_cat(course = certid)
#from Products.zdb import set_trace
#set_trace()
if not s_res:
    return "No students for %s not found"
refreshed = []
refreshed.append("Record started")
for student_id in [st.id  for st in s_res]:
    s_cat.modifyRecord(id = student_id,
                       course = course,
                       faculty = faculty,
                       department = department,
                       )
    refreshed.append("Record %s refreshed" % student_id)
return '\r'.join(refreshed)
