From 825ad9818524cb092dc9a1165464fe23a733d0ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Georg=20Mai=C3=9Fer?= Date: Fri, 13 Oct 2023 18:35:07 +0200 Subject: [PATCH] Nearly ready --- entries_class.php | 4 +-- rule/ftpsyncfiles/rule_class.php | 62 ++++++++++++++++---------------- 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/entries_class.php b/entries_class.php index 3df04f5b..8133f135 100644 --- a/entries_class.php +++ b/entries_class.php @@ -540,7 +540,7 @@ public function process_entries(string $action, $eids, $data = null, bool $confi $addcount = 0; $addmax = $dl->data->maxentries; $perinterval = ($dl->data->intervalcount > 1); - if ($addmax != -1 and has_capability('mod/datalynx:manageentries', $dl->context)) { + if ($addmax != -1 && has_capability('mod/datalynx:manageentries', $dl->context)) { $addmax = -1; } else { if ($addmax != -1) { @@ -553,7 +553,7 @@ public function process_entries(string $action, $eids, $data = null, bool $confi $entry = new stdClass(); // Existing entry from view. - if ($eid > 0 and isset($this->_entries[$eid])) { + if ($eid > 0 && isset($this->_entries[$eid])) { $entries[$eid] = $this->_entries[$eid]; // TODO existing entry *not* from view (import). diff --git a/rule/ftpsyncfiles/rule_class.php b/rule/ftpsyncfiles/rule_class.php index 7067cd70..39d4b602 100644 --- a/rule/ftpsyncfiles/rule_class.php +++ b/rule/ftpsyncfiles/rule_class.php @@ -95,8 +95,9 @@ public function __construct($df = 0, $rule = 0) { * @param \core\event\base $event */ public function trigger(\core\event\base $event) { - global $CFG; + global $CFG, $DB, $USER; require_once("$CFG->dirroot/mod/datalynx/classes/datalynx.php"); + require_once("$CFG->dirroot/mod/datalynx/entries_class.php"); require_once("$CFG->dirroot/mod/datalynx/view/csv/view_class.php"); require_once($CFG->libdir.'/filelib.php'); @@ -104,39 +105,39 @@ public function trigger(\core\event\base $event) { $this->dl = new mod_datalynx\datalynx($did); // Download Server files. - $this->draftitemid = file_get_submitted_draft_itemid('file'); + $this->draftitemid = file_get_unused_draft_itemid(); + $this->fs = get_file_storage(); $this->download_files((int)$did); - $files = $this->fs->get_area_files($this->dl->context, 'mod_datalynx', 'draft', $this->draftitemid); + $context = context_user::instance($USER->id); + + $files = $this->fs->get_area_files($context->id, 'mod_datalynx', 'draft', $this->draftitemid); + + // fieldid? + // für welchen user? if (!empty($files)) { foreach ($files as $file) { - if (file_exists($file) && is_readable($file)) { - $filename = $file->get_filename(); - $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}"} = $this->get_userid_from_filename($filename); - $processed = $dlentries->process_entries('update', $data->eids, $data, true); - } 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.'; - } + + $data = new stdClass(); + $data->eids = []; + + $fields = $this->dl->get_fields(); + + $fieldid = datalynxfield_entryauthor::_USERID; + $filename = $file->get_filename(); + + $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; + $data->{"field_{$fieldid}_{$entryid}_filemanager"} = $this->draftitemid; + $dlentries = new datalynx_entries($this->dl); + // Set teammember from filename. + $data->{"field_{$this->teammemberfieldid}_{$entryid}"} = $this->get_userid_from_filename($filename); + $processed = $dlentries->process_entries('update', $data->eids, $data, true); } } return true; @@ -149,7 +150,7 @@ public function trigger(\core\event\base $event) { * @return void */ private function download_files(int $did): void { - global $CFG; + global $CFG, $USER; $server = $this->sftpserver; $remotedir = $this->sftppath; $username = $this->sftpusername; @@ -189,7 +190,8 @@ private function download_files(int $did): void { $filedata = curl_exec($filehandle); // Todo: get context. - $context = $this->dl->context; + $context = context_user::instance($USER->id); + // $context = $this->dl->context; if ($filedata !== false) {