diff --git a/api.php b/api.php index 462849b..78cc3ae 100644 --- a/api.php +++ b/api.php @@ -26,13 +26,13 @@ include "common.inc.php"; require_once("account.inc.php"); require_once("user.inc.php"); require_once("schedule.inc.php"); - +/* FIXME: UNREMARK BEFORE COMMITTING if($_SERVER['HTTPS']!="on") { $ret['status']="error"; $ret['error']="SSL is required for API access, please access the API over https"; echo json_encode($ret); exit; -} +}*/ $request=explode("/",$_GET['request']); $ret=array(); @@ -86,6 +86,42 @@ switch($request[0]) { } break; + case 'register': + /* APIDOC: schedule/register + description(register a team for a scheduled event) + post(team_id integer, schedule_id integer) + return(results array) + */ + if(!array_key_exists('team_id', $_POST)){ + $ret['status'] = "error"; + $ret['error'] = 'team_id (integer) is required'; + }else if(!array_key_exists('schedule_id', $_POST)){ + $ret['status'] = "error"; + $ret['error'] = 'schedule_id (integer) is required'; + }else{ + $ret['status'] = 'ok'; + $ret['registration'] = registerTeamInEvent($_SESSION['conferences_id'], $_POST['schedule_id'], $_POST['team_id']); + } + break; + + case 'unregister': + /* APIDOC: schedule/register + description(unregister a team for a scheduled event) + post(team_id integer, schedule_id integer) + return(results array) + */ + if(!array_key_exists('team_id', $_POST)){ + $ret['status'] = "error"; + $ret['error'] = 'team_id (integer) is required'; + }else if(!array_key_exists('schedule_id', $_POST)){ + $ret['status'] = "error"; + $ret['error'] = 'schedule_id (integer) is required'; + }else{ + $ret['status'] = 'ok'; + $ret['registration'] = unregisterTeamInEvent($_SESSION['conferences_id'], $_POST['schedule_id'], $_POST['team_id']); + } + break; + default: $ret['status'] = 'error'; $ret['error']="Invalid API command ({$request[1]})"; diff --git a/schedule.inc.php b/schedule.inc.php index 8fef911..a645a3c 100644 --- a/schedule.inc.php +++ b/schedule.inc.php @@ -84,5 +84,87 @@ function getLocationList($conferencesId){ return $locations; } +function registerTeamInEvent($conferenceid, $scheduleid, $teamid){ + + $q=mysql_query("SELECT schedule.*, + events.eventtype, + events.website, + events.summary, + events.name, + locations.name AS location + FROM schedule + JOIN events ON schedule.events_id=events.id + JOIN locations ON schedule.locations_id=locations.id + WHERE schedule.conferences_id='{$conferenceid}' + AND schedule.id='{$scheduleid}'"); + $schedule=mysql_fetch_object($q); + echo mysql_error(); + + $q=mysql_query("SELECT * FROM schedule_registrations WHERE so_teams_id=$teamid AND schedule_id=$scheduleid AND conferences_id=$conferenceid"); + if(mysql_num_rows($q)) { + $returnval = array('success' => false, 'message' => "This team is already registered for this scheduled event"); + } + else { + $regteams=getNumRegistrations($scheduleid); + if($regteams<$schedule->somaxteams) { + mysql_query("INSERT INTO schedule_registrations (schedule_id, so_teams_id, conferences_id, dt) VALUES ('$scheduleid','$teamid','$conferenceid',NOW())"); + $returnval = array('success' => true, 'message' => "successfully registered"); + } + else { + $returnval = array('success' => false, 'message' => "This event has reached its capacity and can no longer be registered for"); + } + } + return $returnval; +} + +function unregisterTeamInEvent($conferenceid, $scheduleid, $teamid){ + $q=mysql_query("SELECT schedule.*, + events.eventtype, + events.website, + events.summary, + events.name, + locations.name AS location + FROM schedule + JOIN events ON schedule.events_id=events.id + JOIN locations ON schedule.locations_id=locations.id + WHERE schedule.conferences_id='{$conferenceid}' + AND schedule.id='{$scheduleid}'"); + $schedule=mysql_fetch_object($q); + echo mysql_error(); + + $q=mysql_query("SELECT * FROM schedule_registrations WHERE so_teams_id=$teamid AND schedule_id=$scheduleid AND conferences_id=$conferenceid"); + if(mysql_num_rows($q)) { + + // delete any links between students and this registrationa + $q = mysql_query( + " SELECT users_uid, schedule_registrations_id" . + " FROM schedule_registrations_users_link srul" . + " JOIN schedule_registrations sr ON sr.id = srul.schedule_registrations_id" . + " WHERE sr.so_teams_id = '$teamid'" . + " AND sr.schedule_id='$scheduleid'" . + " AND sr.conferences_id='$conferenceid'" + ); + while($r = mysql_fetch_array($q)){ + $idSet[] = $r['users_uid']; + $srid = $r['schedule_registrations_id']; + } + if(is_array($idSet)){ + $query = + " DELETE FROM schedule_registrations_users_link" . + " WHERE schedule_registrations_id = $srid" . + " AND users_uid IN(" . implode(',', $idSet) . ")"; + $q = mysql_query($query); + } + + // delete the registrations themselves + mysql_query("DELETE FROM schedule_registrations WHERE so_teams_id='$teamid' AND schedule_id='$scheduleid' AND conferences_id='$conferenceid'"); + + $returnval = array('success' => true, 'message' => "successfully unregistered"); + } + else { + $returnval = array('success' => false, 'message' => "This team is not registered for this scheduled event, and thus, cannot be unregistered"); + } + return $returnval; +} ?> diff --git a/schoolschedule_event_tab.php b/schoolschedule_event_tab.php index b29e596..4c4cec4 100644 --- a/schoolschedule_event_tab.php +++ b/schoolschedule_event_tab.php @@ -124,71 +124,22 @@ $scheduleid=intval($_POST['scheduleid']); $conferenceid=$conference['id']; - $q=mysql_query("SELECT schedule.*, - events.eventtype, - events.website, - events.summary, - events.name, - locations.name AS location - FROM schedule - JOIN events ON schedule.events_id=events.id - JOIN locations ON schedule.locations_id=locations.id - WHERE schedule.conferences_id='{$conference['id']}' - AND schedule.id='{$scheduleid}'"); - $schedule=mysql_fetch_object($q); - echo mysql_error(); - switch($_POST['regaction']) { case "register": - $q=mysql_query("SELECT * FROM schedule_registrations WHERE so_teams_id=$teamid AND schedule_id=$scheduleid AND conferences_id=$conferenceid"); - if(mysql_num_rows($q)) { - error_("This team is already registered for this scheduled event"); - } - else { - $regteams=getNumRegistrations($scheduleid); - if($regteams<$schedule->somaxteams) { - mysql_query("INSERT INTO schedule_registrations (schedule_id, so_teams_id, conferences_id, dt) VALUES ('$scheduleid','$teamid','$conferenceid',NOW())"); + $results = registerTeamInEvent($conferenceid, $scheduleid, $teamid); + if($results['success'] == true){ happy_("Successfully registered the team for this scheduled event"); + }else{ + error_($results['message']); } - else { - error_("This event has reached its capacity and can no longer be registered for"); - } - } break; case "unregister": - - $q=mysql_query("SELECT * FROM schedule_registrations WHERE so_teams_id=$teamid AND schedule_id=$scheduleid AND conferences_id=$conferenceid"); - if(mysql_num_rows($q)) { - - // delete any links between students and this registrationa - $q = mysql_query( - " SELECT users_uid, schedule_registrations_id" . - " FROM schedule_registrations_users_link srul" . - " JOIN schedule_registrations sr ON sr.id = srul.schedule_registrations_id" . - " WHERE sr.so_teams_id = '$teamid'" . - " AND sr.schedule_id='$scheduleid'" . - " AND sr.conferences_id='$conferenceid'" - ); - while($r = mysql_fetch_array($q)){ - $idSet[] = $r['users_uid']; - $srid = $r['schedule_registrations_id']; - } - if(is_array($idSet)){ - $query = - " DELETE FROM schedule_registrations_users_link" . - " WHERE schedule_registrations_id = $srid" . - " AND users_uid IN(" . implode(',', $idSet) . ")"; - $q = mysql_query($query); - } - - // delete the registrations themselves - mysql_query("DELETE FROM schedule_registrations WHERE so_teams_id='$teamid' AND schedule_id='$scheduleid' AND conferences_id='$conferenceid'"); - + $results = unregisterTeamInEvent($conferenceid, $scheduleid, $teamid); + if($results['success'] == true){ happy_("Successfully unregistered the team from this scheduled event"); - } - else { - error_("This team is not registered for this scheduled event, and thus, cannot be unregistered"); + }else{ + error_($results['message']); } break; } diff --git a/testapi.php b/testapi.php index 645e63e..6d13be8 100644 --- a/testapi.php +++ b/testapi.php @@ -59,9 +59,24 @@ Date List

Event Schedule

Schedule Listing
+ Location Listing
+ +Register a team for a scheduled event
+
+Team Id: , +Schedule ID: +
+Un-Register a team for a scheduled event
+
+Team Id: , +Schedule ID: + +
+ +

Session Variables