Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

(no description)

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

Defines 1 class


Class: preset  - X-Ref

Class preset for database activity.

__construct(?manager $manager,bool $isplugin,string $name,string $shortname,?string $description = '',?stored_file $storedfile = null)   X-Ref
Class constructor.

param: manager|null $manager the current instance manager
param: bool $isplugin whether the preset is a plugin or has been saved by the user
param: string $name the preset name
param: string $shortname the preset shortname
param: string|null $description the preset description
param: stored_file|null $storedfile for saved presets, that's the file for the root folder

create_from_storedfile(manager $manager, stored_file $file)   X-Ref
Create a preset instance from a stored file.

param: manager $manager the current instance manager
param: stored_file $file the preset root folder
return: preset|null If the given file doesn't belong to the expected component/filearea/context, null will be returned

create_from_plugin(?manager $manager, string $pluginname)   X-Ref
Create a preset instance from a plugin.

param: manager|null $manager the current instance manager
param: string $pluginname the datapreset plugin name
return: preset|null The plugin preset or null if there is no datapreset plugin with the given name.

create_from_instance(manager $manager, string $presetname, ?string $description = '')   X-Ref
Create a preset instance from a data_record entry, a preset name and a description.

param: manager $manager the current instance manager
param: string $presetname the preset name
param: string|null $description the preset description
return: preset

create_from_fullname(manager $manager, string $fullname)   X-Ref
Create a preset instance from the preset fullname.

The preset fullname is a concatenation of userid and pluginname|presetname used by most
preset pages. Plugins uses userid zero while preset instances has the owner as identifier.

This method will throw an exception if the preset instance has a different userid thant the one
from the $fullname. However, it won't check the current user capabilities.

param: manager $manager the current instance manager
param: string $fullname the preset full name
return: preset

save()   X-Ref
Save this preset.

return: bool true if the preset has been saved; false otherwise.

update_user_preset()   X-Ref
Update the stored user preset.
This method is used internally by the save method.

return: bool true if the preset has been saved; false otherwise.

export()   X-Ref
Export this preset.

return: string the full path to the exported preset file.

get_userid()   X-Ref
Return the preset author.

Preset plugins do not have any user id.

return: int|null the userid or null if it is a plugin

get_fullname()   X-Ref
Return the preset fullname.

Preset fullname is used mostly for urls.

return: string the preset fullname

get_path()   X-Ref
Returns the preset path.

return: string|null the preset path is null for plugins and /presetname/ for saved presets.

get_fields(bool $forpreview = false)   X-Ref
Return the field instances of the preset.

param: bool $forpreview if the fields are only for preview
return: data_field_base[]  and array with field objects

get_field_instance(SimpleXMLElement $fieldinfo,int $id = 0,bool $forpreview = false)   X-Ref
Convert a preset.xml field data into field instance.

param: SimpleXMLElement $fieldinfo the field xml information
param: int $id the field id to use
param: bool $forpreview if the field should support preview
return: data_field_base the field instance

get_fake_field_record(SimpleXMLElement $fieldinfo, int $id = 0)   X-Ref
Generate a fake field record fomr the preset.xml field data.

param: SimpleXMLElement $fieldinfo the field xml information
param: int $id the field id to use
return: stdClass the fake record

get_sample_entries(int $count = 1)   X-Ref
Return sample entries to preview this preset.

param: int $count the number of entries to generate.
return: array of sample entries

load_preset_xml()   X-Ref
Load all the information from the preset.xml.


get_template_content(string $templatename)   X-Ref
Return the template content from the preset.

param: string $templatename the template name
return: string the template content

is_directory_a_preset(string $directory)   X-Ref
Checks if a directory contains all the required files to define a preset.

param: string $directory The patch to check if it contains the preset files or not.
return: bool True if the directory contains all the preset files; false otherwise.

get_name_from_plugin(string $pluginname)   X-Ref
Returns the best name to show for a datapreset plugin.

param: string $pluginname The datapreset plugin name.
return: string The plugin preset name to display.

get_description_from_plugin(string $pluginname)   X-Ref
Returns the description to show for a datapreset plugin.

param: string $pluginname The datapreset plugin name.
return: string The plugin preset description to display.

get_attribute_value(string $filepath, string $name)   X-Ref
Helper to get the value of one of the elements in the presets.xml file.

param: string $filepath The preset filepath.
param: string $name Attribute name to return.
return: string|null The attribute value; null if the it doesn't exist or the file is not a valid XML.

get_filerecord(string $filename, string $filepath, int $userid)   X-Ref
Helper method to get a file record given a filename, a filepath and a userid, for any of the preset files.

param: string $filename The filename for the filerecord that will be returned.
param: string $filepath The filepath for the filerecord that will be returned.
param: int $userid The userid for the filerecord that will be returned.
return: stdClass A filerecord object with the datapreset context, component and filearea and the given information.

get_file(string $filepath, string $filename)   X-Ref
Helper method to retrieve a file.

param: string $filepath the directory to look in
param: string $filename the name of the file we want
return: stored_file|null the file or null if the file doesn't exist.

get_content_from_file(string $filepath, string $filename)   X-Ref
Helper method to retrieve the contents of a file.

param: string $filepath the directory to look in
param: string $filename the name of the file we want
return: string|null the contents of the file or null if the file doesn't exist.

generate_preset_xml()   X-Ref
Helper method to generate the XML for this preset.

return: string The XML for the preset

can_manage()   X-Ref
Checks to see if the user has permission to manage the preset.

return: bool  Returns true if the user can manage this preset, false otherwise.

delete()   X-Ref
Deletes all files related to a saved preset.

return: bool True if the preset is a saved preset and the file exists in the file system; false otherwise.