- use the newer (faster) user load, and always load a full user.

- delete now deprecated code because of the new user loader
This commit is contained in:
dave 2008-10-19 21:42:57 +00:00
parent f9d3913697
commit a0d27df91e
12 changed files with 104 additions and 187 deletions

View File

@ -34,10 +34,6 @@ $preferencechoices=array(
function judge_status_expertise(&$u)
{
global $config;
if($u['load_full'] == false) {
echo "ERROR: judge_status_expertise() called without a full user.";
exit;
}
/* If the judging special awards are active, and the judge has
* selected "I am a special awards judge", then disable
@ -64,10 +60,6 @@ function judge_status_expertise(&$u)
function judge_status_other(&$u)
{
global $config;
if($u['load_full'] == false) {
echo "ERROR: judge_status_other() called without a full user.";
exit;
}
/* They must select a language to judge in */
if(count($u['languages']) < 1) return 'incomplete';
@ -80,10 +72,6 @@ function judge_status_other(&$u)
function judge_status_special_awards(&$u)
{
global $config;
if($u['load_full'] == false) {
echo "ERROR: judge_status_special_awards() called without a full user.";
exit;
}
/* Complete if:
* - judge has selected (none) "no special award preferences"
@ -115,10 +103,7 @@ function judge_status_special_awards(&$u)
function judge_status_update(&$u)
{
if($u['load_full'] == false) {
echo "ERROR: judge_status_update() called without a full user.";
exit;
}
global $config;
if( user_personal_info_status($u) == 'complete'
&& judge_status_expertise($u) == 'complete'

View File

@ -26,7 +26,7 @@
require_once('user.inc.php');
require_once('judge.inc.php');
$u = user_load($_SESSION['users_id'], true);
$u = user_load($_SESSION['users_id']);
//send the header
// $type = $_SESSION['users_type'];
@ -64,7 +64,7 @@
}
user_save($u);
message_push(notice(i18n("Preferences successfully saved")));
$u = user_load($_SESSION['users_id'], true);
$u = user_load($u['id']);
}
judge_status_update($u);

View File

@ -29,7 +29,7 @@
user_auth_required('judge');
$u = user_load($_SESSION['users_id'], true);
$u = user_load($_SESSION['users_id']);
if($u['judge_active'] == 'no') {
message_push(notice(i18n("Your judge role is not active. If you would like to participate as a judge for the %1 %2 please click the '<b>Activate Role</b>' button in the Judge section below",array($config['FAIRYEAR'],$config['fairname']))));

View File

@ -27,7 +27,7 @@
require_once('judge.inc.php');
require_once("questions.inc.php");
$u = user_load($_SESSION['users_id'], true);
$u = user_load($_SESSION['users_id']);
//send the header
send_header('Other Information',
@ -52,7 +52,7 @@
user_save($u);
message_push(notice(i18n("Preferences successfully saved")));
$u = user_load($_SESSION['users_id'], true);
$u = user_load($u['id']);
}
judge_status_update($u);

View File

@ -26,7 +26,7 @@
require_once('user.inc.php');
require_once('judge.inc.php');
$u = user_load($_SESSION['users_id'], true);
$u = user_load($_SESSION['users_id']);
//send the header
send_header('Special Awards',

View File

@ -46,123 +46,76 @@ function user_generate_password($pwlen=8)
function user_load_fair($u)
/* Separate user_load_type functions, these could make additional database
* calls if required */
function user_load_fair(&$u)
{
/* Double check, make sure the user is of this type */
if(!in_array('fair', $u['types'])) return false;
$q = mysql_query("SELECT * FROM users_fair
WHERE users_id='{$u['id']}'
");
if(mysql_num_rows($q)!=1) return false;
$r = mysql_fetch_object($q);
$ret = array();
$ret['fair_active'] = $r->fair_active;
$ret['fairs_id'] = intval($r->fairs_id);
return $ret;
$u['fair_active'] = ($u['fair_active'] == 'yes') ? 'yes' : 'no';
$u['fair_complete'] = ($u['fair_complete'] == 'yes') ? 'yes' : 'no';
$u['fairs_id'] = intval($u['fairs_id']);
return true;
}
function user_load_student($u)
function user_load_student(&$u)
{
/* Double check, make sure the user is of this type */
if(!in_array('student', $u['types'])) return false;
$ret = array();
return $ret;
// $u['student_active'] = ($u['student_active'] == 'yes') ? 'yes' : 'no';
// $u['student_complete'] = ($u['student_complete'] == 'yes') ? 'yes' : 'no';
return false;
}
function user_load_judge($u)
function user_load_judge(&$u)
{
/* Double check, make sure the user is of this type */
if(!in_array('judge', $u['types'])) {
echo 'ERROR: User is not a judge in user_load_judge';
return false;
}
$q = mysql_query("SELECT * FROM users_judge
WHERE users_id='{$u['id']}'");
if(mysql_num_rows($q)!=1) {
echo "DATABASE ERROR: User judge record not found";
print_r($u);
return false;
}
$r = mysql_fetch_object($q);
$ret = array();
$ret['judge_active'] = $r->judge_active;
$ret['judge_complete'] = $r->judge_complete;
$ret['years_school'] = intval($r->years_school);
$ret['years_regional'] = intval($r->years_regional);
$ret['years_national'] = intval($r->years_national);
$ret['willing_chair'] = ($r->willing_chair == 'yes') ? 'yes' : 'no';
$ret['special_award_only'] = ($r->special_award_only == 'yes') ? 'yes' : 'no';
$ret['cat_prefs'] = unserialize($r->cat_prefs);
$ret['div_prefs'] = unserialize($r->div_prefs);
$ret['divsub_prefs'] = unserialize($r->divsub_prefs);
$ret['expertise_other'] = $r->expertise_other;
$ret['languages'] = unserialize($r->languages);
$ret['highest_psd'] = $r->highest_psd;
$u['judge_active'] = ($u['judge_active'] == 'yes') ? 'yes' : 'no';
$u['judge_complete'] = ($u['judge_complete'] == 'yes') ? 'yes' : 'no';
$u['years_school'] = intval($u['years_school']);
$u['years_regional'] = intval($u['years_regional']);
$u['years_national'] = intval($u['years_national']);
$u['willing_chair'] = ($u['willing_chair'] == 'yes') ? 'yes' : 'no';
$u['special_award_only'] = ($u['special_award_only'] == 'yes') ? 'yes' : 'no';
$u['cat_prefs'] = unserialize($u['cat_prefs']);
$u['div_prefs'] = unserialize($u['div_prefs']);
$u['divsub_prefs'] = unserialize($u['divsub_prefs']);
// $u['expertise_other'] = $u['expertise_other'];
$u['languages'] = unserialize($u['languages']);
// $u['highest_psd'] = $u['highest_psd'];
/* Sanity check the arrays, make sure they are arrays */
$should_be_arrays = array('cat_prefs','div_prefs',
'divsub_prefs','languages');
foreach($should_be_arrays as $k) {
if(!is_array($ret[$k])) $ret[$k] = array();
if(!is_array($u[$k])) $u[$k] = array();
}
return $ret;
return true;
}
function user_load_committee($u)
function user_load_committee(&$u)
{
/* Double check, make sure the user is of this type */
if(!in_array('committee', $u['types'])) return false;
$q = mysql_query("SELECT * FROM users_committee
WHERE users_id='{$u['id']}'");
if(mysql_num_rows($q)!=1) return false;
$r = mysql_fetch_object($q);
$ret = array();
$ret['committee_active'] = $r->committee_active;
$ret['emailprivate'] = $r->emailprivate;
$ret['ord'] = intval($r->ord);
$ret['displayemail'] = ($r->displayemail == 'yes') ? 'yes' : 'no';
$ret['access_admin'] = ($r->access_admin == 'yes') ? 'yes' : 'no';
$ret['access_config'] = ($r->access_config == 'yes') ? 'yes' : 'no';
$ret['access_super'] = ($r->access_super == 'yes') ? 'yes' : 'no';
return $ret;
$u['committee_active'] = $u['committee_active'];
$u['emailprivate'] = $u['emailprivate'];
$u['ord'] = intval($u['ord']);
$u['displayemail'] = ($u['displayemail'] == 'yes') ? 'yes' : 'no';
$u['access_admin'] = ($u['access_admin'] == 'yes') ? 'yes' : 'no';
$u['access_config'] = ($u['access_config'] == 'yes') ? 'yes' : 'no';
$u['access_super'] = ($u['access_super'] == 'yes') ? 'yes' : 'no';
return true;
}
function user_load_volunteer($u)
function user_load_volunteer(&$u)
{
/* Double check, make sure the user is of this type */
if(!in_array('volunteer', $u['types'])) return false;
$q = mysql_query("SELECT * FROM users_volunteer
WHERE users_id='{$u['id']}'");
if(mysql_num_rows($q)!=1) {
echo "DATABASE ERROR, loading volunteer for user {$u['id']} returned ".mysql_num_rows($q)." rows.";
return false;
}
$r = mysql_fetch_object($q);
$ret = array();
$ret['volunteer_active'] = $r->volunteer_active;
$ret['volunteer_complete'] = $r->volunteer_complete;
return $ret;
$u['volunteer_active'] = ($u['>volunteer_active'] == 'yes') ? 'yes' : 'no';
$u['volunteer_complete'] = ($u['>volunteer_complete'] == 'yes') ? 'yes' : 'no';
return true;
}
function user_load($user, $load_full=false)
function user_load($user)
{
$id = 0;
/* Sort out the type first */
if(is_array($user)){
/* User already loaded, this is just an extended load */
/* User already loaded*/
$id = $user['id'];
$where = "id='$id'";
$load_base = false;
} else {
$id = intval($user);
@ -179,59 +132,53 @@ function user_load($user, $load_full=false)
// $e = stripslashes($user);
// $where = "email='$e'";
}
$load_base = true;
}
if($load_base) {
$query = "SELECT * FROM users
WHERE
$where
AND deleted='no'
";
$q=mysql_query($query);
/* So, it turns out that doing one big load is faster than loading just
* from the users table then loading only the specific types the user
* has.. go figure. */
$query = "SELECT * FROM `users`
LEFT JOIN `users_committee` ON `users_committee`.`users_id`=`users`.`id`
LEFT JOIN `users_judge` ON `users_judge`.`users_id`=`users`.`id`
LEFT JOIN `users_volunteer` ON `users_volunteer`.`users_id`=`users`.`id`
LEFT JOIN `users_fair` ON `users_fair`.`users_id`=`users`.`id`
WHERE $where
AND `users`.`deleted`='no' ";
$q=mysql_query($query);
if(mysql_num_rows($q)!=1) {
echo "Query [$query] returned ".mysql_num_rows($q)." rows\n";
if(mysql_num_rows($q)!=1) {
echo "Query [$query] returned ".mysql_num_rows($q)." rows\n";
return false;
}
$ret = mysql_fetch_assoc($q);
/* Do we need to do number conversions? */
$ret['id'] = intval($ret['id']);
/* Turn the type into an array, because there could be more than one */
$ts = explode(',', $ret['types']);
$ret['types'] = $ts; /* Now we can use in_array('judge', $ret['types']) ; */
foreach($ret['types'] as $t) {
/* These all pass $ret by reference, and can modify
* $ret */
$r = call_user_func("user_load_$t", $ret);
if($r != true) {
echo "user_load_$t returned false!\n";
return false;
}
$ret = mysql_fetch_assoc($q);
/* Do we need to do number conversions? */
$ret['id'] = intval($ret['id']);
/* Turn the type into an array, because there could be more than one */
$ts = explode(',', $ret['types']);
$ret['types'] = $ts; /* Now we can use in_array('judge', $ret['types']) ; */
} else {
$ret = $user;
}
if($load_full) {
$r = true;
foreach($ret['types'] as $t) {
if($ret['load_full'] == true) continue;
/* These all pass $ret by reference, and can modify
* $ret */
$r = call_user_func("user_load_$t", $ret);
if(!is_array($r)) {
echo "user_load_$t didn't return an array!\n";
return false;
/* It is important that each type database doesn't
have conflicting column names */
/* foreach($r as $k=>$v) {
if(array_key_exists($k, $ret)) {
echo "DATABASE DESIGN ERROR, duplicate user key $k";
exit;
}
/* It is important that each type database doesn't
have conflicting column names */
foreach($r as $k=>$v) {
if(array_key_exists($k, $ret)) {
echo "DATABASE DESIGN ERROR, duplicate user key $k";
exit;
}
}
$ret = array_merge($ret, $r);
}
$ret['load_full'] = true;
} else {
$ret['load_full'] = false;
$ret = array_merge($ret, $r);
*/
}
/* Do this assignment without recursion :) */
@ -377,11 +324,8 @@ function user_save($u)
if($u['password'] != $u['orig']['password'])
user_set_password($u['id'], $u['password']);
/* If this was a full load, do a full save */
if($u['load_full'] == true) {
foreach($u['types'] as $t) {
call_user_func("user_save_$t", $u);
}
foreach($u['types'] as $t) {
call_user_func("user_save_$t", $u);
}
}
@ -553,7 +497,7 @@ function user_dupe($u, $new_year)
$q = mysql_query("UPDATE users SET year='$new_year' WHERE id='$id'");
/* Load the new user */
$u2 = user_load($id, false);
$u2 = user_load($id);
foreach($u2['types'] as $t) {
user_dupe_row("users_$t", 'users_id', $u['id'], $id);
@ -620,7 +564,7 @@ function user_create($type, $u = NULL)
mysql_query("INSERT INTO users_committee(`users_id`, `committee_active`) VALUES ('$uid', 'yes')");
break;
}
return user_load($uid, true);
return user_load($uid);
}
@ -761,11 +705,8 @@ function user_personal_required_fields($type)
return $ret;
}
function user_personal_info_status($u = false)
function user_personal_info_status(&$u)
{
if($u == false) {
$u = user_load($_SESSION['users_id']);
}
$required = array();
foreach($u['types'] as $t) {
$required = array_merge($required,
@ -780,9 +721,12 @@ function user_personal_info_status($u = false)
return 'incomplete';
}
}
/* FIXME: somehow call the $type _status_update() function to update
* the individual $type _complete entry? */
return 'complete';
}
/* user_{$type}_login() is called with a full $u loaded */
function user_committee_login($u)
{
/* Double check, make sure the user is of this type */
@ -791,8 +735,6 @@ function user_committee_login($u)
exit;
}
$u = user_load($u, true);
$_SESSION['access_admin'] = $u['access_admin'];// == 'yes') ? true : false;
$_SESSION['access_config'] = $u['access_config'];// == 'yes') ? true : false;
$_SESSION['access_super'] = $u['access_super'];// == 'yes') ? true : false;
@ -806,7 +748,6 @@ function user_fair_login($u)
exit;
}
$u = user_load($u, true);
$_SESSION['fairs_id'] = $u['fairs_id'];// == 'yes') ? true : false;
}

View File

@ -43,11 +43,12 @@
}
$u = user_load($_SESSION['users_id']);
switch($_POST['action']) {
case 'delete':
//okay here we go, lets get rid of them completely, since this is what theyve asked for
message_push(happy(i18n("Account successfully deleted. Goodbye")));
$u = user_load($_SESSION['users_id'], true);
user_delete($u);
header('location: user_login.php?action=logout');
exit;
@ -55,19 +56,16 @@
case 'remove':
/* Like delete, only we're only deleting a role, not the whole account */
message_push(happy(i18n("$action_what role successfully removed.")));
$u = user_load($_SESSION['users_id'], true);
user_delete($u, $action_type);
break;
case 'activate':
$u = user_load($_SESSION['users_id'], true);
$u["{$action_type}_active"] = 'yes';
user_save($u);
message_push(happy(i18n("$action_what role for %1 successfully activated",array($config['FAIRYEAR']))));
break;
case 'deactivate':
$u = user_load($_SESSION['users_id'], true);
$u["{$action_type}_active"] = 'no';
user_save($u);
message_push(happy(i18n("$action_what role for %1 successfully deactivated",array($config['FAIRYEAR']))));
@ -77,9 +75,7 @@
break;
}
$u = user_load($_SESSION['users_id'], true);
$u = user_load($u['id']);
$type = $_SESSION['users_type'];
$m = $user_what[$type];

View File

@ -51,8 +51,6 @@
if($action == 'add') {
send_header("Select Additional Roles");
$u = user_load($_SESSION['users_id']);
//only display the named greeting if we have their name
echo i18n("Hello <b>%1</b>",array($_SESSION['name']));
echo "<br />";

View File

@ -77,12 +77,12 @@
* - on the committee
* - with admin access */
user_auth_required('committee', 'admin');
$u = user_load($eid, true);
$u = user_load($eid);
} else {
/* Else, force them to edit themselves */
$eid = false;
$u = user_load($_SESSION['users_id'], true);
$u = user_load($_SESSION['users_id']);
}

View File

@ -40,10 +40,7 @@ function volunteer_status_position($u)
function volunteer_status_update(&$u)
{
if($u['load_full'] == false) {
echo "ERROR: volunteer_status_update() called without a full user.";
exit;
}
global $config;
if( user_personal_info_status($u) == 'complete'
&& volunteer_status_position($u) == 'complete' )

View File

@ -30,7 +30,7 @@
user_auth_required('volunteer');
$u = user_load($_SESSION['users_id'], true);
$u = user_load($_SESSION['users_id']);
if($u['volunteer_active'] == 'no') {
message_push(notice(i18n("Your volunteer role is not active. If you would like to participate as a volunteer for the %1 %2 please click the '<b>Activate Role</b>' button in the Volunteer section below",array($config['FAIRYEAR'],$config['fairname']))));

View File

@ -29,7 +29,7 @@
user_auth_required('volunteer');
$u = user_load($_SESSION['users_id'], true);
$u = user_load($_SESSION['users_id']);
if($_POST['action']=="save")