Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [Versions 402 and 403]

(no description)

File Size: 727 lines (30 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: moodle_content_writer  - X-Ref

The moodle_content_writer is the default Moodle implementation of a content writer.

It exports data to a rich tree structure using Moodle's context system,
and produces a single zip file with all content.

Objects of data are stored as JSON.

__construct(writer $writer)   X-Ref
Constructor for the content writer.

Note: The writer factory must be passed.

param: writer          $writer     The factory.

set_context(\context $context)   X-Ref
Set the context for the current item being processed.

param: \context        $context    The context to use

export_data(array $subcontext, \stdClass $data)   X-Ref
Export the supplied data within the current context, at the supplied subcontext.

param: array           $subcontext The location within the current context that this data belongs.
param: \stdClass       $data       The data to be exported
return: content_writer

export_metadata(array $subcontext, string $key, $value, string $description)   X-Ref
Export metadata about the supplied subcontext.

Metadata consists of a key/value pair and a description of the value.

param: array           $subcontext The location within the current context that this data belongs.
param: string          $key        The metadata name.
param: string          $value      The metadata value.
param: string          $description    The description of the value.
return: content_writer

export_related_data(array $subcontext, $name, $data)   X-Ref
Export a piece of related data.

param: array           $subcontext The location within the current context that this data belongs.
param: string          $name       The name of the file to be exported.
param: \stdClass       $data       The related data to export.
return: content_writer

export_custom_file(array $subcontext, $filename, $filecontent)   X-Ref
Export a piece of data in a custom format.

param: array           $subcontext The location within the current context that this data belongs.
param: string          $filename   The name of the file to be exported.
param: string          $filecontent    The content to be exported.

rewrite_pluginfile_urls(array $subcontext, $component, $filearea, $itemid, $text)   X-Ref
Prepare a text area by processing pluginfile URLs within it.

param: array           $subcontext The location within the current context that this data belongs.
param: string          $component  The name of the component that the files belong to.
param: string          $filearea   The filearea within that component.
param: string          $itemid     Which item those files belong to.
param: string          $text       The text to be processed
return: string                      The processed string

export_area_files(array $subcontext, $component, $filearea, $itemid)   X-Ref
Export all files within the specified component, filearea, itemid combination.

param: array           $subcontext The location within the current context that this data belongs.
param: string          $component  The name of the component that the files belong to.
param: string          $filearea   The filearea within that component.
param: string          $itemid     Which item those files belong to.

export_file(array $subcontext, \stored_file $file)   X-Ref
Export the specified file in the target location.

param: array           $subcontext The location within the current context that this data belongs.
param: \stored_file    $file       The file to be exported.

export_user_preference(string $component, string $key, string $value, string $description)   X-Ref
Export the specified user preference.

param: string          $component  The name of the component.
param: string          $key        The name of th key to be exported.
param: string          $value      The value of the preference
param: string          $description    A description of the value
return: content_writer

get_context_path()   X-Ref
Determine the path for the current context.

return: array The context path.

get_path(array $subcontext, string $name)   X-Ref
Get the relative file path within the current context, and subcontext, using the specified filename.

param: string[]        $subcontext The location within the current context to export this data.
param: string          $name       The intended filename, including any extensions.
return: string                      The fully-qualfiied file path.

get_full_path(array $subcontext, string $name)   X-Ref
Get the fully-qualified file path within the current context, and subcontext, using the specified filename.

param: string[]        $subcontext The location within the current context to export this data.
param: string          $name       The intended filename, including any extensions.
return: string                      The fully-qualfiied file path.

get_files_target_path($component, $filearea, $itemid)   X-Ref
Get a path within a subcontext where exported files should be written to.

param: string $component The name of the component that the files belong to.
param: string $filearea The filearea within that component.
param: string $itemid Which item those files belong to.
return: string The path

get_files_target_url($component, $filearea, $itemid)   X-Ref
Get a relative url to the directory of the exported files within a subcontext.

param: string $component The name of the component that the files belong to.
param: string $filearea The filearea within that component.
param: string $itemid Which item those files belong to.
return: string The url

write_data(string $path, string $data)   X-Ref
Write the data to the specified path.

param: string          $path       The path to export the data at.
param: string          $data       The data to be exported.

copy_data(array $path, array $destination)   X-Ref
Copy a file to the specified path.

param: array  $path        Current location of the file.
param: array  $destination Destination path to copy the file to.

prepare_for_export()   X-Ref
This creates three different bits of data from all of the files that will be
exported.
$tree - A multidimensional array of the navigation tree structure.
$treekey - An array with the short path of the file and element data for
html (data_file_{number} or 'No var')
$allfiles - All *.json files that need to be added as an index to be referenced
by the js files to display the user data.

return: array returns a tree, tree key, and a list of all files.

make_tree_object(array $tree, array $treekey, array $currentkey = [])   X-Ref
Add more detail to the tree to help with sorting and display in the renderer.

param: array  $tree       The file structure currently as a multidimensional array.
param: array  $treekey    An array of the current file paths.
param: array  $currentkey The current short path of the tree.
return: array An array of objects that has additional data.

sort_my_list(array &$tree)   X-Ref
Sorts the tree list into an order that makes more sense.
Order is:
1 - Items with a context first, the lower the number the higher up the tree.
2 - Items that are directories.
3 - Items that are log directories.
4 - Links to a page.

param: array $tree The tree structure to order.

check_plugin_is_installed(string $component)   X-Ref
Check to see if a specified plugin is installed.

param: string $component The component name e.g. tool_log
return: bool Whether this component is installed.

write_html_data()   X-Ref
Writes the appropriate files for creating an HTML index page for human navigation of the user data export.


finalise_content()   X-Ref
Perform any required finalisation steps and return the location of the finalised export.

return: string

condense_array(array $array)   X-Ref
Creates a multidimensional array out of array elements.

param: array  $array Array which items are to be condensed into a multidimensional array.
return: array The multidimensional array.

get_file_content(string $filepath)   X-Ref
Get the contents of a file.

param: string $filepath The file path.
return: string contents of the file.