From bb40bf4da176860069389ad4fa391ddcec46e661 Mon Sep 17 00:00:00 2001 From: jacob Date: Mon, 25 Oct 2010 18:07:55 +0000 Subject: [PATCH] Updated the user_load and user_save functions to handle judges special awards selection. Updated judge_special_awards.php to use that method rather than direct SQL queries. --- judge_special_awards.php | 17 +++--- user.inc.php | 111 +++++++++++++++++++++++++++++++++++---- 2 files changed, 109 insertions(+), 19 deletions(-) diff --git a/judge_special_awards.php b/judge_special_awards.php index f87d77d..15303f8 100644 --- a/judge_special_awards.php +++ b/judge_special_awards.php @@ -44,15 +44,16 @@ $u = user_load($eid); switch($_GET['action']) { case 'save': - //first delete all their old associations for this year.. - mysql_query("DELETE FROM judges_specialaward_sel WHERE users_id='{$u['id']}'"); - if(array_key_exists('spaward', $_POST)) { - foreach($_POST['spaward'] AS $aid) { - mysql_query("INSERT INTO judges_specialaward_sel (users_id, award_awards_id) - VALUES ('{$u['id']}','$aid')"); + foreach($u['special_awards'] as $id => $val){ + if(in_array($id, $_POST['spaward'])){ + $u['special_awards'][$id] = 'yes'; + }else{ + $u['special_awards'][$id] = 'no'; + } } } + user_save($u); happy_("Special Award preferences successfully saved"); exit; } @@ -124,8 +125,8 @@ if($_SESSION['embed'] != true) { award_types.id=award_awards.award_types_id AND sponsors.id=award_awards.sponsors_id AND (award_types.type='Special' OR award_types.type='Other') - AND award_awards.year='{$config['FAIRYEAR']}' - AND award_types.year='{$config['FAIRYEAR']}' + AND award_awards.conferences_id='{$conference['id']}' + AND award_types.conferences_id='{$conference['id']}' ORDER BY name"); echo mysql_error(); diff --git a/user.inc.php b/user.inc.php index f526caf..8be022d 100644 --- a/user.inc.php +++ b/user.inc.php @@ -76,10 +76,19 @@ function user_load($users_id, $accounts_id = false) // get a list of all fields relevant to this user $fieldDat = user_get_fields(array_keys($u['roles'])); - $fields = array_unique(array_merge(array_keys($fieldDat), array('id', 'accounts_id', 'conferences_id'))); - // now let's grab the data from the users table - $query = "SELECT users." . implode(", users.", $fields) . ", accounts.email"; + // we need to separate the fields that are in the users table from those in separate tables + $fields = array_unique(array_merge(array_keys($fieldDat), array('id', 'accounts_id', 'conferences_id'))); + $userFields = array(); + $q = mysql_query("DESCRIBE users"); + while($row = mysql_fetch_assoc($q)){ + $userFields[] = $row['Field']; + } + $userFields = array_intersect($fields, $userFields); + $specialFields = array_diff($fields, $userFields); + + // we can start by populating the array with data out of the users table + $query = "SELECT users." . implode(", users.", $userFields) . ", accounts.email"; $query .= " FROM users JOIN accounts ON accounts.id=users.accounts_id"; $query .= " WHERE `users`.`id`='$users_id'"; $q = mysql_query($query); @@ -92,10 +101,10 @@ function user_load($users_id, $accounts_id = false) $u['accounts_id'] = intval($u['accounts_id']); $u['year'] = intval($u['year']); - /* Convenience */ + // Convenience $u['name'] = ($u['firstname'] ? "{$u['firstname']} " : '').$u['lastname']; - /* Email recipient for "to" field on emails */ + // Email recipient for "to" field on emails if( ($u['firstname'] || $u['lastname']) && $u['email']) { //use their full name if we have it //if the name contains anything non-standard, we need to quote it. @@ -170,16 +179,38 @@ function user_load($users_id, $accounts_id = false) break; } } + foreach($should_be_arrays as $k) { if(!is_array($u[$k])) $u[$k] = array(); } + // now let's populate the fields that are not stored in the users table + foreach($specialFields as $field){ + switch($field){ + case 'special_awards': + $q = mysql_query("SELECT award_awards_id aaid FROM judges_specialaward_sel WHERE users_id = {$u['id']}"); + while($r = mysql_fetch_assoc($q)){ + $selected[] = $r['aaid']; + } + $u['special_awards'] = get_special_awards($u['conferences_id']); + foreach($u['special_awards'] as $id => $text){ + if(in_array($id, $selected)){ + $u['special_awards'][$id] = 'yes'; + }else{ + $u['special_awards'][$id] = 'no'; + } + } + break; + + } + + } + /* Do this assignment without recursion :) */ unset($u['orig']); $orig = $u; $u['orig'] = $orig; $u['required_fields']=user_all_fields_required(array_keys($u['roles'])); - return $u; } @@ -268,11 +299,12 @@ function user_get_field_labels(){ 'primary' => 'Primary Contact', 'position' => 'Position', 'notes' => 'Notes', - 'grade' => 'Grade' + 'grade' => 'Grade', + 'special_awards' => 'Special Awards' ); } -function role_field_required($role, $fieldname){ +function user_role_field_required($role, $fieldname){ $returnval = 0; $requiredFields = array( 'judge' => array('years_school','years_regional','years_national','languages'), @@ -329,7 +361,7 @@ function user_get_fields($userRoles = null){ if(!array_key_exists($field, $fields)){ $fields[$field] = array( 'field' => $field, - 'required' => role_field_required($role, $field) + 'required' => user_role_field_required($role, $field) ); } } @@ -440,9 +472,47 @@ function user_get_fields($userRoles = null){ } } + /******* Now we add fields that are not stored directly in the users table ********/ + $specialFieldRoles = array( + 'special_awards' => array('judge', 'student') + ); + + if(count(array_intersect($specialFieldRoles['special_awards'], $userRoles)) > 0){ + //if(in_array('judge', $userRoles)){ + // find out if they have any special awards flagged + $fields['special_awards'] = array(); + $fields['special_awards']['field'] = 'special_awards'; + $fields['special_awards']['display'] = $fieldLabels['special_awards']; + $fields['special_awards']['type'] = 'multiselectlist'; + $fields['special_awards']['options'] = array('yes' => 'Yes', 'no' => 'No'); + $fields['special_awards']['entries'] = get_special_awards($conference['id']); + } return $fields; } +// a convenience function for getting the special awards that are relevant to the specified conference. +// separated because it's used in a couple of spots +function get_special_awards($conferenceId){ + $returnval = array(); + $q=mysql_query("SELECT award_awards.id, + award_awards.name, + award_awards.criteria, + sponsors.organization + FROM award_awards + JOIN award_types ON award_types.id = award_awards.award_types_id + JOIN sponsors ON sponsors.id = award_awards.sponsors_id + WHERE + (award_types.type='Special' OR award_types.type='Other') + AND award_awards.conferences_id='$conferenceId' + AND award_types.conferences_id='$conferenceId' + ORDER BY name"); + + while($row = mysql_fetch_assoc($q)){ + $returnval[$row['id']] = $row['name']; + } + return $returnval; +} + /* FIXME: these are going to need conference IDs too */ function user_load_by_accounts_id_year($uid, $year) { @@ -532,6 +602,27 @@ function user_save(&$u) echo mysql_error(); } + // Save the other user data that is not stored in the users table + if( + array_key_exists('special_awards', $u) && + count(array_diff_assoc($u['special_awards'], $u['orig']['special_awards'])) > 0 + ){ + // this user has an altered special awards selection that needs to be saved + $aaids = array(); + foreach($u['special_awards'] as $id => $yesno){ + if(strtolower(trim($yesno)) == 'yes'){ + $aaids[] = $id; + } + } + mysql_query("DELETE FROM judges_specialaward_sel WHERE users_id = {$u['id']}"); + if(is_array($aaids)){ + $query = "INSERT INTO judges_specialaward_sel (users_id, award_awards_id) VALUES (" . $u['id'] . ", "; + $query .= implode('), (' . $u['id'] . ', ', $aaids); + $query .= ")"; + mysql_query($query); + } + } + /* Record all the data in orig that we saved so subsequent * calls to user_save don't try to overwrite data already * saved to the database */ @@ -539,10 +630,8 @@ function user_save(&$u) $orig = $u; $u['orig'] = $orig; -// print_r($u); } - // mark the role as complete if it's qualifications are met function user_complete_role($users_id, $role){ // avoid SQL injections