Fix for quiz questions import

  1. require 'includes/bootstrap.inc';
  2. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  3.  
  4. global $user;
  5.  
  6. $lines = file('quiz_questions.txt') or die('Unable to read file content');
  7.  
  8. $count = 0;
  9. foreach ($lines as $line) {  
  10.   $line = trim($line);
  11.   if (!empty($line)) {
  12.     $node = new stdClass();
  13.     $node->type = 'multichoice';
  14.     $node->quiz_id = $node->quiz_vid = 12;
  15.     $line = explode(',', $line);    
  16.     $question = array_shift($line);
  17.     $answer = array_pop($line);    
  18.     $options = $line;    
  19.    
  20.     $node->title = $node->body = $node->teaser = trim($question);
  21.     $node->num_answers = 4;    
  22.     $node->answers = array();    
  23.     foreach ($options as $option) {      
  24.       $node->answers[] = array(
  25.         'correct' => (trim($answer) == trim($option)) ? 1 : 0,
  26.         'answer' => trim($option),
  27.         'feedback' => ''      
  28.       );
  29.     }    
  30.     $node->name = $user->name;    
  31.     $node->promote = 0;
  32.     $node->sticky = 0;
  33.     $node->comment = 0;
  34.     $node->moderate = 0;
  35.     $node->multiple_answers = 0;
  36.     $node->more = 0;    
  37.     $node->validate = 1;
  38.     $node->is_new = 1;
  39.     $node->format = 1;
  40.     $node->scored_quiz = 1;
  41.     $node->revision = 1;    
  42.     $node->op = t('Save');
  43.     $node->preview = t('Preview');    
  44.     node_save($node);    
  45.     ++$count;
  46.     echo $count . ' ' . $node->title . ' multchoice question has been created' . PHP_EOL;
  47.   }
  48. }
  49. echo PHP_EOL;
  50. echo $count . ' Questions has been uploaded';

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.