Browse Source

Fix csv file

jmontoyaa 8 years ago
parent
commit
7401aa4076
1 changed files with 26 additions and 0 deletions
  1. 26 0
      main/cron/import_csv.php

+ 26 - 0
main/cron/import_csv.php

@@ -365,6 +365,7 @@ class ImportCsv
      */
     private function importTeachers($file, $moveFile = true)
     {
+        $this->fixCSVFile($file);
         $data = Import::csvToArray($file);
 
         /* Unique identifier: official-code username.
@@ -499,6 +500,7 @@ class ImportCsv
      */
     private function importStudents($file, $moveFile = true)
     {
+        $this->fixCSVFile($file);
         $data = Import::csvToArray($file);
 
         /*
@@ -689,6 +691,7 @@ class ImportCsv
      */
     private function importCalendarStatic($file, $moveFile = true)
     {
+        $this->fixCSVFile($file);
         $data = Import::csvToArray($file);
 
         if (!empty($data)) {
@@ -975,6 +978,7 @@ class ImportCsv
      */
     private function importCourses($file, $moveFile = true, &$teacherBackup = array(), &$groupBackup = array())
     {
+        $this->fixCSVFile($file);
         $data = Import::csvToArray($file);
 
         if (!empty($data)) {
@@ -1860,6 +1864,28 @@ class ImportCsv
             echo $sql.PHP_EOL;
         }
     }
+
+    /**
+     * If csv file ends with '"' character then a '";' is added
+     * @param string $file
+     */
+    private function fixCSVFile($file)
+    {
+        $f = fopen($file, 'r+');
+        $cursor = -1;
+
+        fseek($f, $cursor, SEEK_END);
+        $char = fgetc($f);
+        while ($char === "\n" || $char === "\r") {
+            fseek($f, $cursor--, SEEK_END);
+            $char = fgetc($f);
+        }
+
+        if ($char === "\"") {
+            fseek($f, -1, SEEK_CUR);
+            fwrite($f, '";');
+        }
+    }
 }
 
 use Monolog\Logger;