You are here

function questions_import_submit_csv in Quiz 6.6

Same name and namespace in other branches
  1. 6.3 includes/questions_import/questions_import.admin.inc \questions_import_submit_csv()

@function This function imports multichoice questions from CSV file.

Parameters

$lines: It contains the whole csv file content.

Return value

Return the number of questions successfully imported.

File

includes/questions_import/questions_import.admin.inc, line 795
Administration file for Questions Import module

Code

function questions_import_submit_csv($destination_node, $form, $form_state, $import_id) {
  global $user;
  $row = 0;
  $output = '';
  $file = $form_state['values']['validated_file'];
  $separator = $form_state['values']['field_separator'];

  // iterate through all the lines in the file. Each line corresponds to one quiz question
  $lines = file($file->filepath);
  foreach ($lines as $line) {
    $line = trim($line);
    if (empty($line) || $line[0] === '#') {
      continue;
    }
    $node = new stdClass();

    //create node object
    $node->format = $form_state['values']['input_format'];
    $node->quiz_id = $destination_node->nid;
    $node->quiz_vid = $destination_node->vid;
    $line = explode($separator, $line);

    // The line variable looks something like
    // Question Type, Question, Choice 1, Feedback Ch1, Choice 2, Feedback Ch2, ...,Correct Choice
    // eg multichoice, which of the following is an IDE ?, Ubuntu, Ubuntu is a Linux Distro, Geany, Geany is a text editor for gnome, Eclipse, Eclipse is an IDE, Drupal, Drupal is an kick ass CMS, Eclipse
    $type = questions_import_get_next_csv_field($line);
    if (!in_array($type, quiz_get_questions_type('enabled'))) {
      drupal_set_message(t('Unable to import @type type question. You may need to enable @type module.', array(
        '@type' => $type,
      )), 'error');
      continue;
    }
    $node->type = $type;
    $question = questions_import_get_next_csv_field($line);
    switch ($type) {
      case 'multichoice':
        $correct_answer = array_pop($line);
        $node->num_answers = count($options);
        $node->answers = array();
        while (!empty($line)) {
          $answer = questions_import_get_next_csv_field($line);
          $feedback = questions_import_get_next_csv_field($line);
          $node->answers[] = array(
            'answer' => $answer,
            'feedback' => $feedback === 'nil' ? '' : $feedback,
            'correct' => trim($answer) == trim($correct_answer) ? 1 : 0,
          );
        }
        break;
      case 'true_false':
        $node->correct_answer = questions_import_get_next_csv_field($line) == 'true' ? 1 : 0;
        $feedback = questions_import_get_next_csv_field($line);
        $node->feedback = $feedback === 'nil' ? '' : $feedback;
        break;
      case 'matching':
        $node->match = array();
        while (!empty($line)) {
          $match = array(
            'question' => questions_import_get_next_csv_field($line),
            'answer' => questions_import_get_next_csv_field($line),
            'feedback' => questions_import_get_next_csv_field($line),
          );
          $node->match[] = array(
            'question' => $match['question'],
            'answer' => $match['answer'],
            'feedback' => $match['feedback'] ? '' : $match['feedback'],
          );
        }
        break;
      case 'long_answer':
        $node->maximum_score = questions_import_get_next_csv_field($line);
        break;
      case 'short_answer':
        $evaluation = array(
          'case sensitive match',
          'case insensitive match',
          'regular expression match',
          'manually score match',
        );
        $node->correct_answer = questions_import_get_next_csv_field($line);
        $node->maximum_score = questions_import_get_next_csv_field($line);
        $node->correct_answer_evaluation = (int) array_search(questions_import_get_next_csv_field($line), $evaluation);

        // if array_search return FALSE we convert it to int 0 and take the default format 'case sensitive match'
        break;
      case 'quiz_directions':
        break;
    }
    $node->title = $node->body = $node->teaser = $question;
    node_save(questions_import_node_save_static_data($node));
    db_query("INSERT INTO {quiz_questions_import_items} VALUES (%d, %d, %d)", $import_id, $node->nid, $row);
    ++$row;
  }
  return $row;
}