Example #1
0
 def decode_json_raw(cls, registry: Registry, data, args):
     result = QuizQuestion.decode_question_common(registry, data, args)
     if not args.hide_answers:
         comments = CommentedMap()
         for answer in data['answers']:
             if answer['text'] == 'True':
                 result['answer'] = True if answer['weight'] else False
                 if answer.get('comments_html'):
                     comments['if_true_chosen'] = h2m(
                         answer['comments_html'])
             elif answer.get('comments_html'):
                 comments['if_false_chosen'] = h2m(answer['comments_html'])
         if comments and any(comments.values()):
             result['comments'] = comments
     return result
Example #2
0
 def decode_question(cls, registry: Registry, question, quiz, args):
     question_type = cls.TYPES[question['question_type']]
     if args.combine:
         raw = question_type.decode_json_raw(registry, question, args)
         raw['text'] = h2m(raw['text'])
         return raw, None, None
     local = registry.get_service(args.local_service, 'local')
     title = question['question_name']
     try:
         destination_path = local.find_existing(registry,
                                                args.title,
                                                folder_file=title)
     except FileNotFoundError:
         destination_path = local.make_markdown_filename(title)
         if args.banks:
             first_bank_path = args.banks[0].format(
                 title=make_safe_filename(title),
                 id=question['id'],
                 quiz_title=make_safe_filename(quiz['title']),
                 quiz_id=quiz['id'])
             destination_path = os.path.join(first_bank_path,
                                             destination_path)
         else:
             first_bank_path = make_safe_filename(quiz['title'])
             if args.destination:
                 destination_path = os.path.join(args.destination,
                                                 first_bank_path,
                                                 destination_path)
             else:
                 destination_path = os.path.join(first_bank_path,
                                                 destination_path)
     decoded_markdown = question_type.decode_json(registry, question, args)
     return title, destination_path, decoded_markdown
Example #3
0
 def decode_json(cls, registry: Registry, data: str, args):
     raw_data = json.loads(data)
     return h2m(raw_data['body'], {
         'title': raw_data['title'],
         'resource': 'page',
         'published': raw_data.get('published', False)
     }), []
Example #4
0
 def decode_json(cls, registry: Registry, data: str, args):
     raw_data = json.loads(data)
     result = CommentedMap()
     result['title'] = raw_data['name']
     result['resource'] = 'assignment'
     result['url'] = raw_data['html_url']
     result['published'] = raw_data['published']
     # General settings
     result['settings'] = CommentedMap()
     result['settings']['points_possible'] = raw_data['points_possible']
     result['settings']['grading_type'] = raw_data['grading_type']
     # Submissions
     result['settings']['submission'] = CommentedMap()
     if 'allowed_extensions' in raw_data:
         result['settings']['submission']['allowed_extensions'] = raw_data['allowed_extensions']
     result['settings']['submission']['submission_types'] = raw_data['submission_types']
     if 'external_tool' in raw_data['submission_types']:
         result['settings']['submission']['external_tool'] = raw_data['external_tool_tag_attributes']['url']
     # Timing
     result['settings']['timing'] = CommentedMap()
     result['settings']['timing']['due_at'] = to_friendly_date(raw_data['due_at'])
     result['settings']['timing']['unlock_at'] = to_friendly_date(raw_data['unlock_at'])
     result['settings']['timing']['lock_at'] = to_friendly_date(raw_data['lock_at'])
     # Secrecy
     result['settings']['secrecy'] = CommentedMap()
     result['settings']['secrecy']['anonymize_students'] = raw_data['anonymize_students']
     result['settings']['secrecy']['anonymous_grading'] = raw_data['anonymous_grading']
     return h2m(raw_data['description'], result), []
 def decode_json_raw(cls, registry: Registry, data, args):
     result = QuizQuestion.decode_question_common(registry, data, args)
     result['answers'] = []
     for answer in data['answers']:
         a = CommentedMap()
         html = h2m(answer['html'])
         if args.hide_answers:
             a['possible'] = html
         else:
             if answer['weight']:
                 a['correct'] = html
             else:
                 a['wrong'] = html
             if answer['comments_html']:
                 a['comment'] = h2m(answer['comments_html'])
         result['answers'].append(a)
     return result
Example #6
0
 def decode_question_common(self, registry: Registry, data, args):
     result = CommentedMap()
     result['title'] = data['question_name']
     if not args.combine:
         result['resource'] = 'quiz question'
     result['type'] = data['question_type']
     result['text'] = data['question_text']
     result['points'] = data['points_possible']
     result['comments'] = CommentedMap()
     if data['correct_comments_html']:
         result['comments']['if_correct'] = h2m(data['correct_comments_html'])
     if data['incorrect_comments_html']:
         result['comments']['if_incorrect'] = h2m(data['incorrect_comments_html'])
     if data['neutral_comments_html']:
         result['comments']['always'] = h2m(data['neutral_comments_html'])
     if not result['comments']:
         del result['comments']
     return result
Example #7
0
 def decode_json_raw(cls, registry: Registry, data, args):
     result = QuizQuestion.decode_question_common(registry, data, args)
     if not args.hide_answers:
         result['answers'] = []
         for answer in data['answers']:
             a = CommentedMap()
             a['text'] = answer['text']
             if answer['comments_html']:
                 a['comment'] = h2m(answer['comments_html'])
             result['answers'].append(a)
     return result
Example #8
0
 def decode_json_raw(cls, registry: Registry, data, args):
     result = QuizQuestion.decode_question_common(registry, data, args)
     if not args.hide_answers:
         result['answers'] = CommentedMap()
         for answer in data['answers']:
             blank_id = answer['blank_id']
             if blank_id not in result['answers']:
                 result['answers'][blank_id] = []
             a = CommentedMap()
             a['text'] = answer['text']
             if 'comments_html' in answer and answer['comments_html']:
                 a['comment'] = h2m(answer['comments_html'])
             result['answers'][blank_id].append(a)
     return result
Example #9
0
 def decode_json_raw(cls, registry: Registry, data, args):
     result = QuizQuestion.decode_question_common(registry, data, args)
     if not args.hide_answers:
         result['answers'] = []
         for answer in data['answers']:
             a = CommentedMap()
             if answer['numerical_answer_type'] == 'exact_answer':
                 a['exact'] = answer['exact']
                 a['margin'] = answer['margin']
             elif answer['numerical_answer_type'] == 'range_answer':
                 a['start'] = answer['start']
                 a['end'] = answer['end']
             elif answer['numerical_answer_type'] == 'precision_answer':
                 a['precision'] = answer['precision']
                 a['approximate'] = answer['approximate']
             if answer.get('comments_html'):
                 a['comment'] = h2m(answer['comments_html'])
             result['answers'].append(a)
     return result
Example #10
0
 def decode_json_raw(cls, registry: Registry, data, args):
     result = QuizQuestion.decode_question_common(registry, data, args)
     result['answers'] = CommentedMap()
     for answer in data['answers']:
         blank_id = answer['blank_id']
         if blank_id not in result['answers']:
             result['answers'][blank_id] = []
         a = CommentedMap()
         text = answer['text']
         if args.hide_answers:
             a['possible'] = text
         else:
             if answer['weight']:
                 a['correct'] = text
             else:
                 a['wrong'] = text
             if answer['comments_html']:
                 a['comment'] = h2m(answer['comments_html'])
         result['answers'][blank_id].append(a)
     return result
Example #11
0
 def decode_json_raw(cls, registry: Registry, data, args):
     result = QuizQuestion.decode_question_common(registry, data, args)
     if args.hide_answers:
         result['answers'] = CommentedMap()
         result['answers']['lefts'] = list(
             sorted(set([answer['left'] for answer in data['answers']])))
         result['answers']['rights'] = (
             list(
                 sorted(set([answer['right']
                             for answer in data['answers']]))) +
             data['matching_answer_incorrect_matches'].split("\n"))
     else:
         result['answers'] = []
         for answer in data['answers']:
             a = CommentedMap()
             a['left'] = answer['left']
             a['right'] = answer['right']
             if answer.get('comments_html'):
                 a['comment'] = h2m(answer['comments_html'])
             result['answers'].append(a)
         result["distractors"] = data[
             'matching_answer_incorrect_matches'].split("\n")
     return result
Example #12
0
 def decode_json(cls, registry: Registry, data, args):
     full_json = cls.decode_json_raw(registry, data, args)
     text = full_json.pop('text')
     return h2m(text, full_json)
Example #13
0
 def decode_json(cls, registry: Registry, data: str, args):
     raw_data = json.loads(data)
     result = CommentedMap()
     result['title'] = raw_data['title']
     result['resource'] = 'quiz'
     result['url'] = raw_data['html_url']
     result['published'] = raw_data['published']
     result['settings'] = CommentedMap()
     result['settings']['quiz_type'] = raw_data['quiz_type']
     if raw_data.get('points_possible') is not None:
         result['settings']['points_possible'] = raw_data['points_possible']
     result['settings']['allowed_attempts'] = raw_data['allowed_attempts']
     result['settings']['scoring_policy'] = raw_data['scoring_policy']
     result['settings']['timing'] = CommentedMap()
     result['settings']['timing']['due_at'] = to_friendly_date(
         raw_data['due_at'])
     result['settings']['timing']['unlock_at'] = to_friendly_date(
         raw_data['unlock_at'])
     result['settings']['timing']['lock_at'] = to_friendly_date(
         raw_data['lock_at'])
     result['settings']['secrecy'] = CommentedMap()
     result['settings']['secrecy']['shuffle_answers'] = raw_data[
         'shuffle_answers']
     result['settings']['secrecy']['time_limit'] = raw_data['time_limit']
     result['settings']['secrecy']['one_question_at_a_time'] = raw_data[
         'one_question_at_a_time']
     result['settings']['secrecy']['cant_go_back'] = raw_data[
         'cant_go_back']
     result['settings']['secrecy']['show_correct_answers'] = raw_data[
         'show_correct_answers']
     result['settings']['secrecy'][
         'show_correct_answers_last_attempt'] = raw_data[
             'show_correct_answers_last_attempt']
     result['settings']['secrecy']['show_correct_answers_at'] = raw_data[
         'show_correct_answers_at']
     result['settings']['secrecy']['hide_correct_answers_at'] = raw_data[
         'hide_correct_answers_at']
     result['settings']['secrecy']['hide_results'] = raw_data[
         'hide_results']
     result['settings']['secrecy']['one_time_results'] = raw_data[
         'one_time_results']
     if raw_data['access_code']:
         result['settings']['secrecy']['access_code'] = raw_data[
             'access_code']
     if raw_data['ip_filter']:
         result['settings']['secrecy']['ip_filter'] = raw_data['ip_filter']
     # Handle questions and groups
     result['questions'] = []
     available_groups = raw_data['groups']
     used_groups = {}
     extra_files = []
     for question in raw_data['questions']:
         quiz_question, destination_path, full_body = QuizQuestion.decode_question(
             registry, question, raw_data, args)
         if destination_path is not None:
             extra_files.append((destination_path, full_body))
         quiz_group_id = question.get('quiz_group_id')
         if quiz_group_id is not None:
             quiz_group_id = str(
                 quiz_group_id)  # acbart: JSON only allows string keys
             if quiz_group_id not in used_groups:
                 used_groups[quiz_group_id] = QuizGroup.decode_group(
                     available_groups[quiz_group_id])
                 result['questions'].append(used_groups[quiz_group_id])
             used_groups[quiz_group_id]['questions'].append(quiz_question)
         else:
             result['questions'].append(quiz_question)
     return h2m(raw_data['description'], result), extra_files