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 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 courses = obj.courses_as_student.all() passed_courses = [] 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 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 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} 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 teacheradmin_stats(request, course_slug): course = get_object_or_404(Course, slug=course_slug) is_enrolled = course.students.filter(id=request.user.id).exists() activity = get_db().get_collection('activity') data = { 'enrolled': course.students.count(), 'started': 0, 'completed': 0 } if getattr(course, "threshold", None) is not None: # if the course doesn't support certification, then don't return the # 'passed' stat since it doesn't apply data['passed'] = 0 for student in course.students.all(): if calculate_course_mark(course, student)[0] >= float(course.threshold): data['passed'] += 1 units = course.unit_set.all() kqs = 0 for unit in units: kqs += unit.knowledgequantum_set.count() for student in course.students.all(): user_activity_list = activity.find_one({'user': student.id}, safe=True) if user_activity_list is not None: visited_kqs = user_activity_list.get('courses', {}).get(unicode(course.id), {}).get('kqs', []) if len(visited_kqs) > 0: data['started'] += 1 if len(visited_kqs) == kqs: data['completed'] += 1 return render_to_response('teacheradmin/stats.html', { 'course': course, 'is_enrolled': is_enrolled, 'initial_data': simplejson.dumps(data), }, context_instance=RequestContext(request))
def teacheradmin_stats(request, course_slug): course = get_object_or_404(Course, slug=course_slug) is_enrolled = course.students.filter(id=request.user.id).exists() activity = get_db().get_collection('activity') data = {'enrolled': course.students.count(), 'started': 0, 'completed': 0} if getattr(course, "threshold", None) is not None: # if the course doesn't support certification, then don't return the # 'passed' stat since it doesn't apply data['passed'] = 0 for student in course.students.all(): if calculate_course_mark(course, student)[0] >= float( course.threshold): data['passed'] += 1 units = course.unit_set.all() kqs = 0 for unit in units: kqs += unit.knowledgequantum_set.count() for student in course.students.all(): user_activity_list = activity.find_one({'user': student.id}, safe=True) if user_activity_list is not None: visited_kqs = user_activity_list.get('courses', {}).get( unicode(course.id), {}).get('kqs', []) if len(visited_kqs) > 0: data['started'] += 1 if len(visited_kqs) == kqs: data['completed'] += 1 return render_to_response('teacheradmin/stats.html', { 'course': course, 'is_enrolled': is_enrolled, 'initial_data': simplejson.dumps(data), }, context_instance=RequestContext(request))