diff --git a/test.php b/test.php new file mode 100644 index 00000000..52a7d313 --- /dev/null +++ b/test.php @@ -0,0 +1,145 @@ +. + +/** + * Test file for catquiz + * @package local_catquiz + * @copyright 2023 Wunderbyte GmbH + * @author Georg Maißer + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +use core\dataformat\base; +use local_catquiz\data\catquiz_base; +use local_catquiz\event\catscale_updated; +use local_catquiz\subscription; + +require_once('../../config.php'); + +global $USER; + +$context = \context_system::instance(); + +$PAGE->set_context($context); +require_login(); + +require_capability('local/catquiz:manage_catscales', $context); + +$PAGE->set_url(new moodle_url('/local/catquiz/test.php', [])); + +$title = "Test cases"; +$PAGE->set_title($title); +$PAGE->set_heading($title); + +echo $OUTPUT->header(); + +$sftpserver = 'dedi458.your-server.de/'; +$stfpport = '22'; +$sftppath = '/'; +$sftpusername = 'wunder_1'; +$sftppassword = 'Gcb2BWeBtLpWVZt4'; +$username = $sftpusername; +$password = $sftppassword; + +// Local directory to save downloaded files +$localdir = $CFG->dataroot . '/temp/' . $did . '/'; + +$server = $sftpserver; + +$remotedir = $sftppath; +$localdir = ''; + +$ch = curl_init(); + +curl_setopt($ch, CURLOPT_URL, "sftp://$username:$password@$server$remotedir"); +curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + +$result = curl_exec($ch); + +if ($result === false) { + echo 'cURL error: ' . curl_error($ch); +} else { + // List of remote files and directories. + $remotelist = explode("\n", trim($result)); + + foreach ($remotelist as $line) { + $parts = preg_split('/\s+/', trim($line)); + $file = end($parts); + + if (!empty($file) && $file !== '.' && $file !== '..') { + $remotepath = "$remotedir$file"; + $localpath = $localdir . $file; + + + // Todo: Check filename and get all information. + + $filehandle = curl_init(); + + // Set cURL options for file download. + curl_setopt($filehandle, CURLOPT_URL, "sftp://$username:$password@$server$remotepath"); + curl_setopt($filehandle, CURLOPT_RETURNTRANSFER, 1); + + // Download the file. + $filedata = curl_exec($filehandle); + + // Todo: get context. + $context = context_module::instance(1); + + if ($filedata !== false) { + + // TODO: Store Data in Moodle. + + $fs = get_file_storage(); + $draftitemid = file_get_submitted_draft_itemid('file'); // Assuming 'file' is the form field name. + $file = $fs->create_file_from_string( + [ + 'contextid' => $context->id, // Replace with the appropriate context if necessary. + 'component' => 'user', + 'filearea' => 'draft', + 'itemid' => $draftitemid, + 'filepath' => '/', + 'filename' => $file, + ], + $filedata + ); + + echo "Downloaded $file successfully." . PHP_EOL; + } else { + echo "Failed to download $file." . PHP_EOL; + } + + curl_close($filehandle); + } + } +} + + curl_close($ch); + +if (!is_dir($localdir)) { + // Create the directory if it doesn't exist + if (!mkdir($localdir)) { + // Handle directory creation error (e.g., display an error message) + throw new Exception('Error creating directory'); + } +} + $filenames = scandir($localdir); +if ($filenames) { + return $filenames; +} else { + return []; +} + +echo $OUTPUT->footer();