def transcript(request, course_slug=None): user = request.user course_list = user.courses_as_student.all() course_transcript = None template_name = 'courses/transcript.html' if course_slug: template_name = 'courses/transcript_course.html' course_transcript = get_object_or_404(Course, slug=course_slug) course_list = course_list.filter(slug=course_slug) courses_info = [] use_old_calculus = settings.COURSES_USING_OLD_TRANSCRIPT for course in course_list: cert_url = '' total_mark, units_info = get_course_mark(course, user) award = None passed = False if course.threshold is not None and float( course.threshold) <= total_mark: passed = True cert_url = settings.CERTIFICATE_URL % { 'courseid': course.id, 'email': user.email.lower() } badge = course.completion_badge if badge is not None: try: award = Award.objects.get(badge=badge, user=user) except Award.DoesNotExist: award = Award(badge=badge, user=user) award.save() total_weight_unnormalized, unit_course_counter, course_units = get_course_intermediate_calculations( course) units_info_ordered = [] for unit in course_units: uinfo = next((u for u in units_info if u['unit_id'] == unit.pk), { 'relative_mark': 0, 'mark': 0 }) uinfo['unit'] = unit normalized_unit_weight = normalize_unit_weight( unit, unit_course_counter, total_weight_unnormalized) uinfo['normalized_weight'] = normalized_unit_weight unit_class = get_unit_badge_class(unit) uinfo['badge_class'] = unit_class units_info_ordered.append(uinfo) courses_info.append({ 'course': course, 'units_info': units_info_ordered, 'mark': total_mark, 'award': award, 'passed': passed, 'cert_url': cert_url, 'use_old_calculus': use_old_calculus, }) return render_to_response(template_name, { 'courses_info': courses_info, 'course_transcript': course_transcript, }, context_instance=RequestContext(request))
def transcript(request, course_slug=None): user = request.user course_list = user.courses_as_student.all() course_transcript = None template_name = 'courses/transcript.html' if course_slug: template_name = 'courses/transcript_course.html' course_transcript = get_object_or_404(Course, slug=course_slug) course_list = course_list.filter(slug=course_slug) courses_info = [] use_old_calculus = settings.COURSES_USING_OLD_TRANSCRIPT for course in course_list: cert_url = '' total_mark, units_info = get_course_mark(course, user) award = None passed = False if course.threshold is not None and float(course.threshold) <= total_mark: passed = True cert_url = settings.CERTIFICATE_URL % { 'courseid': course.id, 'email': user.email.lower() } badge = course.completion_badge if badge is not None: try: award = Award.objects.get(badge=badge, user=user) except Award.DoesNotExist: award = Award(badge=badge, user=user) award.save() total_weight_unnormalized, unit_course_counter, course_units = get_course_intermediate_calculations(course) units_info_ordered = [] for unit in course_units: uinfo = next((u for u in units_info if u['unit_id'] == unit.pk), {'relative_mark': 0, 'mark': 0}) uinfo['unit'] = unit normalized_unit_weight = normalize_unit_weight(unit, unit_course_counter, total_weight_unnormalized) uinfo['normalized_weight'] = normalized_unit_weight unit_class = get_unit_badge_class(unit) uinfo['badge_class'] = unit_class units_info_ordered.append(uinfo) courses_info.append({ 'course': course, 'units_info': units_info_ordered, 'mark': total_mark, 'award': award, 'passed': passed, 'cert_url': cert_url, 'use_old_calculus': use_old_calculus, }) return render_to_response(template_name, { 'courses_info': courses_info, 'course_transcript': course_transcript, }, context_instance=RequestContext(request))
def get_csv_from_students_list(course, studentlist): course_file = StringIO.StringIO() course_csv = csv.writer(course_file, quoting=csv.QUOTE_ALL) headers = ["first_name", "last_name", "email", "mark"] course_csv.writerow(headers) h = HTMLParser() if not hasattr(studentlist[:1][0], 'student'): for student in studentlist: row = [] try: mark, mark_info = get_course_mark(course, student) row = [ h.unescape(student.first_name).encode("utf-8", "replace"), h.unescape(student.last_name).encode("utf-8", "replace"), h.unescape(student.email).encode("utf-8", "replace"), "%.2f" % mark ] except: continue course_csv.writerow(row) else: for student in studentlist: row = [] try: mark, mark_info = get_course_mark(course, student.student) row = [ h.unescape(student.student.first_name).encode("utf-8", "replace"), h.unescape(student.student.last_name).encode("utf-8", "replace"), h.unescape(student.student.email).encode("utf-8", "replace"), "%.2f" % mark ] except: continue course_csv.writerow(row) return course_file.getvalue()
def transcript_v2(request, course_slug=None): user = request.user if not user.is_superuser: from django.http import Http404 raise Http404 course_list = request.user.courses_as_student.all() course_transcript = None template_name = 'courses/transcript.html' if course_slug: template_name = 'courses/transcript_course.html' course_transcript = get_object_or_404(Course, slug=course_slug) course_list = course_list.filter(slug=course_slug) courses_info = [] cert_url = '' use_old_calculus = settings.COURSES_USING_OLD_TRANSCRIPT for course in course_list: total_mark, units_info = get_course_mark(course, request.user) award = None passed = False if course.threshold is not None and float(course.threshold) <= total_mark: passed = True cert_url = settings.CERTIFICATE_URL % { 'courseid': course.id, 'email': request.user.email.lower() } badge = course.completion_badge if badge is not None: try: award = Award.objects.get(badge=badge, user=request.user) except Award.DoesNotExist: award = Award(badge=badge, user=request.user) award.save() total_weight_unnormalized, unit_course_counter, course_units = get_course_intermediate_calculations(course) units_pks_ordered = map(lambda x: x[0], course_units.values_list('pk')) def order_units_info(unit): try: return units_pks_ordered.index(unit['unit_id']) except ValueError: return len(units_pks_ordered) units_info = sorted(units_info, key=order_units_info) for idx, unit in enumerate(course_units): try: uinfo = units_info[idx] except IndexError: uinfo = {'relative_mark': 0, 'mark': 0} uinfo['unit'] = unit normalized_unit_weight = normalize_unit_weight(unit, unit_course_counter, total_weight_unnormalized) uinfo['normalized_weight'] = normalized_unit_weight unit_class = get_unit_badge_class(unit) uinfo['badge_class'] = unit_class courses_info.append({ 'course': course, 'units_info': units_info, 'mark': total_mark, 'award': award, 'passed': passed, 'cert_url': cert_url, 'use_old_calculus': use_old_calculus, }) return render_to_response(template_name, { 'courses_info': courses_info, 'course_transcript': course_transcript, }, context_instance=RequestContext(request))
def has_user_passed_course(user, course): passed = False total_mark, units_info = get_course_mark(course, user) if course.threshold is not None and float(course.threshold) <= total_mark: passed = True return passed
def update_kq_mark(db, kq, user, threshold, new_mark_kq=None, new_mark_normalized_kq=None): from moocng.courses.marks import calculate_kq_mark if not new_mark_kq or not new_mark_normalized_kq: new_mark_kq, new_mark_normalized_kq = calculate_kq_mark(kq, user) data_kq = {} data_kq['user_id'] = user.pk data_kq['course_id'] = kq.unit.course.pk data_kq['unit_id'] = kq.unit.pk data_kq['kq_id'] = kq.pk marks_kq = db.get_collection('marks_kq') mark_kq_item = marks_kq.find_one(data_kq) if mark_kq_item: updated_kq_mark = (new_mark_kq != mark_kq_item['mark'] or new_mark_normalized_kq != mark_kq_item['relative_mark']) if updated_kq_mark: marks_kq.update( data_kq, {'$set': {'mark': new_mark_kq, 'relative_mark': new_mark_normalized_kq}}, safe=True ) else: updated_kq_mark = True data_kq['mark'] = new_mark_kq data_kq['relative_mark'] = new_mark_normalized_kq marks_kq.insert(data_kq) if kq.kq_type() == 'PeerReviewAssignment' and threshold is not None: threshold = decimal.Decimal('5.0') # P2P is a special case today = date.today() # badges badges = BadgeByCourse.objects.filter(course_id=kq.unit.course.pk, criteria_type=1) for badge in badges: win = False gotBadge = get_db().get_collection('badge').find_one({'id_badge': badge.id, "id_user": user.pk}) if(not gotBadge): win = True criterias = badge.criteria.split(",") for criteria in criterias: mark = get_db().get_collection('marks_kq').find_one({'user_id': user.pk, "kq_id": int(criteria)}) if(not mark or mark["mark"] < badge.note): win = False if(win): get_db().get_collection('badge').insert({"id_badge":badge.id, "id_user":user.pk, "title":badge.title, "description":badge.description, "color":badge.color, "date": today.isoformat() }) # badge unit checkpoint badges = BadgeByCourse.objects.filter(course_id=kq.unit.course_id, criteria_type=0) course_mark, units_info = get_course_mark(kq.unit.course, user) for badge in badges: win = False if(badge.note <= course_mark): gotBadge = get_db().get_collection('badge').find_one({'id_badge': badge.id, "id_user": user.pk}) if(not gotBadge): try: evaluateUnit = Unit.objects.get(id=badge.criteria) units = Unit.objects.filter(course_id=evaluateUnit.course_id, order__lte = evaluateUnit.order) win = True for aux in units: unit_kqs = aux.knowledgequantum_set.all() for kq in unit_kqs: if not kq.is_completed(user): win = False except: pass if(win): get_db().get_collection('badge').insert({"id_badge":badge.id, "id_user":user.pk, "title":badge.title, "description":badge.description, "color":badge.color, "date": today.isoformat()}) return updated_kq_mark, has_passed_now(new_mark_kq, mark_kq_item, threshold)