You are here

function questions_import_submit_csv in Quiz 6.3

Same name and namespace in other branches
  1. 6.6 includes/questions_import/questions_import.admin.inc \questions_import_submit_csv()
1 call to questions_import_submit_csv()
questions_import_form_submit in includes/questions_import/questions_import.admin.inc

File

includes/questions_import/questions_import.admin.inc, line 521

Code

function questions_import_submit_csv(&$form, &$form_state) {
  global $user;
  $row = 0;
  $output = '';
  $question_type = $form_state['values']['question_type'];
  $quiz_nid = $form_state['values']['quiz_node'];
  $quiz_vid = node_load($quiz_nid);
  $file = file_save_upload('upload');

  //creates drupal file object
  $lines = file($file->filepath);
  if ($question_type == 'multichoice') {
    $separator = $form_state['values']['field_separator'];
    foreach ($lines as $line) {
      $line = check_plain(trim($line));
      if (empty($line)) {
        continue;
      }
      $node = new stdClass();
      $node->type = $form_state['values']['question_type'];

      // multichoice
      $node->quiz_id = $quiz_nid;
      $node->quiz_vid = $quiz_vid->vid;
      $line = explode($separator, $line);
      $question = array_shift($line);
      $answer = array_pop($line);
      $options = $line;
      $node->title = $node->body = $node->teaser = trim($question);
      $node->num_answers = count($options);
      $node->answers = array();
      foreach ($options as $option) {
        $node->answers[] = array(
          'correct' => trim($answer) == trim($option) ? 1 : 0,
          'answer' => trim($option),
          'feedback' => '',
        );
      }
      node_save(questions_import_node_save_static_data($node));
      ++$row;
    }
  }

  /* else if ($question_type == 'true/false') {
     yet to add
    } */
  return $row;
}