Fix for Quiz Questions Import

  1. <?php
  2. // $Id$
  3.  
  4. /**
  5.  * @file
  6.  * Questions Import
  7.  *
  8.  * This module allows you to import questions to quiz node in CSV format.
  9.  */
  10.  
  11. /**
  12.  * Implementation of hook_help().
  13.  */
  14.  
  15. function questions_import_help($path, $arg) {
  16.   if ($path == 'admin/help#questions_import') {
  17.     return '<p>' . t('This module allows you to import questions to quiz node in CSV format.') . '</p>';
  18.   }
  19. }
  20.  
  21. /**
  22.  * Implementation of hook_perm()
  23.  * Valid permissions for this module
  24.  * @return array An array of valid permissions.
  25.  */
  26.  
  27. function questions_import_perm() {
  28.   return array('import_questions');
  29. }
  30.  
  31. /**
  32.  * Implementation of hook_menu
  33.  *
  34.  */
  35.  
  36. function questions_import_menu() {  
  37.   $items = array();  
  38.   $items['admin/quiz/questions_import'] = array(
  39.     'title' => t('Import Quiz Questions'),
  40.     'description' => t('Import Questions to quiz node in CSV format'),
  41.     'page callback' => 'drupal_get_form',
  42.     'page arguments' => array('questions_import_form'),
  43.     'access arguments' => array('import_questions', 'upload files'),
  44.     'type' => MENU_NORMAL_ITEM,    
  45.   );  
  46.   return $items;
  47. }
  48.  
  49.  
  50. /*
  51.  * Implementation of hook_form
  52.  * form to upload questions
  53.  */
  54.  
  55. function questions_import_form()  {
  56.   $form['#attributes'] = array('enctype' => "multipart/form-data");
  57.  
  58.   $form['quiz_node'] = array(
  59.     '#type' => 'select',
  60.     '#title' => t('Quiz Node'),    
  61.     '#options' => questions_import_quiz_node(),    
  62.     '#description' => t('Select the quiz node under which you want to add questions'),
  63.     '#required' => TRUE,
  64.   );
  65.  
  66.   $form['question_type'] = array(
  67.     '#type' => 'select',
  68.     '#title' => t('Question type'),    
  69.     '#options' => questions_import_question_type(),
  70.     '#description' => t('Select the quiz question type you wish to upload'),    
  71.     '#required' => TRUE,
  72.   );
  73.  
  74.   $form['import_type'] = array(
  75.     '#type' => 'select',
  76.     '#title' => t('Import type'),    
  77.     '#options' => questions_import_type(),
  78.     '#description' => t('Select the import type csv, XML'),    
  79.     '#required' => TRUE,
  80.   );
  81.    
  82.   //'upload' will be used in file_check_upload()
  83.   $form['upload'] = array(
  84.     '#type' => 'file',
  85.     '#title' => t('Upload'),
  86.     '#size' => 30,
  87.     '#description' => t('Upload the file that has quiz questions'),    
  88.   );
  89.  
  90.   $form['submit'] = array(
  91.     '#type' => 'submit',
  92.     '#value' => t('import'),
  93.   );
  94.  
  95.   $form['#validate'][] = 'questions_import_form_validate';
  96.   $form['#submit'][] = 'questions_import_form_submit';
  97.  
  98.   return $form;
  99. }
  100.  
  101. /*
  102.  * @return
  103.  * this function return a list of quiz node title and its id.
  104.  */
  105.  
  106. function questions_import_quiz_node() {
  107.   $list = array();
  108.   $results = db_query("SELECT nid, title FROM {node} WHERE type = '%s'", 'quiz');
  109.   while($node = db_fetch_object($results)) {
  110.     $list[$node->nid] = substr($node->title, 0, 20);    
  111.   }
  112.   return $list;
  113. }
  114.  
  115. /*
  116.  * @return
  117.  * this function return a list of available quiz questions type.
  118.  */
  119.  
  120. function questions_import_question_type() {
  121.   $list = array();
  122.   if (module_exists('multichoice')) {
  123.     $list['multichoice'] =  'Multichoice';  
  124.   }
  125.   return $list;
  126. }
  127.  
  128. /*
  129.  *
  130.  */
  131. function questions_import_type() {
  132.   return array('cvs' => 'CVS');
  133. }
  134.  
  135. function questions_import_form_validate(&$node) {
  136.   print_r($node['upload']);exit;
  137. }
  138.  
  139. function questions_import_form_submit() {
  140.   die('i am called');
  141. }

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.