old_contents: '''
  In this module, we will generate a csv report for a given problem id, which
  will include information about how students fared with a given problem id
  
  Usage:
  
  from the reporting directory
  
  python -m problem_ids.get_csv_report_by_problem_id <db_name> <problem_id>
  
  Example:
  
   python -m problem_ids.get_csv_report_by_problem_id body101x i4x://McGillX/Body101x/problem/fd3a83b0c06243b78554b69ad6f65e03
  
  
  '''
  from collections import defaultdict
  import sys
  
  from common.base_edx import EdXConnection
  from common.generate_csv_report import CSV
  
  db_name = sys.argv[1]
  problem_id = sys.argv[2]
  connection = EdXConnection(db_name, 'problem_ids')
  collection = connection.get_access_to_collection()
  
  if len(sys.argv) < 3:
      usage_message = """
      No problem id given as a command line argument. Please provide a problem_id
  
      Usage:
      python get_csv_report_by_problem_id.py <db_name> <problem_id>
  
      """
  
      sys.stderr.write(usage_message)
      sys.exit(1)
  
  def _generate_name_from_problem_id(problem_id, display_name):
      '''
      Generate name of csv output file from problem id
      '''
      return '_'.join(problem_id.split('/')[3:]) + '_' +''.join(e for e in display_name if e.isalnum()) + '.csv'
  
  cursor = collection['problem_ids'].find({'event.problem_id': problem_id})
  display_name = cursor[0]['module']['display_name']
  one_record = cursor[0]['event']
  problemd_ids_keys = sorted(one_record['correct_map'].keys())
  problem_ids = []
  for key in problemd_ids_keys:
      try:
          item = one_record['submission'][key]
          value = item['question']
          problem_ids.append('{0} : {1}'.format(key, value))
      except KeyError:
          problem_ids.append('{0}'.format(key))
  result = []
  for document in cursor:
      answers = [value['answer'] for _, value in sorted(document['event']['submission'].iteritems())]
      result.append([document['username'], document['event']['attempts'], document['module']['display_name'],document['time'], document['event']['success'],
      document['event']['grade'], document['event']['max_grade']] + answers)
  csv_report_name = _generate_name_from_problem_id(problem_id, display_name)
  output = CSV(result, ['Username', 'Attempt Number', 'Module', 'Time', 'Success', 'Grade Achieved', 'Max Grade'] + problem_ids, output_file=csv_report_name)
  output.generate_csv()
  
new_contents: '''
  In this module, we will generate a csv report for a given problem id, which
  will include information about how students fared with a given problem id
  
  Usage:
  
  from the reporting directory
  
  python -m problem_ids.get_csv_report_by_problem_id <db_name> <problem_id>
  
  Example:
  
   python -m problem_ids.get_csv_report_by_problem_id body101x i4x://McGillX/Body101x/problem/fd3a83b0c06243b78554b69ad6f65e03
  
  
  '''
  from collections import defaultdict
  import sys
  
  from common.base_edx import EdXConnection
  from common.generate_csv_report import CSV
  
  db_name = sys.argv[1]
  problem_id = sys.argv[2]
  connection = EdXConnection(db_name, 'problem_ids')
  collection = connection.get_access_to_collection()
  
  if len(sys.argv) < 3:
      usage_message = """
      No problem id given as a command line argument. Please provide a problem_id
  
      Usage:
      python get_csv_report_by_problem_id.py <db_name> <problem_id>
  
      """
  
      sys.stderr.write(usage_message)
      sys.exit(1)
  
  def _generate_name_from_problem_id(problem_id, display_name):
      '''
      Generate name of csv output file from problem id
      '''
      return '_'.join(problem_id.split('/')[3:]) + '_' +''.join(e for e in display_name if e.isalnum()) + '.csv'
  
  cursor = collection['problem_ids'].find({'event.problem_id': problem_id})
  display_name = cursor[0]['module']['display_name']
  one_record = cursor[0]['event']
  problem_ids_keys = sorted(one_record['correct_map'].keys())
  problem_ids = []
  for key in problem_ids_keys:
      try:
          item = one_record['submission'][key]
          value = item['question']
          problem_ids.append('{0} : {1}'.format(key, value))
      except KeyError:
          problem_ids.append('{0}'.format(key))
  result = []
  answers = []
  for document in cursor:
      answers = []
      for key in sorted(document['event']['correct_map'].keys()):
          #if key in submission_keys:
          try:
              answers.append(document['event']['submission'][key]['answer'])
              #print document['event']['submission'][key]['answer']
          except KeyError:
              answers.append('blank')
              #print 'blank'
      print answers
          #answers.append(document['event']['submission'][key]['answer'])
  #answers = [value['answer'] for _, value in sorted(document['event']['submission'].iteritems())]
      result.append([document['username'], document['event']['attempts'], document['module']['display_name'],document['time'], document['event']['success'],
      document['event']['grade'], document['event']['max_grade']] + answers)
  csv_report_name = _generate_name_from_problem_id(problem_id, display_name)
  output = CSV(result, ['Username', 'Attempt Number', 'Module', 'Time', 'Success', 'Grade Achieved', 'Max Grade'] + problem_ids, output_file=csv_report_name)
  output.generate_csv()
  
subject: fix alignment of answers