science-ation/schoolteams.php

137 lines
5.1 KiB
PHP

<?php
require_once('common.inc.php');
require_once('user.inc.php');
require_once("so_teams.inc.php");
user_auth_required("teacher");
$schoolid=user_field_required("schools_id","user_edit.php?tab=school");
if($conference['type'] == 'scienceolympics'){
switch($_GET['action']){
case 'add':
$success = so_team_add($schoolid, $conference['id'], $_POST['teamname']);
echo mysql_Error();
draw_page();
break;
case 'save':
$success = false;
$teamId = intval($_POST['teamId']);
$teamName = mysql_real_escape_string($_POST['teamname']);
// a quick check to make sure the team being updated does indeed belong
// to this school
$query = 'SELECT COUNT(*) AS tally FROM so_teams WHERE schools_id=' . $schoolid . ' AND id=' . $teamId;
$testResults = mysql_fetch_array(mysql_query($query));
if($testResults['tally'] == 1){
// ok, the team belongs to the school that this session belongs to. We can
// can go ahead and save the changes.
$success=so_team_edit($schoolid,$teamId,$teamName);
}
if($success){
happy_("Team successfully updated");
echo("<script type=\"text/javascript\">newname = '$teamName';</script>");
}else{
error_("Unable to update record");
echo("<script type=\"text/javascript\">newname = null;</script>");
}
break;
case 'delete':
$success = false;
$teamId = mysql_real_escape_string($_POST['teamId']);
$teamName = mysql_real_escape_string($_POST['teamname']);
// a quick check to make sure the team being updated does indeed belong
// to this school
$query = 'SELECT COUNT(*) AS tally FROM so_teams WHERE schools_id=' . $schoolid . ' AND id=' . $teamId;
$testResults = mysql_fetch_array(mysql_query($query));
if($testResults['tally'] == 1){
// ok, the team belongs to the school that this session belongs to. We can
// can go ahead and save the changes.
$success=so_team_delete($schoolid,$teamId);
}
if($success){
happy_("Team successfully deleted");
echo("<script type=\"text/javascript\">deleteId = '$teamId';</script>");
}else{
error_("Unable to delete record");
echo("<script type=\"text/javascript\">deleteId = null;</script>");
}
break;
default:
draw_page();
}
}else{
header('Location: schoolaccess.php');
}
function draw_page(){
global $conference;
global $schoolid;
$title = i18n("Manage Teams");
send_header($title, array("Teacher" => "schoolaccess.php"));
?>
<script type="text/javascript">
$(document).ready(function() {
$('#teamaccordion').accordion();
$('#teamaccordion').css('visibility', 'visible');
});
function delete_team(teamId){
var data = $("#editTeam_" + teamId).serializeArray();
$("#debug").load("schoolteams.php?action=delete", data, function(){
$('#teamHeader_' + teamId).remove();
$('#team_' + teamId).remove();
});
}
function saveData(teamId){
var data = $("#editTeam_" + teamId).serializeArray();
$("#debug").load("schoolteams.php?action=save", data, function(){
if(newname != undefined){
newname = '<span class="ui-icon ui-icon-triangle-1-e"></span><a href="#" tabindex="-1">' + newname + '</a>';
$('#teamHeader_' + teamId).html(newname);
}
});
}
</script>
<?php
echo '<div id="teamaccordion" style="width:40em; visibility: hidden; ">';
$teamList = mysql_query("SELECT * FROM so_teams WHERE schools_id = " . $schoolid . " AND conferences_id = " . $conference['id']);
while($teamList && $team = mysql_fetch_array($teamList)){
echo '<h3 id="teamHeader_' . $team['id'] . '"><a href="#">' . $team['name'] . "</a></h3>\n";
echo '<div id="team_' . $team['id'] . '">' . "\n";
echo '<form id="editTeam_' . $team['id'] . '" onsubmit="return false;">';
echo i18n('name') . ': <input type="text" name="teamname" value="' . $team['name'] . '"></input>';
echo '<div style="height:1em;"></div>';
echo "<button onclick=\"saveData('" . $team['id'] . "');\">" . i18n('Save') . '</button>';
$record = mysql_fetch_array(mysql_query("SELECT count(*) AS regCount FROM schedule_registrations WHERE so_teams_id = " . $team['id']));
if($record['regCount'] == 0){
echo "<button onclick=\"delete_team('" . $team['id'] . "');\">" . i18n('Delete') . '</button>';
}else{
echo '<div style="font-size: 80%">';
echo '<a href="schoolschedule.php" style="color:#666">';
if($record['regCount'] > 1){
echo i18n("This team is currently registered for %1 events and can not be deleted.", array($record['regCount']));
}else{
echo i18n("This team is currently registered for an event and can not be deleted.");
}
echo '</a></div>';
}
echo "<input type=\"hidden\" name=\"teamId\" value=\"" . $team['id'] . "\"></input>";
echo '</form>';
echo "</div>";
}
echo '<h3><a href="#">' . i18n("New Team") . '</a></h3>';
echo '<div id="newTeam">';
echo '<form method="POST" action="schoolteams.php?action=add">';
echo '<label>' . i18n('Name') . '</label><input type="text" style="width:100%" name="teamname"></input>' . "\n";
echo '<div style="height:1em;"></div>';
echo '<input type="submit" value="' . i18n("Save") . '"></input>';
echo '</form>';
echo "</div>\n";
echo '</div>';
send_footer();
}
?>