You are here

function questions_import_submit_aiken in Quiz 6.3

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

File

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

Code

function questions_import_submit_aiken(&$form, &$form_state) {
  global $user;
  $row = 0;
  $output = '';
  $line = $options = array();
  $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') {
    while (!empty($lines)) {

      // while not empty of file
      while ($current_line = trim(array_shift($lines))) {
        if (empty($current_line)) {
          break;
        }
        $line[] = check_plain($current_line);
      }

      // read each question and its choices and correct answer.
      // now $line[0] has question, $line[1 to n] has choices and $line[last] has correct answer.
      $question = array_shift($line);
      $answer = array_pop($line);

      // now $line is left only with choices which looks like A) Moodle B) ATutor C) Claroline D) Blackboard etc
      foreach ($line as $l) {
        $option = explode($l[1], $l);
        $options[trim($option[0])] = trim($option[1]);
      }

      //now $options['A'] => Moodle, $options['B'] => ATutor ...
      $correct = substr(trim($answer), '-1');
      $answer = $options[$correct];
      $line = array();
      $node = new stdClass();
      $node->type = $form_state['values']['question_type'];

      // multichoice, true/false
      $node->quiz_id = $quiz_nid;
      $node->quiz_vid = $quiz_vid->vid;
      $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;
}