From 23eb31b1e0d3f8bc7354613d344fe588e40cb352 Mon Sep 17 00:00:00 2001 From: David Bogner Date: Mon, 23 Oct 2023 03:12:08 +0200 Subject: [PATCH] Add regex for matching user with filename --- .../de/datalynxrule_eventnotification.php | 2 ++ .../lang/de/datalynxrule_ftpsyncfiles.php | 2 ++ .../lang/en/datalynxrule_ftpsyncfiles.php | 2 ++ rule/ftpsyncfiles/rule_class.php | 33 +++++++++++-------- rule/ftpsyncfiles/rule_form.php | 5 +++ rule/ftpsyncfiles/version.php | 4 +-- 6 files changed, 32 insertions(+), 16 deletions(-) diff --git a/rule/eventnotification/lang/de/datalynxrule_eventnotification.php b/rule/eventnotification/lang/de/datalynxrule_eventnotification.php index 9116ad36..7a0fc6b8 100644 --- a/rule/eventnotification/lang/de/datalynxrule_eventnotification.php +++ b/rule/eventnotification/lang/de/datalynxrule_eventnotification.php @@ -24,3 +24,5 @@ $string['event'] = 'Datalynx Ereignis'; $string['pluginname'] = 'Ereignisbenachrichtigung'; $string['triggerspecificevent'] = 'Nur bei markierter Checkbox senden'; +$string['regex'] = 'Regulärer Ausdruck, der verwendet wird, um die Kennung aus dem Dateinamen zu extrahieren'; +$string['regex_desc'] = 'Wenn leer gelassen, wird es standardmäßig auf /^(\d+)_/ gesetzt.'; \ No newline at end of file diff --git a/rule/ftpsyncfiles/lang/de/datalynxrule_ftpsyncfiles.php b/rule/ftpsyncfiles/lang/de/datalynxrule_ftpsyncfiles.php index 0adb06d1..c69e39a7 100644 --- a/rule/ftpsyncfiles/lang/de/datalynxrule_ftpsyncfiles.php +++ b/rule/ftpsyncfiles/lang/de/datalynxrule_ftpsyncfiles.php @@ -37,3 +37,5 @@ $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'; +$string['regex'] = 'Regulärer Ausdruck, der verwendet wird, um die Kennung aus dem Dateinamen zu extrahieren. +Wenn leer gelassen, wird standardmäßig /^(\d+)_/ verwendet.'; \ No newline at end of file diff --git a/rule/ftpsyncfiles/lang/en/datalynxrule_ftpsyncfiles.php b/rule/ftpsyncfiles/lang/en/datalynxrule_ftpsyncfiles.php index 71ec1165..1bdd0cc7 100644 --- a/rule/ftpsyncfiles/lang/en/datalynxrule_ftpsyncfiles.php +++ b/rule/ftpsyncfiles/lang/en/datalynxrule_ftpsyncfiles.php @@ -37,3 +37,5 @@ $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'; +$string['regex'] = 'Regular expression used to extract the identifier from the filename. +If left empty it defaults to /^(\d+)_/.'; diff --git a/rule/ftpsyncfiles/rule_class.php b/rule/ftpsyncfiles/rule_class.php index 9b9d0abb..35f6ec5d 100644 --- a/rule/ftpsyncfiles/rule_class.php +++ b/rule/ftpsyncfiles/rule_class.php @@ -67,16 +67,10 @@ class datalynx_rule_ftpsyncfiles extends datalynx_rule_base { */ private \mod_datalynx\datalynx $dl; private ?file_storage $fs; - /** - * @var false|int - */ - private $draftitemid; - /** - * @var mixed - */ private array $sftpsetting; private ?int $filefieldid; private array $files; + private $regex; /** * Class constructor @@ -98,6 +92,7 @@ public function __construct($df = 0, $rule = 0) { $this->teammemberfieldid = $this->rule->param8; $this->authorid = $this->rule->param9; $this->filefieldid = $this->rule->param3; + $this->regex = $this->rule->param6; } /** @@ -118,11 +113,10 @@ public function trigger(\core\event\base $event) { $this->dl = new mod_datalynx\datalynx($did); $this->fs = get_file_storage(); + // Download files to $this->files array indexed by draftitemid. $this->download_files((int)$did); $context = context_user::instance($USER->id); - // $files = $this->fs->get_area_files($context->id, 'mod_datalynx', 'draft', $this->draftitemid); - if (!empty($this->files)) { foreach ($this->files as $draftitemid => $file) { $data = new stdClass(); @@ -222,24 +216,35 @@ private function download_files(int $did): void { } /** - * Find out the user the uploaded file belongs to based on the filename + * Find out the user the uploaded file belongs to based on the filename. + * Regex defaults to ^idnumber_ + * * @param string $filename * @return int */ protected function get_userid_from_filename(string $filename): int { global $DB; + // Extract user identifier from filename using a pattern. + if (empty($this->regex)) { + $this->regex = "/^(\d+)_/"; + } + if (preg_match($this->regex, $filename, $matches)) { + $identifier = $matches[1]; + } else { + return 0; + } switch ($this->matchingfield) { case 'idnumber': - $userid = $DB->get_field('user', 'id', array('idnumber' => $filename)); + $userid = $DB->get_field('user', 'id', array('idnumber' => $identifier)); break; case 'email': - $userid = $DB->get_field('user', 'id', array('email' => $filename)); + $userid = $DB->get_field('user', 'id', array('email' => $identifier)); break; case 'id': - $userid = $filename; + $userid = $identifier; break; case 'username': - $userid = $DB->get_field('user', 'id', array('username' => $filename)); + $userid = $DB->get_field('user', 'id', array('username' => $identifier)); break; default: $userid = 0; diff --git a/rule/ftpsyncfiles/rule_form.php b/rule/ftpsyncfiles/rule_form.php index 76ded2e4..be456cc5 100644 --- a/rule/ftpsyncfiles/rule_form.php +++ b/rule/ftpsyncfiles/rule_form.php @@ -104,6 +104,11 @@ public function rule_definition() { $fields = $this->_df->get_fields_by_type('file', true); $mform->addElement('autocomplete', 'param3', get_string('filefield', 'datalynxrule_ftpsyncfiles'), $fields, $options); + + // Regular expression for extracting the user identifier from the filename. + $mform->addElement('text', 'param6', get_string('regex', 'datalynxrule_ftpsyncfiles')); + $mform->setType('param6', PARAM_TEXT); + } /** diff --git a/rule/ftpsyncfiles/version.php b/rule/ftpsyncfiles/version.php index 5939f754..bed977e0 100644 --- a/rule/ftpsyncfiles/version.php +++ b/rule/ftpsyncfiles/version.php @@ -17,11 +17,11 @@ * * @package datalynxrule * @subpackage ftpsyncfiles - * @copyright 2023 Thomas Winkler + * @copyright 2023 Thomas Winkler, David Bogner * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') or die(); $plugin->component = 'datalynxrule_ftpsyncfiles'; -$plugin->version = 2023101001; +$plugin->version = 2023102300; $plugin->requires = 2014051200; \ No newline at end of file