Example #1
0
    public function test_csv_functions() {
        $csvexport = new csv_export_writer();
        $csvexport->set_filename('unittest');
        foreach ($this->testdata as $data) {
            $csvexport->add_data($data);
        }
        $csvoutput = $csvexport->print_csv_data(true);
        $this->assertEquals($csvoutput, $this->teststring);

        $test_data = csv_export_writer::print_array($this->testdata, 'comma', '"', true);
        $this->assertEquals($test_data, $this->teststring);
    }
Example #2
0
    public function test_csv_functions() {
        $csvexport = new csv_export_writer();
        $csvexport->set_filename('unittest');
        foreach ($this->testdata as $data) {
            $csvexport->add_data($data);
        }
        $csvoutput = $csvexport->print_csv_data(true);
        $this->assertEquals($csvoutput, $this->teststring);

        $test_data = csv_export_writer::print_array($this->testdata, 'comma', '"', true);
        $this->assertEquals($test_data, $this->teststring);

        // Testing that the content is imported correctly.
        $iid = csv_import_reader::get_new_iid('lib');
        $csvimport = new csv_import_reader($iid, 'lib');
        $contentcount = $csvimport->load_csv_content($this->teststring, 'utf-8', 'comma');
        $csvimport->init();
        $dataset = array();
        $dataset[] = $csvimport->get_columns();
        while ($record = $csvimport->next()) {
            $dataset[] = $record;
        }
        $csvimport->cleanup();
        $csvimport->close();
        $this->assertEquals($dataset, $this->testdata);

        // Testing for the wrong count of columns.
        $errortext = get_string('csvweirdcolumns', 'error');
        $iid = csv_import_reader::get_new_iid('lib');
        $csvimport = new csv_import_reader($iid, 'lib');
        $contentcount = $csvimport->load_csv_content($this->teststring2, 'utf-8', 'comma');
        $importerror = $csvimport->get_error();
        $csvimport->cleanup();
        $csvimport->close();
        $this->assertEquals($importerror, $errortext);

        // Testing for empty content
        $errortext = get_string('csvemptyfile', 'error');

        $iid = csv_import_reader::get_new_iid('lib');
        $csvimport = new csv_import_reader($iid, 'lib');
        $contentcount = $csvimport->load_csv_content($this->teststring3, 'utf-8', 'comma');
        $importerror = $csvimport->get_error();
        $csvimport->cleanup();
        $csvimport->close();
        $this->assertEquals($importerror, $errortext);
    }
Example #3
0
/**
 * @global object
 * @param array $export
 * @param string $delimiter_name
 * @param object $database
 * @param int $count
 * @param bool $return
 * @return string|void
 */
function data_export_csv($export, $delimiter_name, $database, $count, $return = false)
{
    global $CFG;
    require_once $CFG->libdir . '/csvlib.class.php';
    $filename = $database . '-' . $count . '-record';
    if ($count > 1) {
        $filename .= 's';
    }
    if ($return) {
        return csv_export_writer::print_array($export, $delimiter_name, '"', true);
    } else {
        csv_export_writer::download_array($filename, $export, $delimiter_name);
    }
}
Example #4
0
 /**
  * Parse this content
  *
  * @global object
  * @global object
  * @param string $content passed by ref for memory reasons, unset after return
  * @param string $encoding content encoding
  * @param string $delimiter_name separator (comma, semicolon, colon, cfg)
  * @param string $column_validation name of function for columns validation, must have one param $columns
  * @param string $enclosure field wrapper. One character only.
  * @return bool false if error, count of data lines if ok; use get_error() to get error string
  */
 function load_csv_content(&$content, $encoding, $delimiter_name, $column_validation = null, $enclosure = '"')
 {
     global $USER, $CFG;
     $this->close();
     $this->_error = null;
     $content = textlib::convert($content, $encoding, 'utf-8');
     // remove Unicode BOM from first line
     $content = textlib::trim_utf8_bom($content);
     // Fix mac/dos newlines
     $content = preg_replace('!\\r\\n?!', "\n", $content);
     // Remove any spaces or new lines at the end of the file.
     if ($delimiter_name == 'tab') {
         // trim() by default removes tabs from the end of content which is undesirable in a tab separated file.
         $content = trim($content, chr(0x20) . chr(0xa) . chr(0xd) . chr(0x0) . chr(0xb));
     } else {
         $content = trim($content);
     }
     $csv_delimiter = csv_import_reader::get_delimiter($delimiter_name);
     // $csv_encode    = csv_import_reader::get_encoded_delimiter($delimiter_name);
     // create a temporary file and store the csv file there.
     $fp = tmpfile();
     fwrite($fp, $content);
     fseek($fp, 0);
     // Create an array to store the imported data for error checking.
     $columns = array();
     // str_getcsv doesn't iterate through the csv data properly. It has
     // problems with line returns.
     while ($fgetdata = fgetcsv($fp, 0, $csv_delimiter, $enclosure)) {
         // Check to see if we have an empty line.
         if (count($fgetdata) == 1) {
             if ($fgetdata[0] !== null) {
                 // The element has data. Add it to the array.
                 $columns[] = $fgetdata;
             }
         } else {
             $columns[] = $fgetdata;
         }
     }
     $col_count = 0;
     // process header - list of columns
     if (!isset($columns[0])) {
         $this->_error = get_string('csvemptyfile', 'error');
         fclose($fp);
         return false;
     } else {
         $col_count = count($columns[0]);
     }
     // Column validation.
     if ($column_validation) {
         $result = $column_validation($columns[0]);
         if ($result !== true) {
             $this->_error = $result;
             fclose($fp);
             return false;
         }
     }
     $this->_columns = $columns[0];
     // cached columns
     // check to make sure that the data columns match up with the headers.
     foreach ($columns as $rowdata) {
         if (count($rowdata) !== $col_count) {
             $this->_error = get_string('csvweirdcolumns', 'error');
             fclose($fp);
             $this->cleanup();
             return false;
         }
     }
     $filename = $CFG->tempdir . '/csvimport/' . $this->_type . '/' . $USER->id . '/' . $this->_iid;
     $filepointer = fopen($filename, "w");
     // The information has been stored in csv format, as serialized data has issues
     // with special characters and line returns.
     $storedata = csv_export_writer::print_array($columns, ',', '"', true);
     fwrite($filepointer, $storedata);
     fclose($fp);
     fclose($filepointer);
     $datacount = count($columns);
     return $datacount;
 }
 public function test_csv_functions()
 {
     global $CFG;
     $csvexport = new csv_export_writer();
     $csvexport->set_filename('unittest');
     foreach ($this->testdata as $data) {
         $csvexport->add_data($data);
     }
     $csvoutput = $csvexport->print_csv_data(true);
     $this->assertEquals($csvoutput, $this->teststring);
     $test_data = csv_export_writer::print_array($this->testdata, 'comma', '"', true);
     $this->assertEquals($test_data, $this->teststring);
     // Testing that the content is imported correctly.
     $iid = csv_import_reader::get_new_iid('lib');
     $csvimport = new csv_import_reader($iid, 'lib');
     $contentcount = $csvimport->load_csv_content($this->teststring, 'utf-8', 'comma');
     $csvimport->init();
     $dataset = array();
     $dataset[] = $csvimport->get_columns();
     while ($record = $csvimport->next()) {
         $dataset[] = $record;
     }
     $csvimport->cleanup();
     $csvimport->close();
     $this->assertEquals($dataset, $this->testdata);
     // Testing for the wrong count of columns.
     $errortext = get_string('csvweirdcolumns', 'error');
     $iid = csv_import_reader::get_new_iid('lib');
     $csvimport = new csv_import_reader($iid, 'lib');
     $contentcount = $csvimport->load_csv_content($this->teststring2, 'utf-8', 'comma');
     $importerror = $csvimport->get_error();
     $csvimport->cleanup();
     $csvimport->close();
     $this->assertEquals($importerror, $errortext);
     // Testing for empty content
     $errortext = get_string('csvemptyfile', 'error');
     $iid = csv_import_reader::get_new_iid('lib');
     $csvimport = new csv_import_reader($iid, 'lib');
     $contentcount = $csvimport->load_csv_content($this->teststring3, 'utf-8', 'comma');
     $importerror = $csvimport->get_error();
     $csvimport->cleanup();
     $csvimport->close();
     $this->assertEquals($importerror, $errortext);
     // Testing for a tab separated file.
     // The tab separated file has a trailing tab and extra blank lines at the end of the file.
     $filename = $CFG->dirroot . '/lib/tests/fixtures/tabfile.csv';
     $fp = fopen($filename, 'r');
     $tabdata = fread($fp, filesize($filename));
     fclose($fp);
     $iid = csv_import_reader::get_new_iid('tab');
     $csvimport = new csv_import_reader($iid, 'tab');
     $contentcount = $csvimport->load_csv_content($tabdata, 'utf-8', 'tab');
     // This should import four rows including the headings.
     $this->assertEquals($contentcount, 4);
     // Testing for empty lines.
     $iid = csv_import_reader::get_new_iid('blanklines');
     $csvimport = new csv_import_reader($iid, 'blanklines');
     $contentcount = $csvimport->load_csv_content($this->teststring4, 'utf-8', 'comma');
     // Five lines including the headings should be imported.
     $this->assertEquals($contentcount, 5);
 }