def completed(request, index): """function previous This function handles retrieving the prev lesson. Args: request (HTTPRequest): A http request object created automatically by Django. index (int): The index of the lesson to retrieve Returns: HttpResponse: A generated http response object to the request depending on whether or not the user is authenticated. """ if request.method == 'GET': if user_auth(request): current_user = UserInformation.objects.get(user=User.objects.get( email=request.user.email)) if not_complete(request): main_set = MainSet.objects.filter( set_name=current_user.current_main_set)[0] item = main_set.lessons.all()[0] for count, item in enumerate(main_set.lessons.all()): if count == index: break current_lesson = Lesson.objects.get(lesson_title=item.first_in_set) set_len = len(current_user.current_main_set.lessons.all()) print("set len: ", set_len) num_done = finished_lesson_count(current_user) # create an ordered set ordered_set = current_user.current_main_set.lessons.all() count2 = 0 for count2, item in enumerate( current_user.current_main_set.lessons.all()): if item == current_user.current_lesson_set: break if index <= count2: lesson_info = get_log_data(current_user, index) print("lesson info: ", index) return render( request, "tutor/tutor.html", { 'lesson': current_lesson, 'lesson_code': lesson_info[0], 'concept': current_lesson.lesson_concept.all(), 'referenceSet': current_lesson.reference_set.all(), 'currLessonNum': current_user.current_lesson_index, 'setLength': set_len, 'finished_count': num_done, 'orderedSet': ordered_set, 'mood': lesson_info[1], 'past': lesson_info[2], 'completedLessonNum': count2, 'review': current_lesson.correct_feedback, 'index': index }) return redirect("tutor:tutor") return redirect("accounts:profile")
def completed(request, index): """function previous This function handles retrieving the prev lesson. Args: request (HTTPRequest): A http request object created automatically by Django. index (int): The index of the lesson to retrieve Returns: HttpResponse: A generated http response object to the request depending on whether or not the user is authenticated. """ if request.method == 'GET': if user_auth(request): current_user = UserInformation.objects.get(user=User.objects.get(email=request.user.email)) if not not_complete(request): current_set = current_user.completed_sets.lessons.all() else: current_set = current_user.current_lesson_set.lessons.all() current_lesson = Lesson.objects.get(lesson_index=index) set_len = current_set.filter(is_alternate=False).count() print(set_len) num_done = finished_lesson_count(current_user) if index <= current_user.completed_lesson_index: lesson_info = get_log_data(current_user, index) print("lesson info: ", index) return render(request, "tutor/tutor.html", {'lesson': current_lesson, 'lesson_code': lesson_info[0], 'concept': current_lesson.lesson_concept.all(), 'referenceSet': current_lesson.reference_set.all(), 'currLessonNum': current_user.current_lesson_index, 'setLength': set_len, 'finished_count': num_done, 'currSet': current_set, 'mood': lesson_info[1], 'past': lesson_info[2], 'completedLessonNum': current_user.completed_lesson_index, 'review': current_lesson.correct_feedback}) return redirect("tutor:tutor") return redirect("accounts:profile")
def tutor(request): """function tutor This function handles the view for the tutor page of the application. Args: request (HTTPRequest): A http request object created automatically by Django. Returns: HttpResponse: A generated http response object to the request depending on whether or not the user is authenticated. """ print(request) # Case 1: We have received a POST request submitting code (needs a lot of work) if request.method == 'POST': # Case 1a: if the user exists if user_auth(request): # submitted_json = json.loads(request.body.decode('utf-8')) # if success, return next lesson # if fail, do something # Case 1aa: if the user has not completed set current_user = UserInformation.objects.get(user=User.objects.get(email=request.user.email)) print(current_user.current_lesson_name) current_lesson = Lesson.objects.get(lesson_name=current_user.current_lesson_name) print('current_lesson: ', current_lesson) submitted_answer = reverse_mutate(json.loads(request.body.decode('utf-8'))['answer'].replace(" ", ""), get_inv_key()) print('submitted answer: ' + submitted_answer) #Need to include submitted_answer in log data because it has the original variables log_data(request, submitted_answer) status = json.loads(request.body.decode('utf-8'))['status'] print("status: ", status) if status == "success": current_user.completed_lesson_index = current_lesson.lesson_index if Lesson.objects.filter(lesson_name=current_lesson.correct).exists(): current_user.current_lesson_index = Lesson.objects.get( lesson_name=current_lesson.correct).lesson_index else: print("Lesson does not exist") print("completed index: ", current_user.completed_lesson_index) print("current index: ", current_user.current_lesson_index) if Lesson.objects.filter(lesson_index=current_user.current_lesson_index).exists(): curr_lesson = Lesson.objects.get(lesson_index=current_user.current_lesson_index) print('curr_lesson: ', current_lesson) current_user.current_lesson_name = curr_lesson.lesson_name current_user.save() if current_user.completed_lesson_index == current_user.current_lesson_index: current_user.completed_sets = current_user.current_lesson_set current_user.current_lesson_set = None current_user.save() print("in done: ", current_user.current_lesson_set) # print(feedback['type']) # goto = alternate_lesson_check(current_lesson, feedback['type']) # feedback.update({'newLessonIndex': str(Lesson.objects.get(lesson_name=goto.lesson_name).lesson_index)}) # feedback.update({'newLessonCode': Lesson.objects.get(lesson_name=goto.lesson_name).code.lesson_code}) # feedback.update({'newLessonEx': Lesson.objects.get(lesson_name=goto.lesson_name).reason.reasoning_type}) return JsonResponse(check_feedback(current_lesson, submitted_answer, status)) # Case 2: We have received a GET request requesting code elif request.method == 'GET': # Ensure user exists # Case 2a: if the user exists print(not_complete(request)) if user_auth(request) and not_complete(request): # Case 2aa: if the user has a current set current_user = UserInformation.objects.get(user=User.objects.get(email=request.user.email)) current_set = current_user.current_lesson_set.lessons.all() set_len = current_set.filter(is_alternate=False).count() print(set_len) num_done = finished_lesson_count(current_user) # Case 2aaa: if the current set has a lesson of index that the user is on, set to current lesson if Lesson.objects.filter(lesson_index=current_user.current_lesson_index).exists(): current_lesson = Lesson.objects.get(lesson_index=current_user.current_lesson_index) # check if mutatable then mutate: code_after_mutate = can_mutate(request) if current_lesson.reason.reasoning_type == 'MC' or current_lesson.reason.reasoning_type == 'Both': return render(request, "tutor/tutor.html", {'lesson': current_lesson, 'lesson_code': code_after_mutate, 'concept': current_lesson.lesson_concept.all(), 'referenceSet': current_lesson.reference_set.all(), 'reason': current_lesson.reason.reasoning_question, 'mc_set': current_lesson.reason.mc_set.all(), 'currLessonNum': current_user.current_lesson_index, 'completedLessonNum': current_user.completed_lesson_index, 'setLength': set_len, 'finished_count': num_done, 'currSet': current_set, 'mood': current_user.mood, 'review': 'none', 'screen_record': current_lesson.screen_record, 'audio_record': current_lesson.audio_record, 'audio_transcribe': current_lesson.audio_transcribe, 'user_email': request.user.email}) # Case 2aaab: if question is of type Text elif current_lesson.reason.reasoning_type == 'Text': return render(request, "tutor/tutor.html", {'lesson': current_lesson, 'lesson_code': code_after_mutate, 'concept': current_lesson.lesson_concept.all(), 'referenceSet': current_lesson.reference_set.all(), 'reason': current_lesson.reason.reasoning_question, 'currLessonNum': current_user.current_lesson_index, 'completedLessonNum': current_user.completed_lesson_index, 'setLength': set_len, 'finished_count': num_done, 'currSet': current_set, 'mood': current_user.mood, 'review': 'none', 'screen_record': current_lesson.screen_record, 'audio_record': current_lesson.audio_record, 'audio_transcribe': current_lesson.audio_transcribe, 'user_email': request.user.email}) # Case 2aaac: if question is of type none return render(request, "tutor/tutor.html", {'lesson': current_lesson, 'lesson_code': code_after_mutate, 'concept': current_lesson.lesson_concept.all(), 'referenceSet': current_lesson.reference_set.all(), 'currLessonNum': current_user.current_lesson_index, 'completedLessonNum': current_user.completed_lesson_index, 'setLength': set_len, 'finished_count': num_done, 'currSet': current_set, 'mood': current_user.mood, 'review': 'none', 'screen_record': current_lesson.screen_record, 'audio_record': current_lesson.audio_record, 'audio_transcribe': current_lesson.audio_transcribe, 'user_email': request.user.email}) return redirect("accounts:profile")
def tutor(request): """function tutor This function handles the view for the tutor page of the application. Args: request (HTTPRequest): A http request object created automatically by Django. Returns: HttpResponse: A generated http response object to the request depending on whether or not the user is authenticated. """ print(request) # Case 1: We have received a POST request submitting code (needs a lot of work) if request.method == 'POST': # Case 1a: if the user exists if user_auth(request): # submitted_json = json.loads(request.body.decode('utf-8')) # if success, return next lesson # if fail, do something # Case 1aa: if the user has not completed set current_user = UserInformation.objects.get(user=User.objects.get( email=request.user.email)) current_lesson = Lesson.objects.get( lesson_name=current_user.current_lesson_name) status = json.loads(request.body.decode('utf-8'))['status'] print("status: ", status) submitted_answer = json.loads( request.body.decode('utf-8'))['answer'].replace(" ", "") submitted_answer = reverse_mutate(submitted_answer) log_data(request) unlock_next = False if status == "success": log_lesson(request) main_set = MainSet.objects.filter( set_name=current_user.current_main_set)[0] print(main_set) # if they are correct in a alt lesson, find correct to send to print("current_lesson.is_alternate: ", current_lesson.is_alternate, " current_user.current_lesson_index: ", current_user.current_lesson_index) if current_lesson.is_alternate and current_user.current_lesson_index != 0: print(current_lesson.correct, "%%%%%%%%%%") current_user.current_lesson_name = Lesson.objects.get( lesson_name=current_lesson.correct).lesson_name index = 0 current_set = Lesson.objects.get( lesson_name=current_user.current_lesson_name) print("CURRENT LESSON NAME: ", current_set, " ***** ", current_user.current_lesson_set.lessons.all()) if current_set in current_user.current_lesson_set.lessons.all( ): print("PRINT LESSONS: ", current_user.current_lesson_set.lessons.all()) #if current_user.current_lesson_name in current_user.current_lesson_set.lessons.all(): for index, item in enumerate( current_user.current_lesson_set.lessons.all()): print(index, "&&&&&&&&&", item.lesson_name) if item.lesson_name == current_lesson.correct: break current_user.current_lesson_index = index else: for index, item in enumerate(main_set.lessons.all()): if item == current_user.current_lesson_set: break next_set = LessonSet.objects.get( set_name=main_set.lessons.all()[index + 1]) print("***************** ", next_set) current_user.current_lesson_set = next_set current_user.current_lesson_name = next_set.first_in_set.lesson_name current_user.current_lesson_index = 0 current_user.save() unlock_next = True return JsonResponse( check_feedback(current_lesson, submitted_answer, status, unlock_next)) # find the index of the next lesson set by enumerating query set of all sets for index, item in enumerate(main_set.lessons.all()): if item == current_user.current_lesson_set: break # return if last set to go through print("|||||||", index, "|||||||", len(main_set.lessons.all())) if index + 1 >= len(main_set.lessons.all()): current_user.completed_sets.add( current_user.current_main_set) current_user.current_lesson_set = None current_user.current_main_set = None current_user.save() unlock_next = True print("in done: ", current_user.current_lesson_set) return JsonResponse( check_feedback(current_lesson, submitted_answer, status, unlock_next)) # return render(request, "accounts:profile") next_set = LessonSet.objects.get( set_name=main_set.lessons.all()[index + 1]) current_user.current_lesson_set = next_set current_user.current_lesson_name = next_set.first_in_set.lesson_name current_user.save() # if a user is not successful and there are alternates available print(current_lesson.sub_lessons_available, "%%%%%%%%%%") if status != "success" and current_lesson.sub_lessons_available: lesson_type = check_type(current_lesson, submitted_answer, status) alt_lesson = alternate_lesson_check( current_lesson, lesson_type) # how to set this and render new page # check if we changed to an alternate if Lesson.objects.get( lesson_title=alt_lesson ).lesson_name != current_user.current_lesson_name: unlock_next = True current_user.current_lesson_name = Lesson.objects.get( lesson_title=alt_lesson).lesson_name for index, item in enumerate( current_user.current_lesson_set.lessons.all()): if item == alt_lesson: break current_user.current_lesson_index = index current_user.save() print("******* ", alt_lesson, " ", index) return JsonResponse( check_feedback(current_lesson, submitted_answer, status, unlock_next)) # Case 2: We have received a GET request requesting code elif request.method == 'GET': # Ensure user exists # Case 2a: if the user exists print("in the get") if user_auth(request) and not_complete(request): # Case 2aa: if the user has a current set current_user = UserInformation.objects.get(user=User.objects.get( email=request.user.email)) current_set = current_user.current_lesson_set.lessons.all() set_len = len(current_user.current_main_set.lessons.all()) print(set_len) num_done = finished_lesson_count(current_user) print("===============", num_done) # Case 2aaa: if the current set has a lesson of index that the user is on, set to current lesson if Lesson.objects.filter(lesson_title=current_set[ current_user.current_lesson_index]).exists(): current_lesson = Lesson.objects.get(lesson_title=current_set[ current_user.current_lesson_index]) print("in if 1 - Current lesson: ", current_lesson) # Just as we are altering the code here with mutate, this will pull the previous answer # to put in place for sub lessons. What identifiers do we need? current_lesson.code.lesson_code = can_mutate(current_lesson) current_lesson.code.lesson_code = replace_previous( current_user, current_lesson.code.lesson_code, current_lesson.is_alternate) # create an ordered set index = 0 for index, item in enumerate( current_user.current_main_set.lessons.all()): if item == current_user.current_lesson_set: break if current_lesson.reason.reasoning_type == 'MC' or current_lesson.reason.reasoning_type == 'Both': return render( request, "tutor/tutor.html", { 'lesson': current_lesson, 'lesson_code': current_lesson.code.lesson_code, 'concept': current_lesson.lesson_concept.all(), 'referenceSet': current_lesson.reference_set.all(), 'reason': current_lesson.reason.reasoning_question, 'mc_set': current_lesson.reason.mc_set.all(), 'currLessonNum': current_user.current_lesson_index, 'completedLessonNum': current_user.completed_lesson_index, 'setLength': set_len, 'finished_count': num_done, 'orderedSet': current_user.current_main_set.lessons.all(), 'mood': current_user.mood, 'review': 'none', 'screen_record': current_lesson.screen_record, 'audio_record': current_lesson.audio_record, 'audio_transcribe': current_lesson.audio_transcribe, 'user_email': request.user.email, 'index': index }) # Case 2aaab: if question is of type Text elif current_lesson.reason.reasoning_type == 'Text': return render( request, "tutor/tutor.html", { 'lesson': current_lesson, 'lesson_code': current_lesson.code.lesson_code, 'concept': current_lesson.lesson_concept.all(), 'referenceSet': current_lesson.reference_set.all(), 'reason': current_lesson.reason.reasoning_question, 'currLessonNum': current_user.current_lesson_index, 'completedLessonNum': current_user.completed_lesson_index, 'setLength': set_len, 'finished_count': num_done, 'orderedSet': current_user.current_main_set.lessons.all(), 'mood': current_user.mood, 'review': 'none', 'screen_record': current_lesson.screen_record, 'audio_record': current_lesson.audio_record, 'audio_transcribe': current_lesson.audio_transcribe, 'user_email': request.user.email, 'index': index }) # Case 2aaac: if question is of type none return render( request, "tutor/tutor.html", { 'lesson': current_lesson, 'lesson_code': current_lesson.code.lesson_code, 'concept': current_lesson.lesson_concept.all(), 'referenceSet': current_lesson.reference_set.all(), 'currLessonNum': current_user.current_lesson_index, 'completedLessonNum': current_user.completed_lesson_index, 'setLength': set_len, 'finished_count': num_done, 'orderedSet': current_user.current_main_set.lessons.all(), 'mood': current_user.mood, 'review': 'none', 'screen_record': current_lesson.screen_record, 'audio_record': current_lesson.audio_record, 'audio_transcribe': current_lesson.audio_transcribe, 'user_email': request.user.email, 'index': index }) return redirect("accounts:profile")
def tutor(request, assignmentID): """function tutor This function handles giving code to the browser. Args: request (HTTPRequest): A http request object created automatically by Django. assignmentID (Integer): ID of the assignment that the user is requesting code for Returns: HttpResponse: A generated http response object to the request depending on whether or not the user is authenticated. """ if request.method == 'GET': if assignment_auth(request, assignmentID): # Case 2a: User is valid and is taking this assignment current_user = UserInformation.objects.get(user=User.objects.get( email=request.user.email)) assignment = Assignment.objects.get(id=assignmentID) _, set_index, current_lesson, _, is_alternate = \ assignment.get_user_lesson(current_user.id) num_done = finished_lesson_count(current_user) if current_lesson.is_parsons: return redirect('parsons:parsons', assignmentID) # Just as we are altering the code here with mutate, this will pull the previous answer # to put in place for sub lessons. What identifiers do we need? current_lesson.code.lesson_code = can_mutate(current_lesson) current_lesson.code.lesson_code = replace_previous( current_user, current_lesson.code.lesson_code, is_alternate) current_lesson.code.lesson_code = clean_variable( current_lesson.code.lesson_code) # Case 2aa: if questions if MC or Both if current_lesson.reason.reasoning_type in ('MC', 'Both'): return render( request, "tutor/tutor.html", { 'lesson': current_lesson, 'assignment': assignment, 'lesson_code': current_lesson.code.lesson_code, 'concept': current_lesson.lesson_concept.all(), 'referenceSet': current_lesson.reference_set, 'reason': current_lesson.reason.reasoning_question, 'mc_set': current_lesson.reason.mc_set.all(), 'setLength': assignment.main_set.length(), 'finished_count': num_done, 'orderedSet': assignment.main_set.sets(), 'mood': current_user.mood, 'review': 'none', 'screen_record': current_lesson.screen_record, 'audio_record': current_lesson.audio_record, 'audio_transcribe': current_lesson.audio_transcribe, 'user_email': request.user.email, 'index': set_index, 'isParsons': current_lesson.is_parsons }) # Case 2ab: if question is of type Text elif current_lesson.reason.reasoning_type == 'Text': return render( request, "tutor/tutor.html", { 'lesson': current_lesson, 'assignment': assignment, 'lesson_code': current_lesson.code.lesson_code, 'concept': current_lesson.lesson_concept.all(), 'referenceSet': current_lesson.reference_set, 'reason': current_lesson.reason.reasoning_question, 'setLength': assignment.main_set.length(), 'finished_count': num_done, 'orderedSet': assignment.main_set.sets(), 'mood': current_user.mood, 'review': 'none', 'screen_record': current_lesson.screen_record, 'audio_record': current_lesson.audio_record, 'audio_transcribe': current_lesson.audio_transcribe, 'user_email': request.user.email, 'index': set_index, 'isParsons': current_lesson.is_parsons }) # Case 2ac: if question is of type none return render( request, "tutor/tutor.html", { 'lesson': current_lesson, 'assignment': assignment, 'lesson_code': current_lesson.code.lesson_code, 'concept': current_lesson.lesson_concept.all(), 'referenceSet': current_lesson.reference_set, 'setLength': assignment.main_set.length(), 'finished_count': num_done, 'orderedSet': assignment.main_set.sets(), 'mood': current_user.mood, 'review': 'none', 'screen_record': current_lesson.screen_record, 'audio_record': current_lesson.audio_record, 'audio_transcribe': current_lesson.audio_transcribe, 'user_email': request.user.email, 'index': set_index, 'isParsons': current_lesson.is_parsons }) return redirect("accounts:profile")
def parsons_problem(request, assignmentID): if request.method == 'GET': if assignment_auth(request, assignmentID): # Case 2a: User is valid and is taking this assignment current_user = UserInformation.objects.get(user=User.objects.get(email=request.user.email)) assignment = Assignment.objects.get(id=assignmentID) _, set_index, current_lesson, _, is_alternate = \ assignment.get_user_lesson(current_user.id) num_done = finished_lesson_count(current_user) if not current_lesson.is_parsons: return redirect('tutor:tutor', assignmentID) # Just as we are altering the code here with mutate, this will pull the previous answer # to put in place for sub lessons. What identifiers do we need? current_lesson.code.lesson_code = can_mutate(current_lesson) current_lesson.code.lesson_code = replace_previous(current_user, current_lesson.code.lesson_code, is_alternate) current_lesson.code.lesson_code = clean_variable(current_lesson.code.lesson_code) lesson_code = split_lesson_code(current_lesson) # Case 2aa: if questions if MC or Both if current_lesson.reason.reasoning_type == 'MC' or current_lesson.reason.reasoning_type == 'Both': return render(request, 'parsons/parsons_problem.html', {'lesson': current_lesson, 'assignment': assignment, 'concept': current_lesson.lesson_concept.all(), 'referenceSet': current_lesson.reference_set, 'reason': current_lesson.reason.reasoning_question, 'mc_set': current_lesson.reason.mc_set.all(), 'setLength': assignment.main_set.length(), 'finished_count': num_done, 'orderedSet': assignment.main_set.sets(), 'mood': current_user.mood, 'review': 'none', 'screen_record': current_lesson.screen_record, 'audio_record': current_lesson.audio_record, 'audio_transcribe': current_lesson.audio_transcribe, 'user_email': request.user.email, 'index': set_index, 'beginSetCode': lesson_code['begin_set'], 'endSetCode': lesson_code['end_set'], 'sortCode': lesson_code['sort'], 'comments': lesson_code['comments'], 'confirms': lesson_code['confirms'], 'multiConfirms': current_lesson.multi_confirms, 'isParsons': current_lesson.is_parsons, 'hasDistractors': current_lesson.has_distractors}) # Case 2ab: if question is of type Text elif current_lesson.reason.reasoning_type == 'Text': return render(request, 'parsons/parsons_problem.html', {'lesson': current_lesson, 'assignment': assignment, 'concept': current_lesson.lesson_concept.all(), 'referenceSet': current_lesson.reference_set, 'reason': current_lesson.reason.reasoning_question, 'setLength': assignment.main_set.length(), 'finished_count': num_done, 'orderedSet': assignment.main_set.sets(), 'mood': current_user.mood, 'review': 'none', 'screen_record': current_lesson.screen_record, 'audio_record': current_lesson.audio_record, 'audio_transcribe': current_lesson.audio_transcribe, 'user_email': request.user.email, 'index': set_index, 'beginSetCode': lesson_code['begin_set'], 'endSetCode': lesson_code['end_set'], 'sortCode': lesson_code['sort'], 'comments': lesson_code['comments'], 'confirms': lesson_code['confirms'], 'multiConfirms': current_lesson.multi_confirms, 'isParsons': current_lesson.is_parsons, 'hasDistractors': current_lesson.has_distractors}) # Case 2ac: if question is of type none return render(request, 'parsons/parsons_problem.html', {'lesson': current_lesson, 'assignment': assignment, 'concept': current_lesson.lesson_concept.all(), 'referenceSet': current_lesson.reference_set, 'setLength': assignment.main_set.length(), 'finished_count': num_done, 'orderedSet': assignment.main_set.sets(), 'mood': current_user.mood, 'review': 'none', 'screen_record': current_lesson.screen_record, 'audio_record': current_lesson.audio_record, 'audio_transcribe': current_lesson.audio_transcribe, 'user_email': request.user.email, 'index': set_index, 'beginSetCode': lesson_code['begin_set'], 'endSetCode': lesson_code['end_set'], 'sortCode': lesson_code['sort'], 'comments': lesson_code['comments'], 'confirms': lesson_code['confirms'], 'multiConfirms': current_lesson.multi_confirms, 'isParsons': current_lesson.is_parsons, 'hasDistractors': current_lesson.has_distractors}) return redirect("accounts:profile")