Skip to content

Commit

Permalink
WIP: File import rule
Browse files Browse the repository at this point in the history
  • Loading branch information
dasistwas committed Oct 13, 2023
1 parent 1c3a94b commit 6b6655c
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 151 deletions.
11 changes: 5 additions & 6 deletions classes/datalynx.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,15 @@ public function __construct($d = 0, $id = 0) {
* Get datalynx object by instanceid (id of datalynx table)
*
* @param $instanceid
* @return mod_datalynx\datalynx
* @throws \coding_exception
* @return datalynx
*/
public static function get_datalynx_by_instance($instanceid) {
public static function get_datalynx_by_instance($instanceid): datalynx {
$cm = get_coursemodule_from_instance('datalynx', $instanceid);
return new mod_datalynx\datalynx($instanceid, $cm->id);
return new datalynx($instanceid, $cm->id);
}

/**
* Get datalynx id.
* Get datalynx table id. Not cmid.
*
* @return mixed
*/
Expand Down Expand Up @@ -1029,7 +1028,7 @@ public function get_fieldname($key) {
* @param string $sort
* @return datalynxfield_base[]
*/
public function get_fields($exclude = null, $menu = false, $forceget = false, $sort = '') {
public function get_fields($exclude = null, $menu = false, $forceget = false, $sort = ''): array {
global $DB;

if (!$this->fields or $forceget) {
Expand Down
2 changes: 1 addition & 1 deletion classes/event/cron_trigger.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected function init() {
* @return string
*/
public static function get_name() {
return get_string('datalynx_cron_trigger', 'mod_datalynx');
return get_string('cron_trigger', 'mod_datalynx');
}

/**
Expand Down
10 changes: 7 additions & 3 deletions classes/task/cron_trigger.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,13 @@ public function execute() {
global $DB;
$records = $DB->get_records('datalynx_rules', null, '', 'DISTINCT dataid');
foreach ($records as $record) {
$df = new \mod_datalynx\datalynx($record->dataid);
$event = \mod_datalynx\event\cron_trigger::create(array('context' => $df->context, 'objectid' => $df->id()));
$event->trigger();
// Needed to prevent errors. In the past datalynx_rules was not deleted when dl instance was deleted.
// TODO: In upgrade.php remove all deleted rules.
if ($DB->record_exists('datalynx', ['id' => $record->dataid])) {
$df = new \mod_datalynx\datalynx($record->dataid);
$event = \mod_datalynx\event\cron_trigger::create(array('context' => $df->context, 'objectid' => $df->id()));
$event->trigger();
}
}
}
}
11 changes: 11 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,17 @@ function xmldb_datalynx_upgrade($oldversion) {
// Datalynx savepoint reached.
upgrade_mod_savepoint(true, 2022080800, 'datalynx');
}
if ($oldversion < 2023101202) {
// Get all fieldids of type teammemberselect.

$ids = $DB->get_fieldset_select('datalynx', 'id', 'id > 0');
if (!empty($ids)){
list($sql, $params) = $DB->get_in_or_equal($ids, SQL_PARAMS_QM, 'param', false);
$DB->delete_records_select('datalynx_rules', 'dataid ' . $sql, $params);
}
// Datalynx savepoint reached.
upgrade_mod_savepoint(true, 2023101202, 'datalynx');
}
return true;
}

Expand Down
3 changes: 2 additions & 1 deletion lang/de/datalynx.php
Original file line number Diff line number Diff line change
Expand Up @@ -897,4 +897,5 @@
$string['noselection'] = "Keine Auswahl";

// Task.
$string['datalynx_cron_trigger'] = "DL Cron Job";
$string['event_cron_trigger'] = "Datalynx Scheduled Task";
$string['cron_trigger'] = "Datalynx Scheduled Task";
3 changes: 2 additions & 1 deletion lang/en/datalynx.php
Original file line number Diff line number Diff line change
Expand Up @@ -939,4 +939,5 @@
$string['noselection'] = "Nothing selected";

// Task.
$string['datalynx_cron_trigger'] = "DL Cron Job";
$string['event_cron_trigger'] = "Datalynx Scheduled Task";
$string['cron_trigger'] = "Datalynx Scheduled Task";
1 change: 1 addition & 0 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ function datalynx_delete_instance($id) {
$DB->delete_records('datalynx_views', array('dataid' => $id));
$DB->delete_records('datalynx_filters', array('dataid' => $id));
$DB->delete_records('datalynx_entries', array('dataid' => $id));
$DB->delete_records('datalynx_rules', array('dataid' => $id));

// Delete the instance itself.
$result = $DB->delete_records('datalynx', array('id' => $id));
Expand Down
1 change: 1 addition & 0 deletions rule/ftpsyncfiles/lang/de/datalynxrule_ftpsyncfiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@
$string['sftpusername'] = 'STFP Username';
$string['sftppassword'] = 'STFP Passwort';
$string['sftppath'] = 'STFP Path';
$string['sftpport'] = 'STFP Port';
1 change: 1 addition & 0 deletions rule/ftpsyncfiles/lang/en/datalynxrule_ftpsyncfiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@
$string['sftpusername'] = 'STFP Username';
$string['sftppassword'] = 'STFP Password';
$string['sftppath'] = 'STFP Path';
$string['sftpport'] = 'STFP Port';
196 changes: 88 additions & 108 deletions rule/ftpsyncfiles/rule_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ class datalynx_rule_ftpsyncfiles extends datalynx_rule_base {
const USERNAME = 2;

const USEREMAIL = 3;
private $matchingfield;
private $teammemberfieldid;
private $authorid;
private \mod_datalynx\datalynx $dl;

/**
* Class constructor
*
* @param datalynx|int $df datalynx id or class object
* @param stdClass|int $rule rule id or DB record
* @param int $df datalynx id or class object
* @param int $rule rule id or DB record
*/
public function __construct($df = 0, $rule = 0) {
parent::__construct($df, $rule);
Expand All @@ -58,8 +62,15 @@ public function __construct($df = 0, $rule = 0) {
$this->sftpusername = $this->rule->param4;
$this->sftppassword = $this->rule->param5;
$this->sftppath = $this->rule->param6;
$this->matchingfield = $this->rule->param7;
$this->teammemberfieldid = $this->rule->param8;
$this->authorid = $this->rule->param9;
}

/**
* Based on a triggered event we start downloading files.
* @param \core\event\base $event
*/
public function trigger(\core\event\base $event) {
global $CFG;
require_once("$CFG->dirroot/mod/datalynx/classes/datalynx.php");
Expand All @@ -69,12 +80,9 @@ public function trigger(\core\event\base $event) {
$did = $event->get_data()['objectid'];

// Download Server files.
$dl = $this->download_files($did);
$filenames = $this->download_files((int)$did);

$instance = new datalynxview_csv($did);
$df = new mod_datalynx\datalynx($did);

// $file = $CFG->dirroot . '/mod/datalynx/testfile/1_test.csv';
$this->dl = new mod_datalynx\datalynx($did);
$fs = get_file_storage();

// Scan folder
Expand Down Expand Up @@ -103,125 +111,97 @@ public function trigger(\core\event\base $event) {
// ];
// $fs->create_file_from_pathname($filerecord, $file);

if (file_exists($file) && is_readable($file)) {
$filecontents = file_get_contents($file);

if ($filecontents !== false) {
$data = new stdClass();
$data->eids = array();
$df = new mod_datalynx\datalynx($did);
$fields = $df->get_fields();
// Get all the fields and write it into fieldsettings array. Needed for process_csv function.
$fieldsettings = [];
foreach ($fields as $field => $value) {
$fieldsettings[$field] = [$value->field->name => array('name' => $value->field->name)];
if (!empty($filenames)) {
foreach ($filenames as $filename) {
if (file_exists($file) && is_readable($file)) {
$filecontents = file_get_contents($file);
if ($filecontents !== false) {
$data = new stdClass();
$data->eids = [];

$fieldid = datalynxfield_entryauthor::_USERID;
$entryid = -1;
$data->eids[$entryid] = $entryid;
// TODO: If filename is not userid get userid here.
// Entry author is specified in the rule settings:
$data->{"field_{$fieldid}_{$entryid}"} = $this->authorid;
$dlentries = new datalynx_entries($this->dl);

// Set teammember from filename.
$data->{"field_{$this->teammemberfieldid}_{$entryid}"} = $filename;
$processed = $dlentries->process_entries('update', $data->eids, $data, true);

// Get all the fields and write it into fieldsettings array.
$fieldsettings = [];
foreach ($fields as $field => $value) {
$fieldsettings[$field] = [$value->field->name => array('name' => $value->field->name)];
}

} else {
// handle the case where reading the file failed.
echo 'Error reading the file.';
}
} else {
// handle the case where the file does not exist or is not readable.
echo 'File does not exist or is not readable.';
}
// Options array for process_csv
$options = array(
'settings' => $fieldsettings,
);
$data->ftpsyncmode = true;
$data = $instance->process_csv($data, $filecontents, $options);
$instance->execute_import($data);
} else {
// handle the case where reading the file failed.
echo 'Error reading the file.';
}
} else {
// handle the case where the file does not exist or is not readable.
echo 'File does not exist or is not readable.';
}

return true;
}

/**
* Download the files from the server and place it in a temporary folder.
*
* @param integer $did
*
* @return void
* @return array
*/
private function download_files(int $did) {
private function download_files(int $did): array {
global $CFG;
// Initialize cURL.
$c = curl_init("sftp://$this->sftpusername:$this->sftppassword@$this->sftpserver:$this->stfpport/$this->sftppath");

// Set cURL options for SFTP.
curl_setopt($c, CURLOPT_RETURNTRANSFER, CURLPROTO_SFTP);

// Get a list of files in the SFTP folder.
$list = curl_exec($c);
$info = curl_getinfo($c);

// Close the cURL connection.
curl_close($c);

// Split the directory listing into an array of file names.
$lines = explode("\n", trim($list));

foreach ($lines as $line) {
// Split each line by whitespace and extract the last field as the file name.
$fields = preg_split('/\s+/', trim($line));
$filename = end($fields);

// Skip directories and any other non-file entries.
if ($filename && strpos($filename, '.') !== 0) {
$filenames[] = $filename;
}
$server = "sftp://" . $this->sftpserver . ":" . $this->stfpport . "/" . $this->sftppath;
$username = $this->sftpusername;
$password = $this->sftppassword;

// Local directory to save downloaded files
$localdir = $CFG->dataroot . '/temp/' . $did . '/';

// Initialize cURL session
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $server);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($ch, CURLOPT_FILE, null); // Output to standard output
curl_setopt($ch, CURLOPT_VERBOSE, true); // For debugging

// Execute cURL session
$result = curl_exec($ch);

// Check for errors
if ($result === false) {
$error = curl_error($ch);
$info = curl_getinfo($ch);
mtrace('cURL Error: ' . json_encode($error) . json_encode($info));
} else {
mtrace('Files downloaded successfully.');
}

$datadir = $CFG->dataroot . '/temp/csvimport/moddatalynx/' . $did . '/';
if (!is_dir($datadir)) {
// Close cURL session
curl_close($ch);

if (!is_dir($localdir)) {
// Create the directory if it doesn't exist
if (!make_temp_directory('/csvimport/moddatalynx/' .$did .'/')) {
if (!mkdir($localdir)) {
// Handle directory creation error (e.g., display an error message)
throw new Exception('Error creating sapfiles directory');
throw new Exception('Error creating directory');
}
}
// Loop through the file names and download each file.
foreach ($filenames as $filename) {
// Initialize a new cURL handle to download the file.
$c = curl_init("sftp://$this->sftpusername:$this->sftppassword@$this->sftpserver:$this->stfpport/$this->sftppath/$filename");

// Set cURL options for SFTP file transfer.
$destinationpath = $CFG->dataroot . '/temp/csvimport/moddatalynx/' . $did . '/' . $filename; // Replace with your local download path
$fp = fopen($destinationpath, 'w');

curl_setopt($c, CURLOPT_FILE, $fp);
curl_setopt($c, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);

// Execute the cURL request to download the file.
curl_exec($c);

// Close the cURL handle and the local file pointer.
curl_close($c);
fclose($fp);
return true;
$filenames = scandir($localdir);
if ($filenames) {
return $filenames;
} else {
return [];
}
}

/**
* Check how to get the userid based on rule setting
*
* @param string $userinfo
*
* @return void
*/
// private function get_userid(string $userinfo) {
// switch ($this->mode) {
// case self::USERID:
// $userid = 2; // Replace with the actual user ID
// break;
// case self::USERNAME:
// // $userid =
// break;
// case self::USEREMAIL:

// break;
// default:
// echo "Invalid mode selected.";
// break;
// }
// }
}
Loading

0 comments on commit 6b6655c

Please sign in to comment.