You are here

function questions_import_moodle_create_node in Quiz 6.6

1 call to questions_import_moodle_create_node()
questions_import_submit_moodle_format in includes/questions_import/questions_import.admin.inc

File

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

Code

function questions_import_moodle_create_node($destination_node, $mq, $form_state, $row, $import_id) {
  $qmap_drupal_to_moodle = array(
    // Drupal => Moodle
    'long_answer' => 'essay',
    'short_answer' => 'shortanswer',
    'choice' => 'multichoice',
    'true_false' => 'truefalse',
    'quiz_directions' => 'description',
    'matching' => 'match',
  );
  $qmap_moodle_to_drupal = array_flip($qmap_drupal_to_moodle);
  global $user;
  $node = new stdClass();
  $node->type = $qmap_moodle_to_drupal[$mq->qtype];
  $node->title = $mq->name;
  $node->teaser = $node->body = stripslashes_safe($mq->questiontext);

  // a la Moodle
  $node->uid = $user->uid;
  $node->format = $form_state['values']['input_format'];
  $node->log = 'Imported with Moodle importer.';
  $node->quiz_id = $destination_node->nid;
  $node->quiz_vid = $destination_node->vid;

  // Already set the type of the node, this is for specialized processing
  switch (strtolower($mq->qtype)) {
    case 'match':
      $node->match = array();
      foreach ($mq->subquestions as $index => $sub_question) {
        $sub_answer = $mq->subanswers[$index];

        // TODO what about 'feedback' ?
        $matching_pair = array(
          'question' => $sub_question,
          'answer' => $sub_answer,
        );
        $node->match[] = $matching_pair;
      }
      break;
    case 'shortanswer':

      // Drupal short_answer node fields: maximum_score, correct_answer, correct_answer_evaluation
      $node->correct_answer = $mq->answer[1];
      $node->correct_answer_evaluation = ShortAnswerQuestion::ANSWER_INSENSITIVE_MATCH;
      $node->maximum_score = $mq->defaultgrade;
      break;
    case 'multichoice':

      // Moodle name for our 'choice'
      $node->alternatives = array();

      // whether user can make multiple selections
      $node->choice_multi = !$mq->single;

      // Add answer alternatives
      foreach ($mq->answer as $index => $answer) {

        // choice module has weird keys for array
        $node->alternatives[] = array(
          // answer, answer_format, feedback_if_chosen, feedback_if_chosen_format,
          // feedback_if_not_chosen, feedback_if_not_chosen_format,
          // score_if_chosen, score_if_not_chosen
          'answer' => $answer,
          'answer_format' => $form_state['values']['input_format'],
          'feedback_if_chosen' => $mq->feedback[$index],
          'feedback_if_chosen_format' => $mq->feedback[$index],
          'feedback_if_not_chosen' => '',
          'feedback_if_not_chosen_format' => $mq->feedback[$index],
          'score_if_chosen' => $mq->fraction[$index],
          'score_if_not_chosen' => 0,
        );
      }
      break;
  }
  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);
}