You are here

function questions_import_validate_csv in Quiz 6.6

Same name and namespace in other branches
  1. 6.3 includes/questions_import/questions_import.admin.inc \questions_import_validate_csv()

@function This function checks whether the csv import file is in proper format or not.

File

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

Code

function questions_import_validate_csv($file, $separator) {
  $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>';
  }
  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);
      switch ($field[0]) {
        case 'multichoice':
          $error_msg .= count(fields) < 4 ? '<p>' . t('line : ') . $row . ' ' . $line . ' </p>' : '';
          break;
        case 'true_false':
          $error_msg .= '';
      }
    }
  }
  $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;
}