def update_mark(submitted): from moocng.courses.marks import calculate_kq_mark, calculate_unit_mark, calculate_course_mark updated_kq_mark = updated_unit_mark = updated_course_mark = False passed_kq = passed_unit = passed_course = False kq = KnowledgeQuantum.objects.get(pk=submitted['kq_id']) unit = kq.unit course = kq.unit.course user = User.objects.get(pk=submitted['user_id']) mark_kq, mark_normalized_kq = calculate_kq_mark(kq, user) db = get_db() # KQ updated_kq_mark, passed_kq = update_kq_mark(db, kq, user, course.threshold, new_mark_kq=mark_kq, new_mark_normalized_kq=mark_normalized_kq) # UNIT if not updated_kq_mark: return (updated_kq_mark, updated_unit_mark, updated_course_mark, passed_kq, passed_unit, passed_course) mark_unit, mark_normalized_unit = calculate_unit_mark(kq.unit, user) updated_unit_mark, passed_unit = update_unit_mark(db, unit, user, course.threshold, new_mark_unit=mark_unit, new_mark_normalized_unit=mark_normalized_unit) # COURSE if not updated_unit_mark: return (updated_kq_mark, updated_unit_mark, updated_course_mark, passed_kq, passed_unit, passed_course) mark_course, units_info = calculate_course_mark(unit.course, user) updated_course_mark, passed_course = update_course_mark(db, course, user, mark_course) return (updated_kq_mark, updated_unit_mark, updated_course_mark, passed_kq, passed_unit, passed_course)
def handle(self, *args, **options): if not options["filename"]: raise CommandError("-f filename.zip is required") limit = options["limit"] if options["courses"]: courses = options["courses"] courses = Course.objects.filter(slug__in=options["courses"]) else: courses = Course.objects.all() if not courses: raise CommandError("Courses not found") if options["filename"].endswith(".zip"): self.filename = options["filename"] else: self.filename = "%s.zip" % options["filename"] h = HTMLParser() zip = ZipFile(self.filename, mode="w") for course in courses: self.message("Adding course file %s.csv" % course.slug) 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) students = course.students.all()[:limit] headers = ["first_name", "last_name", "email"] for student in students: row = [] for field in headers: fieldvalue = getattr(student, field) row.append(h.unescape(fieldvalue).encode("ascii", "ignore")) mark, mark_info = calculate_course_mark(course, student) row.append(mark) course_csv.writerow(row) course_fileinfo = ZipInfo("%s.csv" % course.slug) course_file.seek(0) zip.writestr(course_fileinfo, course_file.read()) course_file.close() zip.close() self.message("Created %s file" % self.filename)
def handle(self, *args, **options): if not options["filename"]: raise CommandError("-f filename.zip is required") limit = options["limit"] if options["courses"]: courses = options["courses"] courses = Course.objects.filter(slug__in=options["courses"]) else: courses = Course.objects.all() if not courses: raise CommandError("Courses not found") if options["filename"].endswith(".zip"): self.filename = options["filename"] else: self.filename = "%s.zip" % options["filename"] h = HTMLParser() zip = ZipFile(self.filename, mode="w") for course in courses: self.message("Adding course file %s.csv" % course.slug) course_file = StringIO.StringIO() course_csv = csv.writer(course_file, quoting=csv.QUOTE_ALL) headers = ["email", "mark"] course_csv.writerow(headers) students = course.students.all()[:limit] for student in students: row = [] fieldvalue = getattr(student, 'email') row.append(h.unescape(fieldvalue.encode("ascii", "ignore"))) mark, mark_info = calculate_course_mark(course, student) row.append(mark) course_csv.writerow(row) course_fileinfo = ZipInfo("%s.csv" % course.slug) course_file.seek(0) zip.writestr(course_fileinfo, course_file.read()) course_file.close() zip.close() self.message("Created %s file" % self.filename)
def transcript(request, course_slug=None): """ Transcript is the main view of the user progress, here the user is show with the overall progress per course, quallifications and badges if he obtained any. We also give the access to the certification. :permissions: login :context: course, units_info, mark, award, passed, cert_url, use_old_calculus .. versionadded:: 0.1 """ course_list = request.user.courses_as_student.all() if course_slug: course = get_object_or_404(Course, slug=course_slug) course_list = course_list.filter(slug=course_slug) courses_info = [] cert_url = "" for course in course_list: use_old_calculus = False if course.slug in settings.COURSES_USING_OLD_TRANSCRIPT: use_old_calculus = True total_mark, units_info = calculate_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() for idx, uinfo in enumerate(units_info): unit_class = get_unit_badge_class(uinfo["unit"]) units_info[idx]["badge_class"] = unit_class if (not use_old_calculus and uinfo["unit"].unittype == "n") or not units_info[idx]["use_unit_in_total"]: units_info[idx]["hide"] = True 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( "courses/transcript.html", {"courses_info": courses_info, "course_slug": course_slug}, context_instance=RequestContext(request), )
def transcript(request): """ Transcript is the main view of the user progress, here the user is show with the overall progress per course, quallifications and badges if he obtained any. We also give the access to the certification. :permissions: login :context: course, units_info, mark, award, passed, cert_url, use_old_calculus .. versionadded:: 0.1 """ course_list = request.user.courses_as_student.all() courses_info = [] cert_url = '' for course in course_list: use_old_calculus = False if course.slug in settings.COURSES_USING_OLD_TRANSCRIPT: use_old_calculus = True total_mark, units_info = calculate_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() for idx, uinfo in enumerate(units_info): unit_class = get_unit_badge_class(uinfo['unit']) units_info[idx]['badge_class'] = unit_class if (not use_old_calculus and uinfo['unit'].unittype == 'n') or \ not units_info[idx]['use_unit_in_total']: units_info[idx]['hide'] = True 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('courses/transcript.html', { 'courses_info': courses_info, }, context_instance=RequestContext(request))
def transcript(request): """ Transcript is the main view of the user progress, here the user is show with the overall progress per course, quallifications and badges if he obtained any. We also give the access to the certification. :permissions: login :context: course, units_info, mark, award, passed, cert_url, use_old_calculus .. versionadded:: 0.1 """ course_list = request.user.courses_as_student.all() courses_info = [] cert_url = '' for course in course_list: use_old_calculus = False if course.slug in settings.COURSES_USING_OLD_TRANSCRIPT: use_old_calculus = True total_mark, units_info = calculate_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() for idx, uinfo in enumerate(units_info): unit_class = get_unit_badge_class(uinfo['unit']) units_info[idx]['badge_class'] = unit_class if (not use_old_calculus and uinfo['unit'].unittype == 'n') or \ not units_info[idx]['use_unit_in_total']: units_info[idx]['hide'] = True 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('courses/transcript.html', { 'courses_info': courses_info, }, context_instance=RequestContext(request))
def update_mark(submitted): from moocng.courses.marks import calculate_kq_mark, calculate_unit_mark, calculate_course_mark updated_kq_mark = updated_unit_mark = updated_course_mark = False passed_kq = passed_unit = passed_course = False kq = KnowledgeQuantum.objects.get(pk=submitted['kq_id']) unit = kq.unit course = kq.unit.course user = User.objects.get(pk=submitted['user_id']) mark_kq, mark_normalized_kq = calculate_kq_mark(kq, user) db = get_db() # KQ updated_kq_mark, passed_kq = update_kq_mark( db, kq, user, course.threshold, new_mark_kq=mark_kq, new_mark_normalized_kq=mark_normalized_kq) # UNIT if not updated_kq_mark: return (updated_kq_mark, updated_unit_mark, updated_course_mark, passed_kq, passed_unit, passed_course) mark_unit, mark_normalized_unit = calculate_unit_mark(kq.unit, user) updated_unit_mark, passed_unit = update_unit_mark( db, unit, user, course.threshold, new_mark_unit=mark_unit, new_mark_normalized_unit=mark_normalized_unit) # COURSE if not updated_unit_mark: return (updated_kq_mark, updated_unit_mark, updated_course_mark, passed_kq, passed_unit, passed_course) mark_course, units_info = calculate_course_mark(unit.course, user) updated_course_mark, passed_course = update_course_mark( db, course, user, mark_course) return (updated_kq_mark, updated_unit_mark, updated_course_mark, passed_kq, passed_unit, passed_course)
def transcript(request): course_list = request.user.courses_as_student.all() courses_info = [] cert_url = '' for course in course_list: use_old_calculus = False if course.slug in settings.COURSES_USING_OLD_TRANSCRIPT: use_old_calculus = True total_mark, units_info = calculate_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() for idx, uinfo in enumerate(units_info): unit_class = get_unit_badge_class(uinfo['unit']) units_info[idx]['badge_class'] = unit_class if (not use_old_calculus and uinfo['unit'].unittype == 'n') or \ not units_info[idx]['use_unit_in_total']: units_info[idx]['hide'] = True 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('courses/transcript.html', { 'courses_info': courses_info, }, context_instance=RequestContext(request))
def get_passed_courses(self, request, **kwargs): # In tastypie, the override_urls don't call # Authentication/Authorization self.is_authenticated(request) self.is_authorized(request) obj = self.get_object(request, kwargs) if isinstance(obj, HttpResponse): return obj passed_courses = [] if 'courseid' in request.GET: courseid = int(request.GET.get('courseid')) courses = obj.courses_as_student.filter(id=courseid) else: courses = obj.courses_as_student.all() for course in courses: if course.threshold is not None: total_mark, units_info = calculate_course_mark(course, obj) if float(course.threshold) <= total_mark: passed_courses.append(course) return self.alt_get_list(request, passed_courses)
def update_course_mark(db, course, user, new_mark_course=None): from moocng.courses.marks import calculate_course_mark if not new_mark_course: new_mark_course, units_info = calculate_course_mark(course, user) data_course = {} data_course['user_id'] = user.pk data_course['course_id'] = course.pk marks_course = db.get_collection('marks_course') mark_course_item = marks_course.find_one(data_course) if mark_course_item: updated_course_mark = new_mark_course != mark_course_item['mark'] if updated_course_mark: marks_course.update( data_course, {'$set': {'mark': new_mark_course}}, safe=True ) else: updated_course_mark = True data_course['mark'] = new_mark_course marks_course.insert(data_course) return updated_course_mark, has_passed_now(new_mark_course, mark_course_item, course.threshold)
def update_course_mark(db, course, user, new_mark_course=None): from moocng.courses.marks import calculate_course_mark if not new_mark_course: new_mark_course, units_info = calculate_course_mark(course, user) data_course = {} data_course['user_id'] = user.pk data_course['course_id'] = course.pk marks_course = db.get_collection('marks_course') mark_course_item = marks_course.find_one(data_course) if mark_course_item: updated_course_mark = new_mark_course != mark_course_item['mark'] if updated_course_mark: marks_course.update(data_course, {'$set': { 'mark': new_mark_course }}, safe=True) else: updated_course_mark = True data_course['mark'] = new_mark_course marks_course.insert(data_course) return updated_course_mark, has_passed_now(new_mark_course, mark_course_item, course.threshold)
def handle(self, *args, **options): if len(args) != 1: raise CommandError('Wrong number of arguments') try: if args[0].isdigit(): course = Course.objects.only('id', 'slug').get(id=args[0]) else: course = Course.objects.only('id', 'slug').get(slug=args[0]) except Course.DoesNotExist: raise CommandError('The course defined by "%s" does not exist' % args[0]) filename = options.get('filename') or '%s.csv' % course.slug # Global stats (course level), repeated in every student units = course.unit_set.all().count() kqs = KnowledgeQuantum.objects.filter( unit__course__id=course.id).count() completed_first_unit = 0 db = get_db() activity = db.get_collection('activity') rows = [] badge = course.completion_badge for student in course.students.all(): row = [ student.username, student.get_full_name().encode('utf-8', 'ignore'), student.date_joined.isoformat(), course.id, units, kqs, ] course_act = activity.find( { 'course_id': course.id, 'user_id': student.id, }, sort=[ ('_id', ASCENDING), ]) course_act_count = course_act.count() # Per students stats if course_act_count > 0: row.append(course_act[0]['_id'].generation_time.isoformat()) else: row.append('N/A') progress = (course_act_count * 100) / kqs row.append(progress) completed_units = 0 first = True for unit in course.unit_set.only('id').all(): kqs_in_unit = unit.knowledgequantum_set.count() act = activity.find({ 'user_id': student.id, 'unit_id': unit.id, }).count() if kqs_in_unit == act: completed_units += 1 if first: first = False completed_first_unit += completed_units row.append(completed_units) row.append(course_act_count) mark, _ = calculate_course_mark(course, student) row.append(mark) if badge: row.append( Award.objects.filter(badge=badge, user=student).exists()) else: row.append('N/A') rows.append(row) with open(filename, 'wb') as csvfile: writer = csv.writer(csvfile) writer.writerow([ 'email', 'full_name', 'platform_date_joined', 'course_id', 'course_units', 'course_nuggets', 'first_activity_date', 'progress_percentage', 'completed_units', 'completed_nuggets', 'score', 'got_completion_badge', ]) writer.writerows(rows)
def handle(self, *args, **options): if len(args) != 1: raise CommandError('Wrong number of arguments') try: if args[0].isdigit(): course = Course.objects.only('id', 'slug').get(id=args[0]) else: course = Course.objects.only('id', 'slug').get(slug=args[0]) except Course.DoesNotExist: raise CommandError('The course defined by "%s" does not exist' % args[0]) filename = options.get('filename') or '%s.csv' % course.slug # Global stats (course level), repeated in every student units = course.unit_set.all().count() kqs = KnowledgeQuantum.objects.filter(unit__course__id=course.id).count() completed_first_unit = 0 db = get_db() activity = db.get_collection('activity') rows = [] badge = course.completion_badge for student in course.students.all(): row = [ student.username, student.get_full_name().encode('utf-8', 'ignore'), student.date_joined.isoformat(), course.id, units, kqs, ] course_act = activity.find( { 'course_id': course.id, 'user_id': student.id, }, sort=[('_id', ASCENDING), ] ) course_act_count = course_act.count() # Per students stats if course_act_count > 0: row.append(course_act[0]['_id'].generation_time.isoformat()) else: row.append('N/A') progress = (course_act_count * 100) / kqs row.append(progress) completed_units = 0 first = True for unit in course.unit_set.only('id').all(): kqs_in_unit = unit.knowledgequantum_set.count() act = activity.find({ 'user_id': student.id, 'unit_id': unit.id, }).count() if kqs_in_unit == act: completed_units += 1 if first: first = False completed_first_unit += completed_units row.append(completed_units) row.append(course_act_count) mark, _ = calculate_course_mark(course, student) row.append(mark) if badge: row.append(Award.objects.filter(badge=badge, user=student).exists()) else: row.append('N/A') rows.append(row) with open(filename, 'wb') as csvfile: writer = csv.writer(csvfile) writer.writerow([ 'email', 'full_name', 'platform_date_joined', 'course_id', 'course_units', 'course_nuggets', 'first_activity_date', 'progress_percentage', 'completed_units', 'completed_nuggets', 'score', 'got_completion_badge', ]) writer.writerows(rows)