Skip to content

Commit

Permalink
Add regex for matching user with filename
Browse files Browse the repository at this point in the history
  • Loading branch information
dasistwas committed Oct 23, 2023
1 parent cb5b275 commit 11c1e1f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
2 changes: 2 additions & 0 deletions rule/ftpsyncfiles/lang/de/datalynxrule_ftpsyncfiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
2 changes: 2 additions & 0 deletions rule/ftpsyncfiles/lang/en/datalynxrule_ftpsyncfiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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+)_/.';
33 changes: 19 additions & 14 deletions rule/ftpsyncfiles/rule_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions rule/ftpsyncfiles/rule_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

/**
Expand Down
4 changes: 2 additions & 2 deletions rule/ftpsyncfiles/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 11c1e1f

Please sign in to comment.