Variables

Appzio Variables

One of the key concepts in Appzio is variables. Variables are key – value pairs bound to the user and used to store different data. They are used all around the platform – from storing credentials, to storing additional user data, to storing the current registration phase for the user. Variables can’t collide, so unless explicitly specified you will always access and save the variables for the current application user.

getSavedVariable

Looks up and returns the value of a variable for the current user. This method only looks for variables in the database.

Available Parameters

varname

Name of the variable

default

Default: false | Value that should be returned if the variable is not found

Example

// Get the current registration phase
$this->getSavedVariable('reg_phase')

getSubmitVariable

Looks up and returns a variable. Will only look in the submitted variables in the submitvariables array. If not found by name, it will get the variable id and look for that – again in the submitvariables. If not found then it will return false.

Available Parameters

varid

Identifier of the variable

Example

// Get the submitted email value
$this->getSubmitVariable('email');

getVariable

Looks up and returns a variable. This method will search both in the submitted variables and in storage. If you know where the variable is stored the best practice is to use the corresponding method for that.

Available Parameters

varid

Identifier of the variable

Example

// Get the name variable
$this->getVariable('name');

getVariableId

If the variable exists in the database it will be submitted with it’s ID as it’s key. This method returns the ID of the variable based on it’s name.

Available Parameters

varname

The variable name

Example

// Get the email variable's id
$id_email = $this->getVariableId('email');

// Get the submitted email variable by it's id
$email = $this->getSubmitVariable($id_email);

getSubmittedVariableByName

This method will look for a variable in the submitvariables array. It will look both for a variable with this name, or use getVariableId() under the hood to find it. Returns default value if it doesn’t.

Available Parameters

varname

The variable name

default

Default: false | Default return value if variable is not found

Example

// Will look for the email variable in the submitted vars
$this->getSubmittedVariableByName('email');

requireVariable

Will look for a variable in the submitvariables and the database. Accepts an error message as a second argument, throws a default one if not given one.

Available Parameters

varname

The variable name

customerror

Default: false | Custom error message if variable is not found

Example

// Get the email variable or an error message if it's not found
$this->requireVariable('email', 'The email is required.');

getArrayOfPlayvariables

Returns an array of all variables for a particular user. Used when displaying user data.

Available Parameters

playid

The user id

Example

$vars = AeplayVariable::getArrayOfPlayvariables($this->playid);

loadVariableContent

Loads all variables for the currently logged user into the varcontent property. Uses getArrayOfPlayvariables with the current user’s id.

Example

$this->loadVariableContent()

saveVariable

Save a variable in the database for the currently logged user.

Available Parameters

variable

The variable name

value

The variable value

Example

$this->saveVariable('email', $this->getSubmitVariable('email'));

deleteVariable

Delete a variable for the currently logged user.

Available Parameters

varname

The variable name

Example

$this->deleteVariable('token');

addToVariable

Variables are often used to store JSON encoded data like settings or additional user information. This method allows you to push items to that array. The platform expects this variable to be an array, if it’s empty or it is a regular string it will be overwritten.

Available Parameters

variable

The variable name

value

The variable value

Example

$this->addToVariable($jsonVariable, $item);

removeFromVariable

Removes an item from the variable. The platform expects the variable to be a JSON encoded array.

Available Parameters

variable

The variable name

value

The value that is to be removed

Example

$this->removeFromVariable($jsonVariable, $value);