Moodle PHP Documentation 4.5
Moodle 4.5dev (Build: 20240606) (d3ae1391abe)
mod_data\preset Class Reference

Public Member Functions

 can_manage ()
 Checks to see if the user has permission to manage the preset.
 
 delete ()
 Deletes all files related to a saved preset.
 
 export ()
 Export this preset.
 
 get_fields (bool $forpreview=false)
 Return the field instances of the preset.
 
 get_fullname ()
 Return the preset fullname.
 
 get_path ()
 Returns the preset path.
 
 get_sample_entries (int $count=1)
 Return sample entries to preview this preset.
 
 get_template_content (string $templatename)
 Return the template content from the preset.
 
 get_userid ()
 Return the preset author.
 
 save ()
 Save this preset.
 

Static Public Member Functions

static create_from_fullname (manager $manager, string $fullname)
 Create a preset instance from the preset fullname.
 
static create_from_instance (manager $manager, string $presetname, ?string $description='')
 Create a preset instance from a data_record entry, a preset name and a description.
 
static create_from_plugin (?manager $manager, string $pluginname)
 Create a preset instance from a plugin.
 
static create_from_storedfile (manager $manager, stored_file $file)
 Create a preset instance from a stored file.
 
static get_description_from_plugin (string $pluginname)
 Returns the description to show for a datapreset plugin.
 
static get_file (string $filepath, string $filename)
 Helper method to retrieve a file.
 
static get_name_from_plugin (string $pluginname)
 Returns the best name to show for a datapreset plugin.
 
static is_directory_a_preset (string $directory)
 Checks if a directory contains all the required files to define a preset.
 

Public Attributes

string $description
 The preset description.
 
bool $isplugin
 whether the preset is a plugin or has been saved by the user.
 
string $name
 The preset name.
 
string $shortname
 The preset shortname.
 
stored_file $storedfile
 For saved presets that's the file object for the root folder.
 

Protected Member Functions

 __construct (?manager $manager, bool $isplugin, string $name, string $shortname, ?string $description='', ?stored_file $storedfile=null)
 Class constructor.
 
 generate_preset_xml ()
 Helper method to generate the XML for this preset.
 
 load_preset_xml ()
 Load all the information from the preset.xml.
 

Static Protected Member Functions

static get_attribute_value (string $filepath, string $name)
 Helper to get the value of one of the elements in the presets.xml file.
 
static get_content_from_file (string $filepath, string $filename)
 Helper method to retrieve the contents of a file.
 
static get_filerecord (string $filename, string $filepath, int $userid)
 Helper method to get a file record given a filename, a filepath and a userid, for any of the preset files.
 

Protected Attributes

stdClass null $xmlinfo = null
 the preset.xml parsed information.
 

Constructor & Destructor Documentation

◆ __construct()

mod_data\preset::__construct ( ?manager $manager,
bool $isplugin,
string $name,
string $shortname,
?string $description = '',
?stored_file $storedfile = null )
protected

Class constructor.

Parameters
manager | null$managerthe current instance manager
bool$ispluginwhether the preset is a plugin or has been saved by the user
string$namethe preset name
string$shortnamethe preset shortname
string | null$descriptionthe preset description
stored_file | null$storedfilefor saved presets, that's the file for the root folder
Exceptions
invalid_parameter_exception

Member Function Documentation

◆ can_manage()

mod_data\preset::can_manage ( )

Checks to see if the user has permission to manage the preset.

Return values
boolReturns true if the user can manage this preset, false otherwise.

◆ create_from_fullname()

static mod_data\preset::create_from_fullname ( manager $manager,
string $fullname )
static

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.

Parameters
manager$managerthe current instance manager
string$fullnamethe preset full name
Return values
preset

◆ create_from_instance()

static mod_data\preset::create_from_instance ( manager $manager,
string $presetname,
?string $description = '' )
static

Create a preset instance from a data_record entry, a preset name and a description.

Parameters
manager$managerthe current instance manager
string$presetnamethe preset name
string | null$descriptionthe preset description
Return values
preset

◆ create_from_plugin()

static mod_data\preset::create_from_plugin ( ?manager $manager,
string $pluginname )
static

Create a preset instance from a plugin.

Parameters
manager | null$managerthe current instance manager
string$pluginnamethe datapreset plugin name
Return values
preset|nullThe plugin preset or null if there is no datapreset plugin with the given name.

◆ create_from_storedfile()

static mod_data\preset::create_from_storedfile ( manager $manager,
stored_file $file )
static

Create a preset instance from a stored file.

Parameters
manager$managerthe current instance manager
stored_file$filethe preset root folder
Return values
preset|nullIf the given file doesn't belong to the expected component/filearea/context, null will be returned

◆ delete()

mod_data\preset::delete ( )

Deletes all files related to a saved preset.

Return values
boolTrue if the preset is a saved preset and the file exists in the file system; false otherwise.

◆ export()

mod_data\preset::export ( )

Export this preset.

Return values
stringthe full path to the exported preset file.

◆ generate_preset_xml()

mod_data\preset::generate_preset_xml ( )
protected

Helper method to generate the XML for this preset.

Return values
stringThe XML for the preset

◆ get_attribute_value()

static mod_data\preset::get_attribute_value ( string $filepath,
string $name )
staticprotected

Helper to get the value of one of the elements in the presets.xml file.

Parameters
string$filepathThe preset filepath.
string$nameAttribute name to return.
Return values
string|nullThe attribute value; null if the it doesn't exist or the file is not a valid XML.

◆ get_content_from_file()

static mod_data\preset::get_content_from_file ( string $filepath,
string $filename )
staticprotected

Helper method to retrieve the contents of a file.

Parameters
string$filepaththe directory to look in
string$filenamethe name of the file we want
Return values
string|nullthe contents of the file or null if the file doesn't exist.

◆ get_description_from_plugin()

static mod_data\preset::get_description_from_plugin ( string $pluginname)
static

Returns the description to show for a datapreset plugin.

Parameters
string$pluginnameThe datapreset plugin name.
Return values
stringThe plugin preset description to display.

◆ get_fields()

mod_data\preset::get_fields ( bool $forpreview = false)

Return the field instances of the preset.

Parameters
bool$forpreviewif the fields are only for preview
Return values
data_field_base[]and array with field objects

◆ get_file()

static mod_data\preset::get_file ( string $filepath,
string $filename )
static

Helper method to retrieve a file.

Parameters
string$filepaththe directory to look in
string$filenamethe name of the file we want
Return values
stored_file|nullthe file or null if the file doesn't exist.

◆ get_filerecord()

static mod_data\preset::get_filerecord ( string $filename,
string $filepath,
int $userid )
staticprotected

Helper method to get a file record given a filename, a filepath and a userid, for any of the preset files.

Parameters
string$filenameThe filename for the filerecord that will be returned.
string$filepathThe filepath for the filerecord that will be returned.
int$useridThe userid for the filerecord that will be returned.
Return values
stdClassA filerecord object with the datapreset context, component and filearea and the given information.

◆ get_fullname()

mod_data\preset::get_fullname ( )

Return the preset fullname.

Preset fullname is used mostly for urls.

Return values
stringthe preset fullname

◆ get_name_from_plugin()

static mod_data\preset::get_name_from_plugin ( string $pluginname)
static

Returns the best name to show for a datapreset plugin.

Parameters
string$pluginnameThe datapreset plugin name.
Return values
stringThe plugin preset name to display.

◆ get_path()

mod_data\preset::get_path ( )

Returns the preset path.

Return values
string|nullthe preset path is null for plugins and /presetname/ for saved presets.

◆ get_sample_entries()

mod_data\preset::get_sample_entries ( int $count = 1)

Return sample entries to preview this preset.

Parameters
int$countthe number of entries to generate.
Return values
arrayof sample entries

◆ get_template_content()

mod_data\preset::get_template_content ( string $templatename)

Return the template content from the preset.

Parameters
string$templatenamethe template name
Return values
stringthe template content

◆ get_userid()

mod_data\preset::get_userid ( )

Return the preset author.

Preset plugins do not have any user id.

Return values
int|nullthe userid or null if it is a plugin

◆ is_directory_a_preset()

static mod_data\preset::is_directory_a_preset ( string $directory)
static

Checks if a directory contains all the required files to define a preset.

Parameters
string$directoryThe patch to check if it contains the preset files or not.
Return values
boolTrue if the directory contains all the preset files; false otherwise.

◆ save()

mod_data\preset::save ( )

Save this preset.

Return values
booltrue if the preset has been saved; false otherwise.

Member Data Documentation

◆ $shortname

string mod_data\preset::$shortname

The preset shortname.

For datapreset plugins that is the folder; for saved presets, that's the preset name.

◆ $storedfile

stored_file mod_data\preset::$storedfile

For saved presets that's the file object for the root folder.

It's null for plugins or for presets that haven't been saved yet.


The documentation for this class was generated from the following file: