Skip to content

Commit

Permalink
WIP: Finishing file import
Browse files Browse the repository at this point in the history
  • Loading branch information
dasistwas committed Oct 15, 2023
1 parent ed3d112 commit f5e8c94
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 39 deletions.
38 changes: 18 additions & 20 deletions rule/ftpsyncfiles/rule_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class datalynx_rule_ftpsyncfiles extends datalynx_rule_base {
* @var mixed
*/
private array $sftpsetting;
private int $filefieldid;
private ?int $filefieldid;
private array $files;

/**
Expand All @@ -86,12 +86,14 @@ class datalynx_rule_ftpsyncfiles extends datalynx_rule_base {
*/
public function __construct($df = 0, $rule = 0) {
parent::__construct($df, $rule);
$this->sftpsetting = unserialize($this->rule->param2);
$this->sftpserver = $this->sftpsetting['sftpserver'];
$this->sftpport = $this->sftpsetting['sftpport'];
$this->sftpusername = $this->sftpsetting['sftpusername'];
$this->sftppassword = $this->sftpsetting['sftppassword'];
$this->sftppath = $this->sftpsetting['sftppath'];
if (isset($this->rule->param2)) {
$this->sftpsetting = unserialize($this->rule->param2);
$this->sftpserver = $this->sftpsetting['sftpserver'];
$this->sftpport = $this->sftpsetting['sftpport'];
$this->sftpusername = $this->sftpsetting['sftpusername'];
$this->sftppassword = $this->sftpsetting['sftppassword'];
$this->sftppath = $this->sftpsetting['sftppath'];
}
$this->matchingfield = $this->rule->param7;
$this->teammemberfieldid = $this->rule->param8;
$this->authorid = $this->rule->param9;
Expand All @@ -118,28 +120,25 @@ public function trigger(\core\event\base $event) {
$this->fs = get_file_storage();
$this->download_files((int)$did);

$context = context_user::instance($USER->id);
// $context = context_user::instance($USER->id);

// $files = $this->fs->get_area_files($context->id, 'mod_datalynx', 'draft', $this->draftitemid);

if (!empty($this->files)) {
foreach ($files as $file) {

foreach ($this->files as $draftitemid => $file) {
$data = new stdClass();
$data->eids = [];

$fieldid = datalynxfield_entryauthor::_USERID;
$filename = $file->get_filename();

$entryid = -1;
$data->eids[$entryid] = $entryid;
// TODO: If filename is not userid get userid here.
$data->{"field_{$fieldid}_{$entryid}"} = $this->authorid;
$data->{"field_{$this->filefieldid}_{$entryid}_filemanager"} = $this->draftitemid;
$data->{"field_{$this->filefieldid}_{$entryid}_content"} = 1;
$data->{"field_{$this->filefieldid}_{$entryid}_filemanager"} = $draftitemid;
$data->{"field_{$this->filefieldid}_{$entryid}"} = 1;
$dlentries = new datalynx_entries($this->dl);
// Set teammember from filename.
$data->{"field_{$this->teammemberfieldid}_{$entryid}"} = [$this->get_userid_from_filename($filename)];
$data->{"field_{$this->teammemberfieldid}_{$entryid}"} = ["$this->get_userid_from_filename($filename)"];
$processed = $dlentries->process_entries('update', $data->eids, $data, true);
}
}
Expand Down Expand Up @@ -193,25 +192,24 @@ private function download_files(int $did): void {
$filedata = curl_exec($filehandle);

// Todo: get context.
$context = context_user::instance($USER->id);
// $context = $this->dl->context;
// $context = context_user::instance($USER->id);
$context = $this->dl->context;

if ($filedata !== false) {

// TODO: Store Data in Moodle.
$draftitemid = file_get_unused_draft_itemid();
file_prepare_draft_area($draftitemid, $context->id, 'mod_datalynx', 'content', null);
$this->files[$draftitemid] = $this->fs->create_file_from_string(
[
'contextid' => $context->id, // Replace with the appropriate context if necessary.
'component' => 'mod_datalynx',
'filearea' => 'draft',
'filearea' => 'content',
'itemid' => $draftitemid,
'filepath' => '/',
'filename' => $filename,
],
$filedata
);

echo "Downloaded $filename successfully." . PHP_EOL;
} else {
echo "Failed to download $filename." . PHP_EOL;
Expand Down
32 changes: 17 additions & 15 deletions rule/ftpsyncfiles/rule_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,23 @@ public function data_preprocessing(&$data) {
/**
*/
public function set_data($data) {
$sftpsetting = unserialize($data->param2);
if (isset($sftpsetting['sftpserver'])) {
$data->sftpserver = $sftpsetting['sftpserver'];
}
if (isset($sftpsetting['sftpport'])) {
$data->sftpport = $sftpsetting['sftpport'];
}
if (isset($sftpsetting['sftpusername'])) {
$data->sftpusername = $sftpsetting['sftpusername'];
}
if (isset($sftpsetting['sftppassword'])) {
$data->sftppassword = $sftpsetting['sftppassword'];
}
if (isset($sftpsetting['sftppath'])) {
$data->sftppath = $sftpsetting['sftppath'];
if (!empty($data->param2)) {
$sftpsetting = unserialize($data->param2);
if (isset($sftpsetting['sftpserver'])) {
$data->sftpserver = $sftpsetting['sftpserver'];
}
if (isset($sftpsetting['sftpport'])) {
$data->sftpport = $sftpsetting['sftpport'];
}
if (isset($sftpsetting['sftpusername'])) {
$data->sftpusername = $sftpsetting['sftpusername'];
}
if (isset($sftpsetting['sftppassword'])) {
$data->sftppassword = $sftpsetting['sftppassword'];
}
if (isset($sftpsetting['sftppath'])) {
$data->sftppath = $sftpsetting['sftppath'];
}
}
parent::set_data($data);
}
Expand Down
10 changes: 6 additions & 4 deletions rule/rule_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,12 @@ public function definition() {
* @param array|stdClass $data
*/
public function set_data($data) {
$selectedevents = unserialize($data->param1);
if ($selectedevents) {
foreach ($selectedevents as $eventname) {
$data->$eventname = true;
if (!empty($data->param1)) {
$selectedevents = unserialize($data->param1);
if ($selectedevents) {
foreach ($selectedevents as $eventname) {
$data->$eventname = true;
}
}
}
parent::set_data($data);
Expand Down

0 comments on commit f5e8c94

Please sign in to comment.