Commit 9417b9f9 authored by Richard Mansfield's avatar Richard Mansfield
Browse files
parents 874d5ae9 650d0306
......@@ -1191,6 +1191,31 @@ function db_array_to_ph($array) {
return array_pad(array(), count($array), '?');
}
/**
* This function starts a smart transaction
*
*/
function db_begin() {
global $db;
$db->StartTrans();
}
/**
* This function commits a smart transaction
*
* If the transaction has failed for any reason, an exception is thrown.
*
*/
function db_commit() {
global $db;
if ($db->HasFailedTrans()) {
$db->CompleteTrans();
throw new SQLException('Transaction Failed');
}
$db->CompleteTrans();
}
?>
......@@ -68,6 +68,7 @@ function form_render_date($element, Form $form) {
return $result;
}
/** gets the value explicitly from the request */
function form_get_value_date($element, Form $form) {
$name = $element['name'];
$global = ($form->get_method() == 'get') ? $_GET : $_POST;
......@@ -78,6 +79,7 @@ function form_get_value_date($element, Form $form) {
return $time;
}
/** helper: used when rendering the element, to get the value for it */
function form_render_select_get_value($timeperiod, $min, $max, $element, Form $form) {
static $lookup = array(
'year' => 0,
......
......@@ -30,10 +30,19 @@ defined('INTERNAL') || die();
* Checks whether the field has been specified.
*
* @param string $field The field to check
* @param array $element The element to check
* @return string The error message, if the value is invalid.
*/
function form_rule_required($field) {
function form_rule_required($field, $element) {
// The array test is for using the "required" rule on file elements
$function = 'form_is_empty_' . $element['type'];
if (function_exists($function)) {
if ($function($field, $element)) {
return get_string('This field is required');
}
return;
}
if ($field == '' || is_array($field) && !empty($field['error'])) {
return get_string('This field is required');
}
......
......@@ -512,8 +512,10 @@ function clean_filename($filename) {
*
*/
function json_headers() {
// @todo martyn This should be changed, but for now it's useful for debugging
// header('Content-type: text/x-json');
header('Content-type: text/plain');
header('Pragma: no-cache');
}
/**
......@@ -527,7 +529,7 @@ function json_headers() {
* @return string The value of the parameter
*
*/
function get_variable($name) {
function param_variable($name) {
// if it's not set and we have a default
if (!isset($_REQUEST[$name]) && func_num_args() == 2) {
$php_work_around = func_get_arg(1);
......@@ -553,13 +555,13 @@ function get_variable($name) {
* @return string The value of the parameter
*
*/
function get_integer($name) {
function param_integer($name) {
if (func_num_args() == 2) {
$php_work_around = func_get_arg(1);
$value = get_variable($name,$php_work_around);
$value = param_variable($name,$php_work_around);
}
else {
$value = get_variable($name);
$value = param_variable($name);
}
if (preg_match('/^\d+$/',$value)) {
......@@ -569,6 +571,71 @@ function get_integer($name) {
throw new ParameterException("Parameter '$name' = '$value' is not an integer");
}
/**
* This function returns a GET or POST parameter as an alpha string with optional
* default. If the default isn't specified and the parameter hasn't been sent,
* a ParameterException exception is thrown. Likewise, if the parameter isn't a
* valid alpha string, a ParameterException exception is thrown
*
* Valid characters are a-z and A-Z
*
* @param string The GET or POST parameter you want returned
* @param mixed [optional] the default value for this parameter
*
* @return string The value of the parameter
*
*/
function param_alpha($name) {
if (func_num_args() == 2) {
$php_work_around = func_get_arg(1);
$value = param_variable($name,$php_work_around);
}
else {
$value = param_variable($name);
}
if (preg_match('/^[a-zA-Z]$/',$value)) {
return $value;
}
throw new ParameterException("Parameter '$name' = '$value' is not an alpha");
}
/**
* This function returns a GET or POST parameter as an array of integers with optional
* default. If the default isn't specified and the parameter hasn't been sent,
* a ParameterException exception is thrown. Likewise, if the parameter isn't a
* valid integer list , a ParameterException exception is thrown.
*
* An integer list is integers separated by commas (with optional whitespace),
* or just whitespace which indicates an empty list
*
* @param string The GET or POST parameter you want returned
* @param mixed [optional] the default value for this parameter
*
* @return array The value of the parameter
*
*/
function param_integer_list($name) {
if (func_num_args() == 2) {
$php_work_around = func_get_arg(1);
$value = param_variable($name,$php_work_around);
}
else {
$value = param_variable($name);
}
if ($value == '') {
return array();
}
if (preg_match('/^(\d+(,\d+)*)$/',$value)) {
return array_map('intval', explode(',', $value));
}
throw new ParameterException("Parameter '$name' = '$value' is not an integer list");
}
/**
* This function returns a GET or POST parameter as a boolean.
*
......@@ -577,8 +644,8 @@ function get_integer($name) {
* @return string The value of the parameter
*
*/
function get_boolean($name) {
$value = get_variable($name, false);
function param_boolean($name) {
$value = param_variable($name, false);
if (empty($value) || $value == 'off' || $value == 'no') {
return false;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment