You are here

function questions_import_validate_csv in Quiz 6.3

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

File

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

Code

function questions_import_validate_csv($file, $separator, $question_type) {
  $error_msg = '';
  $row = 0;
  $lines = file($file->filepath);

  //reads the whole file content to an array
  if (empty($lines)) {
    return '<p>' . t('No lines were found in @filename.', array(
      '@filename' => $file->filename,
    )) . '</p>';
  }
  if ($question_type = 'multichoice') {
    foreach ($lines as $line) {
      $line = check_plain(trim($line));
      if (!empty($line)) {
        ++$row;

        // alway use pre_increment it is faster than post increment
        $fields = explode($separator, $line);
        $field_count = count($fields);

        /* a line should have minmum of four fields i.e
         * question, option1, option2, correct option
         * or maximum of 6 fields where additional two options will be option3 and option4 */
        if ($field_count < 4) {
          $error_msg .= '<p>' . t('line : ') . $row . ' ' . $line . ' </p>';
        }
      }
    }

    //exit;
  }
  $error_msg .= !empty($error_msg) ? '<p>' . t('CSV Import Failed. These lines were found to have an invalid number of fields in @filename.', array(
    '@filename' => $file->filename,
  )) . '</p>' : '';
  return $error_msg;
}