diff --git a/schoolstudents.php b/schoolstudents.php index b95917b..0c0dd12 100644 --- a/schoolstudents.php +++ b/schoolstudents.php @@ -4,32 +4,67 @@ require_once('user.inc.php'); if($_SESSION['schoolid'] && $_SESSION['schoolaccesscode'] && $conference['type'] == 'scienceolympics'){ - if($_GET['action'] == 'new'){ - $results = process_newRecord($_POST['firstName'], $_POST['lastName'], $_POST['email']); - if($results !== true){ - echo ""; - error_($results); - }else{ - echo ""; - } - }else if($_GET['action'] == 'delete'){ - if(!delete_record($_POST['uid'])){ - echo ""; - error_("Unable to delete record"); - }else{ - echo ""; - } - }else{ - $title = i18n("Manage Students"); - send_header($title, array("School Home" => "schoolaccess.php")); - draw_javascript(); - draw_list(); - send_footer(); + switch($_GET['action']){ + case 'new': + $results = process_newRecord($_POST['firstName'], $_POST['lastName'], $_POST['email']); + if($results !== true){ + echo ""; + error_($results); + }else{ + echo ""; + } + break; + + case 'update': + $results = alter_record($_POST['recordId'], $_POST['firstName'], $_POST['lastName'], $_POST['email']); + if($results !== true){ + echo ""; + error_($results); + }else{ + echo ""; + } + break; + + case 'delete': + if(!delete_record($_POST['uid'])){ + echo ""; + error_("Unable to delete record"); + }else{ + echo ""; + } + break; + + default: + $title = i18n("Manage Students"); + send_header($title, array("School Home" => "schoolaccess.php")); + draw_javascript(); + draw_list(); + send_footer(); } }else{ header('Location: schoolaccess.php'); } +// alter an existing user record. returns true on success, error message on failure +function alter_record($uid, $firstName, $lastName, $email){ + global $conference; + $returnval = true; + $firstName = trim($firstName); + $lastName = trim($lastName); + $email = strtolower(trim($email)); + $user = user_load(null, $uid); + if($user){ + $user['firstname'] = $firstName; + $user['lastname'] = $lastName; + $user['email'] = $email; + user_save($user); + echo user_row($uid, $user['username'], $firstName, $lastName, $email); + }else{ + $returnval = "User not found"; + } + return $returnval; +} + // create a new record with the given first name last name and e-mail address // return true on success, error message on failure function process_newRecord($firstName, $lastName, $email){ @@ -40,25 +75,18 @@ function process_newRecord($firstName, $lastName, $email){ $uid = null; // make sure we are actually given a first and last name - if(strlen($firstName) == 0 || strlen($lastName) == 0) return "First and last names are required fields"; + if(strlen($firstName) == 0 || strlen($lastName) == 0){ + return "First and last names are required fields"; + } + + // if they have an e-mail address, make sure it's not already in use if($email != null){ $user = user_load_by_email($email); - }else{ $user = false; } if($user != false){ - // we're adding an existing user. First find out if they are in the school we have specified return "e-mail address is already in use"; -// $user['firstname'] = $firstName; -// $user['lastname'] = $lastName; - -/* - $query = "INSERT INTO users_conferences_link(conferences_id, users_uid) VALUES("; - $query .= $conference['id'] . ', ' . $user['uid']; - $firstName = $user['firstname']; - $lastName = $user['lastname']; -*/ }else{ // we're creating a new user if(strlen($email) != 0){ @@ -87,20 +115,27 @@ function process_newRecord($firstName, $lastName, $email){ $user = user_create('student', $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); - $uid = $user['uid']; } + $uid = $user['uid']; echo user_row($uid, $username, $firstName, $lastName, $email); return true; } +// generate the table row for thisa given record function user_row($uid, $username, $firstName, $lastName, $email){ $rval = "