def add_questions(exercise_node, question_list): EXPECTED_QUESTION_TYPES = [ INPUT_QUESTION, MULTIPLE_SELECTION, SINGLE_SELECTION, FREE_RESPONSE, PERSEUS_QUESTION ] for q in question_list: question_type = q.get('question_type') if question_type not in EXPECTED_QUESTION_TYPES: LOGGER.critical(question_type) raise NotImplementedError( 'Unexpected question type found in channel json.') question_text = q.get('question') hints = q.get('hints') hints = hints if isinstance(hints, str) else [hint for hint in hints or []] if question_type == exercises.MULTIPLE_SELECTION: q_obj = questions.MultipleSelectQuestion( id=q['id'], question=question_text, correct_answers=[answer for answer in q['correct_answers']], all_answers=[answer for answer in q['all_answers']], hints=hints, ) exercise_node.add_question(q_obj) elif question_type == exercises.SINGLE_SELECTION: q_obj = questions.SingleSelectQuestion( id=q['id'], question=question_text, correct_answer=q['correct_answer'], all_answers=[answer for answer in q['all_answers']], hints=hints, ) exercise_node.add_question(q_obj) elif question_type == exercises.INPUT_QUESTION: q_obj = questions.InputQuestion( id=q['id'], question=question_text, answers=[answer for answer in q['answers']], hints=hints, ) exercise_node.add_question(q_obj) elif question_type == exercises.PERSEUS_QUESTION: q_obj = questions.PerseusQuestion( id=q['id'], raw_data=q.get('item_data'), source_url="https://www.khanacademy.org/", ) exercise_node.add_question(q_obj) else: raise UnknownQuestionTypeError( "Unrecognized question type '{0}': accepted types are {1}". format(question_type, [key for key, value in exercises.question_choices]))
def create_question(raw_question): if raw_question["type"] == exercises.MULTIPLE_SELECTION: return questions.MultipleSelectQuestion( id=raw_question["id"], question=raw_question["question"], correct_answers=raw_question["correct_answers"], all_answers=raw_question["all_answers"], hints=raw_question.get("hints"), randomize = True, ) if raw_question["type"] == exercises.SINGLE_SELECTION: return questions.SingleSelectQuestion( id=raw_question["id"], question=raw_question["question"], correct_answer=raw_question["correct_answer"], all_answers=raw_question["all_answers"], hints=raw_question.get("hints"), randomize = True ) if raw_question["type"] == exercises.INPUT_QUESTION: return questions.InputQuestion( id=raw_question["id"], question=raw_question["question"], answers=raw_question["answers"], hints=raw_question.get("hints"), ) else: raise UnknownQuestionTypeError("Unrecognized question type '{0}': accepted types are {1}".format(raw_question["type"], [key for key, value in exercises.question_choices]))
def create_question(raw_question): question = parse_images(raw_question.get('question')) hints = raw_question.get('hints') hints = parse_images(hints) if isinstance( hints, str) else [parse_images(hint) for hint in hints or []] if raw_question["type"] == exercises.MULTIPLE_SELECTION: return questions.MultipleSelectQuestion( id=raw_question["id"], question=question, correct_answers=[ parse_images(answer) for answer in raw_question['correct_answers'] ], all_answers=[ parse_images(answer) for answer in raw_question['all_answers'] ], hints=hints, ) if raw_question["type"] == exercises.SINGLE_SELECTION: return questions.SingleSelectQuestion( id=raw_question["id"], question=question, correct_answer=parse_images(raw_question['correct_answer']), all_answers=[ parse_images(answer) for answer in raw_question['all_answers'] ], hints=hints, ) if raw_question["type"] == exercises.INPUT_QUESTION: return questions.InputQuestion( id=raw_question["id"], question=question, answers=[ parse_images(answer) for answer in raw_question['answers'] ], hints=hints, ) if raw_question["type"] == exercises.PERSEUS_QUESTION: return questions.PerseusQuestion( id=raw_question["id"], raw_data=parse_images(raw_question.get('item_data')), source_url="https://www.google.com/", ) else: raise UnknownQuestionTypeError( "Unrecognized question type '{0}': accepted types are {1}".format( raw_question["type"], [key for key, value in exercises.question_choices]))
def create_exercise_questions(exercise_node, exercise_data): """ Generates exercise questions based on data Args: - exercise_node (ExerciseNode) node to add questions to - exercise_data (dict) data regarding exercises to create Returns None """ # Iterate through exercise question data for question_index, question in enumerate(exercise_data): # Generate a unique assessment_id for each question assessment_id = "{}- {}".format(exercise_node.source_id, question_index) # Filter list of answers to be added to the questions correct_answers = [ answer for answer, correct in question['answers'].items() if correct ] all_answers = [answer for answer, _ in question['answers'].items()] # Create a multiple selection question if specified if question["type"] == exercises.MULTIPLE_SELECTION: exercise_node.add_question( questions.MultipleSelectQuestion( id=assessment_id, question=question['question'], correct_answers=correct_answers, all_answers=all_answers, )) # Create a single selection question if specified elif question["type"] == exercises.SINGLE_SELECTION: exercise_node.add_question( questions.SingleSelectQuestion( id=assessment_id, question=question['question'], correct_answer=correct_answers[0], all_answers=all_answers))