Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue889 - Add maxlinelength as config for reader_csv #890

Open
wants to merge 1 commit into
base: MOODLE_401_STABLE
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions classes/local/step/reader_csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class reader_csv extends reader_step {
/** @var string */
const DEFAULT_DELIMETER = ',';

/** @var int */
const DEFAULT_MAXLINELENGTH = 1000;


/**
* Return the definition of the fields available in this form.
*
Expand All @@ -45,6 +49,7 @@ public static function form_define_fields(): array {
'overwriteheaders' => ['type' => PARAM_BOOL],
'continueonerror' => ['type' => PARAM_BOOL],
'delimiter' => ['type' => PARAM_TEXT],
'maxlinelength' => ['type' => PARAM_INT],
];
}

Expand All @@ -68,6 +73,7 @@ public function csv_contents_generator() {
$overwriteheaders = !empty($config->overwriteheaders);
$continueonerror = !empty($config->continueonerror);
$delimiter = $config->delimiter ?: self::DEFAULT_DELIMETER;
$maxlinelength = $config->maxlinelength ?: self::DEFAULT_MAXLINELENGTH;
$path = $this->enginestep->engine->resolve_path($config->path);

if (($handle = @fopen($path, 'r')) === false) {
Expand Down Expand Up @@ -190,5 +196,16 @@ public function form_add_custom_inputs(\MoodleQuickForm &$mform) {
['placeholder' => self::DEFAULT_DELIMETER],
self::DEFAULT_DELIMETER
);
// Max line length.
$mform->addElement(
'text',
'config_maxlinelength',
get_string('reader_csv:maxlinelength', 'tool_dataflows'),
['placeholder' => self::DEFAULT_MAXLINELENGTH],
self::DEFAULT_MAXLINELENGTH
);
$mform->setType('config_maxlinelength', PARAM_INT);
$mform->addRule('config_maxlinelength', null, 'numeric', null, 'client');

}
}
1 change: 1 addition & 0 deletions lang/en/tool_dataflows.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@
$string['reader_csv:continueonerror'] = 'Continue on parsing errors';
$string['reader_csv:continueonerror_help'] = 'If checked, the step will continue reading the next row of data if there are parsing errors on the current.';
$string['reader_csv:header_field_count_mismatch'] = 'Row #{$a->rownumber}: Number of fields ({$a->numfields}) should match number of headers ({$a->numheaders})';
$string['reader_csv:maxlinelength'] = 'Max Line Length';

// Reader JSON.
$string['reader_json:arrayexpression_help'] = 'Nested array to extract from JSON. For example, {$a->expression} will return the users array from the following JSON (If empty it is assumed the starting point of the JSON file is an array):{$a->jsonexample}';
Expand Down
5 changes: 5 additions & 0 deletions tests/tool_dataflows_reader_csv_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function test_csv_with_headers_included_in_the_file_contents() {
'path' => $inputpath,
'headers' => '',
'delimiter' => ',',
'maxlinelength' => '1000',
]);

// Create test input file.
Expand Down Expand Up @@ -97,6 +98,7 @@ public function test_csv_with_custom_headers_configured() {
'path' => $inputpath,
'headers' => implode(',', $headers),
'delimiter' => ',',
'maxlinelength' => '1000',
]);

// Create test input file.
Expand Down Expand Up @@ -138,6 +140,7 @@ public function test_bad_input_stream() {
'path' => $path,
'headers' => '',
'delimiter' => ',',
'maxlinelength' => '1000',
]);

$this->expectException('\moodle_exception');
Expand Down Expand Up @@ -167,6 +170,7 @@ public function test_csv_with_overwrite_headers_configured() {
'headers' => implode(',', $headers),
'overwriteheaders' => '1',
'delimiter' => ',',
'maxlinelength' => '1000',
]);

// Create test input file.
Expand Down Expand Up @@ -224,6 +228,7 @@ public function create_dataflow(): array {
'path' => $inputpath,
'headers' => '',
'delimiter' => ',',
'maxlinelength' => '1000',
]);
$dataflow->add_step($reader);
$steps[$reader->id] = $reader;
Expand Down