##parameters= # $Id: create_level.py 3362 2008-03-20 16:20:15Z joachim $ """ process the Application Form return html renderer + psm """ request = context.REQUEST import DateTime,logging try: from Products.zdb import set_trace except: def set_trace(): pass from Products.AdvancedQuery import Eq, Between, Le,In try: aq_portal = context.portal_catalog.evalAdvancedQuery except: aq_portal = context.portal_catalog_real.evalAdvancedQuery aq_courses = context.courses_catalog.evalAdvancedQuery aq_results = context.course_results.evalAdvancedQuery current = DateTime.DateTime() request = context.REQUEST response = request.RESPONSE redirect = response.redirect logger = logging.getLogger('Skins.create_level') # mtool = context.portal_membership # member = mtool.getAuthenticatedMember() # member_id = str(member) # requested_id = context.getStudentId() # #set_trace() # create_level = request.form.get('create_level',None) # if not create_level or not requested_id or\ # (not context.isStaff() and member_id != requested_id): # logger.info('%s tried to access %s' % (member_id,requested_id)) # return redirect("%s/srp_anonymous_view" % context.portal_url()) # student_id = context.getStudentId() info = context.waeup_tool.getAccessInfo(context) student_id = info['student_id'] if student_id is None: return None #lt = context.portal_layouts #wt = context.waeup_tool student = context.students_catalog.getRecordByKey(student_id) cert_id = student.course current_level = student.level current_session = student.session in_progress = request.get('in_progress','not started') if aq_results(Eq('student_id',student_id) & Eq('level_id',current_level)): logger.info('%s tried to create already existing level %s' % (student.id,current_level)) return logger.info('%s started to create level %s' % (student.id,current_level)) if context.hasObject(current_level): #level = getattr(context,"%s" % current_level) return response.redirect("%s/%s" % (context.absolute_url(),current_level)) else: context.invokeFactory('StudentStudyLevel',"%s" % current_level) level = getattr(context,"%s" % current_level) context.portal_workflow.doActionFor(level,'open') level.getContent().edit(mapping={'session': current_session}) #context.portal_workflow.doActionFor(level,'close_for_edit') level_id = level.getId() session_id = context.getLevelSession(level.getContent(),student_id,level_id) results = [] records = [] if student.matric_no: results = context.results_import(matric_no = student.matric_no) l = [] #level_nr,dummy = divmod(int(current_level),100) # fetch session courses from the certificate res = context.portal_catalog(portal_type="Certificate", id = cert_id) if res: cert = res[0] path = cert.getPath() #academics_level = 100 * level_nr if int(current_level) > 0: academics_level = 100 * (int(current_level)/100) else: academics_level = '000' query = Eq("path","%s/%s" % (path,academics_level)) &\ Eq('portal_type','CertificateCourse') courses = aq_portal(query) for c in courses: course_id = "%s" % c.getId if course_id in l: # don't add twice continue l += course_id, d = context.getCourseInfo(c.getId) d['carry_over'] = False d['core_or_elective'] = getattr(c.getObject().getContent(),'core_or_elective') d['course_id'] = course_id d['student_id'] = student_id = context.getStudentId() d['level_id'] = level_id d['session_id'] = session_id records.append(d) # fetch carry_over courses from results_import if student.session in ('6', '06') and results: carry_overs = results for co in carry_overs: course_id = co.CosCode if course_id in l: # don't add twice continue # if level.hasObject(course_id): #already created # continue carry_over = False query = Eq('code',co.CosCode) c_res = aq_courses(query) if not c_res: #logger.info('%s", course %s not found in courses_catalog' % (student.id,co.CosCode)) continue course_cat_entry = c_res[0] passmark = getattr(course_cat_entry,'passmark',None) if not passmark or not co.Score: carry_over = co.GRADE in ('D','E','F') else: try: score = int(co.Score) except: # logger.info('%s, course %s has invalid score %s"' % (student.id, # course_id, # co.Score)) continue if int(c_res[0].passmark) <= score: continue carry_over = True if not carry_over: continue l += course_id, d = context.getCourseInfo(co.CosCode) d['course_id'] = course_id d['carry_over'] = carry_over d['code'] = course_id d['student_id'] = student_id d['level_id'] = level_id d['session_id'] = session_id records.append(d) # fetch carry_over courses from the course_results catalog else: res = aq_results(Eq('student_id',student_id), sortSpecs=('level_id',)) for result in res: course_id = result.code if course_id in l: continue query = Eq('code',course_id) c_res = aq_courses(query) if not c_res: #logger.info('%s", course %s not found in courses_catalog' % (student.id,result.CosCode)) continue course_cat_entry = c_res[0] passmark = getattr(course_cat_entry,'passmark',None) try: score = int(result.score) except: # logger.info('%s, course %s has invalid score %s"' % (student.id, # course_id, # result.Score)) continue if int(c_res[0].passmark) <= score: continue l += course_id, d = context.getCourseInfo(course_id) d['course_id'] = course_id d['carry_over'] = True d['code'] = course_id d['student_id'] = student_id d['level_id'] = level_id d['session_id'] = session_id records.append(d) if records: existing_uids = context.course_results.addMultipleRecords(records) if existing_uids: logger.info('%s level %s %s existing keys' % (student.id, current_level, ",".join(existing_uids))) logger.info('%s finished to create level %s' % (student.id,current_level)) return response.redirect("%s/%s" % (context.absolute_url(),current_level))