Browse Source

Finished csv export

Igor 3 năm trước cách đây
mục cha
commit
ef39d48ef2
1 tập tin đã thay đổi với 35 bổ sung13 xóa
  1. 35 13
      locallib.php

+ 35 - 13
locallib.php

@@ -417,6 +417,17 @@ class iassign {
     $json_obj = new stdclass();
     $json_obj->submissions = [];
 
+    $csvfields = [];
+    $csvfields[] = 'submissionid';
+    $csvfields[] = 'file';
+    $csvfields[] = 'filesize';
+    $csvfields[] = 'timestamp';
+    $csvfields[] = 'humandate';
+    $csvfields[] = 'grade';
+    $csvfields[] = 'userid';
+    $csvfields[] = 'exerciseid';
+    $json_obj->submissions[] = $csvfields;
+
     $exercise_dir = [];
     $exercises_list = []; 
     foreach ($list as $item) {
@@ -481,15 +492,15 @@ class iassign {
               $new_file = $individual_dir . DIRECTORY_SEPARATOR . iassign_utils::format_filename('submission_' . ($i++) . '_' . date_format($date, 'Y-m-d_H-i-s') . '.' . $extension_ilm);
               $zip->add_file_from_string($new_file, $item->answer);
 
-              $temp_json = new stdclass();
-              $temp_json->submissionid = intval($item->id);
-              $temp_json->file = $new_file;
-              $temp_json->filesize = strlen($new_file);
-              $temp_json->timestamp = intval($item->timecreated);
-              $temp_json->humandate = date_format($date, 'Y-m-d H-i-s');
-              $temp_json->grade = doubleval($item->grade);
-              $temp_json->userid = intval($item->userid);
-              $temp_json->exerciseid = intval($exercise->id);
+              $temp_json = [];
+              $temp_json[] = intval($item->id);
+              $temp_json[] = $new_file;
+              $temp_json[] = strlen($new_file);
+              $temp_json[] = intval($item->timecreated);
+              $temp_json[] = date_format($date, 'Y-m-d H-i-s');
+              $temp_json[] = doubleval($item->grade);
+              $temp_json[] = intval($item->userid);
+              $temp_json[] = intval($exercise->id);
 
               $json_obj->submissions[] = $temp_json;
              }
@@ -498,13 +509,25 @@ class iassign {
       }
       
     }
-
-    $zip->add_file_from_string('index.json', json_encode($json_obj, JSON_PRETTY_PRINT));
-
+    $csv_str = "";
+    foreach($json_obj->submissions as $sub) {
+      $csv_str .= $this->csvstr($sub) . "\n";
+    }
+    $zip->add_file_from_string('index.csv', $csv_str);
     $zip->close();
     iassign_utils::download_file($zip_filename);
     exit;
   }
+  
+  function csvstr(array $fields) : string {
+    $f = fopen('php://memory', 'r+');
+    if (fputcsv($f, $fields) === false) {
+        return false;
+    }
+    rewind($f);
+    $csv_line = stream_get_contents($f);
+    return rtrim($csv_line);
+  }
 
   /// Download submissions from table iassign_allsubmissions
   function download_submissions () {
@@ -700,7 +723,6 @@ class iassign {
               url, 
               "popup",
               "width=900,height=900");
-
           }
         </script>';