mirror of
https://github.com/moodle/moodle.git
synced 2025-04-21 00:12:56 +02:00
MDL-40084 mod_data: Refactor importing data
Function data_import_csv has been deprecated.
This commit is contained in:
parent
bdf02ec088
commit
1296a2b868
40
mod/data/classes/local/csv_importer.php
Normal file
40
mod/data/classes/local/csv_importer.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace mod_data\local;
|
||||
|
||||
use file_serving_exception;
|
||||
use zip_archive;
|
||||
|
||||
/**
|
||||
* CSV importer class for importing data and - if needed - files as well from a zip archive.
|
||||
*
|
||||
* @package mod_data
|
||||
* @copyright 2023 ISB Bayern
|
||||
* @author Philipp Memmel
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class csv_importer extends importer {
|
||||
|
||||
/**
|
||||
* Declares the importer to use a csv file as data file.
|
||||
*
|
||||
* @see importer::get_import_data_file_extension()
|
||||
*/
|
||||
public function get_import_data_file_extension(): string {
|
||||
return 'csv';
|
||||
}
|
||||
}
|
77
mod/data/classes/local/importer.php
Normal file
77
mod/data/classes/local/importer.php
Normal file
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace mod_data\local;
|
||||
|
||||
use coding_exception;
|
||||
use core_php_time_limit;
|
||||
use file_packer;
|
||||
use file_serving_exception;
|
||||
use zip_archive;
|
||||
|
||||
/**
|
||||
* Importer class for importing data.
|
||||
*
|
||||
* @package mod_data
|
||||
* @copyright 2023 ISB Bayern
|
||||
* @author Philipp Memmel
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
abstract class importer {
|
||||
|
||||
/** @var string The import file path of the file which data should be imported from. */
|
||||
protected string $importfilepath;
|
||||
|
||||
/** @var string The original name of the import file name including extension of the file which data should be imported from. */
|
||||
protected string $importfilename;
|
||||
|
||||
/** @var string $importfiletype The file type of the import file. */
|
||||
protected string $importfiletype;
|
||||
|
||||
/**
|
||||
* Creates an importer object.
|
||||
*
|
||||
* This object can be used to import data from data files (like csv).
|
||||
*
|
||||
* @param string $importfilepath the complete path of the import file including filename
|
||||
* @param string $importfilename the import file name as uploaded by the user
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public function __construct(string $importfilepath, string $importfilename) {
|
||||
$this->importfilepath = $importfilepath;
|
||||
$this->importfilename = $importfilename;
|
||||
$this->importfiletype = pathinfo($importfilename, PATHINFO_EXTENSION);
|
||||
if ($this->importfiletype !== $this->get_import_data_file_extension()) {
|
||||
throw new coding_exception('Only ' . $this->get_import_data_file_extension() . '" files are allowed.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the file extension of the import data file which is being used, for example 'csv' for a csv importer.
|
||||
*
|
||||
* @return string the file extension of the export data file
|
||||
*/
|
||||
abstract public function get_import_data_file_extension(): string;
|
||||
|
||||
/**
|
||||
* Returns the file content of the data file.
|
||||
*
|
||||
* @return false|string the data file content as string; false, if file cannot be found/read
|
||||
*/
|
||||
public function get_data_file_content(): false|string {
|
||||
return file_get_contents($this->importfilepath);
|
||||
}
|
||||
}
|
185
mod/data/classes/local/mod_data_csv_importer.php
Normal file
185
mod/data/classes/local/mod_data_csv_importer.php
Normal file
@ -0,0 +1,185 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace mod_data\local;
|
||||
|
||||
use coding_exception;
|
||||
use context_module;
|
||||
use core_php_time_limit;
|
||||
use core_tag_tag;
|
||||
use core_user;
|
||||
use csv_import_reader;
|
||||
use dml_exception;
|
||||
use moodle_exception;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* CSV importer class for importing data.
|
||||
*
|
||||
* @package mod_data
|
||||
* @copyright 2023 ISB Bayern
|
||||
* @author Philipp Memmel
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class mod_data_csv_importer extends csv_importer {
|
||||
|
||||
/** @var array Log entries for successfully added records. */
|
||||
private array $addedrecordsmessages = [];
|
||||
|
||||
/**
|
||||
* Import records for a data instance from csv data.
|
||||
*
|
||||
* @param stdClass $cm Course module of the data instance.
|
||||
* @param stdClass $data The data instance.
|
||||
* @param string $encoding The encoding of csv data.
|
||||
* @param string $fielddelimiter The delimiter of the csv data.
|
||||
*
|
||||
* @throws coding_exception
|
||||
* @throws dml_exception
|
||||
* @throws moodle_exception
|
||||
*/
|
||||
public function import_csv(stdClass $cm, stdClass $data, string $encoding, string $fielddelimiter): void {
|
||||
global $CFG, $DB;
|
||||
// Large files are likely to take their time and memory. Let PHP know
|
||||
// that we'll take longer, and that the process should be recycled soon
|
||||
// to free up memory.
|
||||
core_php_time_limit::raise();
|
||||
raise_memory_limit(MEMORY_HUGE);
|
||||
|
||||
$iid = csv_import_reader::get_new_iid('moddata');
|
||||
$cir = new csv_import_reader($iid, 'moddata');
|
||||
|
||||
$context = context_module::instance($cm->id);
|
||||
|
||||
$readcount = $cir->load_csv_content($this->get_data_file_content(), $encoding, $fielddelimiter);
|
||||
if (empty($readcount)) {
|
||||
throw new \moodle_exception('csvfailed', 'data', "{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}");
|
||||
} else {
|
||||
if (!$fieldnames = $cir->get_columns()) {
|
||||
throw new \moodle_exception('cannotreadtmpfile', 'error');
|
||||
}
|
||||
|
||||
// Check the fieldnames are valid.
|
||||
$rawfields = $DB->get_records('data_fields', ['dataid' => $data->id], '', 'name, id, type');
|
||||
$fields = [];
|
||||
$errorfield = '';
|
||||
$usernamestring = get_string('username');
|
||||
$safetoskipfields = [get_string('user'), get_string('email'),
|
||||
get_string('timeadded', 'data'), get_string('timemodified', 'data'),
|
||||
get_string('approved', 'data'), get_string('tags', 'data')];
|
||||
$userfieldid = null;
|
||||
foreach ($fieldnames as $id => $name) {
|
||||
if (!isset($rawfields[$name])) {
|
||||
if ($name == $usernamestring) {
|
||||
$userfieldid = $id;
|
||||
} else if (!in_array($name, $safetoskipfields)) {
|
||||
$errorfield .= "'$name' ";
|
||||
}
|
||||
} else {
|
||||
// If this is the second time, a field with this name comes up, it must be a field not provided by the user...
|
||||
// like the username.
|
||||
if (isset($fields[$name])) {
|
||||
if ($name == $usernamestring) {
|
||||
$userfieldid = $id;
|
||||
}
|
||||
unset($fieldnames[$id]); // To ensure the user provided content fields remain in the array once flipped.
|
||||
} else {
|
||||
$field = $rawfields[$name];
|
||||
$filepath = "$CFG->dirroot/mod/data/field/$field->type/field.class.php";
|
||||
if (!file_exists($filepath)) {
|
||||
$errorfield .= "'$name' ";
|
||||
continue;
|
||||
}
|
||||
require_once($filepath);
|
||||
$classname = 'data_field_' . $field->type;
|
||||
$fields[$name] = new $classname($field, $data, $cm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($errorfield)) {
|
||||
throw new \moodle_exception('fieldnotmatched', 'data',
|
||||
"{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}", $errorfield);
|
||||
}
|
||||
|
||||
$fieldnames = array_flip($fieldnames);
|
||||
|
||||
$cir->init();
|
||||
while ($record = $cir->next()) {
|
||||
$authorid = null;
|
||||
if ($userfieldid) {
|
||||
if (!($author = core_user::get_user_by_username($record[$userfieldid], 'id'))) {
|
||||
$authorid = null;
|
||||
} else {
|
||||
$authorid = $author->id;
|
||||
}
|
||||
}
|
||||
if ($recordid = data_add_record($data, 0, $authorid)) { // Add instance to data_record.
|
||||
foreach ($fields as $field) {
|
||||
$fieldid = $fieldnames[$field->field->name];
|
||||
if (isset($record[$fieldid])) {
|
||||
$value = $record[$fieldid];
|
||||
} else {
|
||||
$value = '';
|
||||
}
|
||||
|
||||
if (method_exists($field, 'update_content_import')) {
|
||||
$field->update_content_import($recordid, $value, 'field_' . $field->field->id);
|
||||
} else {
|
||||
$content = new stdClass();
|
||||
$content->fieldid = $field->field->id;
|
||||
$content->content = $value;
|
||||
$content->recordid = $recordid;
|
||||
$DB->insert_record('data_content', $content);
|
||||
}
|
||||
}
|
||||
|
||||
if (core_tag_tag::is_enabled('mod_data', 'data_records') &&
|
||||
isset($fieldnames[get_string('tags', 'data')])) {
|
||||
$columnindex = $fieldnames[get_string('tags', 'data')];
|
||||
$rawtags = $record[$columnindex];
|
||||
$tags = explode(',', $rawtags);
|
||||
foreach ($tags as $tag) {
|
||||
$tag = trim($tag);
|
||||
if (empty($tag)) {
|
||||
continue;
|
||||
}
|
||||
core_tag_tag::add_item_tag('mod_data', 'data_records', $recordid, $context, $tag);
|
||||
}
|
||||
}
|
||||
|
||||
$this->addedrecordsmessages[] = get_string('added', 'moodle',
|
||||
count($this->addedrecordsmessages) + 1)
|
||||
. ". " . get_string('entry', 'data')
|
||||
. " (ID $recordid)\n";
|
||||
}
|
||||
}
|
||||
$cir->close();
|
||||
$cir->cleanup(true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the array of messages for added records.
|
||||
*
|
||||
* For each successfully added record the array contains a log message.
|
||||
*
|
||||
* @return array Array of message strings: For each added record one message string
|
||||
*/
|
||||
public function get_added_records_messages(): array {
|
||||
return $this->addedrecordsmessages;
|
||||
}
|
||||
}
|
@ -267,3 +267,142 @@ function data_get_exportdata($dataid, $fields, $selectedfields, $currentgroup=0,
|
||||
$line--;
|
||||
return $exportdata;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated since Moodle 4.3, importing is now being done by \mod_data\local\mod_data_csv_importer::import_csv
|
||||
* Import records for a data instance from csv data.
|
||||
*
|
||||
* @param object $cm Course module of the data instance.
|
||||
* @param object $data The data instance.
|
||||
* @param string $csvdata The csv data to be imported.
|
||||
* @param string $encoding The encoding of csv data.
|
||||
* @param string $fielddelimiter The delimiter of the csv data.
|
||||
* @return int Number of records added.
|
||||
*/
|
||||
function data_import_csv($cm, $data, &$csvdata, $encoding, $fielddelimiter) {
|
||||
global $CFG, $DB;
|
||||
|
||||
debugging('Function data_import_csv has been deprecated. '
|
||||
. 'Importing is now being done by \mod_data\local\mod_data_csv_importer::import_csv.',
|
||||
DEBUG_DEVELOPER);
|
||||
|
||||
// Large files are likely to take their time and memory. Let PHP know
|
||||
// that we'll take longer, and that the process should be recycled soon
|
||||
// to free up memory.
|
||||
core_php_time_limit::raise();
|
||||
raise_memory_limit(MEMORY_EXTRA);
|
||||
|
||||
$iid = csv_import_reader::get_new_iid('moddata');
|
||||
$cir = new csv_import_reader($iid, 'moddata');
|
||||
|
||||
$context = context_module::instance($cm->id);
|
||||
|
||||
$readcount = $cir->load_csv_content($csvdata, $encoding, $fielddelimiter);
|
||||
$csvdata = null; // Free memory.
|
||||
if (empty($readcount)) {
|
||||
throw new \moodle_exception('csvfailed', 'data', "{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}");
|
||||
} else {
|
||||
if (!$fieldnames = $cir->get_columns()) {
|
||||
throw new \moodle_exception('cannotreadtmpfile', 'error');
|
||||
}
|
||||
|
||||
// Check the fieldnames are valid.
|
||||
$rawfields = $DB->get_records('data_fields', array('dataid' => $data->id), '', 'name, id, type');
|
||||
$fields = array();
|
||||
$errorfield = '';
|
||||
$usernamestring = get_string('username');
|
||||
$safetoskipfields = array(get_string('user'), get_string('email'),
|
||||
get_string('timeadded', 'data'), get_string('timemodified', 'data'),
|
||||
get_string('approved', 'data'), get_string('tags', 'data'));
|
||||
$userfieldid = null;
|
||||
foreach ($fieldnames as $id => $name) {
|
||||
if (!isset($rawfields[$name])) {
|
||||
if ($name == $usernamestring) {
|
||||
$userfieldid = $id;
|
||||
} else if (!in_array($name, $safetoskipfields)) {
|
||||
$errorfield .= "'$name' ";
|
||||
}
|
||||
} else {
|
||||
// If this is the second time, a field with this name comes up, it must be a field not provided by the user...
|
||||
// like the username.
|
||||
if (isset($fields[$name])) {
|
||||
if ($name == $usernamestring) {
|
||||
$userfieldid = $id;
|
||||
}
|
||||
unset($fieldnames[$id]); // To ensure the user provided content fields remain in the array once flipped.
|
||||
} else {
|
||||
$field = $rawfields[$name];
|
||||
$filepath = "$CFG->dirroot/mod/data/field/$field->type/field.class.php";
|
||||
if (!file_exists($filepath)) {
|
||||
$errorfield .= "'$name' ";
|
||||
continue;
|
||||
}
|
||||
require_once($filepath);
|
||||
$classname = 'data_field_' . $field->type;
|
||||
$fields[$name] = new $classname($field, $data, $cm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($errorfield)) {
|
||||
throw new \moodle_exception('fieldnotmatched', 'data',
|
||||
"{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}", $errorfield);
|
||||
}
|
||||
|
||||
$fieldnames = array_flip($fieldnames);
|
||||
|
||||
$cir->init();
|
||||
$recordsadded = 0;
|
||||
while ($record = $cir->next()) {
|
||||
$authorid = null;
|
||||
if ($userfieldid) {
|
||||
if (!($author = core_user::get_user_by_username($record[$userfieldid], 'id'))) {
|
||||
$authorid = null;
|
||||
} else {
|
||||
$authorid = $author->id;
|
||||
}
|
||||
}
|
||||
if ($recordid = data_add_record($data, 0, $authorid)) { // Add instance to data_record.
|
||||
foreach ($fields as $field) {
|
||||
$fieldid = $fieldnames[$field->field->name];
|
||||
if (isset($record[$fieldid])) {
|
||||
$value = $record[$fieldid];
|
||||
} else {
|
||||
$value = '';
|
||||
}
|
||||
|
||||
if (method_exists($field, 'update_content_import')) {
|
||||
$field->update_content_import($recordid, $value, 'field_' . $field->field->id);
|
||||
} else {
|
||||
$content = new stdClass();
|
||||
$content->fieldid = $field->field->id;
|
||||
$content->content = $value;
|
||||
$content->recordid = $recordid;
|
||||
$DB->insert_record('data_content', $content);
|
||||
}
|
||||
}
|
||||
|
||||
if (core_tag_tag::is_enabled('mod_data', 'data_records') &&
|
||||
isset($fieldnames[get_string('tags', 'data')])) {
|
||||
$columnindex = $fieldnames[get_string('tags', 'data')];
|
||||
$rawtags = $record[$columnindex];
|
||||
$tags = explode(',', $rawtags);
|
||||
foreach ($tags as $tag) {
|
||||
$tag = trim($tag);
|
||||
if (empty($tag)) {
|
||||
continue;
|
||||
}
|
||||
core_tag_tag::add_item_tag('mod_data', 'data_records', $recordid, $context, $tag);
|
||||
}
|
||||
}
|
||||
|
||||
$recordsadded++;
|
||||
print get_string('added', 'moodle', $recordsadded) . ". " . get_string('entry', 'data') . " (ID $recordid)<br />\n";
|
||||
}
|
||||
}
|
||||
$cir->close();
|
||||
$cir->cleanup(true);
|
||||
return $recordsadded;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -86,16 +86,32 @@ echo $OUTPUT->header();
|
||||
echo $OUTPUT->heading_with_help(get_string('uploadrecords', 'mod_data'), 'uploadrecords', 'mod_data');
|
||||
|
||||
if ($formdata = $form->get_data()) {
|
||||
$filecontent = $form->get_file_content('recordsfile');
|
||||
$recordsadded = data_import_csv($cm, $data, $filecontent, $formdata->encoding, $formdata->fielddelimiter);
|
||||
$uploadedfilepath = $form->save_temp_file('recordsfile');
|
||||
$filestempdir = null;
|
||||
|
||||
if ($recordsadded > 0) {
|
||||
echo $OUTPUT->notification($recordsadded. ' '. get_string('recordssaved', 'data'), '');
|
||||
} else {
|
||||
echo $OUTPUT->notification(get_string('recordsnotsaved', 'data'), 'notifysuccess');
|
||||
if (!$uploadedfilepath) {
|
||||
throw new coding_exception('No file uploaded.');
|
||||
}
|
||||
|
||||
echo $OUTPUT->continue_button($redirectbackto);
|
||||
$importer = new \mod_data\local\mod_data_csv_importer($uploadedfilepath, $form->get_new_filename('recordsfile'));
|
||||
|
||||
if (!$importer->get_data_file_content()) {
|
||||
echo $OUTPUT->notification(get_string('errordatafilenotfound', 'data'),
|
||||
\core\output\notification::NOTIFY_ERROR);
|
||||
} else {
|
||||
$importer->import_csv($cm, $data, $formdata->encoding, $formdata->fielddelimiter);
|
||||
unlink($uploadedfilepath);
|
||||
|
||||
$addedrecordsmessages = $importer->get_added_records_messages();
|
||||
echo html_writer::div(implode('<br/>', $addedrecordsmessages));
|
||||
if (count($addedrecordsmessages) > 0) {
|
||||
echo $OUTPUT->notification(count($addedrecordsmessages) . ' ' . get_string('recordssaved', 'data'),
|
||||
\core\output\notification::NOTIFY_SUCCESS);
|
||||
} else {
|
||||
echo $OUTPUT->notification(get_string('recordsnotsaved', 'data'),
|
||||
\core\output\notification::NOTIFY_ERROR);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/// Upload records section. Only for teachers and the admin.
|
||||
echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
|
||||
|
@ -152,6 +152,7 @@ $string['entrieslefttoadd'] = 'You must add {$a->entriesleft} more entry/entries
|
||||
$string['entrieslefttoaddtoview'] = 'You must add {$a->entrieslefttoview} more entry/entries before you can view other participants\' entries.';
|
||||
$string['entry'] = 'Entry';
|
||||
$string['entrysaved'] = 'Your entry has been saved';
|
||||
$string['errordatafilenotfound'] = 'The file could not be imported. Please upload a CSV file.';
|
||||
$string['errormustbeteacher'] = 'You need to be a teacher to use this page!';
|
||||
$string['errorpresetexists'] = 'A preset with this name already exists.';
|
||||
$string['errorpresetexistsbutnotoverwrite'] = 'A preset with this name already exists. Choose a different name.';
|
||||
|
133
mod/data/lib.php
133
mod/data/lib.php
@ -2978,139 +2978,6 @@ function data_supports($feature) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Import records for a data instance from csv data.
|
||||
*
|
||||
* @param object $cm Course module of the data instance.
|
||||
* @param object $data The data instance.
|
||||
* @param string $csvdata The csv data to be imported.
|
||||
* @param string $encoding The encoding of csv data.
|
||||
* @param string $fielddelimiter The delimiter of the csv data.
|
||||
* @return int Number of records added.
|
||||
*/
|
||||
function data_import_csv($cm, $data, &$csvdata, $encoding, $fielddelimiter) {
|
||||
global $CFG, $DB;
|
||||
// Large files are likely to take their time and memory. Let PHP know
|
||||
// that we'll take longer, and that the process should be recycled soon
|
||||
// to free up memory.
|
||||
core_php_time_limit::raise();
|
||||
raise_memory_limit(MEMORY_EXTRA);
|
||||
|
||||
$iid = csv_import_reader::get_new_iid('moddata');
|
||||
$cir = new csv_import_reader($iid, 'moddata');
|
||||
|
||||
$context = context_module::instance($cm->id);
|
||||
|
||||
$readcount = $cir->load_csv_content($csvdata, $encoding, $fielddelimiter);
|
||||
$csvdata = null; // Free memory.
|
||||
if (empty($readcount)) {
|
||||
throw new \moodle_exception('csvfailed', 'data', "{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}");
|
||||
} else {
|
||||
if (!$fieldnames = $cir->get_columns()) {
|
||||
throw new \moodle_exception('cannotreadtmpfile', 'error');
|
||||
}
|
||||
|
||||
// Check the fieldnames are valid.
|
||||
$rawfields = $DB->get_records('data_fields', array('dataid' => $data->id), '', 'name, id, type');
|
||||
$fields = array();
|
||||
$errorfield = '';
|
||||
$usernamestring = get_string('username');
|
||||
$safetoskipfields = array(get_string('user'), get_string('email'),
|
||||
get_string('timeadded', 'data'), get_string('timemodified', 'data'),
|
||||
get_string('approved', 'data'), get_string('tags', 'data'));
|
||||
$userfieldid = null;
|
||||
foreach ($fieldnames as $id => $name) {
|
||||
if (!isset($rawfields[$name])) {
|
||||
if ($name == $usernamestring) {
|
||||
$userfieldid = $id;
|
||||
} else if (!in_array($name, $safetoskipfields)) {
|
||||
$errorfield .= "'$name' ";
|
||||
}
|
||||
} else {
|
||||
// If this is the second time, a field with this name comes up, it must be a field not provided by the user...
|
||||
// like the username.
|
||||
if (isset($fields[$name])) {
|
||||
if ($name == $usernamestring) {
|
||||
$userfieldid = $id;
|
||||
}
|
||||
unset($fieldnames[$id]); // To ensure the user provided content fields remain in the array once flipped.
|
||||
} else {
|
||||
$field = $rawfields[$name];
|
||||
$filepath = "$CFG->dirroot/mod/data/field/$field->type/field.class.php";
|
||||
if (!file_exists($filepath)) {
|
||||
$errorfield .= "'$name' ";
|
||||
continue;
|
||||
}
|
||||
require_once($filepath);
|
||||
$classname = 'data_field_' . $field->type;
|
||||
$fields[$name] = new $classname($field, $data, $cm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($errorfield)) {
|
||||
throw new \moodle_exception('fieldnotmatched', 'data',
|
||||
"{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}", $errorfield);
|
||||
}
|
||||
|
||||
$fieldnames = array_flip($fieldnames);
|
||||
|
||||
$cir->init();
|
||||
$recordsadded = 0;
|
||||
while ($record = $cir->next()) {
|
||||
$authorid = null;
|
||||
if ($userfieldid) {
|
||||
if (!($author = core_user::get_user_by_username($record[$userfieldid], 'id'))) {
|
||||
$authorid = null;
|
||||
} else {
|
||||
$authorid = $author->id;
|
||||
}
|
||||
}
|
||||
if ($recordid = data_add_record($data, 0, $authorid)) { // Add instance to data_record.
|
||||
foreach ($fields as $field) {
|
||||
$fieldid = $fieldnames[$field->field->name];
|
||||
if (isset($record[$fieldid])) {
|
||||
$value = $record[$fieldid];
|
||||
} else {
|
||||
$value = '';
|
||||
}
|
||||
|
||||
if (method_exists($field, 'update_content_import')) {
|
||||
$field->update_content_import($recordid, $value, 'field_' . $field->field->id);
|
||||
} else {
|
||||
$content = new stdClass();
|
||||
$content->fieldid = $field->field->id;
|
||||
$content->content = $value;
|
||||
$content->recordid = $recordid;
|
||||
$DB->insert_record('data_content', $content);
|
||||
}
|
||||
}
|
||||
|
||||
if (core_tag_tag::is_enabled('mod_data', 'data_records') &&
|
||||
isset($fieldnames[get_string('tags', 'data')])) {
|
||||
$columnindex = $fieldnames[get_string('tags', 'data')];
|
||||
$rawtags = $record[$columnindex];
|
||||
$tags = explode(',', $rawtags);
|
||||
foreach ($tags as $tag) {
|
||||
$tag = trim($tag);
|
||||
if (empty($tag)) {
|
||||
continue;
|
||||
}
|
||||
core_tag_tag::add_item_tag('mod_data', 'data_records', $recordid, $context, $tag);
|
||||
}
|
||||
}
|
||||
|
||||
$recordsadded++;
|
||||
print get_string('added', 'moodle', $recordsadded) . ". " . get_string('entry', 'data') . " (ID $recordid)<br />\n";
|
||||
}
|
||||
}
|
||||
$cir->close();
|
||||
$cir->cleanup(true);
|
||||
return $recordsadded;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// File API //
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -16,6 +16,11 @@
|
||||
|
||||
namespace mod_data;
|
||||
|
||||
use coding_exception;
|
||||
use dml_exception;
|
||||
use mod_data\local\mod_data_csv_importer;
|
||||
use moodle_exception;
|
||||
|
||||
/**
|
||||
* Unit tests for import.php.
|
||||
*
|
||||
@ -79,6 +84,7 @@ class import_test extends \advanced_testcase {
|
||||
|
||||
/**
|
||||
* Test uploading entries for a data instance without userdata.
|
||||
*
|
||||
* @throws dml_exception
|
||||
*/
|
||||
public function test_import(): void {
|
||||
@ -88,10 +94,9 @@ class import_test extends \advanced_testcase {
|
||||
'teacher' => $teacher,
|
||||
] = $this->get_test_data();
|
||||
|
||||
$filecontent = file_get_contents(__DIR__ . '/fixtures/test_data_import.csv');
|
||||
ob_start();
|
||||
data_import_csv($cm, $data, $filecontent, 'UTF-8', 'comma');
|
||||
ob_end_clean();
|
||||
$importer = new mod_data_csv_importer(__DIR__ . '/fixtures/test_data_import.csv',
|
||||
'test_data_import.csv');
|
||||
$importer->import_csv($cm, $data, 'UTF-8', 'comma');
|
||||
|
||||
// No userdata is present in the file: Fallback is to assign the uploading user as author.
|
||||
$expecteduserids = array();
|
||||
@ -110,6 +115,9 @@ class import_test extends \advanced_testcase {
|
||||
* Test uploading entries for a data instance with userdata.
|
||||
*
|
||||
* At least one entry has an identifiable user, which is assigned as author.
|
||||
*
|
||||
* @throws coding_exception
|
||||
* @throws moodle_exception
|
||||
* @throws dml_exception
|
||||
*/
|
||||
public function test_import_with_userdata(): void {
|
||||
@ -120,10 +128,9 @@ class import_test extends \advanced_testcase {
|
||||
'student' => $student,
|
||||
] = $this->get_test_data();
|
||||
|
||||
$filecontent = file_get_contents(__DIR__ . '/fixtures/test_data_import_with_userdata.csv');
|
||||
ob_start();
|
||||
data_import_csv($cm, $data, $filecontent, 'UTF-8', 'comma');
|
||||
ob_end_clean();
|
||||
$importer = new mod_data_csv_importer(__DIR__ . '/fixtures/test_data_import_with_userdata.csv',
|
||||
'test_data_import_with_userdata.csv');
|
||||
$importer->import_csv($cm, $data, 'UTF-8', 'comma');
|
||||
|
||||
$expecteduserids = array();
|
||||
$expecteduserids[1] = $student->id; // User student exists and is assigned as author.
|
||||
@ -143,6 +150,8 @@ class import_test extends \advanced_testcase {
|
||||
* This should test the corner case, in which a user has defined a data fields, which has the same name
|
||||
* as the current lang string for username. In that case, the first Username entry is used for the field.
|
||||
* The second one is used to identify the author.
|
||||
*
|
||||
* @throws moodle_exception
|
||||
* @throws coding_exception
|
||||
* @throws dml_exception
|
||||
*/
|
||||
@ -161,10 +170,9 @@ class import_test extends \advanced_testcase {
|
||||
$fieldrecord->type = 'text';
|
||||
$generator->create_field($fieldrecord, $data);
|
||||
|
||||
$filecontent = file_get_contents(__DIR__ . '/fixtures/test_data_import_with_field_username.csv');
|
||||
ob_start();
|
||||
data_import_csv($cm, $data, $filecontent, 'UTF-8', 'comma');
|
||||
ob_end_clean();
|
||||
$importer = new mod_data_csv_importer(__DIR__ . '/fixtures/test_data_import_with_field_username.csv',
|
||||
'test_data_import_with_field_username.csv');
|
||||
$importer->import_csv($cm, $data, 'UTF-8', 'comma');
|
||||
|
||||
$expecteduserids = array();
|
||||
$expecteduserids[1] = $student->id; // User student exists and is assigned as author.
|
||||
@ -193,7 +201,7 @@ class import_test extends \advanced_testcase {
|
||||
|
||||
foreach ($expectedcontent[$identifier] as $field => $value) {
|
||||
$this->assertEquals($value, $record->items[$field]->content,
|
||||
"The value of field \"$field\" for the record at position \"$identifier\" ".
|
||||
"The value of field \"$field\" for the record at position \"$identifier\" " .
|
||||
"which is \"{$record->items[$field]->content}\" does not match the expected value \"$value\".");
|
||||
}
|
||||
}
|
||||
@ -205,8 +213,9 @@ class import_test extends \advanced_testcase {
|
||||
* This should test the corner case, in which a user has defined a data fields, which has the same name
|
||||
* as the current lang string for username. In that case, the only Username entry is used for the field.
|
||||
* The author should not be set.
|
||||
* @throws coding_exception
|
||||
*
|
||||
* @throws dml_exception
|
||||
* @throws moodle_exception
|
||||
*/
|
||||
public function test_import_with_field_username_without_userdata(): void {
|
||||
[
|
||||
@ -223,10 +232,9 @@ class import_test extends \advanced_testcase {
|
||||
$fieldrecord->type = 'text';
|
||||
$generator->create_field($fieldrecord, $data);
|
||||
|
||||
$filecontent = file_get_contents(__DIR__ . '/fixtures/test_data_import_with_userdata.csv');
|
||||
ob_start();
|
||||
data_import_csv($cm, $data, $filecontent, 'UTF-8', 'comma');
|
||||
ob_end_clean();
|
||||
$importer = new mod_data_csv_importer(__DIR__ . '/fixtures/test_data_import_with_userdata.csv',
|
||||
'test_data_import_with_userdata.csv');
|
||||
$importer->import_csv($cm, $data, 'UTF-8', 'comma');
|
||||
|
||||
// No userdata is present in the file: Fallback is to assign the uploading user as author.
|
||||
$expecteduserids = array();
|
||||
@ -251,7 +259,7 @@ class import_test extends \advanced_testcase {
|
||||
|
||||
foreach ($expectedcontent[$identifier] as $field => $value) {
|
||||
$this->assertEquals($value, $record->items[$field]->content,
|
||||
"The value of field \"$field\" for the record at position \"$identifier\" ".
|
||||
"The value of field \"$field\" for the record at position \"$identifier\" " .
|
||||
"which is \"{$record->items[$field]->content}\" does not match the expected value \"$value\".");
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,9 @@ information provided here is intended especially for developers.
|
||||
refactoring of the way data is being exported. This is now being done by new exporter classes
|
||||
\mod_data\local\csv_exporter and \mod_data\local\ods_exporter (inheriting from exporter base class
|
||||
\mod_data\local\exporter) as well as \mod_data\local\exporter_utils::data_exportdata().
|
||||
* Function data_import_csv() has been deprecated and moved to deprecatedlib due to a bigger rework of the way data is
|
||||
being imported. This is now being done by new importer class \mod_data\local\mod_data_csv_importer inheriting from new
|
||||
classes \mod_data\local\csv_importer and \mod_data\local\importer.
|
||||
|
||||
== 4.2 ==
|
||||
* The field base class now has a method validate(). Overwrite it in the field type to provide validation of field type's
|
||||
|
Loading…
x
Reference in New Issue
Block a user