Skip to content

Commit

Permalink
WIP Import Bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dasistwas committed Oct 15, 2023
1 parent 180dffa commit ed3d112
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 38 deletions.
2 changes: 1 addition & 1 deletion rule/eventnotification/rule_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function rule_definition() {
$mform->addElement('select', 'param2', get_string('from'), $options);

// Recipient.
$grp = array();
$grp = [];
$grp[] = &$mform->createElement('checkbox', 'author', null, get_string('author', 'datalynx'), array('size' => 1));

$options = array('multiple' => true);
Expand Down
6 changes: 6 additions & 0 deletions rule/ftpsyncfiles/lang/de/datalynxrule_ftpsyncfiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@
$string['sftppassword'] = 'STFP Passwort';
$string['sftppath'] = 'STFP Path';
$string['sftpport'] = 'STFP Port';

$string['matchfields'] = 'Importierte Daten entsprechend den Formularfeldern zuordnen';
$string['identifier'] = 'Im Dateinamen verwendetes Identifikationsmerkmal, um die Datei dem richtigen User zuzuordnen';
$string['teammemberfield'] = 'Formularfeld, das verwendet wird um den User zu bestimmen, dem die Datei zugeordnet werden soll';
$string['manager'] = 'Der User, der die Einträge verwaltet';
$string['filefield'] = 'Feld in dem die Datei gespeichert werden soll';
6 changes: 6 additions & 0 deletions rule/ftpsyncfiles/lang/en/datalynxrule_ftpsyncfiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@
$string['sftppassword'] = 'STFP Password';
$string['sftppath'] = 'STFP Path';
$string['sftpport'] = 'STFP Port';

$string['matchfields'] = 'Match imported data to the matching form fields';
$string['identifier'] = 'Identifier used in the filename to match a specific user.';
$string['teammemberfield'] = 'Form field to use for selecting the user the file is assigned to';
$string['manager'] = 'User who manages all entries';
$string['filefield'] = 'Field where the file should be saved';
43 changes: 23 additions & 20 deletions rule/ftpsyncfiles/rule_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ class datalynx_rule_ftpsyncfiles extends datalynx_rule_base {
* @var false|int
*/
private $draftitemid;
/**
* @var mixed
*/
private array $sftpsetting;
private int $filefieldid;
private array $files;

/**
* Class constructor
Expand All @@ -80,14 +86,16 @@ class datalynx_rule_ftpsyncfiles extends datalynx_rule_base {
*/
public function __construct($df = 0, $rule = 0) {
parent::__construct($df, $rule);
$this->sftpserver = $this->rule->param2;
$this->sftpport = $this->rule->param3;
$this->sftpusername = $this->rule->param4;
$this->sftppassword = $this->rule->param10;
$this->sftppath = $this->rule->param6;
$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;
$this->filefieldid = $this->rule->param3;
}

/**
Expand All @@ -96,7 +104,7 @@ public function __construct($df = 0, $rule = 0) {
*/
public function trigger(\core\event\base $event) {

global $CFG, $DB, $USER;
global $CFG, $USER;
require_once("$CFG->dirroot/mod/datalynx/classes/datalynx.php");
require_once("$CFG->dirroot/mod/datalynx/field/entryauthor/field_class.php");
require_once("$CFG->dirroot/mod/datalynx/entries_class.php");
Expand All @@ -107,20 +115,14 @@ public function trigger(\core\event\base $event) {
$did = $event->get_data()['objectid'];
$this->dl = new mod_datalynx\datalynx($did);

// Download Server files.
$this->draftitemid = file_get_unused_draft_itemid();

$this->fs = get_file_storage();
$this->download_files((int)$did);

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

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

// fieldid?
// für welchen user?
// $files = $this->fs->get_area_files($context->id, 'mod_datalynx', 'draft', $this->draftitemid);

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

$data = new stdClass();
Expand All @@ -133,11 +135,11 @@ public function trigger(\core\event\base $event) {
$data->eids[$entryid] = $entryid;
// TODO: If filename is not userid get userid here.
$data->{"field_{$fieldid}_{$entryid}"} = $this->authorid;
$data->{"field_{$fieldid}_{$entryid}_filemanager"} = $this->draftitemid;
$data->{"field_{$fieldid}_{$entryid}_content"} = 1;
$data->{"field_{$this->filefieldid}_{$entryid}_filemanager"} = $this->draftitemid;
$data->{"field_{$this->filefieldid}_{$entryid}_content"} = 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 All @@ -151,7 +153,7 @@ public function trigger(\core\event\base $event) {
* @return void
*/
private function download_files(int $did): void {
global $CFG, $USER;
global $USER;
$server = $this->sftpserver;
$remotedir = $this->sftppath;
$username = $this->sftpusername;
Expand Down Expand Up @@ -197,12 +199,13 @@ private function download_files(int $did): void {
if ($filedata !== false) {

// TODO: Store Data in Moodle.
$file = $this->fs->create_file_from_string(
$draftitemid = file_get_unused_draft_itemid();
$this->files[$draftitemid] = $this->fs->create_file_from_string(
[
'contextid' => $context->id, // Replace with the appropriate context if necessary.
'component' => 'mod_datalynx',
'filearea' => 'draft',
'itemid' => $this->draftitemid,
'itemid' => $draftitemid,
'filepath' => '/',
'filename' => $filename,
],
Expand Down
111 changes: 94 additions & 17 deletions rule/ftpsyncfiles/rule_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,52 @@ public function rule_definition() {

$mform->addElement('header', 'settingshdr', get_string('sftpsettings',
'datalynxrule_ftpsyncfiles'));
$mform->addElement('text', 'param2', get_string('sftpserver', 'datalynxrule_ftpsyncfiles'));

$mform->addElement('hidden', 'param2', '');
$mform->setType('param2', PARAM_TEXT);
$mform->addElement('text', 'param3', get_string('sftpport', 'datalynxrule_ftpsyncfiles'));
$mform->setType('param3', PARAM_INT);
$mform->addElement('text', 'param4', get_string('sftpusername', 'datalynxrule_ftpsyncfiles'));
$mform->setType('param4', PARAM_TEXT);
$mform->addElement('text', 'param10', get_string('sftppassword', 'datalynxrule_ftpsyncfiles'));
$mform->setType('param10', PARAM_TEXT);
$mform->addElement('text', 'param6', get_string('sftppath', 'datalynxrule_ftpsyncfiles'));
$mform->setType('param6', PARAM_SAFEPATH);
$mform->addElement('header', 'settingsprofile', get_string('fields', 'datalynx'));

// SFTP connection and credentials. Save in param2 as serialized data.
$grp = [];
$grp[] = &$mform->createElement('static', '', '',
"<br><b class=\"w-100 mt-3\">" . get_string('sftpserver', 'datalynxrule_ftpsyncfiles') . "</b>");
$grp[] = &$mform->createElement('text', 'sftpserver', null,
get_string('sftpserver', 'datalynxrule_ftpsyncfiles'));

$grp[] = &$mform->createElement('static', '', '',
"<br><b class=\"w-100 mt-3\">" . get_string('sftpport', 'datalynxrule_ftpsyncfiles') . "</b>");
$grp[] = &$mform->createElement('text', 'sftpport',
get_string('sftpport', 'datalynxrule_ftpsyncfiles'));

$grp[] = &$mform->createElement('static', '', '',
"<br><b class=\"w-100 mt-3\">" . get_string('sftpusername', 'datalynxrule_ftpsyncfiles') . "</b>");
$grp[] = &$mform->createElement('text', 'sftpusername',
get_string('sftpusername', 'datalynxrule_ftpsyncfiles'));

$grp[] = &$mform->createElement('static', '', '',
"<br><b class=\"w-100 mt-3\">" . get_string('sftppassword', 'datalynxrule_ftpsyncfiles') . "</b>");
$grp[] = &$mform->createElement('text', 'sftppassword',
get_string('sftppassword', 'datalynxrule_ftpsyncfiles'));

$grp[] = &$mform->createElement('static', '', '',
"<br><b class=\"w-100 mt-3\">" . get_string('sftppath', 'datalynxrule_ftpsyncfiles') . "</b>");
$grp[] = &$mform->createElement('text', 'sftppath',
get_string('sftppath', 'datalynxrule_ftpsyncfiles'));

$mform->addGroup($grp, 'sftpgrp',
get_string('sftpsettings', 'datalynxrule_ftpsyncfiles'), $br, false);

$mform->setType('sftppath', PARAM_SAFEPATH);
$mform->setType('sftpserver', PARAM_TEXT);
$mform->setType('sftppassword', PARAM_TEXT);
$mform->setType('sftpport', PARAM_INT);
$mform->setType('sftpusername', PARAM_TEXT);

$mform->addElement('header', 'settingsprofile',
get_string('matchfields', 'datalynxrule_ftpsyncfiles'));

$standardfields = array('idnumber' => 'idnumber', 'email' => 'email', 'id' => 'id', 'username' => 'username');
$mform->addElement('autocomplete', 'param7', get_string('profilefields', 'core_admin'),
$mform->addElement('autocomplete', 'param7',
get_string('identifier', 'datalynxrule_ftpsyncfiles'),
$standardfields);

$fields = $this->_df->get_fields(null, false, true);
Expand All @@ -58,32 +91,76 @@ public function rule_definition() {
}
asort($fieldnames);
$options = array('multiple' => false);
$mform->addElement('autocomplete', 'param8', get_string('teammemberselect', 'datalynx'),
$fieldnames, $options);
$mform->addElement('autocomplete', 'param8',
get_string('teammemberfield', 'datalynxrule_ftpsyncfiles'), $fieldnames, $options);
$potentialusers = get_users_by_capability($this->_df->context, 'mod/datalynx:manageentries');
$choosuser = [];
foreach ($potentialusers as $user) {
$choosuser[$user->id] = $user->firstname . ' ' . $user->lastname;
}
$mform->addElement('autocomplete', 'param9', get_string('user'),
$choosuser, $options);
$mform->addElement('autocomplete', 'param9',
get_string('manager', 'datalynxrule_ftpsyncfiles'), $choosuser, $options);

$fields = $this->_df->get_fields_by_type('file', true);
$mform->addElement('autocomplete', 'param3',
get_string('filefield', 'datalynxrule_ftpsyncfiles'), $fields, $options);
}

/**
* @param $data
* @return void
*/
public function data_preprocessing(&$data) {
}

/**
*/
public function set_data($data) {
$this->data_preprocessing($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'];
}
parent::set_data($data);
}

/**
* @param $slashed
* @return object
*/
public function get_data($slashed = true) {
return parent::get_data($slashed);
$data = parent::get_data($slashed);
$sftpsetting = [];
if (isset($data->sftpserver)) {
$sftpsetting['sftpserver'] = $data->sftpserver;
}
if (isset($data->sftpport)) {
$sftpsetting['sftpport'] = $data->sftpport;
}
if (isset($data->sftpusername)) {
$sftpsetting['sftpusername'] = $data->sftpusername;
}
if (isset($data->sftppassword)) {
$sftpsetting['sftppassword'] = $data->sftppassword;
}
if (isset($data->sftppath)) {
$sftpsetting['sftppath'] = $data->sftppath;
}
// Aggregate all form fields into param2 table field.
if (isset($sftpsetting) && isset($data)) {
$data->param2 = serialize($sftpsetting);
}
return $data;
}
}

0 comments on commit ed3d112

Please sign in to comment.