On this page

All pages






The export object provides a files array where files for the export can be added. But this just makes sure the actual file is exported. There won't be any information about which data record the files are linked to. We need to add this missing information in each item which exports files.

If an item has one or more files associated to it's data records then it needs to:

  1. Add file information to the data record the file is associated to
  2. Add the files to the export→files[] array

Data format for files

Files added to the data records need to have the following format:

[
	'fileid' => $file->get_id(),
	'filename' => $file->get_filename(),
	'contenthash' => $file->get_contenthash() // with the hash the user can find the file in the exported archive
]

The key for the files in the specific record should always be 'files' but depending on the item the files can be more detailed structured inside the files array. Forum posts for example have attachment and post files so the structure can look like that:

$record = [
	...
	'files' => [
		'attachments' => [
			[ ... ],
			[ ... ],
			...
		],
		'posts' => [
			[ ... ],
			[ ... ],
			...
		]
	]
];

Helper method

There's a helper method in the export class \totara_userdata\userdata\export::add_file() which adds a file to the export→files[] array and returns the standardised data to be added to the data.

Example
$export = new export();

// Load all records
$forumposts = ...;

// Go through all records which can have files associated to
foreach ($forumposts as $post) {
	// Prepare files entry for this post
    $post['files'] = [];

    // Load files for this post
    $postfiles = ...
    foreach ($postfiles as $file) {
		// Add the file information to the record
		// and add it to the files array (without it the file won't be exported)
	    $post['files'][] = $export->add_file($file);
    }
	// Export the data
	$export->data[] = $post;
}