Fix some user/account bugs

Start fixing the schoolstudents page (it now adds users, but it doesnt re-load them or edit htem or delete them)
This commit is contained in:
james 2010-07-27 19:06:36 +00:00
parent 32dd83fbde
commit 2c7e23b276
5 changed files with 53 additions and 40 deletions

View File

@ -100,12 +100,25 @@ function account_load($id)
$id = intval($id); $id = intval($id);
$q = mysql_query("SELECT * FROM accounts WHERE id='$id'"); $q = mysql_query("SELECT * FROM accounts WHERE id='$id'");
if(mysql_num_rows($q) == 0) { if(mysql_num_rows($q) == 0) {
echo "No such account $id"; return false;
exit;
} }
if(mysql_num_rows($q) > 1) { if(mysql_num_rows($q) > 1) {
echo "More than one account returned for $id"; return false;
exit; }
$a = mysql_fetch_assoc($q);
return $a;
}
function account_load_by_username($username)
{
$un = mysql_real_escape_string($username);
$q = mysql_query("SELECT * FROM accounts WHERE username='$un'");
if(mysql_num_rows($q) == 0) {
return false;
}
if(mysql_num_rows($q) > 1) {
return false;
} }
$a = mysql_fetch_assoc($q); $a = mysql_fetch_assoc($q);
@ -118,7 +131,7 @@ function account_create($username)
global $config; global $config;
/* Sanity check username */ /* Sanity check username */
if(!user_valid_user($username)) { if(!account_valid_user($username)) {
return -1; return -1;
} }

View File

@ -1 +1 @@
204 205

2
db/db.update.205.sql Normal file
View File

@ -0,0 +1,2 @@
ALTER TABLE `accounts` CHANGE `id` `id` INT( 11 ) NOT NULL AUTO_INCREMENT;
ALTER TABLE `accounts` ADD `created` DATETIME NOT NULL;

View File

@ -1,6 +1,7 @@
<?php <?php
require_once('common.inc.php'); require_once('common.inc.php');
require_once('user.inc.php'); require_once('user.inc.php');
require_once('account.inc.php');
if($_SESSION['schoolid'] && $_SESSION['schoolaccesscode'] && $conference['type'] == 'scienceolympics'){ if($_SESSION['schoolid'] && $_SESSION['schoolaccesscode'] && $conference['type'] == 'scienceolympics'){
@ -80,48 +81,46 @@ function process_newRecord($firstName, $lastName, $email){
} }
// if they have an e-mail address, make sure it's not already in use // if they have an e-mail address, make sure it's not already in use
if($email != null){ if($email){
$user = user_load_by_email($email); $account = account_load_by_username($email);
}else{ }else{
$user = false; $account = false;
} }
if($user != false){
return "e-mail address is already in use"; if(!$account) {
}else{ if($email) {
// we're creating a new user $username=$email;
if(strlen($email) != 0){ }
if(!isEmailAddress($email)){ else {
// not a valid e-mail address
return "Invalid e-mail address";
}else{
// new e-mail address specified. That'll be the username
$username = $email;
}
}else{
// generate a user name // generate a user name
$nameBase = substr(strtolower($firstName), 0, 1) . strtolower($lastName); $nameBase = substr(strtolower($firstName), 0, 1) . strtolower($lastName);
$suffix = ''; $suffix = '';
do{ do{
$q = mysql_fetch_array(mysql_query('SELECT COUNT(*) AS tally FROM users WHERE username="' . ($nameBase . $suffix) . '";')); $q = mysql_fetch_array(mysql_query('SELECT COUNT(*) AS tally FROM accounts WHERE username="' . ($nameBase . $suffix) . '";'));
if($q['tally'] > 0){ if($q['tally'] > 0){
if($suffix == '') $suffix = 1; if($suffix == '') $suffix = 1;
else $suffix++; else $suffix++;
} }
}while($q['tally'] > 0); }while($q['tally'] > 0);
$username = $nameBase . $suffix; $username = $nameBase . $suffix;
} }
// now that we have the username we want to use, let's create the user // now that we have the username we want to use, let's create the user
$user = user_create('student', $username); $account=account_create($username);
$user['firstname'] = $firstName;
$user['lastname'] = $lastName;
$user['active'] = 'yes';
$user['complete'] = 'yes';
if($username == $email)
$user['email'] = $email;
$user['schools_id'] = $_SESSION['schoolid'];
user_save($user);
} }
//next, we try to load their user record
$user = user_load(0,$account['id']);
if(!$user) {
$user=user_create($account['id']);
}
user_add_role($user,'student');
//we're gonna set teh firstname/lastname too
$user['firstname'] = $firstName;
$user['lastname'] = $lastName;
//and dont forget the school id, because we know what at this point
$user['schools_id'] = $_SESSION['schoolid'];
user_save($user);
$uid = $user['uid']; $uid = $user['uid'];
echo user_row($uid, $username, $firstName, $lastName, $email); echo user_row($uid, $username, $firstName, $lastName, $email);
@ -131,9 +130,7 @@ function process_newRecord($firstName, $lastName, $email){
// generate the table row for thisa given record // generate the table row for thisa given record
function user_row($uid, $username, $firstName, $lastName, $email){ function user_row($uid, $username, $firstName, $lastName, $email){
$rval = "<tr id=\"$uid\">"; $rval = "<tr id=\"$uid\">";
$rval .= "<td onclick=\"populate($uid);\""; $rval .= "<td style=\"cursor: pointer;\" onclick=\"populate($uid);\"";
$rval .= " onmouseover=\"document.body.style.cursor='pointer';\"";
$rval .= " onmouseout=\"document.body.style.cursor='auto';\"";
$rval .= ">$username</td>"; $rval .= ">$username</td>";
$rval .= "<td>$firstName</td>"; $rval .= "<td>$firstName</td>";
@ -263,7 +260,7 @@ function draw_list(){
<th><?=i18n("Username");?></th> <th><?=i18n("Username");?></th>
<th><?=i18n("First Name");?></th> <th><?=i18n("First Name");?></th>
<th><?=i18n("Last Name");?></th> <th><?=i18n("Last Name");?></th>
<th><?=i18n("Email Address");?></th> <th><?=i18n("Email Address / Username");?><br />(Leave blank to auto-generate)</th>
<th></th> <th></th>
</tr> </tr>
<tr> <tr>

View File

@ -42,7 +42,7 @@ function user_load($users_id, $accounts_id = false)
$query = "SELECT * FROM users JOIN accounts ON accounts.id=users.accounts_id WHERE "; $query = "SELECT * FROM users JOIN accounts ON accounts.id=users.accounts_id WHERE ";
if($accounts_id != false) { if($accounts_id != false) {
$accounts_id = intval($accounts_id); $accounts_id = intval($accounts_id);
$query .= "`users`.`accounts_id`='$accounts_id' ORDER BY `users`.`year` DESC LIMIT 1"; $query .= "`users`.`accounts_id`='$accounts_id' LIMIT 1";
} else { } else {
$id = intval($users_id); $id = intval($users_id);
$query .= " `users`.`id`='$id'"; $query .= " `users`.`id`='$id'";
@ -53,7 +53,7 @@ function user_load($users_id, $accounts_id = false)
if(mysql_num_rows($q) > 1) { if(mysql_num_rows($q) > 1) {
echo "ERROR: More than one user.\n"; echo "ERROR: More than one user.\n";
exit; return false;
} }
/* Load the user */ /* Load the user */
@ -443,7 +443,7 @@ function user_dupe($u, $new_year)
* a student from co-existing with any other role . */ * a student from co-existing with any other role . */
function user_add_role_allowed(&$u, $role) function user_add_role_allowed(&$u, $role)
{ {
foreach(array_keys($u['roles']) as $ur) { foreach(array_keys($u['orig']['roles']) as $ur) {
switch($ur) { switch($ur) {
case 'student': case 'student':
/* Student cant' add any other role */ /* Student cant' add any other role */
@ -485,6 +485,7 @@ function user_create($accounts_id, $conferences_id=0)
/* Make sure the user doesn't already exist */ /* Make sure the user doesn't already exist */
$q = mysql_query("SELECT id FROM users WHERE accounts_id='$accounts_id' AND conferences_id='$conferences_id'"); $q = mysql_query("SELECT id FROM users WHERE accounts_id='$accounts_id' AND conferences_id='$conferences_id'");
echo mysql_error();
if(mysql_num_rows($q)) { if(mysql_num_rows($q)) {
echo "ERROR: user_create called for a user that already exists.\n"; echo "ERROR: user_create called for a user that already exists.\n";
exit; exit;