function validateas($value, $validatename, $lineno, $fieldname = '')
{
    // Validates each field based on information in the $validate array
    global $USER;
    global $validate;
    $fieldname == '' and $fieldname = $validatename;
    isset($validate[$validatename]) or csverror('Coding Error: Unvalidated field type: "' . $validatename . '"', 'uploadcourse.php?sesskey=' . $USER->sesskey);
    $format = $validate[$validatename];
    switch ($format[0]) {
        case 1:
            // String
            if (($maxlen = $format[1]) != 0) {
                // Max length?
                strlen($value) <= $format[1] or csverror('Invalid value for field ' . $fieldname . ' (length &gt; ' . $format[1] . '). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
            }
            if ($format[2] == 1) {
                // Not null?
                checkisstring($value) or csverror('Invalid value for field ' . $fieldname . ' (only spaces or missing). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
            }
            break;
        case 2:
            // Integer
            checkisint($value) or csverror('Invalid value for field ' . $fieldname . ' (not an integer). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
            if (($max = $format[1]) != 0) {
                // Max value?
                $value <= $max or csverror('Invalid value for field ' . $fieldname . ' (&gt; ' . $max . '). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
            }
            $min = $format[2];
            // Min value
            $value >= $min or csverror('Invalid value for field ' . $fieldname . ' (&lt; ' . $min . '). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
            break;
        case 3:
            // Timestamp - validates and converts to Unix Time
            if (($value = strtotime($value)) < 1) {
                csverror('Invalid value for field ' . $fieldname . ' (Bad Timestamp). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
            }
            break;
        case 4:
            // Domain
            $validvalues = explode(',', $format[1]);
            if (array_search($value, $validvalues) === false) {
                csverror('Invalid value for field ' . $fieldname . ' (Must be one of {' . $format[1] . '}). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
            }
            break;
        case 5:
            // Category
            if (checkisint($value)) {
                // It's a Category ID Number
                categoryexists_ex($value) or csverror('Invalid value for field ' . $fieldname . ' (No Category with ID ' . $value . '). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
            } elseif (checkisstring($value)) {
                // It's a Category Path string
                $value = trim(str_replace('\\', '/', $value), " \t\n\r\v/");
                // Clean path, ensuring all slashes are forward ones
                strlen($value) > 0 or csverror('Invalid value for field ' . $fieldname . ' (Path string not set). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
                unset($cats);
                $cats = explode('/', $value);
                // Break up path into array
                count($cats) > 0 or csverror('Invalid value for field ' . $fieldname . ' (Path string "' . $value . '" invalid - not delimited correctly). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
                foreach ($cats as $n => $item) {
                    // Validate the path
                    $item = trim($item);
                    // Remove whitespace
                    strlen($item) <= 30 or csverror('Invalid value for field ' . $fieldname . ' (Category name "' . $item . '" length &gt; 30). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
                    checkisstring($item) or csverror('Invalid value for field ' . $fieldname . ' (Path string "' . $value . '" invalid - category name at position ' . ($n + 1) . ' as shown is invalid). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
                }
                $value = $cats;
                // Return the array
                unset($cats);
            } else {
                csverror('Invalid value for field ' . $fieldname . ' (not an integer or string). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
            }
            break;
        case 6:
            // User ID or Name (Search String)
            $value = trim($value);
            if (checkisint($value)) {
                // User ID
                userexists_ex($value) or csverror('Invalid value for field ' . $fieldname . ' (No User with ID ' . $value . '). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
            } elseif (checkisstring($value)) {
                // User Search String
                // Only PHP5 supports named arguments
                $usersearch = get_users_listing('lastaccess', 'ASC', 0, 99999, mysql_real_escape_string($value), '', '');
                if (isset($usersearch) and $usersearch !== false and is_array($usersearch) and ($ucount = count($usersearch)) > 0) {
                    $ucount == 1 or csverror('Invalid value for field ' . $fieldname . ' (Search string ambiguous; returned multiple [' . $ucount . '] results). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
                    reset($usersearch);
                    $uid = key($usersearch);
                    checkisint($uid) && userexists_ex($uid) or csverror('Invalid value for field ' . $fieldname . ' (Search string returned a nonexistent user ?!). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
                    $value = $uid;
                    // Return found user id
                } else {
                    csverror('Invalid value for field ' . $fieldname . ' (Search string returned no results). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
                }
            } else {
                if ($format[2] == 1) {
                    // Not null?
                    csverror('Invalid value for field ' . $fieldname . ' (only spaces or missing). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
                }
            }
            break;
        default:
            csverror('Coding Error: Bad field validation type: "' . $fieldname . '"', 'uploadcourse.php?sesskey=' . $USER->sesskey);
            break;
    }
    return $value;
}
function validateas($value, $validatename, $lineno, $fieldname = '')
{
    // Validates each field based on information in the $validate array
    global $USER;
    global $validate;
    $fieldname == '' and $fieldname = $validatename;
    isset($validate[$validatename]) or csverror('Coding Error: Unvalidated field type: "' . $validatename . '"', 'uploadcourse.php?sesskey=' . $USER->sesskey);
    $format = $validate[$validatename];
    switch ($format[0]) {
        case 1:
            // String
            if (($maxlen = $format[1]) != 0) {
                // Max length?
                strlen($value) <= $format[1] or csverror('Invalid value for field ' . $fieldname . ' (length &gt; ' . $format[1] . '). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
            }
            if ($format[2] == 1) {
                // Not null?
                checkisstring($value) or csverror('Invalid value for field ' . $fieldname . ' (only spaces or missing). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
            }
            break;
        case 2:
            // Integer
            checkisint($value) or csverror('Invalid value for field ' . $fieldname . ' (not an integer). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
            if (($max = $format[1]) != 0) {
                // Max value?
                $value <= $max or csverror('Invalid value for field ' . $fieldname . ' (&gt; ' . $max . '). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
            }
            $min = $format[2];
            // Min value
            $value >= $min or csverror('Invalid value for field ' . $fieldname . ' (&lt; ' . $min . '). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
            break;
        case 3:
            // Timestamp - validates and converts to Unix Time
            if (($value = strtotime($value)) < 1) {
                csverror('Invalid value for field ' . $fieldname . ' (Bad Timestamp). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
            }
            break;
        case 4:
            // Domain
            $validvalues = explode(',', $format[1]);
            if (array_search($value, $validvalues) === false) {
                csverror('Invalid value for field ' . $fieldname . ' (Must be one of {' . $format[1] . '}). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
            }
            break;
        case 5:
            // Category
            if (checkisint($value)) {
                // It's a Category ID Number
                categoryexists_ex($value) or csverror('Invalid value for field ' . $fieldname . ' (No Category with ID ' . $value . '). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
            } elseif (checkisstring($value)) {
                // It's a Category Path string
                $value = trim(str_replace('\\', '/', $value), " \t\n\r\v/");
                // Clean path, ensuring all slashes are forward ones
                strlen($value) > 0 or csverror('Invalid value for field ' . $fieldname . ' (Path string not set). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
                unset($cats);
                $cats = explode('/', $value);
                // Break up path into array
                count($cats) > 0 or csverror('Invalid value for field ' . $fieldname . ' (Path string "' . $value . '" invalid - not delimited correctly). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
                foreach ($cats as $n => $item) {
                    // Validate the path
                    $item = trim($item);
                    // Remove whitespace
                    strlen($item) <= 30 or csverror('Invalid value for field ' . $fieldname . ' (Category name "' . $item . '" length &gt; 30). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
                    checkisstring($item) or csverror('Invalid value for field ' . $fieldname . ' (Path string "' . $value . '" invalid - category name at position ' . ($n + 1) . ' as shown is invalid). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
                }
                $value = $cats;
                // Return the array
                unset($cats);
            } else {
                csverror('Invalid value for field ' . $fieldname . ' (not an integer or string). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
            }
            break;
        case 6:
            // User ID or Name (Search String)
            if (!empty($value)) {
                $value = trim(clean_param($value, PARAM_TEXT));
                var_dump($value);
                if (count($user = get_record('user', 'idnumber', $value)) == 1) {
                    $value = $user->id;
                } else {
                    csverror('Invalid value for field ' . $fieldname . ' (No User with IDnumber ' . $value . '). ' . get_string('erroronline', 'error', $lineno) . ". " . get_string('processingstops', 'error'), 'uploadcourse.php?sesskey=' . $USER->sesskey);
                }
            }
            break;
        default:
            csverror('Coding Error: Bad field validation type: "' . $fieldname . '"', 'uploadcourse.php?sesskey=' . $USER->sesskey);
            break;
    }
    return $value;
}