From e6dfe1c47644a370d64c3f9b5b05abea63f9b7ac Mon Sep 17 00:00:00 2001 From: jacob Date: Fri, 18 Feb 2011 19:19:56 +0000 Subject: [PATCH] Added joinProject function in register_participants.inc.php, and added the api for it at /api/project/join --- api.php | 15 ++++++++++++--- register_participants.inc.php | 33 ++++++++++++++++++++++++++++++++- testapi.php | 8 ++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/api.php b/api.php index e854c68..242c288 100644 --- a/api.php +++ b/api.php @@ -39,7 +39,6 @@ $ret=array(); $logPath = get_logpath(); $fout = fopen("$logPath/api.log", "a"); -$t = date("Y-m-d H:i:s"); fwrite($fout, " --- request at " . date("Y-m-d H:i:s") . " ---\n"); fwrite($fout, "\$_GET = \n" . print_r($_GET, true) . "\n"); fwrite($fout, "\$_POST = \n" . print_r($_POST, true) . "\n"); @@ -971,8 +970,18 @@ switch($request[0]) { */ case 'join': // this should let somone join a specific registration (think "team") - $ret['status'] = "error"; - $ret['error'] = $_GET['request'] . " functionality not yet implemented"; + if(!(array_key_exists('registrations_id', $_POST) && array_key_exists('email', $_POST))){ + $ret['status'] = "error"; + $ret['error'] = "Missing required parameters"; + break; + } + $result = joinProject($_POST['registrations_id'], $_POST['email']); + if($result == "ok"){ + $ret['status'] = "ok"; + }else{ + $ret['status'] = "error"; + $ret['error'] = $result; + } break; /* APIDOC: project/remove diff --git a/register_participants.inc.php b/register_participants.inc.php index 2aaf1a4..75269e0 100644 --- a/register_participants.inc.php +++ b/register_participants.inc.php @@ -702,12 +702,43 @@ function addProject($registrations_id){ //now query the one we just inserted $q = mysql_query("SELECT * FROM projects WHERE registrations_id='$registrations_id' AND conferences_id='{$conference['id']}'"); if(mysql_error()) { - $returnval = "register_participants.inc.php::addProject -> " . $returnval; + $returnval = "register_participants.inc.php::addProject -> " . mysql_error(); }else{ $returnval = mysql_fetch_assoc($q); } return $returnval; } +// 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){ + $uid = $_SESSION['users_id']; + if(getRegistrationsId($uid) !== null){ + return 'register_participants.inc.php::joinProject -> you are already registered for a project'; + } + + // let's avoid an SQL naughtiness + $email = mysql_real_escape_string($email); + $registrations_id = intval($registrations_id); + + $query = mysql_query("SELECT COUNT(*) as tally FROM registrations WHERE email = '$email' AND num = $registrations_id"); + if(mysql_error()){ + return "register_participants.inc.php::joinProject -> " . mysql_error(); + } + $result = mysql_fetch_assoc($query); + if($result['tally'] != 1){ + return "register_participants.inc.php::joinProject -> invalid email or registration 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"); + if(mysql_error()){ + return "register_participants.inc.php::joinProject -> " . mysql_error(); + } + + return 'ok'; +} ?> diff --git a/testapi.php b/testapi.php index 7a52636..080f232 100644 --- a/testapi.php +++ b/testapi.php @@ -106,6 +106,14 @@ email address:
+

Project related commands

+

Join a project

+
+
+
+ +
+

Teacher Specific Commands

Science Olympic Teams List