Starting on code for getting relevant field descriptions for user data depending on

the roles for that user.
This commit is contained in:
jacob 2010-10-21 21:56:09 +00:00
parent 3867b9b539
commit bae700dead

View File

@ -38,6 +38,7 @@ function user_valid_role($role)
function user_load($users_id, $accounts_id = false)
{
user_get_fields(array('judge'));
/* Load user, join accounts so we also load the email, superuser flag */
//hand-code the list here because we dont want all the old stuff that hasnt been removed yet like username/password access_*, etc.
if($accounts_id != false) {
@ -251,6 +252,198 @@ function user_load_by_email($email)
return false;
}
function user_get_role_fields($role){
switch($role){
case 'committee':
$fields = array('emailprivate','ord','displayemail');
break;
case 'judge':
$fields = array('years_school','years_regional','years_national',
'willing_chair','special_award_only',
'cat_prefs','div_prefs','divsub_prefs',
'expertise_other','languages', 'highest_psd');
break;
case 'student':
$fields = array('grade', 'schools_id');
break;
case 'fair':
$fields = array('fairs_id');
break;
case 'sponsor':
$fields = array('sponsors_id','primary','position','notes');
break;
case 'teacher':
$fields = array();
break;
case 'volunteer':
$fields = array('languages');
break;
default:
$fields = array();
}
return $fields;
}
function user_get_field_labels(){
return array(
'salutation' => 'Salutation',
'firstname' => 'First Name',
'lastname' => 'Last Name',
'sex' => 'Sex',
'phonehome' => 'Home Phone',
'phonework' => 'Work Phone',
'phonecell' => 'Cell Phone',
'fax' => 'Fax Number',
'organization' => 'Organization',
'birthdate' => 'Date of Birth',
'lang' => 'Language',
'address' => 'Address',
'address2' => '',
'city' => 'City',
'province' => 'Province',
'postalcode' => 'Postal Code',
'firstaid' => 'First Aid',
'cpr' => 'CPR',
'displayemail' => 'Display Email',
'years_school' => 'Years experience judging at school level',
'years_regional' => 'Years experience judging at regional level',
'years_national' => 'Years experience judging at national level',
'willing_chair' => 'Willing to lead a judging team',
'special_award_only' => 'Judging only for a special award',
'languages' => 'Languages',
);
}
function role_field_required($role, $fieldname){
$returnval = 0;
$requiredFields = array(
'judge' => array('years_school','years_regional','years_national','languages'),
'student' => array('schools_id'),
'fair' => array('fairs_id'),
'sponsor' => array('sponsors_id','primary','position'),
'volunteer' => array('languages')
);
if(array_key_exists($role, $requiredFields)){
if(in_array($fieldname, $requiredFields[$role])){
$returnval = 1;
}
}
return $returnval;
}
// accepts either an array of roles (eg. {'judge', 'teacher', 'foo'}), a single one as a string (eg. 'judge'), or a null value (no roles at all)
function user_get_fields($userRoles = null){
global $roles;
if($userRoles == null){
$userRoles = array();
}else if(!is_array($userRoles)){
// assume that they passed a string identifying a single role (eg. "judge")
$userRoles = array($userRoles);
}
// scrub for only valid roles, and fetch their special fields
$enabledFields = array();
$requiredFields = array();
$roleFields = array();
foreach($userRoles as $role){
if(array_key_exists($role, $roles)){
$requiredFields = array_merge($requiredFields, user_fields_required($role));
$enabledFields = array_merge($enabledFields, user_fields_enabled($role));
$roleFields[$role] = user_get_role_fields($role);
}
}
// build a list of all fields that are applicable to this user, and assemble them into an array
$fields = array();
foreach($requiredFields as $field){
if(!array_key_exists($field, $fields)){
$fields[$field] = array(
'field' => $field,
'required' => 1
);
}
}
foreach($roleFields as $role => $fieldList){
foreach($fieldList as $field){
if(!array_key_exists($field, $fields)){
$fields[$field] = array(
'field' => $field,
'required' => role_field_required($role, $field)
);
}
}
}
foreach($enabledFields as $field){
if(!array_key_exists($field, $fields)){
$fields[$field] = array(
'field' => $field,
'required' => 0
);
}
}
// get the field types
$query = mysql_query("DESCRIBE users");
$fieldType = array();
while($row = mysql_fetch_assoc($query)){
$fieldType[$row['Field']] = $row['Type'];
}
$fieldLabels = user_get_field_labels();
foreach($fields as $fieldName => $field){
$ftype = $fieldType[$fieldName];
if(array_key_exists($fieldName, $fieldLabels)){
$fields[$fieldName]['display'] = $fieldLabels[$fieldName];
}
switch($fieldName){
case 'languages':
case 'cat_prefs':
case 'div_prefs':
case 'divsub_prefs':
// cases not yet handled
break;
default:
if(!strncasecmp($ftype, "varchar", 7)){
$parts = explode(')', $ftype);
$parts = explode('(', $parts[0]);
$fields[$fieldName]['type'] = $parts[0] . ':' . $parts[1];
}else if(!strncasecmp($ftype, "enum", 4)){
$fields[$fieldName]['type'] = 'enum';
$fields[$fieldName]['options'] = array();
$parts = explode("'", $ftype);
for($n = 1; $n < count($parts); $n += 2){
$fields[$fieldName]['options'][$parts[$n]] = ucfirst($parts[$n]);
}
}else if(!strcmp($ftype, "date")){
$fields[$fieldName]['type'] = $fieldType[$fieldName];
}else if(!strncmp($ftype, "tinyint", 7)){
$fields[$fieldName]['type'] = 'integer';
}else if(!strncmp($ftype, "tinytext", 8)){
$fields[$fieldName]['type'] = 'text';
}else{
$fields[$fieldName]['type'] = "ERROR:" . $fieldType[$fieldName];
}
}
}
/*
echo "<pre>";
print_r(($userRoles));
print_r($fields);
echo "</pre>";
*/
return $fields;
}
/* FIXME: these are going to need conference IDs too */
function user_load_by_accounts_id_year($uid, $year)
{