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

@ -13,7 +13,7 @@
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details. General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to along with this program; see the file COPYING. If not, write to
@ -22,50 +22,56 @@
*/ */
?> ?>
<? <?
require("common.inc.php"); require("common.inc.php");
require_once("schedule.inc.php"); require_once("schedule.inc.php");
require_once("user.inc.php"); require_once("user.inc.php");
user_auth_required("teacher"); user_auth_required("teacher");
$schoolid=user_field_required("schools_id","user_edit.php?tab=school"); $schoolid=user_field_required("schools_id","user_edit.php?tab=school");
$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)) {
//cant change this echo "Invalid date";
$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}'"); exit;
$parameters=mysql_fetch_object($q); }
$starthour=$parameters->minhour; $eventList = getEventList($conference['id'], $school->id);
$endhour=$parameters->maxfinish;
//do some sanity checks // get the minimum and maximum hours from the schedule
if($starthour<0 || $starthour>24) $starthour=8; $starthour = $endhour = null;
if($endhour<$starthour) foreach($eventList as $event){
$endhour=$starthour+10; if($event['date'] == $date){
if($endhour<0 || $endhour>24) $endhour=15; $sh = $event['hour'];
$eh = $sh + ceil($event['duration'] / 60);
if($endhour === null || $endhour < $eh) $endhour = $eh;
if($starthour === null || $starthour > $sh) $starthour = $sh;
}
}
//minute increment //do some sanity checks
$increment=15; if($starthour<0 || $starthour>24) $starthour=8;
if($endhour<$starthour)
$endhour=$starthour+10;
if($endhour<0 || $endhour>24) $endhour=15;
if(!eregi("[0-9]{4}-[0-9]{2}-[0-9]{2}",$date)) { //minute increment
echo "Invalid date"; $increment=15;
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)) { $locations[$r->id]=$r->name;
$locations[$r->id]=$r->name; }
} if(!count($locations)) {
if(!count($locations)) { echo error(i18n("There are no locations defined."));
echo error(i18n("There are no locations defined.")); exit;
exit; }
}
echo "<table class=\"schedule\" id=\"schedule\">\n"; echo "<table class=\"schedule\" id=\"schedule\">\n";
echo "<tr>"; echo "<tr>";
@ -93,69 +99,74 @@
$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;
echo "<span class=\"scheduleevent_title\">";
echo $r->title;
echo "</span>";
echo "<br />";
$starttime=strtotime($r->hour.":".$r->minute);
$endtime=$starttime+$r->duration*60;
echo format_time($starttime);
echo " to ";
echo format_time($endtime);
if($r->eventtype=="scienceolympic") { // 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 $title;
echo "</span>";
echo "<br />"; echo "<br />";
echo i18n("Capacity").": "; echo format_time($starttime);
$regteams=getNumRegistrations($r->id); echo " to ";
$maxteams=$r->somaxteams; echo format_time($endtime);
echo i18n("%1 of %2",array($regteams,$maxteams))."<br />";
if($eventType == "scienceolympic") {
$regq=mysql_query("SELECT so_teams.name, schedule_registrations.id echo "<br />";
FROM schedule_registrations echo i18n("Capacity").": ";
JOIN so_teams ON so_teams_id=so_teams.id $regteams = getNumRegistrations($eid);
WHERE schedule_registrations.conferences_id='{$conference['id']}' $maxteams= $event['somaxteams'];
AND so_teams.schools_id={$school->id} echo i18n("%1 of %2", array($regteams, $maxteams)) . "<br />";
AND schedule_registrations.schedule_id='$r->id'"); foreach($event['registrations'] as $regr){
echo mysql_error(); echo "<div style=\"padding: 2px; border: 1px solid black; background-color: yellow;\" onclick=\"handleTeamClick(event, {$regr['id']}, $eid);\">";
while($regr=mysql_fetch_object($regq)) { echo $regr['name'];
echo "<div style=\"padding: 2px; border: 1px solid black; background-color: yellow;\" onclick=\"handleTeamClick(event, {$regr->id}, {$r->events_id});\">"; echo "</div>";
echo $regr->name; }
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;
echo "</script>"; echo "</script>";
}else if($_GET['action']=="loadevent") { }else if($_GET['action']=="loadevent") {
}else if($_GET['action']=="saveevent") { }else if($_GET['action']=="saveevent") {
}else if($_GET['action'] == "saveteamlist"){ }else if($_GET['action'] == "saveteamlist"){
// 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
@ -173,9 +184,9 @@
$query .= implode(',', $valueSet); $query .= implode(',', $valueSet);
$results = mysql_query($query); $results = mysql_query($query);
}else if($_GET['action'] == "getteamlist"){ }else if($_GET['action'] == "getteamlist"){
// first we'll get the basic info, and do a quick check that the selected team is in this user's domain // first we'll get the basic info, and do a quick check that the selected team is in this user's domain
if(!array_key_exists('regId', $_POST)){ if(!array_key_exists('regId', $_POST)){
echo "error\n"; echo "error\n";
return; return;
} }
@ -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){
@ -222,8 +237,8 @@
// get the necessary data about the event itself // get the necessary data about the event itself
$query = "SELECT sominteamsize, somaxteamsize, title FROM schedule JOIN" $query = "SELECT sominteamsize, somaxteamsize, title FROM schedule JOIN"
. " schedule_registrations sr ON sr.schedule_id = schedule.id WHERE" . " schedule_registrations sr ON sr.schedule_id = schedule.id WHERE"
. " sr.id = " . $_POST['regId']; . " sr.id = " . $_POST['regId'];
$results = mysql_fetch_array(mysql_query($query)); $results = mysql_fetch_array(mysql_query($query));
$minTeamSize = $results['sominteamsize']; $minTeamSize = $results['sominteamsize'];
$maxTeamSize = $results['somaxteamsize']; $maxTeamSize = $results['somaxteamsize'];
@ -265,17 +280,17 @@
echo "max_size=" . $results['default_max_team_size'] . ';'; echo "max_size=" . $results['default_max_team_size'] . ';';
*/ */
echo '</script>'; echo '</script>';
}else{ }else{
send_header("Event Registration", send_header("Event Registration",
array('School Home' => 'schoolaccess.php'), array('School Home' => 'schoolaccess.php'),
"events_scheduling" ); "events_scheduling" );
echo "<br />"; echo "<br />";
?> ?>
<script type="text/javascript" src="js/listSelector.js"></script> <script type="text/javascript" src="js/listSelector.js"></script>
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function() { $(document).ready(function() {
$(".date").datepicker({ dateFormat: 'yy-mm-dd' }); $(".date").datepicker({ dateFormat: 'yy-mm-dd' });
changeDate(); changeDate();
@ -295,9 +310,9 @@
placeEvents(); placeEvents();
} }
); );
}); });
function handleTeamClick(event, regId, eventId){ function handleTeamClick(event, regId, eventId){
var e = event || window.event; var e = event || window.event;
e.stopPropagation(); e.stopPropagation();
params={ params={
@ -374,10 +389,10 @@
} }
}); });
}); });
} }
// save the selected team members for the selected team in the selected event // save the selected team members for the selected team in the selected event
function saveTeamList(ddiv, regId, eventId){ function saveTeamList(ddiv, regId, eventId){
selectedList = selector.getLeftList(); selectedList = selector.getLeftList();
params = { params = {
'regId' : regId, 'regId' : regId,
@ -393,39 +408,39 @@
ddiv.dialog("close"); ddiv.dialog("close");
}); });
} }
function changeDate() { function changeDate() {
$("#schedulediv").load("schoolschedule.php?action=loadschedule",{date:$("#date").val()},function() { $("#schedulediv").load("schoolschedule.php?action=loadschedule",{date:$("#date").val()},function() {
placeEvents(); placeEvents();
}); });
} }
function placeEvents() { function placeEvents() {
$('.scheduleevent').each(function(idx,item) { $('.scheduleevent').each(function(idx,item) {
var eventid=item.id.substr(6); var eventid=item.id.substr(6);
var eventobj=eventdivs[eventid]; var eventobj=eventdivs[eventid];
var tablecellid=eventobj.hour+'_'+eventobj.minute+'_'+eventobj.location; var tablecellid=eventobj.hour+'_'+eventobj.minute+'_'+eventobj.location;
if($("#"+tablecellid).length) { if($("#"+tablecellid).length) {
var eheight=((eventobj.duration/15)*<?=$ROWHEIGHT?>)-<? echo $BORDERSIZE*2; ?>; var eheight=((eventobj.duration/15)*<?=$ROWHEIGHT?>)-<? echo $BORDERSIZE*2; ?>;
var ewidth=$("#"+tablecellid).width()-<? echo $BORDERSIZE; ?>; var ewidth=$("#"+tablecellid).width()-<? echo $BORDERSIZE; ?>;
var p=$("#"+tablecellid).offset(); var p=$("#"+tablecellid).offset();
$("#"+item.id).css(p); $("#"+item.id).css(p);
$("#"+item.id).show(); $("#"+item.id).show();
$("#"+item.id).height(eheight); $("#"+item.id).height(eheight);
$("#"+item.id).width(ewidth); $("#"+item.id).width(ewidth);
} }
else { else {
$("#"+item.id).hide(); $("#"+item.id).hide();
} }
} }
); );
} }
var scheduleId; var scheduleId;
function viewEvent(id) { function viewEvent(id) {
scheduleId=id; scheduleId=id;
var eventobj=eventdivs[id]; var eventobj=eventdivs[id];
@ -445,48 +460,47 @@
} }
} }
function saveEvent() { function saveEvent() {
var o=$("#edit_event_form").serializeArray(); var o=$("#edit_event_form").serializeArray();
//alert(o); //alert(o);
$("#debug").load("schoolschedule.php?action=saveevent",o,function() { $("#debug").load("schoolschedule.php?action=saveevent",o,function() {
changeDate(); changeDate();
}); });
} }
</script> </script>
<? <?
$q=mysql_query("SELECT DISTINCT(date) AS date FROM schedule WHERE conferences_id='{$conference['id']}' ORDER BY date"); $q=mysql_query("SELECT DISTINCT(date) AS date FROM schedule WHERE conferences_id='{$conference['id']}' ORDER BY date");
$dates=array(); $dates=array();
while($r=mysql_fetch_object($q)) { while($r=mysql_fetch_object($q)) {
$dates[]=$r->date; $dates[]=$r->date;
} }
if(count($dates)) if(count($dates))
$editdate=$dates[0]; $editdate=$dates[0];
else else
list($editdate,$bla)=explode(" ",$config['dates']['fairdate']); list($editdate,$bla)=explode(" ",$config['dates']['fairdate']);
echo "<form method=\"post\">\n"; echo "<form method=\"post\">\n";
echo i18n("Schedule date").": "; echo i18n("Schedule date").": ";
echo "<select id=\"date\" name=\"date\" onchange=\"changeDate()\">"; echo "<select id=\"date\" name=\"date\" onchange=\"changeDate()\">";
foreach($dates AS $d) { foreach($dates AS $d) {
echo "<option value=\"$d\">".format_date($d)."</option>\n"; echo "<option value=\"$d\">".format_date($d)."</option>\n";
} }
echo "</select>"; echo "</select>";
echo "</form>\n"; echo "</form>\n";
?> ?>
<hr /> <hr />
<div id="schedulediv"> <div id="schedulediv">
</div> </div>
<div id="event_dialog"> <div id="event_dialog">
<? include "schoolschedule_event_dialog.php"; ?> <? include "schoolschedule_event_dialog.php"; ?>
</div> </div>
<div id="selector_dialog"></div> <div id="selector_dialog"></div>
<? <?
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);