|
@@ -14,7 +14,7 @@
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
-function create_submission($data, $gradeimporterid, $userid) {
|
|
|
+function create_submission ($data, $gradeimporterid, $userid) {
|
|
|
global $DB;
|
|
|
$timenow = time();
|
|
|
if (empty($data->id)) {
|
|
@@ -47,34 +47,57 @@ function create_submission($data, $gradeimporterid, $userid) {
|
|
|
|
|
|
|
|
|
|
|
|
-function store_files($context, $cm, $data) {
|
|
|
+function store_files ($context, $cm, $data) {
|
|
|
+ global $CFG;
|
|
|
$fs = get_file_storage();
|
|
|
$files = $fs->get_area_files($context->id, 'mod_gradeimporter',
|
|
|
'submissionfiles', $data->id
|
|
|
);
|
|
|
foreach ($files as $file) {
|
|
|
- $extension = explode(".", $file->get_filename())[1];
|
|
|
- if ($extension == 'csv') {
|
|
|
+ if ($file->get_mimetype() == 'text/csv') {
|
|
|
read_csv($file->get_content(), $context, $data);
|
|
|
return;
|
|
|
}
|
|
|
+ if ($file->get_mimetype() == 'application/zip') {
|
|
|
+ // If it is a .zip file, extract files from the zip
|
|
|
+ $zipfiles = read_zip($file, $context, $fs);
|
|
|
+ // Search a .csv file
|
|
|
+ foreach ($zipfiles as $file) {
|
|
|
+
|
|
|
+ if ($file->get_mimetype() == 'text/csv') {
|
|
|
+ // If csv is found, pass it to read_csv()
|
|
|
+ read_csv($file->get_content(), $context, $data, $zipfiles);
|
|
|
+ // Only expects one csv so doesn't look for another
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // Finished processing files from zip, so delete temp copies
|
|
|
+ $fs->delete_area_files($context->id, 'mod_gradeimporter', 'unpacktemp', 0);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-function read_csv($content, $context, $data) {
|
|
|
+function read_csv ($content, $context, $data, $zipfiles = null) {
|
|
|
+
|
|
|
$csv = prepare_csv($content);
|
|
|
foreach ($csv as $feedback) {
|
|
|
+ $feedbackid = store_feedback($feedback, $context->id, $data->id);
|
|
|
if ($feedback['file'] != "") {
|
|
|
// If feedback has associated file, insert it into pluginfile
|
|
|
- $feedbackfilepath = store_feedback_file($feedback, $context, $data, $filepath);
|
|
|
+ foreach ($zipfiles as $file) {
|
|
|
+ // Search in zipfiles to find same name then feedback['file']
|
|
|
+ if ($feedback['file'] == $file->get_filename()) {
|
|
|
+ $feedbackfileinfo = store_feedback_file($feedback, $context, $data, $file, $feedbackid);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // Already used all files, so delete extra copies
|
|
|
+
|
|
|
}
|
|
|
- echo "Before store_feedback</br>";
|
|
|
- store_feedback($feedback, $context->id, $data->id);
|
|
|
- echo "After store_feedback</br>";
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-function prepare_csv($content) {
|
|
|
+function prepare_csv ($content) {
|
|
|
$csvlines = explode(PHP_EOL, $content);
|
|
|
$csv = array();
|
|
|
foreach ($csvlines as $line) {
|
|
@@ -93,7 +116,7 @@ function prepare_csv($content) {
|
|
|
return $outputcsv;
|
|
|
}
|
|
|
|
|
|
-function store_feedback($feedback, $contextid, $submissionid) {
|
|
|
+function store_feedback ($feedback, $contextid, $submissionid) {
|
|
|
global $DB;
|
|
|
// Prepare data to submit to gradeimporter_feedback table
|
|
|
$entry = new stdClass();
|
|
@@ -116,7 +139,7 @@ function store_feedback($feedback, $contextid, $submissionid) {
|
|
|
|
|
|
}
|
|
|
|
|
|
-function store_feedback_file($feedback, $context, $data, $filepath) {
|
|
|
+function store_feedback_file ($feedback, $context, $submission, $filetostore, $feedbackid) {
|
|
|
// Prepare file
|
|
|
$fs = get_file_storage();
|
|
|
$fileinfo = array(
|
|
@@ -124,14 +147,43 @@ function store_feedback_file($feedback, $context, $data, $filepath) {
|
|
|
'component' => 'mod_gradeimporter',
|
|
|
'filearea' => 'submissionfiles',
|
|
|
'itemid' => 0,
|
|
|
- 'filepath' => "/$data->id/{$feedback[id]}/",
|
|
|
- 'filename' => $feedback['file'],
|
|
|
- 'timecreated' => time(),
|
|
|
- 'timemodified' => time()
|
|
|
+ 'filepath' => "/",
|
|
|
+ 'filename' => $filetostore->get_filename(),
|
|
|
+ 'timecreated' => time(), 'timemodified' => time()
|
|
|
);
|
|
|
|
|
|
- // Move file from temp to pluginfile
|
|
|
- $fs->create_file_from_pathname($fileinfo, $filepath);
|
|
|
+ $fileinfo = $fs->create_file_from_storedfile($fileinfo, $filetostore);
|
|
|
+
|
|
|
+ return $fileinfo;
|
|
|
+}
|
|
|
+
|
|
|
+function read_zip ($file, $context, $fs) {
|
|
|
+ global $CFG;
|
|
|
+ // Get file packer to unpack zip
|
|
|
+ $packer = get_file_packer('application/zip');
|
|
|
+
|
|
|
+ // Clear area_files if it has been used before
|
|
|
+ $fs->delete_area_files($context->id,
|
|
|
+ 'mod_gradeimporter',
|
|
|
+ 'unpacktemp'
|
|
|
+ );
|
|
|
+
|
|
|
+ // Extract to temp areafiles
|
|
|
+ $file->extract_to_storage($packer,
|
|
|
+ $context->id,
|
|
|
+ 'mod_gradeimporter',
|
|
|
+ 'unpacktemp',
|
|
|
+ 0,
|
|
|
+ $CFG->tempdir,
|
|
|
+ false
|
|
|
+ );
|
|
|
+
|
|
|
+ // Get extracted files from unpacktemp area file
|
|
|
+ $tempfiles = $fs->get_area_files($context->id,
|
|
|
+ 'mod_gradeimporter',
|
|
|
+ 'unpacktemp'
|
|
|
+ );
|
|
|
|
|
|
- return $fileinfo['filepath'];
|
|
|
+ // Returns an array of files object
|
|
|
+ return $tempfiles;
|
|
|
}
|