You are here

function results_export_as_teaser_view in Quiz 6.6

Same name and namespace in other branches
  1. 6.5 includes/results_export/results_export.admin.inc \results_export_as_teaser_view()
1 string reference to 'results_export_as_teaser_view'
results_export_menu in includes/results_export/results_export.module
Implementation of hook_menu

File

includes/results_export/results_export.admin.inc, line 37

Code

function results_export_as_teaser_view($nid, $export_type) {
  $results = _quiz_get_results($nid);
  $quiz = current($results);
  $header = array(
    t('node ID'),
    t('Quiz Title'),
    t('Username'),
    t('Result ID'),
    t('Start Time'),
    t('End Time'),
    t('Time Taken'),
    t('Score'),
  );
  while (list($key, $result) = each($results)) {
    $rows[] = array(
      'nid' => $result['nid'],
      'title' => $result['title'],
      'name' => check_plain($result['name']),
      'result_id' => $result['result_id'],
      'time_start' => format_date($result['time_start'], 'small'),
      'time_end' => $result['time_end'] > 0 ? format_date($result['time_end'], 'small') : t('In Progress'),
      'time_taken' => $result['time_end'] > 0 ? $result['time_end'] - $result['time_start'] : time() - $result['time_start'],
      'score' => $result['time_end'] ? $result['score'] : t('--'),
    );
  }
  switch ($export_type) {
    case 'html':
      $output = qp(QueryPath::HTML_STUB)
        ->find('title')
        ->text($quiz['title'])
        ->find(':root body')
        ->append(str_replace(' ', '', theme('table', $header, $rows)))
        ->find('table')
        ->attr('border', 1)
        ->top()
        ->html();
      break;
    case 'xml':
      $node = node_load($nid);
      $output = qp('<?xml version="1.0" ?><quiz><quizinfo/><resultSet/></quiz>', 'quizinfo')
        ->append('<authorName>' . $node->name . '</authorName>')
        ->append('<authorId>' . $node->uid . '</authorId>')
        ->append('<title>' . $node->title . '</title>')
        ->append('<description>' . $node->body . '</description>')
        ->append('<nodeId>' . $node->nid . '</nodeId>')
        ->append('<createDate>' . $node->created . '</createDate>')
        ->append('<lastUpdateDate>' . $node->changed . '</lastUpdateDate>')
        ->top()
        ->find('resultSet');
      foreach ($rows as $row) {
        $output
          ->branch()
          ->append('<result/>')
          ->find('result:last')
          ->append('<name>' . $row['name'] . '</name>')
          ->append('<resultId>' . $row['result_id'] . '</resultId>')
          ->append('<startTime>' . $row['time_start'] . '</startTime>')
          ->append('<endTime>' . $row['time_end'] . '</endTime>')
          ->append('<timeTaken>' . $row['time_taken'] . '</timeTaken>')
          ->append('<score>' . $row['score'] . '</score>');
      }
      $output = $output
        ->top()
        ->xml();
      break;
    case 'csv':
      $output = implode(',', $header);
      foreach ($rows as $row) {
        $output .= "\n" . implode(',', $row);
      }
      break;
  }
  $filename = str_replace(' ', '-', $quiz['title'] . '.' . $export_type);
  results_export_invoke_file_transfer($filename, $output);
}