Skip to content

Commit

Permalink
Nearly ready
Browse files Browse the repository at this point in the history
  • Loading branch information
georgmaisser committed Oct 13, 2023
1 parent cb66673 commit 825ad98
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
4 changes: 2 additions & 2 deletions entries_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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).
Expand Down
62 changes: 32 additions & 30 deletions rule/ftpsyncfiles/rule_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,48 +95,49 @@ 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');

$did = $event->get_data()['objectid'];
$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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {

Expand Down

0 comments on commit 825ad98

Please sign in to comment.