Skip to content

Commit

Permalink
Improvment: Skip empty records on import
Browse files Browse the repository at this point in the history
  • Loading branch information
georgmaisser committed Jul 12, 2024
1 parent a01334c commit 9a01bf4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
11 changes: 10 additions & 1 deletion classes/form/csvimport.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use context;
use context_system;
use context_module;
use core_form\dynamic_form;
use moodle_url;
use stdClass;
Expand Down Expand Up @@ -174,7 +175,15 @@ public function set_data_for_dynamic_submission(): void {
*/
protected function get_context_for_dynamic_submission(): context {

return context_system::instance();
$mform = $this->_form;
$data = (object) $this->_ajaxformdata;

if (!empty($data->cmid)) {
$context = context_module::instance($data->cmid);
} else {
$context = context_system::instance();
}
return $context;
}

/**
Expand Down
17 changes: 14 additions & 3 deletions classes/import/fileparser.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,15 @@ private function checksuccess() {
*/
private function validate_data($csvrecord, $line) {
// Validate data.

$recordempty = true;
foreach ($csvrecord as $column => $value) {

// We want to have at least one column with data, even when nothing is mandatory.
if (!empty($value) && $column !== 'cmid') {
$recordempty = false;
}

// Value "0" counts as value and returns valueisset true.
!$valueisset = (("" !== $value) && (null !== $value)) ? true : false;

Expand All @@ -367,7 +374,7 @@ private function validate_data($csvrecord, $line) {
}
} else {
// Validation of field type.
switch($this->get_param_value($column, "type")) {
switch ($this->get_param_value($column, "type")) {
case "date":
if (!$this->validate_datefields($value)) {
$format = $this->settings->dateformat;
Expand All @@ -381,7 +388,7 @@ private function validate_data($csvrecord, $line) {
break;
}
// Validation of field format.
switch($this->get_param_value($column, "format")) {
switch ($this->get_param_value($column, "format")) {
case PARAM_INT:
$value = $this->cast_string_to_int($value);
if (is_string($value)) {
Expand All @@ -403,8 +410,12 @@ private function validate_data($csvrecord, $line) {
break;
}
}

};

if ($recordempty) {
$this->add_csverror("No data was found in this record", $linevalues);
return false;
}
return true;
}
/**
Expand Down

0 comments on commit 9a01bf4

Please sign in to comment.