/** * Quick add file * * @param void * @return null */ function quick_add() { if (!File::canAdd($this->logged_user, $this->active_project)) { $this->httpError(HTTP_ERR_FORBIDDEN, lang("You don't have permission for this action"), true, true); } // if $this->skip_layout = true; $file_data = $this->request->post('file'); if (!is_array($file_data)) { $file_data = array('visibility' => $this->active_project->getDefaultVisibility()); } // if $this->smarty->assign(array('file_data' => $file_data, 'form_urls' => $form_urls, 'quick_add_url' => assemble_url('project_files_quick_add', array('project_id' => $this->active_project->getId())))); if ($this->request->isSubmitted()) { db_begin_work(); $this->active_file = new File(); $attached = attach_from_files($this->active_file, $this->logged_user); // Do we have an upload error? if (is_error($attached) || $attached != 1) { $this->httpError(HTTP_ERR_OPERATION_FAILED, lang("You haven't selected any file for upload"), true, true); } // if $this->active_file->setAttributes($file_data); $this->active_file->setBody(clean(array_var($file_data, 'body', null))); if ($this->active_file->getName() == '') { $this->active_file->setName($this->active_file->pending_files[0]['name']); } // if $this->active_file->setRevision(1); $this->active_file->setProjectId($this->active_project->getId()); $this->active_file->setCreatedBy($this->logged_user); $this->active_file->setState(STATE_VISIBLE); //$this->active_file->setVisibility(VISIBILITY_NORMAL); $save = $this->active_file->save(); if ($save && !is_error($save)) { $subscribers = array($this->logged_user->getId()); if (is_foreachable($this->request->post('notify_users'))) { $subscribers = array_merge($subscribers, $this->request->post('notify_users')); } else { $subscribers[] = $this->active_project->getLeaderId(); } // if if (!in_array($this->active_project->getLeaderId(), $subscribers)) { $subscribers[] = $this->active_project->getLeaderId(); } // if Subscriptions::subscribeUsers($subscribers, $this->active_file); db_commit(); $this->active_file->ready(); $this->smarty->assign(array('active_file' => $this->active_file, 'file_data' => array('visibility' => $this->active_project->getDefaultVisibility()), 'project_id' => $this->active_project->getId())); } else { db_rollback(); $this->httpError(HTTP_ERR_OPERATION_FAILED, $save->getErrorsAsString(), true, true); } // if } // if }
public function uploadZip($userid, $courseid) { // error array of strings $errors = array(); LTutor::generatepath($this->config['DIR']['temp']); $tempDir = $this->tempdir($this->config['DIR']['temp'], 'extractZip', $mode = 0775); $body = File::decodeFile($this->app->request->getBody()); //1 file-Object $filename = $tempDir . '/' . $courseid . '.zip'; file_put_contents($filename, $body->getBody(true)); unset($body); $zip = new ZipArchive(); $zip->open($filename); $zip->extractTo($tempDir . '/files'); $zip->close(); unlink($filename); ///$this->deleteDir(dirname($filename)); unset($zip); $files = $tempDir . '/files'; // check if csv file exists if (file_exists($files . '/Liste.csv')) { $csv = fopen($files . '/Liste.csv', "r"); if (($transactionId = fgetcsv($csv, 0, ';')) === false) { fclose($csv); $this->deleteDir($tempDir); $this->app->response->setStatus(409); $errors[] = 'empty .csv file'; $this->app->response->setBody(json_encode($errors)); $this->app->stop(); } $result = Request::routeRequest('GET', '/transaction/authentication/TutorCSV_' . $userid . '_' . $courseid . '/transaction/' . $transactionId[0], array(), '', $this->_getTransaction, 'transaction'); if (isset($result['status']) && $result['status'] == 200 && isset($result['content'])) { $transaction = Transaction::decodeTransaction($result['content']); $transaction = json_decode($transaction->getContent(), true); unset($result); $defaultOrder = array('ID', 'NAME', 'USERNAME', 'POINTS', 'MAXPOINTS', 'OUTSTANDING', 'STATUS', 'TUTORCOMMENT', 'STUDENTCOMMENT', 'FILE'); $currectOrder = $defaultOrder; $markings = array(); while (($row = fgetcsv($csv, 0, ';')) !== false) { if (substr($row[0], 0, 2) == '--') { $row[0] = substr($row[0], 2); if (in_array(strtoupper($row[0]), $defaultOrder)) { $currectOrder = array(); foreach ($row as $ro) { $currectOrder[strtoupper($ro)] = count($currectOrder); } } } elseif (implode('', $row) != '' && substr($row[0], 0, 2) != '--') { if (isset($currectOrder['ID']) && !isset($row[$currectOrder['ID']]) || isset($currectOrder['POINTS']) && !isset($row[$currectOrder['POINTS']]) || isset($currectOrder['FILE']) && !isset($row[$currectOrder['FILE']]) || isset($currectOrder['TUTORCOMMENT']) && !isset($row[$currectOrder['TUTORCOMMENT']]) || isset($currectOrder['OUTSTANDING']) && !isset($row[$currectOrder['OUTSTANDING']]) || isset($currectOrder['STATUS']) && !isset($row[$currectOrder['STATUS']])) { $errors[] = 'invalid Liste.csv'; fclose($csv); $this->deleteDir($tempDir); $this->app->response->setStatus(409); $this->app->response->setBody(json_encode($errors)); $this->app->stop(); } $markingId = isset($currectOrder['ID']) ? $row[$currectOrder['ID']] : null; $points = isset($currectOrder['POINTS']) ? $row[$currectOrder['POINTS']] : null; $points = str_replace(',', '.', $points); $markingFile = isset($currectOrder['FILE']) ? $row[$currectOrder['FILE']] : null; // check if markingId exists in transaction if (!isset($transaction['markings'][$markingId])) { // unknown markingId fclose($csv); $this->deleteDir($tempDir); $this->app->response->setStatus(409); $errors[] = "unknown ID: {$markingId}"; $this->app->response->setBody(json_encode($errors)); $this->app->stop(); } ///var_dump($transaction['markings'][$markingId]); $markingData = $transaction['markings'][$markingId]; // checks whether the points are less or equal to the maximum points if ($points > $markingData['maxPoints'] || $points < 0) { // too much points ///fclose($csv); ///$this->deleteDir($tempDir); ///$this->app->response->setStatus(409); ///$errors[] = "incorrect points in marking: {$markingId}"; ///$this->app->response->setBody(json_encode($errors)); ///$this->app->stop(); } // checks if file with this markingid exists if ($markingFile == null || $markingFile == '' || file_exists($files . '/' . $markingFile)) { if ($markingFile != '' && $markingFile != null) { $fileAddress = $files . '/' . $markingFile; ///file_get_contents($files.'/'.$markingFile); // file $fileInfo = pathinfo($markingFile); $file = new File(); $file->setDisplayName($fileInfo['basename']); $file->setBody(Reference::createReference($fileAddress)); } else { $file = null; } if (isset($transaction['markings'][$markingId]['submissionId']) && $transaction['markings'][$markingId]['submissionId'] < 0) { // create new submission object $submissionId = $transaction['markings'][$markingId]['submissionId']; $studentId = $transaction['markings'][$markingId]['studentId']; $exerciseId = $transaction['markings'][$markingId]['exerciseId']; $submission = Submission::createSubmission(null, $studentId, null, $exerciseId, null, 1, time(), null, $leaderId, 1); $submission->setSelectedForGroup('1'); ///echo json_encode($submission);return; $result = Request::routeRequest('POST', '/submission', array(), json_encode($submission), $this->_postSubmission, 'submission'); if ($result['status'] == 201) { $transaction['markings'][$markingId]['submissionId'] = json_decode($result['content'], true)['id']; } } // create new marking object $marking = Marking::createMarking($markingId < 0 ? null : $markingId, $userid, null, $transaction['markings'][$markingId]['submissionId'], isset($currectOrder['TUTORCOMMENT']) ? $row[$currectOrder['TUTORCOMMENT']] : null, isset($currectOrder['OUTSTANDING']) ? $row[$currectOrder['OUTSTANDING']] : null, isset($currectOrder['STATUS']) ? $row[$currectOrder['STATUS']] : null, $points, time(), $file == null ? 1 : 0); $marking->setFile($file); /*array( 'id' => ($markingId<0 ? null : $markingId), 'points' => $points, 'outstanding' => isset($currectOrder['OUTSTANDING']) ? $row[$currectOrder['OUTSTANDING']] : null, 'tutorId' => $userid, 'tutorComment' => isset($currectOrder['TUTORCOMMENT']) ? $row[$currectOrder['TUTORCOMMENT']] : null, 'file' => $file, 'status' => isset($currectOrder['STATUS']) ? $row[$currectOrder['STATUS']] : null, 'date' => time(), 'hideFile' => );*/ $markings[] = $marking; } else { //if file with this markingid not exists $errors[] = 'File does not exist: ' . $markingFile; fclose($csv); $this->deleteDir($tempDir); $this->app->response->setStatus(409); $this->app->response->setBody(json_encode($errors)); $this->app->stop(); } } } $mark = @json_encode($markings); if ($mark !== false) { ///echo json_encode($markings); return; //request to database to edit the markings $result = Request::routeRequest('POST', '/marking', array(), $mark, $this->_postMarking, 'marking'); /// TODO: prüfen ob jede hochgeladen wurde if ($result['status'] != 201) { $errors[] = 'send markings failed'; } } else { $errors[] = 'invalid input'; } } else { $errors[] = 'no transaction data'; } fclose($csv); } else { // if csv file does not exist $errors[] = '.csv file does not exist in uploaded zip-Archiv'; } $this->deleteDir($tempDir); $this->app->response->setBody(json_encode($errors)); if (!($errors == array())) { $this->app->response->setStatus(409); } }
Authentication::checkRights(PRIVILEGE_LEVEL::TUTOR, $cid, $uid, $globalUserData); $langTemplate = 'TutorUpload_Controller'; Language::loadLanguageFile('de', $langTemplate, 'json', dirname(__FILE__) . '/'); if (isset($_POST['action']) && $_POST['action'] == 'TutorUpload') { if (isset($_FILES['MarkingFile'])) { $file = $_FILES['MarkingFile']; $error = $file['error']; if ($error == 0) { $filePath = $file['tmp_name']; $displayName = $file['name']; $type = $file["type"]; // checks file ending if (MimeReader::get_mime($filePath) == "application/zip") { // creates the JSON object containing the file $file = new File(); $file->setBody(Reference::createReference($filePath)); $file->setTimeStamp(time()); $file->setDisplayName($displayName); $file = File::encodeFile($file); // sends the JSON object to the logic $URI = $logicURI . "/tutor/user/{$uid}/course/{$cid}"; $error = http_post_data($URI, $file, true, $message); if ($message == "201" || $message == "200") { $successmsg = Language::Get('main', 'sucessFileUpload', $langTemplate); $notifications[] = MakeNotification('success', $successmsg); } else { $errors = @json_decode($error); if ($errors !== null) { foreach ($errors as $err) { $notifications[] = MakeNotification('error', $err); }
/** * Adds a file. * * Called when this component receives an HTTP DELETE request to * /file/$path(/) * * @param String the path, where the file should be stored */ public function postPathFile($path) { Logger::Log('starts POST postFile', LogLevel::DEBUG); $this->_app->response->setStatus(201); $body = $this->_app->request->getBody(); $fileObjects = File::decodeFile($body); if (!is_array($path)) { $path = array($path); } $temp = ""; foreach ($path as $part) { if ($part != '') { $temp .= $part . '/'; } } $path = $temp; // always been an array $arr = true; if (!is_array($fileObjects)) { $fileObjects = array($fileObjects); $arr = false; } $res = array(); foreach ($fileObjects as $fileObject) { if ($fileObject->getBody() == null && $fileObject->getAddress() == null) { $result = new File(); $result->setBody(null); $result->setStatus(409); $res[] = $result; continue; } $result = LFileHandler2::add($this->_db, $this->_fs, $path, array(), $fileObject); if ($result !== null) { $result->setStatus(201); $result->setBody(null); $res[] = $result; } else { $result = $fileObject; $result->setBody(null); $result->setStatus(409); $result->addMessage("Die Datei konnte nicht gespeichert werden."); $res[] = $result; Logger::Log('POST postPathFile failed', LogLevel::ERROR); } } if (!$arr && count($res) == 1) { $res = $res[0]; } $this->_app->response->setBody(File::encodeFile($res)); }