From 40e139b7a141687a6c4d192576d62ece4ece9673 Mon Sep 17 00:00:00 2001 From: jacob Date: Fri, 18 Feb 2011 20:25:47 +0000 Subject: [PATCH] Fix for logical error in how I connected users to registrations. Had the wrong ID field connecting them. --- api.php | 59 ++++++++++++++++++----------------- register_participants.inc.php | 49 ++++++++++++++++++++++++----- 2 files changed, 72 insertions(+), 36 deletions(-) diff --git a/api.php b/api.php index 242c2885..7254383b 100644 --- a/api.php +++ b/api.php @@ -882,15 +882,15 @@ switch($request[0]) { } // we start by creating a registration - $regNumber = addRegistration($_SESSION['users_id']); - if(!is_numeric($regNumber)){ + $regId = addRegistration($_SESSION['users_id']); + if(!is_numeric($regId)){ $ret['status'] = 'error'; - $ret['error'] = $regNumber; + $ret['error'] = $regId; break; } // now we add a project to that registration - $project = addProject($regNumber); + $project = addProject($regId); if(!is_array($project)){ $ret['status'] = 'error'; $ret['error'] = $project; @@ -899,26 +899,8 @@ switch($request[0]) { // if we got this far, then all's good and we can return the project data $ret['status'] = 'ok'; - $ret['project'] = getProject($regNumber); + $ret['project'] = getProject($regId); break; - // remarking this code for now as it may get used very shortly for a project update - // functionality. Was previously in the "add" post - /* - // and then save the posted data to that project - $params['project_id'] = $project['id']; - foreach($_POST as $fieldName){ - $params[$fieldName] = $_POST[$fieldName]; - } - $message = saveProjectData($params); - if($message != 'success'){ - $ret['status'] = 'error'; - $ret['error'] = $message; - break; - } - break; - */ - - /* APIDOC: project/view description(Displays the current project information. project array: project_id integer, projectdivisions_id integer, title varchar(255), language char(2), req_electricity enum('no', 'yes'), req_table enum('no', 'yes'), req_special varchar(128), summary text) @@ -966,16 +948,16 @@ switch($request[0]) { break; /* APIDOC: project/join - description(join an existing project - not yet implemented) + post(registration_number integer, email varchar(64)) + description(join an existing project, identified and confirmed by the registration number and e-mail address stored in the registrations table) */ case 'join': - // this should let somone join a specific registration (think "team") - if(!(array_key_exists('registrations_id', $_POST) && array_key_exists('email', $_POST))){ + if(!(array_key_exists('registration_number', $_POST) && array_key_exists('email', $_POST))){ $ret['status'] = "error"; $ret['error'] = "Missing required parameters"; break; } - $result = joinProject($_POST['registrations_id'], $_POST['email']); + $result = joinProject($_POST['registration_number'], $_POST['email']); if($result == "ok"){ $ret['status'] = "ok"; }else{ @@ -985,11 +967,28 @@ switch($request[0]) { break; /* APIDOC: project/remove - description(remove an existing project - not yet implemented + post(registration_number integer) + description(remove the current user from an existing project. If no other users are in the project, then it is deleted.) */ case 'remove': $ret['status'] = "error"; $ret['error'] = $_GET['request'] . " functionality not yet implemented"; + /* + if(!array_key_exists('registration_number', $_POST)){ + $ret['status'] = 'error'; + $ret['error'] = 'registration_number (integer) is required'; + break; + } + + $result = removeProject($_POST['registration_number']); + if($result != 'ok'){ + $ret['status'] = "error"; + $ret['error'] = $result; + break; + } + + $ret['status'] = 'ok'; + */ break; case 'mentor': @@ -1017,6 +1016,10 @@ switch($request[0]) { $ret['status'] = "error"; $ret['error'] = $_GET['request'] . " functionality not yet implemented"; break; + + default: + $ret['status']="error"; + $ret['error']="invalid project API command ({$request[2]})"; } break; diff --git a/register_participants.inc.php b/register_participants.inc.php index 75269e03..cc8a865d 100644 --- a/register_participants.inc.php +++ b/register_participants.inc.php @@ -604,7 +604,7 @@ function getNewRegNum(){ return $regnum; } -// add a registration record and return it's unique "num" id +// add a registration record and return it's unique id // returns an error message if the user is alredy registered function addRegistration($userId){ global $conference; @@ -640,10 +640,12 @@ function addRegistration($userId){ $err = mysql_error(); if($err){ return "register_participants.inc.php::addRegistration -> " . $err; + }else{ + $regid = mysql_insert_id(); } // update the user now, connecting them to that registration - $query = "UPDATE users SET registrations_id = $regnum WHERE id = $userId"; + $query = "UPDATE users SET registrations_id = $regid WHERE id = $userId"; mysql_query($query); $err = mysql_error(); if($err){ @@ -652,7 +654,7 @@ function addRegistration($userId){ // ok, if the flow hits this point, then we've successfully added the registration and // linked the user to it. Return the registration number - return $regnum; + return $regid; } // get the registration id for a specific user. @@ -712,7 +714,7 @@ function addProject($registrations_id){ // join an existing project // perhaps a bit of a misnomer as it's actually the registration that's being joined, but meh. // return 'ok' on success, error message on failure -function joinProject($registrations_id, $email){ +function joinProject($registration_number, $email){ $uid = $_SESSION['users_id']; if(getRegistrationsId($uid) !== null){ return 'register_participants.inc.php::joinProject -> you are already registered for a project'; @@ -720,20 +722,22 @@ function joinProject($registrations_id, $email){ // let's avoid an SQL naughtiness $email = mysql_real_escape_string($email); - $registrations_id = intval($registrations_id); + $registration_number = intval($registration_number); - $query = mysql_query("SELECT COUNT(*) as tally FROM registrations WHERE email = '$email' AND num = $registrations_id"); + $query = mysql_query("SELECT id FROM registrations WHERE email = '$email' AND num = $registration_number"); if(mysql_error()){ return "register_participants.inc.php::joinProject -> " . mysql_error(); } $result = mysql_fetch_assoc($query); - if($result['tally'] != 1){ + if(!$result){ return "register_participants.inc.php::joinProject -> invalid email or registration id"; } + $registration_id = $result['id']; + // ok, if we've made it this far, they've correctly added the info that we verify with. Go ahead // and add them to the registration - $result = mysql_query("UPDATE users SET registrations_id = $registrations_id WHERE id = $uid"); + $result = mysql_query("UPDATE users SET registrations_id = $registration_id WHERE id = $uid"); if(mysql_error()){ return "register_participants.inc.php::joinProject -> " . mysql_error(); } @@ -741,4 +745,33 @@ function joinProject($registrations_id, $email){ return 'ok'; } +// disassociate the active user from the specified project registration. If the registration no longer +// has any users connected to it, delete it, and any projects tied to it +/* +function removeProject($registration_id){ + // make sure this user is indeed connected to the specified project + $uid = $_SESSION['users_id']; + $regId = getRegistrationsId($uid); + $registration_number = intval($registration_number); + if($regId != $registration_number){ + return 'register_participants.inc.php::removeProject -> you are not connected to that project'; + } + + mysql_query("UPDATE users SET registrations_id = null WHERE ud = $uid"); + if(mysql_error()){ + return "register_participants.inc.php::removeProject -> " . mysql_error(); + } + + // now let's see if anyone else is connected to that registration + $q = mysql_query("SELECT COUNT(*) AS tally FROM users WHERE registrations_id = $registration_number"); + $result = mysql_fetch_assoc($q); + if($result['tally'] == 0){ + //nobody wants the poor lonely registration. Let's put it out of it's misery + mysql_query("DELETE FROM registrations WHERE num = $registration_number"); + mysql_query("DELETE FROM projects WHERE registrations_id + } + + return 'ok'; +} +*/ ?>