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";
require_once("account.inc.php");
require_once("user.inc.php");
require_once("schedule.inc.php");
if($_SERVER['HTTPS']!="on") {
$ret['status']="error";
@ -37,6 +38,61 @@ $request=explode("/",$_GET['request']);
$ret=array();
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":
/* APIDOC: conferences/switch
description(switches the active conference)

View File

@ -44,5 +44,45 @@ function getNumRegistrations($scheduleid) {
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,
but WITHOUT ANY WARRANTY; without even the implied warranty of
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
along with this program; see the file COPYING. If not, write to
@ -22,50 +22,56 @@
*/
?>
<?
require("common.inc.php");
require_once("schedule.inc.php");
require_once("user.inc.php");
require("common.inc.php");
require_once("schedule.inc.php");
require_once("user.inc.php");
user_auth_required("teacher");
$schoolid=user_field_required("schools_id","user_edit.php?tab=school");
user_auth_required("teacher");
$schoolid=user_field_required("schools_id","user_edit.php?tab=school");
$q=mysql_query("SELECT * FROM schools WHERE id='".$schoolid."'");
echo mysql_error();
$school=mysql_fetch_object($q);
if($school) {
$q=mysql_query("SELECT * FROM schools WHERE id='".$schoolid."'");
echo mysql_error();
$school=mysql_fetch_object($q);
if(!$school) exit;
if($_GET['action']=="loadschedule") {
$date=$_POST['date'];
//cant change this
$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);
if($_GET['action']=="loadschedule") {
$date = $_POST['date'];
if(!eregi("[0-9]{4}-[0-9]{2}-[0-9]{2}",$date)) {
echo "Invalid date";
exit;
}
$starthour=$parameters->minhour;
$endhour=$parameters->maxfinish;
$eventList = getEventList($conference['id'], $school->id);
//do some sanity checks
if($starthour<0 || $starthour>24) $starthour=8;
if($endhour<$starthour)
$endhour=$starthour+10;
if($endhour<0 || $endhour>24) $endhour=15;
// get the minimum and maximum hours from the schedule
$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;
}
}
//minute increment
$increment=15;
//do some sanity checks
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)) {
echo "Invalid date";
exit;
}
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");
while($r=mysql_fetch_object($q)) {
$locations[$r->id]=$r->name;
}
if(!count($locations)) {
echo error(i18n("There are no locations defined."));
exit;
}
//minute increment
$increment=15;
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");
while($r=mysql_fetch_object($q)) {
$locations[$r->id]=$r->name;
}
if(!count($locations)) {
echo error(i18n("There are no locations defined."));
exit;
}
echo "<table class=\"schedule\" id=\"schedule\">\n";
echo "<tr>";
@ -93,69 +99,74 @@
$js="var eventdivs=new Array();\n";
//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
$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}'");
echo mysql_error();
while($r=mysql_fetch_object($q)) {
echo "<div class=\"scheduleevent scheduleevent_{$r->eventtype}\" id=\"event_{$r->id}\" onclick=\"viewEvent($r->id)\">";
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(is_array($eventList)){
foreach($eventList as $event){
// skip this event if it's not on the day we're looking at
if($event['date'] != $date) continue;
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 i18n("Capacity").": ";
$regteams=getNumRegistrations($r->id);
$maxteams=$r->somaxteams;
echo i18n("%1 of %2",array($regteams,$maxteams))."<br />";
$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='{$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 format_time($starttime);
echo " to ";
echo format_time($endtime);
if($eventType == "scienceolympic") {
echo "<br />";
echo i18n("Capacity").": ";
$regteams = getNumRegistrations($eid);
$maxteams= $event['somaxteams'];
echo i18n("%1 of %2", array($regteams, $maxteams)) . "<br />";
foreach($event['registrations'] as $regr){
echo "<div style=\"padding: 2px; border: 1px solid black; background-color: yellow;\" onclick=\"handleTeamClick(event, {$regr['id']}, $eid);\">";
echo $regr['name'];
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";
$x++;
echo "</div>";
$js .= "eventdivs[$eid]={hour:$hour,minute:$minute,location:$location,duration:$duration,title:'".rawurlencode($title)."',eventtype:'$eventType'};\n";
}
}else{
// it's actually an error message
echo $eventList;
}
echo "<script type=\"text/javascript\">\n";
echo $js;
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);
// get a list of all students that could be put on this team
$results = mysql_query("SELECT * FROM users_student
JOIN users ON users_student.users_id = users.uid
JOIN users_conferences_link ucl ON ucl.users_uid = users_student.users_id
WHERE schools_id = $schoolid
AND ucl.conferences_id={$conference['id']}
AND users.deleted = 'no'");
$query =
"SELECT * FROM users"
. " JOIN user_roles ON user_roles.users_id = users.id"
. " JOIN roles ON roles.id = user_roles.roles_id"
. " WHERE schools_id = $schoolid"
. " AND conferences_id = {$conference['id']}"
. " AND deleted = 'no'"
. " AND roles.type = 'student'";
$availList = array();
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
@ -173,9 +184,9 @@
$query .= implode(',', $valueSet);
$results = mysql_query($query);
}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
if(!array_key_exists('regId', $_POST)){
}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
if(!array_key_exists('regId', $_POST)){
echo "error\n";
return;
}
@ -193,16 +204,19 @@
$teamName = $record['name'];
// 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 .= ' JOIN users_conferences_link ucl ON ucl.users_uid = users_student.users_id';
$query .= ' WHERE schools_id = ' . $schoolid;
$query .= ' AND ucl.conferences_id=' . $conference['id'];
$query .= ' AND users.deleted = "no"';
$query =
"SELECT * FROM users"
. " JOIN user_roles ON user_roles.users_id = users.id"
. " JOIN roles ON roles.id = user_roles.roles_id"
. " WHERE schools_id = $schoolid"
. " AND conferences_id = {$conference['id']}"
. " AND deleted = 'no'"
. " AND roles.type = 'student'";
$results = mysql_query($query);
$availList = array();
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
@ -210,6 +224,7 @@
$query .= " WHERE users_uid IN (";
$query .= implode(',', array_keys($availList)) . ')';
$query .= " AND schedule_registrations_id = " . $_POST['regId'];
echo "\n[[[ $query ]]]\n";
$results = mysql_query($query);
$selectedList = array();
if($results){
@ -222,8 +237,8 @@
// get the necessary data about the event itself
$query = "SELECT sominteamsize, somaxteamsize, title FROM schedule JOIN"
. " schedule_registrations sr ON sr.schedule_id = schedule.id WHERE"
. " sr.id = " . $_POST['regId'];
. " schedule_registrations sr ON sr.schedule_id = schedule.id WHERE"
. " sr.id = " . $_POST['regId'];
$results = mysql_fetch_array(mysql_query($query));
$minTeamSize = $results['sominteamsize'];
$maxTeamSize = $results['somaxteamsize'];
@ -265,17 +280,17 @@
echo "max_size=" . $results['default_max_team_size'] . ';';
*/
echo '</script>';
}else{
}else{
send_header("Event Registration",
send_header("Event Registration",
array('School Home' => 'schoolaccess.php'),
"events_scheduling" );
echo "<br />";
?>
<script type="text/javascript" src="js/listSelector.js"></script>
<script type="text/javascript">
echo "<br />";
?>
<script type="text/javascript" src="js/listSelector.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(document).ready(function() {
$(".date").datepicker({ dateFormat: 'yy-mm-dd' });
changeDate();
@ -295,9 +310,9 @@
placeEvents();
}
);
});
});
function handleTeamClick(event, regId, eventId){
function handleTeamClick(event, regId, eventId){
var e = event || window.event;
e.stopPropagation();
params={
@ -374,10 +389,10 @@
}
});
});
}
}
// save the selected team members for the selected team in the selected event
function saveTeamList(ddiv, regId, eventId){
// save the selected team members for the selected team in the selected event
function saveTeamList(ddiv, regId, eventId){
selectedList = selector.getLeftList();
params = {
'regId' : regId,
@ -393,39 +408,39 @@
ddiv.dialog("close");
});
}
}
function changeDate() {
$("#schedulediv").load("schoolschedule.php?action=loadschedule",{date:$("#date").val()},function() {
placeEvents();
});
}
function changeDate() {
$("#schedulediv").load("schoolschedule.php?action=loadschedule",{date:$("#date").val()},function() {
placeEvents();
});
}
function placeEvents() {
$('.scheduleevent').each(function(idx,item) {
var eventid=item.id.substr(6);
var eventobj=eventdivs[eventid];
function placeEvents() {
$('.scheduleevent').each(function(idx,item) {
var eventid=item.id.substr(6);
var eventobj=eventdivs[eventid];
var tablecellid=eventobj.hour+'_'+eventobj.minute+'_'+eventobj.location;
var tablecellid=eventobj.hour+'_'+eventobj.minute+'_'+eventobj.location;
if($("#"+tablecellid).length) {
var eheight=((eventobj.duration/15)*<?=$ROWHEIGHT?>)-<? echo $BORDERSIZE*2; ?>;
var ewidth=$("#"+tablecellid).width()-<? echo $BORDERSIZE; ?>;
var p=$("#"+tablecellid).offset();
$("#"+item.id).css(p);
$("#"+item.id).show();
$("#"+item.id).height(eheight);
$("#"+item.id).width(ewidth);
}
else {
$("#"+item.id).hide();
}
}
);
}
if($("#"+tablecellid).length) {
var eheight=((eventobj.duration/15)*<?=$ROWHEIGHT?>)-<? echo $BORDERSIZE*2; ?>;
var ewidth=$("#"+tablecellid).width()-<? echo $BORDERSIZE; ?>;
var p=$("#"+tablecellid).offset();
$("#"+item.id).css(p);
$("#"+item.id).show();
$("#"+item.id).height(eheight);
$("#"+item.id).width(ewidth);
}
else {
$("#"+item.id).hide();
}
}
);
}
var scheduleId;
function viewEvent(id) {
var scheduleId;
function viewEvent(id) {
scheduleId=id;
var eventobj=eventdivs[id];
@ -445,48 +460,47 @@
}
}
}
function saveEvent() {
var o=$("#edit_event_form").serializeArray();
//alert(o);
$("#debug").load("schoolschedule.php?action=saveevent",o,function() {
changeDate();
});
}
</script>
function saveEvent() {
var o=$("#edit_event_form").serializeArray();
//alert(o);
$("#debug").load("schoolschedule.php?action=saveevent",o,function() {
changeDate();
});
}
</script>
<?
$q=mysql_query("SELECT DISTINCT(date) AS date FROM schedule WHERE conferences_id='{$conference['id']}' ORDER BY date");
$dates=array();
while($r=mysql_fetch_object($q)) {
<?
$q=mysql_query("SELECT DISTINCT(date) AS date FROM schedule WHERE conferences_id='{$conference['id']}' ORDER BY date");
$dates=array();
while($r=mysql_fetch_object($q)) {
$dates[]=$r->date;
}
if(count($dates))
$editdate=$dates[0];
else
list($editdate,$bla)=explode(" ",$config['dates']['fairdate']);
}
if(count($dates))
$editdate=$dates[0];
else
list($editdate,$bla)=explode(" ",$config['dates']['fairdate']);
echo "<form method=\"post\">\n";
echo i18n("Schedule date").": ";
echo "<select id=\"date\" name=\"date\" onchange=\"changeDate()\">";
foreach($dates AS $d) {
echo "<option value=\"$d\">".format_date($d)."</option>\n";
}
echo "</select>";
echo "</form>\n";
?>
<hr />
<div id="schedulediv">
</div>
<div id="event_dialog">
echo "<form method=\"post\">\n";
echo i18n("Schedule date").": ";
echo "<select id=\"date\" name=\"date\" onchange=\"changeDate()\">";
foreach($dates AS $d) {
echo "<option value=\"$d\">".format_date($d)."</option>\n";
}
echo "</select>";
echo "</form>\n";
?>
<hr />
<div id="schedulediv">
</div>
<div id="event_dialog">
<? include "schoolschedule_event_dialog.php"; ?>
</div>
<div id="selector_dialog"></div>
<?
</div>
<div id="selector_dialog"></div>
<?
send_footer();
send_footer();
}
}
?>

View File

@ -57,6 +57,13 @@ Date List
<input type="submit" value="Edit Team">
</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>
<?
echo json_encode($_SESSION);