As a student representing my college, I am organizing an online quiz on open source for a group of five colleges on 18 April 2009 which has more than 100 questions. I was lazy to upload question manually one by one so i wrote a quiz_questions_import script based on this article http://acquia.com/blog/migrating-drupal-way-part-i-creating-node. Bulk question import is a something often often asked feature in quiz issue queue so like to share this script with you guys here.
quiz_questions_import.php
<?php
require 'includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
// for explanation please refer the above article link
global $user;
$lines = file('quiz_questions.txt') or die('Unable to read file content');
$count = 0;
foreach ($lines as $line) {
$line = trim($line);
if (!empty($line)) {
$node = new stdClass();
$node->type = 'multichoice';
$node->quiz_id = $node->quiz_vid = 12;
$line = explode(',', $line);
$question = array_shift($line);
$answer = array_pop($line);
$options = $line;
$node->title = $node->body = $node->teaser = trim($question);
$node->num_answers = 4;
$node->answers = array();
foreach ($options as $option) {
$node->answers[] = array(
'correct' => (trim($answer) == trim($option)) ? 1 : 0,
'answer' => trim($option),
'feedback' => ''
);
}
$node->name = $user->name;
$node->promote = 0;
$node->sticky = 0;
$node->comment = 0;
$node->moderate = 0;
$node->multiple_answers = 0;
$node->more = 0;
$node->validate = 1;
$node->is_new = 1;
$node->format = 1;
$node->scored_quiz = 1;
$node->revision = 1;
$node->op = t('Save');
$node->preview = t('Preview');
node_save($node);
//module_invoke($node->type, 'insert',$node);
++$count;
echo $count . ' ' . $node->title . ' multchoice question has been created' . PHP_EOL;
}
}
echo PHP_EOL;
echo $count . ' Questions has been uploaded';
?>This script needs a text file named quiz_questions.txt in the same directory where you have quiz_questions_import.php. quiz_questions.txt suppose to have only one questions per line in this format.
question,option1,option2,option3,option4,correct_optioneg quiz_questions.txt
Popular text editor in GNU/Linux is ...,Vi,Notepad++,wordpad,Openbook,Vi
Command to change process prority...,nice,more,chps,ps,nice
Function in php can be defined using the keyword...,fn,def,define,function,functionThis script supports only multichoice questions type and no answers feedback option. Dont forget to change the line number 16 : $node->quiz_id = $node->quiz_vid = 12; which says the node id under which you want to upload the questions. This script works fine for me though it shows some mysql key warning messages.
You are suppose to run this script from drupal home directory where you have the following files and sub directory
CHANGELOG.txt INSTALL.mysql.txt MAINTAINERS.txt robots.txt update.php
COPYRIGHT.txt INSTALL.pgsql.txt misc scripts UPGRADE.txt
cron.php install.php modules sites xmlrpc.php
includes INSTALL.txt profiles test.php
index.php LICENSE.txt quiz_questions.txt themesI am more likely to submit a patch to integrate this script into quiz this weekend. In case if you find any mistakes in this script please let me know.
--
Thanks
Sivaji

Comments
Multiple Answers Field
Hi Sivaji,
First, let me say thanks for all of your work on this code... I am quickly climbing the Drupal learning curve here - just started with Drupal two weeks ago.
I have a couple thousand questions to import - using Quiz3... is there a way to do it in the UI that I am missing? I just used this script of yours after massaging my data with sed and awk to fit your format... it seems to have worked well... thanks a million (or at least a couple thousand)!
A couple of notes for total newbies:
1) A quiz must be first created... so that it has a node VID to attach all of your questions to
2) that node VID of your new quiz can be found in the "node" table (for line 16 of this code)
Only one odd occurrence, Sivaji, is that all questions were entered with the "number of answers" field set to "2" in the "quiz_node_question_properties" table... any idea why this is happening? Seems like your script attempts to set multiple answers=0... but the box is checked when i edit the individual questions.
http://www.packetbrain.com - Cisco training
http://www.packetbrain.com - Cisco training
@packetbrain Multichoice
@packetbrain
Multichoice questions import feature has been ported to quiz 3.x and Quiz 4.x-dev has import feature works with all question type. I wrote this script long back, quiz module is undergoing various changes and improvement in the recent past. so i would suggest you to use quiz questions_import module instead using this script.
@other
Please use quiz questions_import module to achieve this task.
--
sivaji
sivaji.ubuntu-tam.org
Where to find quiz_import.module
I searched hi and low for this import module before using your script - since I have seen a few posts about it... do I have to use a development snapshot to gain access to quiz import? This will be a production site within the next month... so I have not loaded anything other than official release modules so far.
I am using the 6.x-3.0 release... and the "includes" directory (which is where I think it is supposed to be - based on other posts) only contains a "views" folder.
http://drupal.org/project/quiz
I guess I am missing something... could you fill in the blank?
http://www.packetbrain.com/practice-exams - Cisco training
http://www.packetbrain.com - Cisco training
oops.. you are correct.
oops.. you are correct. Questions_import module is not there in quiz 3.x. It is designed to be a quiz addon so you can get questions_import module from dev snapshot and use it with quiz 3.x. Feel free to create an issue if it doesn't work as expected.
Note : You need to refer quiz 4.x-dev Examples directory files for sample import file.
[edit]
Now questions_import.module makes use of function available in quiz.module. You are more likely to "get call to undefined function in ...." error if you use it just like that - refer http://drupal.org/node/548948
--
sivaji
sivaji.ubuntu-tam.org
Thanks Sivaji...
"Unified Communications Manager Practice Exam View Manage questions Quiz admin Edit Track
139 questions were imported successfully in 0.6544 seconds."
-- ok so the import module in the development release worked for me..thanks!
looking forward to see your GSOC work... while I have your attention let me ask one more feature question:
My original (uninformed) plan was to create a community driven bank of practice questions where each user could rate other user's generated questions with the fivestar module (which in turn uses the voting api I think?)... I have enabled fivestar on every question... but I dont think that Quiz module ever looks at the fivestar ratings.
Ideally what I would like to do is one or two things - something like this:
1) If a quiz question is rated 2 stars or less (after 5 or more votes), then it is removed from the pool of questions for an exam
2) All remaining questions are selected in order of star ratings (so all 5 star questions come first, then 4, then 3)
Is any work being done for anything like this? If not, I may just write some code myself to do it... dont think the logic will be too hard to implement from what I have seen of the code.
http://www.packetbrain.com/practice-exam - Cisco training
http://www.packetbrain.com - Cisco training
how to import using the Addon
Hi Packetbrain and Sivaji,
I am new to drupal and just included the question_import Addon. I wanted to know how to actually use this feature?
I have the txt file ready with the questions in the required format, but where to execute the import addon??
Thanks so much
Cool my code works. Sorry i
Cool my code works. Sorry i haven't used fivestar module with quiz yet :(
--
sivaji
sivaji.ubuntu-tam.org
community driven bank
packetbrain,
A community driven bank of questions about sums up the project I'm working on. I'm designing for mostly an academic context, but I'd be happy to make a professional version too. Would you like to collaborate?
http://openeducationresearch.org/
http://educoder.org/
http://openeducationresearch.org/
Sounds good, Turadg
sent you an email via the "contact" page on this site...
http://www.packetbrain.com - Cisco training
http://www.packetbrain.com - Cisco training
Error while importing
Hi Sivaji,
i followed your instruction importing Question Import (using drupal 6.x-3.0) and now while importing, i'm getting follwoing error
Unable to import , type question. You may need to enable , module.
the above repeat for each question, though quiz created (if i opt import in new quiz)
Am i missing something???
I'm attaching test file which i'm importing (its test file, where i followed your CSV template file)
Thks in advance
Vikas
Hi Sivaji 1.I have followed
Hi Sivaji
1.I have followed the above instructions to import bulk quiz questions.I have created the file as well as necessary changes.but still after uploading the txt file i can see that the questions are uploaded in quiz directions type not in multiple choice type as desired. I have attached a snapshot as well as the text file.Could you help me on this?.
2.I have tried on quiz 4 importing options.I have used a csv file for uploading questions. am still getting the same issue as above said.
I could not attach a jpg file here and also mailed to your gmail id regarding this issue.