diff --git a/classes/form/csvimport.php b/classes/form/csvimport.php index fce727783..7368f11b7 100644 --- a/classes/form/csvimport.php +++ b/classes/form/csvimport.php @@ -25,6 +25,7 @@ use context; use context_system; +use context_module; use core_form\dynamic_form; use moodle_url; use stdClass; @@ -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; } /** diff --git a/classes/import/fileparser.php b/classes/import/fileparser.php index 8bb352efc..913aefd43 100644 --- a/classes/import/fileparser.php +++ b/classes/import/fileparser.php @@ -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; @@ -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; @@ -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)) { @@ -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; } /**