Added some API functionality used in schoolschedule.php

This commit is contained in:
jacob 2010-09-28 20:09:31 +00:00
parent eb3c8f0d77
commit 0a02525d14
4 changed files with 290 additions and 173 deletions

56
api.php
View File

@ -25,6 +25,7 @@
include "common.inc.php"; include "common.inc.php";
require_once("account.inc.php"); require_once("account.inc.php");
require_once("user.inc.php"); require_once("user.inc.php");
require_once("schedule.inc.php");
if($_SERVER['HTTPS']!="on") { if($_SERVER['HTTPS']!="on") {
$ret['status']="error"; $ret['status']="error";
@ -37,6 +38,61 @@ $request=explode("/",$_GET['request']);
$ret=array(); $ret=array();
switch($request[0]) { switch($request[0]) {
case 'locations':
switch($request[1]){
case 'list':
/* APIDOC: locations/list
description(lists locations at the current conference)
return(list array)
*/
$locationData = getLocationList($_SESSION['conferences_id']);
if(is_array($locationData)){
$ret['status'] = 'ok';
$ret['list'] = $locationData;
}else{
$ret['status'] = 'error';
$ret['error'] = $locationData;
}
break;
default:
$ret['status'] = 'error';
$ret['error']="Invalid API command ({$request[1]})";
}
break;
case "schedule":
$u = user_load($_SESSION['users_id']);
$school_id = null;
if($u['schools_id']) {
$school_id=$u['schools_id'];
}else{
$ret['status'] = 'error';
$ret['error']="Not connected to a school";
break;
}
switch($request[1]){
case 'list':
/* APIDOC: events/list
description(gets a list of all events at the current conference, and team information if applicable)
return(list array)
*/
$eventData = getEventList($_SESSION['conferences_id'], $school_id);
if(is_array($eventData)){
$ret['status'] = 'ok';
$ret['list'] = $eventData;
}else{
$ret['status'] = 'error';
$ret['error'] = $eventData;
}
break;
default:
$ret['status'] = 'error';
$ret['error']="Invalid API command ({$request[1]})";
}
break;
case "conferences": case "conferences":
/* APIDOC: conferences/switch /* APIDOC: conferences/switch
description(switches the active conference) description(switches the active conference)

View File

@ -44,5 +44,45 @@ function getNumRegistrations($scheduleid) {
return $r->num; return $r->num;
} }
function getEventList($conferencesId, $school_id){
global $conference;
$events = array();
$q=mysql_query("SELECT schedule.*, events.name, events.eventtype FROM schedule JOIN events ON schedule.events_id=events.id WHERE schedule.conferences_id='{$conference['id']}'");
$err = mysql_error();
if($err != "") return($err);
while($event = mysql_fetch_assoc($q)) {
if($event['eventtype'] == "scienceolympic") {
$regteams = getNumRegistrations($event['id']);
$maxteams = $event['somaxteams'];
$regq = mysql_query("SELECT so_teams.name, schedule_registrations.id
FROM schedule_registrations
JOIN so_teams ON so_teams_id=so_teams.id
WHERE schedule_registrations.conferences_id='{$conferencesId}'
AND so_teams.schools_id=$school_id
AND schedule_registrations.schedule_id='{$event['id']}'");
$err = mysql_error();
if($err != "") break;
$event['registrations'] = array();
while($regr = mysql_fetch_assoc($regq)) {
$event['registrations'][] = $regr;
}
}
$events[] = $event;
}
if($err != "") return($err);
return $events;
}
function getLocationList($conferencesId){
global $conference;
$q=mysql_query("SELECT * FROM locations WHERE conferences_id='{$conference['id']}' ORDER BY name");
while($r=mysql_fetch_object($q)) {
$locations[$r->id]=$r->name;
}
return $locations;
}
?> ?>

View File

@ -32,17 +32,27 @@
$q=mysql_query("SELECT * FROM schools WHERE id='".$schoolid."'"); $q=mysql_query("SELECT * FROM schools WHERE id='".$schoolid."'");
echo mysql_error(); echo mysql_error();
$school=mysql_fetch_object($q); $school=mysql_fetch_object($q);
if($school) { if(!$school) exit;
if($_GET['action']=="loadschedule") { if($_GET['action']=="loadschedule") {
$date = $_POST['date']; $date = $_POST['date'];
if(!eregi("[0-9]{4}-[0-9]{2}-[0-9]{2}",$date)) {
echo "Invalid date";
exit;
}
//cant change this $eventList = getEventList($conference['id'], $school->id);
$q=mysql_query("SELECT MIN(hour) AS minhour, MAX(hour) AS maxhour, CEIL(MAX(hour)+duration/60) AS maxfinish FROM schedule WHERE conferences_id='{$conference['id']}' AND date='{$date}'");
$parameters=mysql_fetch_object($q);
$starthour=$parameters->minhour; // get the minimum and maximum hours from the schedule
$endhour=$parameters->maxfinish; $starthour = $endhour = null;
foreach($eventList as $event){
if($event['date'] == $date){
$sh = $event['hour'];
$eh = $sh + ceil($event['duration'] / 60);
if($endhour === null || $endhour < $eh) $endhour = $eh;
if($starthour === null || $starthour > $sh) $starthour = $sh;
}
}
//do some sanity checks //do some sanity checks
if($starthour<0 || $starthour>24) $starthour=8; if($starthour<0 || $starthour>24) $starthour=8;
@ -53,10 +63,6 @@
//minute increment //minute increment
$increment=15; $increment=15;
if(!eregi("[0-9]{4}-[0-9]{2}-[0-9]{2}",$date)) {
echo "Invalid date";
exit;
}
echo "<h3>".i18n("Schedule for %1",array(format_date($date)))."</h3>"; echo "<h3>".i18n("Schedule for %1",array(format_date($date)))."</h3>";
$q=mysql_query("SELECT * FROM locations WHERE conferences_id='{$conference['id']}' ORDER BY name"); $q=mysql_query("SELECT * FROM locations WHERE conferences_id='{$conference['id']}' ORDER BY name");
while($r=mysql_fetch_object($q)) { while($r=mysql_fetch_object($q)) {
@ -93,46 +99,51 @@
$js="var eventdivs=new Array();\n"; $js="var eventdivs=new Array();\n";
//now make all our DIV's for the events that are scheduled in the database //now make all our DIV's for the events that are scheduled in the database
$x=0;
//they will be moved by javascript after the fact //they will be moved by javascript after the fact
$q=mysql_query("SELECT schedule.*, events.name, events.eventtype FROM schedule JOIN events ON schedule.events_id=events.id WHERE schedule.conferences_id='{$conference['id']}' AND date='{$date}'"); if(is_array($eventList)){
echo mysql_error(); foreach($eventList as $event){
while($r=mysql_fetch_object($q)) { // skip this event if it's not on the day we're looking at
echo "<div class=\"scheduleevent scheduleevent_{$r->eventtype}\" id=\"event_{$r->id}\" onclick=\"viewEvent($r->id)\">"; if($event['date'] != $date) continue;
// some variables for convenience. Used repeatedly
$eid = $event['id'];
$hour = $event['hour'];
$minute = $event['minute'];
$location = $event['locations_id'];
$duration = $event['duration'];
$eventType = $event['eventtype'];
$title = $event['title'];
$starttime = strtotime($hour . ":" . $minute);
$endtime = $starttime + $duration * 60;
echo "<div class=\"scheduleevent scheduleevent_$eventType\" id=\"event_$eid\" onclick=\"viewEvent($eid)\">";
echo "<span class=\"scheduleevent_title\">"; echo "<span class=\"scheduleevent_title\">";
echo $r->title; echo $title;
echo "</span>"; echo "</span>";
echo "<br />"; echo "<br />";
$starttime=strtotime($r->hour.":".$r->minute);
$endtime=$starttime+$r->duration*60;
echo format_time($starttime); echo format_time($starttime);
echo " to "; echo " to ";
echo format_time($endtime); echo format_time($endtime);
if($r->eventtype=="scienceolympic") { if($eventType == "scienceolympic") {
echo "<br />"; echo "<br />";
echo i18n("Capacity").": "; echo i18n("Capacity").": ";
$regteams=getNumRegistrations($r->id); $regteams = getNumRegistrations($eid);
$maxteams=$r->somaxteams; $maxteams= $event['somaxteams'];
echo i18n("%1 of %2", array($regteams, $maxteams)) . "<br />"; echo i18n("%1 of %2", array($regteams, $maxteams)) . "<br />";
foreach($event['registrations'] as $regr){
$regq=mysql_query("SELECT so_teams.name, schedule_registrations.id echo "<div style=\"padding: 2px; border: 1px solid black; background-color: yellow;\" onclick=\"handleTeamClick(event, {$regr['id']}, $eid);\">";
FROM schedule_registrations echo $regr['name'];
JOIN so_teams ON so_teams_id=so_teams.id
WHERE schedule_registrations.conferences_id='{$conference['id']}'
AND so_teams.schools_id={$school->id}
AND schedule_registrations.schedule_id='$r->id'");
echo mysql_error();
while($regr=mysql_fetch_object($regq)) {
echo "<div style=\"padding: 2px; border: 1px solid black; background-color: yellow;\" onclick=\"handleTeamClick(event, {$regr->id}, {$r->events_id});\">";
echo $regr->name;
echo "</div>"; echo "</div>";
} }
} }
echo "</div>"; echo "</div>";
$js.="eventdivs[$r->id]={hour:$r->hour,minute:$r->minute,location:$r->locations_id,duration:$r->duration,title:'".rawurlencode($r->title)."',eventtype:'$r->eventtype'};\n"; $js .= "eventdivs[$eid]={hour:$hour,minute:$minute,location:$location,duration:$duration,title:'".rawurlencode($title)."',eventtype:'$eventType'};\n";
$x++; }
}else{
// it's actually an error message
echo $eventList;
} }
echo "<script type=\"text/javascript\">\n"; echo "<script type=\"text/javascript\">\n";
echo $js; echo $js;
@ -145,17 +156,17 @@
// print_r($_POST); // print_r($_POST);
// get a list of all students that could be put on this team // get a list of all students that could be put on this team
$query =
$results = mysql_query("SELECT * FROM users_student "SELECT * FROM users"
JOIN users ON users_student.users_id = users.uid . " JOIN user_roles ON user_roles.users_id = users.id"
JOIN users_conferences_link ucl ON ucl.users_uid = users_student.users_id . " JOIN roles ON roles.id = user_roles.roles_id"
WHERE schools_id = $schoolid . " WHERE schools_id = $schoolid"
AND ucl.conferences_id={$conference['id']} . " AND conferences_id = {$conference['id']}"
AND users.deleted = 'no'"); . " AND deleted = 'no'"
. " AND roles.type = 'student'";
$availList = array(); $availList = array();
while($record = mysql_fetch_array($results)){ while($record = mysql_fetch_array($results)){
$availList[$record['uid']] = $record['firstname'] . ' ' . $record['lastname']; $availList[$record['accounts_id']] = $record['firstname'] . ' ' . $record['lastname'];
} }
// let's empty the current list of students for this team // let's empty the current list of students for this team
@ -193,16 +204,19 @@
$teamName = $record['name']; $teamName = $record['name'];
// get a list of all students that could be put on this team // get a list of all students that could be put on this team
$query = 'SELECT * FROM users_student';
$query .= ' JOIN users ON users_student.users_id = users.uid'; $query =
$query .= ' JOIN users_conferences_link ucl ON ucl.users_uid = users_student.users_id'; "SELECT * FROM users"
$query .= ' WHERE schools_id = ' . $schoolid; . " JOIN user_roles ON user_roles.users_id = users.id"
$query .= ' AND ucl.conferences_id=' . $conference['id']; . " JOIN roles ON roles.id = user_roles.roles_id"
$query .= ' AND users.deleted = "no"'; . " WHERE schools_id = $schoolid"
. " AND conferences_id = {$conference['id']}"
. " AND deleted = 'no'"
. " AND roles.type = 'student'";
$results = mysql_query($query); $results = mysql_query($query);
$availList = array(); $availList = array();
while($record = mysql_fetch_array($results)){ while($record = mysql_fetch_array($results)){
$availList[$record['uid']] = $record['firstname'] . ' ' . $record['lastname']; $availList[$record['accounts_id']] = $record['firstname'] . ' ' . $record['lastname'];
} }
// now find out which of these students are already selected // now find out which of these students are already selected
@ -210,6 +224,7 @@
$query .= " WHERE users_uid IN ("; $query .= " WHERE users_uid IN (";
$query .= implode(',', array_keys($availList)) . ')'; $query .= implode(',', array_keys($availList)) . ')';
$query .= " AND schedule_registrations_id = " . $_POST['regId']; $query .= " AND schedule_registrations_id = " . $_POST['regId'];
echo "\n[[[ $query ]]]\n";
$results = mysql_query($query); $results = mysql_query($query);
$selectedList = array(); $selectedList = array();
if($results){ if($results){
@ -488,5 +503,4 @@
send_footer(); send_footer();
} }
}
?> ?>

View File

@ -57,6 +57,13 @@ Date List
<input type="submit" value="Edit Team"> <input type="submit" value="Edit Team">
</form> </form>
<h1>Event Schedule</h1>
<a href="api/schedule/list">Schedule Listing</a><br/>
<a href="api/locations/list">Location Listing</a><br/>
</form>
<h1>Session Variables</h1> <h1>Session Variables</h1>
<? <?
echo json_encode($_SESSION); echo json_encode($_SESSION);