From 7a530277efa6f69f0cf3c64e1d368f4e8314ec30 Mon Sep 17 00:00:00 2001 From: thepurpleblob Date: Fri, 10 Jun 2005 09:51:25 +0000 Subject: [PATCH] Added isset_param() convenience function to test if a param is set. To get the nasty activity in one place and to improve code readability. --- lib/moodlelib.php | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 3558b2c8bf9..78897a053a7 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -171,6 +171,21 @@ function optional_param($varname, $default=NULL, $options=PARAM_CLEAN) { return clean_param($param, $options); } +/** + * Convenience function to test if a parameter is set + * @param string $varname name of the parameter + * @return boolean true if set otherwise false + */ +function isset_param($varname) { + if (isset($_GET[$varname])) { + return true; + } + if (isset($_POST[$varname])) { + return true; + } + return false; +} + /** * Used by {@link optional_param()} and {@link required_param()} to * clean the variables and/or cast to specific types, based on @@ -415,13 +430,29 @@ function require_variable($var) { function optional_variable(&$var, $default=0) { global $CFG; if (!empty($CFG->disableglobalshack)) { - error( 'The optional_variable() function is deprecated.' ); + error( "The optional_variable() function is deprecated ($var, $default)." ); } if (! isset($var)) { $var = $default; } } + +/** + * If a variable is empty set it to the default + * otherwise leave it alone + * @param mixed $var the variable to test + * @param mixed $default the default value + * @return boolean true if variable has changed + */ +function set_default( &$var, $default ) { + if (empty($var)) { + $var = $default; + return true; + } + return false; +} + /** * Set a key in global configuration *