def switch_course_open_status(request, course_id, is_open): if not isInstructor(request): return invalidAccess() course_id = int(course_id) course = Course.objects.filter(id=course_id) if course.count() == 0: return invalidAccess() if course[0].instructor.id != request.session['user_id']: return invalidAccess() course.update(is_open=is_open) return redirect('/course/' + str(course_id))
def index(request, course_id, module_id): if isInstructor(request): course = Course.objects.filter(id=course_id) module = Module.objects.filter(id=module_id, course=course_id) components = Component.objects.filter(module=module_id).order_by('order_no') if course.count() == 0 or module.count() == 0: return invalidAccess() course = course[0] module = module[0] if course.instructor.id != request.session['user_id']: return invalidAccess() output = {'course': course, 'module': module, 'components': components} return HttpResponse(render(request, 'course/module/index.html', output)) return invalidAccess()
def add(request): if not isInstructor(request): return invalidAccess() if 'name' not in request.POST or 'category' not in request.POST or 'description' not in request.POST: output = {'categories': Category.objects.all()} return HttpResponse(render(request, 'course/add.html', output)) name = request.POST['name'] category = Category.objects.filter(id=request.POST['category'])[0] description = request.POST['description'] instructor = User.objects.filter(id=request.session['user_id'])[0] course = Course(name=name, category=category, description=description, instructor=instructor, is_open=0) course.save() return HttpResponse(course.id)
def add(request, course_id): if not isInstructor(request): return invalidAccess() course = Course.objects.filter(id=course_id) if course.count() == 0: return invalidAccess() course = course[0] if 'name' not in request.POST: output = {'course': course} return HttpResponse(render(request, 'course/module/add.html', output)) if course.instructor.id != request.session['user_id']: return invalidAccess() name = request.POST['name'] lastModule = Module.objects.filter(course=course_id).order_by('-order_no') order_no = lastModule.count() module = Module(name=name, course=course, order_no=order_no) module.save() return HttpResponse(module.id)
def add(request, course_id, module_id): if not isInstructor(request): return invalidAccess() course = Course.objects.filter(id=course_id) module = Module.objects.filter(id=module_id, course=course_id) if course.count() == 0 or module.count() == 0: return invalidAccess() course = course[0] module = module[0] if 'name' not in request.POST or 'type' not in request.POST: types = ComponentType.objects.all().order_by('id') output = {'course': course, 'module': module, 'types': types} return HttpResponse( render(request, 'course/module/component/add.html', output)) if course.instructor.id != request.session['user_id']: return invalidAccess() name = request.POST['name'] type = ComponentType.objects.filter(id=request.POST['type']) if type.count() == 0: return invalidAccess() type = type[0] def create_specific_component(type): switcher = { "Text": createNewText, "Image": createNewImage, "File": createNewFile, "Quiz": createNewQuiz, "Youtube": createNewYoutube, } return switcher.get(type.name)() reference = create_specific_component(type) lastComponent = Component.objects.filter( module=module_id).order_by('order_no') order_no = lastComponent.count() component = Component(name=name, module=module, type=type, order_no=order_no, reference=reference) component.save() return redirect('/course/' + str(course_id) + '/module/' + str(module_id) + '/component/' + str(component.id))
def save_order(request, course_id): if not isInstructor(request) or 'order' not in request.POST: return invalidAccess() course_id = int(course_id) course = Course.objects.filter(id=course_id) if course.count() == 0: return invalidAccess() course = course[0] if course.instructor.id != request.session['user_id']: return invalidAccess() order = json.loads(request.POST['order']) for val in order: module = Module.objects.filter(id=val) if module.count() == 0 or module[0].course.id != course_id: return invalidAccess() for i, val in enumerate(order): module = Module.objects.filter(id=val) module.update(order_no=i) return HttpResponse('success')
def index(request, course_id): course = Course.objects.filter(id=course_id) if course.count() == 0: return invalidAccess() course = course[0] modules = Module.objects.filter(course=course_id).order_by('order_no') if isParticipant(request): if course.is_open == 0: return HttpResponse('Course is closed') enrolledCourse = EnrolledCourse.objects.filter( course=course_id, user=request.session['user_id']) if enrolledCourse.count() == 0: return invalidAccess() enrolledCourse = enrolledCourse[0] inProgress = Status.objects.filter(name="In Progress")[0].id completed = Status.objects.filter(name="Completed")[0].id retake = Status.objects.filter(name="Retake")[0].id currentStatus = enrolledCourse.status.id if currentStatus != inProgress and currentStatus != completed and currentStatus != retake: return invalidAccess() for i, module in enumerate(modules): components = Component.objects.filter( module=module).order_by('order_no') modules[i].components = components module_progress = ModuleProgress.objects.filter( enrolled_course=enrolledCourse, module=module) if module_progress.count() == 0: module_progress = ModuleProgress( enrolled_course=enrolledCourse, module=module, status=enrolledCourse.status) module_progress.save() else: module_progress = module_progress[0] modules[i].module_progress = module_progress if i == 0: modules[i].can_view = True if module.components.count() == 0: ModuleProgress.objects.filter( id=module_progress.id).update(status=completed) if i < modules.count() - 1: modules[i + 1].can_view = False if module_progress.status.id == completed: modules[i + 1].can_view = True if modules[i].can_view == True and module.components.count( ) == 0: modules[i + 1].can_view = True ModuleProgress.objects.filter( id=module_progress.id).update(status=completed) if i == modules.count() - 1: if modules[i].can_view == True and module.components.count( ) == 0: ModuleProgress.objects.filter( id=module_progress.id).update(status=completed) module_progress = ModuleProgress.objects.filter( id=module_progress.id)[0] if module_progress.status.id == completed: if enrolledCourse.status.id != completed: EnrolledCourse.objects.filter( course=course_id, user=request.session['user_id']).update( status=completed, completion_date=datetime.now()) output = {'course': course, 'modules': modules} output = getEnrolledCourse(request, output) if currentStatus == inProgress or currentStatus == retake: output['action'] = 'drop' if currentStatus == completed: if EnrolledCourse.objects.filter( user=request.session['user_id']).filter( Q(status__name="In Progress") | Q(status__name="Retake")).count() == 0: output['action'] = 'retake' return HttpResponse( render(request, 'course/index-participant.html', output)) if isInstructor(request): if course.instructor.id != request.session['user_id']: return InvalidAccess() output = {'course': course, 'modules': modules} return HttpResponse( render(request, 'course/index-instructor.html', output)) return HttpResponse('Unknown Request')
def index(request, course_id, module_id, component_id): if isParticipant(request): course_id = int(course_id) component = Component.objects.filter(id=component_id, module=module_id) if component.count() == 0: return invalidAccess() component = component[0] if component.module.course.id != course_id: return invalidAccess() inProgress = Status.objects.filter(name="In Progress")[0] completed = Status.objects.filter(name="Completed")[0] dropped = Status.objects.filter(name="Dropped")[0].id enrolledCourse = EnrolledCourse.objects.filter( course=course_id, user=request.session['user_id']) if enrolledCourse.count() == 0: return redirect('/course/' + str(course_id) + '/overview') enrolledCourse = enrolledCourse[0] currentStatus = enrolledCourse.status.id if currentStatus == dropped: return redirect('/course/' + str(course_id) + '/overview') moduleProgress = ModuleProgress.objects.filter( enrolled_course=enrolledCourse.id, module=module_id) if moduleProgress.count() == 0: module = Module.objects.filter(id=module_id, course=course_id) moduleProgress = ModuleProgress(enrolled_course=enrolledCourse, module=module, status=inProgress) moduleProgress.save() else: moduleProgress = moduleProgress[0] componentProgress = ComponentProgress.objects.filter( module_progress=moduleProgress, component=component_id) if componentProgress.count() == 0: component = Component.objects.filter(id=component_id, module=module_id) componentProgress = ComponentProgress( module_progress=moduleProgress, component=component, status=completed) else: componentProgress.update(status=completed) componentProgressTotal = ComponentProgress.objects.filter( module_progress=moduleProgress).count() componentProgressCompleted = ComponentProgress.objects.filter( module_progress=moduleProgress, status=completed.id).count() if componentProgressCompleted == componentProgressTotal: moduleProgress = ModuleProgress.objects.filter( enrolled_course=enrolledCourse.id, module=module_id) moduleProgress.update(status=completed) output = {'component': component} output = getEnrolledCourse(request, output) def view_specific_component(request, output): switcher = { "Text": viewText, "Image": viewImage, "File": viewFile, "Quiz": viewQuiz, "Youtube": viewYoutube, } type = output['component'].type.name reference = output['component'].reference return switcher.get(type)(request, output, reference) return view_specific_component(request, output) if isInstructor(request): course = Course.objects.filter(id=course_id) module = Module.objects.filter(id=module_id, course=course_id) component = Component.objects.filter(id=component_id, module=module_id) if course.count() == 0 or module.count() == 0 or component.count( ) == 0: return invalidAccess() course = course[0] module = module[0] component = component[0] if course.instructor.id != request.session['user_id']: return InvalidAccess() output = {'course': course, 'module': module, 'component': component} if request.method == 'POST': def save_specific_component(request, output): switcher = { "Text": saveText, "Image": saveImage, "File": saveFile, "Quiz": saveQuiz, "Youtube": saveYoutube, } type = output['component'].type.name reference = output['component'].reference return switcher.get(type)(request, output, reference) return save_specific_component(request, output) def render_specific_component(request, output): switcher = { "Text": renderText, "Image": renderImage, "File": renderFile, "Quiz": renderQuiz, "Youtube": renderYoutube, } type = output['component'].type.name reference = output['component'].reference return switcher.get(type)(request, output, reference) return render_specific_component(request, output) return invalidAccess()