def post(self, q_id): global errorFactor # required only for taking answers user = users.get_current_user() if not user: self.redirect(users.create_login_url(self.request.uri)) userState = fetchGlobal(user) a_id = self.request.get("answer") question = None answer = None try: question = fetchQuestion(int(q_id)) answer = getAnswer(int(a_id)) except dbhelper.InvalidIdError: self.response.out.write("F") return if answer is not None: result = insertQuestionAnswered(user, question.key, answer.key, evaluation=True) self.response.out.write(result) else: # invalid answer given self.response.out.write("F") # decide if data is sufficient to end test if userState.inflexion_1 and userState.inflexion_2: (b_mle, se) = calculateMLE(userState.theta, user) if se < errorFactor: # precise enough, end test userState.isTestFinished = True userState.put() return
def getNextQuestion(global_state, user): global jumpFactor # get the list of all questions faced allFaced = fetchAllQuestionsParamsTestModule(user) userState = fetchGlobal(user) allFacedCount = len(allFaced) print allFaced # get the last two questions, observe the trend if allFacedCount >= 2: (a1, b1, c1, sec_last) = allFaced[len(allFaced) - 2] (a2, b2, c2, last) = allFaced[len(allFaced) - 1] else: # faced only 1 question till now (a1, b1, c1, sec_last) = allFaced[len(allFaced) - 1] (a2, b2, c2, last) = allFaced[len(allFaced) - 1] # filling with duplicate values lasts = [] if sec_last == globals.correctAnswer: lasts.append(True) else: lasts.append(False) if last == globals.correctAnswer: lasts.append(True) else: lasts.append(False) print lasts # calculate the nature of next question # choose next question according to nature (nextNature, inf1, inf2) = calculateNatureOfNextQuestion(lasts, userState.inflexion_1, userState.inflexion_2) userState.inflexion_1 = inf1 userState.inflexion_2 = inf2 qs = None if nextNature == globals.tougherQuestion: try: qs = fetchMoreDifficultQuestion(userState.theta * jumpFactor, user) except NoMoreQuestionError: # handle error with a page, terminate test raise print "Asking tougher" userState.theta = qs.b if nextNature == globals.easierQuestion: try: qs = fetchLessDifficultQuestion(userState.theta, user) except NoMoreQuestionError: raise print "Asking easier" userState.theta = qs.b if nextNature == globals.maxInfoQuestion: (b_mle, se) = calculateMLE(userState.theta, user) userState.theta = b_mle try: qs = fetchMostInformativeQuestion(userState, user) except NoMoreQuestionError: raise print "Asking max info" # test is finished if userState.inflexion_1 and userState.inflexion_2: (b_mle, se) = calculateMLE(userState.theta, user) if se < errorFactor: # precise enough, end test userState.isTestFinished = True # userState.isTestFinished=True # the latest(maximum possible) theta estimation, according to correctness of last answer # at the end, save user state userState.put() return qs