Turn project_editor.php into ajax handlers

This commit is contained in:
dave 2010-01-21 20:32:57 +00:00
parent c463c13f01
commit b15ad9a7e0

View File

@ -22,46 +22,59 @@
*/ */
?> ?>
<? <?
require("../common.inc.php"); require_once('../common.inc.php');
require_once("../user.inc.php"); require_once('../user.inc.php');
require("../register_participants.inc.php"); require_once('../register_participants.inc.php');
user_auth_required('committee', 'admin'); user_auth_required('committee', 'admin');
//send the header $registrations_id=intval($_GET['id']);
send_header("Project Editor", $action = $_GET['action'];
array('Committee Main' => 'committee_main.php',
'Administration' => 'admin/index.php',
'Participant Registration' => 'admin/registration.php',
'Registration List and Statistics' => 'admin/registration_list.php')
);
echo "<br />"; switch($action) {
case 'project_load':
project_load();
break;
case 'project_regenerate_number':
/* Save first */
project_save();
if($_POST['registration_id']) $registration_id=$_POST['registration_id']; /* Now generate */
else if($_GET['registration_id']) $registration_id=$_GET['registration_id']; $q=mysql_query("SELECT id FROM projects WHERE registrations_id='{$registrations_id}' AND year='{$config['FAIRYEAR']}'");
$i=mysql_fetch_assoc($q);
$id = $i['id'];
if($_POST['action']=="genprojnum") {
$id = intval($_POST['id']);
mysql_query("UPDATE projects SET projectnumber=NULL,projectsort=NULL, mysql_query("UPDATE projects SET projectnumber=NULL,projectsort=NULL,
projectnumber_seq='0',projectsort_seq='0' projectnumber_seq='0',projectsort_seq='0'
WHERE id='$id'"); WHERE id='$id'");
echo mysql_error(); echo mysql_error();
list($pn,$ps,$pns,$pss) = generateProjectNumber($registration_id); list($pn,$ps,$pns,$pss) = generateProjectNumber($registrations_id);
// print("Generated Project Number [$pn]"); // print("Generated Project Number [$pn]");
mysql_query("UPDATE projects SET projectnumber='$pn',projectsort='$ps', mysql_query("UPDATE projects SET projectnumber='$pn',projectsort='$ps',
projectnumber_seq='$pns',projectsort_seq='$pss' projectnumber_seq='$pns',projectsort_seq='$pss'
WHERE id='$id'"); WHERE id='$id'");
echo mysql_error(); happy_("Generated and Saved Project Number: $pn");
break;
case 'project_save':
project_save();
break;
default:
break;
}
exit;
function project_save()
{
global $registrations_id, $config;
//first, lets make sure this project really does belong to them
$q=mysql_query("SELECT * FROM projects WHERE registrations_id='{$registrations_id}' AND year='{$config['FAIRYEAR']}'");
$projectinfo=mysql_fetch_object($q);
if(!projectinfo) {
echo error(i18n("Invalid project to update"));
} }
if($_POST['action']=="save")
{
// {
//first, lets make sure this project really does belong to them
$q=mysql_query("SELECT * FROM projects WHERE id='".$_POST['id']."' AND registrations_id='".$registration_id."' AND year='".$config['FAIRYEAR']."'");
if($projectinfo=mysql_fetch_object($q))
{
$summarywords=preg_split("/[\s,]+/",$_POST['summary']); $summarywords=preg_split("/[\s,]+/",$_POST['summary']);
$summarywordcount=count($summarywords); $summarywordcount=count($summarywords);
if($summarywordcount>$config['participant_project_summary_wordmax']) if($summarywordcount>$config['participant_project_summary_wordmax'])
@ -69,12 +82,10 @@
else else
$summarycountok=1; $summarycountok=1;
if($config['participant_project_title_charmax'] && strlen(stripslashes($_POST['title']))>$config['participant_project_title_charmax']) //0 for no limit, eg 255 database field limit if($config['participant_project_title_charmax'] && strlen(stripslashes($_POST['title']))>$config['participant_project_title_charmax']) { //0 for no limit, eg 255 database field limit
{
$title=substr(stripslashes($_POST['title']),0,$config['participant_project_title_charmax']); $title=substr(stripslashes($_POST['title']),0,$config['participant_project_title_charmax']);
echo error(i18n("Project title truncated to %1 characters",array($config['participant_project_title_charmax']))); error_("Project title truncated to %1 characters",array($config['participant_project_title_charmax']));
} } else
else
$title=stripslashes($_POST['title']); $title=stripslashes($_POST['title']);
mysql_query("UPDATE projects SET ". mysql_query("UPDATE projects SET ".
@ -89,67 +100,56 @@
"projectsort='".mysql_escape_string(stripslashes($_POST['projectsort']))."'". "projectsort='".mysql_escape_string(stripslashes($_POST['projectsort']))."'".
"WHERE id='".$_POST['id']."'"); "WHERE id='".$_POST['id']."'");
echo mysql_error(); echo mysql_error();
echo notice(i18n("Project information successfully updated")); happy_("Project information successfully updated");
//check if they changed the project number //check if they changed the project number
if($_POST['projectnumber']!=$projectinfo->projectnumber) if($_POST['projectnumber']!=$projectinfo->projectnumber) {
{
//check if hte new one is available //check if hte new one is available
$q=mysql_query("SELECT * FROM projects WHERE year='".$config['FAIRYEAR']."' AND projectnumber='".$_POST['projectnumber']."'"); $q=mysql_query("SELECT * FROM projects WHERE year='".$config['FAIRYEAR']."' AND projectnumber='".$_POST['projectnumber']."'");
if(mysql_num_rows($q)) if(mysql_num_rows($q)) {
{ error_("Could not change project number. %1 is already in use",array($_POST['projectnumber']));
echo error(i18n("Could not change project number. %1 is already in use",array($_POST['projectnumber']))); } else {
}
else
{
mysql_query("UPDATE projects SET mysql_query("UPDATE projects SET
projectnumber='".$_POST['projectnumber']."' projectnumber='".$_POST['projectnumber']."'
WHERE id='".$_POST['id']."'"); WHERE id='".$_POST['id']."'");
echo happy(i18n("Project number successfully changed to %1",array($_POST['projectnumber']))); happy_("Project number successfully changed to %1",array($_POST['projectnumber']));
} }
} }
} }
else
{
echo error(i18n("Invalid project to update"));
}
//}
}
function project_load()
{
global $registrations_id, $config;
//now lets find out their MAX grade, so we can pre-set the Age Category //now lets find out their MAX grade, so we can pre-set the Age Category
$q=mysql_query("SELECT MAX(grade) AS maxgrade FROM students WHERE registrations_id='".$registration_id."'"); $q=mysql_query("SELECT MAX(grade) AS maxgrade FROM students WHERE registrations_id='".$registrations_id."'");
$gradeinfo=mysql_fetch_object($q); $gradeinfo=mysql_fetch_object($q);
//now lets grab all the age categories, so we can choose one based on the max grade //now lets grab all the age categories, so we can choose one based on the max grade
$q=mysql_query("SELECT * FROM projectcategories WHERE year='".$config['FAIRYEAR']."' ORDER BY id"); $q=mysql_query("SELECT * FROM projectcategories WHERE year='".$config['FAIRYEAR']."' ORDER BY id");
while($r=mysql_fetch_object($q)) while($r=mysql_fetch_object($q)) {
{
//save these in an array, just incase we need them later (FIXME: remove this array if we dont need it) //save these in an array, just incase we need them later (FIXME: remove this array if we dont need it)
$agecategories[$r->id]['category']=$r->category; $agecategories[$r->id]['category']=$r->category;
$agecategories[$r->id]['mingrade']=$r->mingrade; $agecategories[$r->id]['mingrade']=$r->mingrade;
$agecategories[$r->id]['maxgrade']=$r->maxgrade; $agecategories[$r->id]['maxgrade']=$r->maxgrade;
if($gradeinfo->maxgrade >= $r->mingrade && $gradeinfo->maxgrade <= $r->maxgrade) if($gradeinfo->maxgrade >= $r->mingrade && $gradeinfo->maxgrade <= $r->maxgrade)
{
$projectcategories_id=$r->id; $projectcategories_id=$r->id;
} }
}
//now select their project info //now select their project info
$q=mysql_query("SELECT * FROM projects WHERE registrations_id='".$registration_id."' AND year='".$config['FAIRYEAR']."'"); $q=mysql_query("SELECT * FROM projects WHERE registrations_id='".$registrations_id."' AND year='".$config['FAIRYEAR']."'");
//check if it exists, if we didnt find any record, lets insert one //check if it exists, if we didnt find any record, lets insert one
$projectinfo=mysql_fetch_object($q); $projectinfo=mysql_fetch_object($q);
//make sure that if they changed their grade on the student page, we update their projectcategories_id accordingly //make sure that if they changed their grade on the student page, we update their projectcategories_id accordingly
if($projectcategories_id && $projectinfo->projectcategories_id!=$projectcategories_id) if($projectcategories_id && $projectinfo->projectcategories_id!=$projectcategories_id) {
{
echo notice(i18n("Age category changed, updating to %1",array($agecategories[$projectcategories_id]['category']))); echo notice(i18n("Age category changed, updating to %1",array($agecategories[$projectcategories_id]['category'])));
mysql_query("UPDATE projects SET projectcategories_id='$projectcategories_id' WHERE id='$projectinfo->id'"); mysql_query("UPDATE projects SET projectcategories_id='$projectcategories_id' WHERE id='$projectinfo->id'");
} }
//output the current status
//output the current status
?> ?>
<script language="javascript" type="text/javascript"> <script language="javascript" type="text/javascript">
@ -172,44 +172,55 @@ function countwords()
} }
</script> </script>
<? <?
if($projectinfo)
{
echo "<form name=\"projectform\" method=\"post\" action=\"project_editor.php\">\n"; if(!$projectinfo) {
echo "<input type=\"hidden\" name=\"action\" value=\"save\">\n"; echo error(i18n("Invalid project to edit"));
echo "<input type=\"hidden\" name=\"id\" value=\"$projectinfo->id\">\n"; exit;
echo "<input type=\"hidden\" name=\"registration_id\" value=\"$registration_id\">\n"; }
echo "<table>\n";
echo "<tr><td>".i18n("Project Title").": </td><td><input type=\"text\" name=\"title\" size=\"50\" value=\"".htmlspecialchars($projectinfo->title)."\" />".REQUIREDFIELD; ?>
<form id="project_form">
<input type="hidden" name="id" value="<?=$projectinfo->id?>">
<table>
<tr> <td><?=i18n("Project Title")?>: </td>
<td><input type="text" name="title" size="50" value="<?=htmlspecialchars($projectinfo->title)?>" /><?=REQUIREDFIELD?>
<?
if($config['participant_project_title_charmax']) if($config['participant_project_title_charmax'])
echo i18n("(Max %1 characters)",array($config['participant_project_title_charmax'])); echo i18n("(Max %1 characters)",array($config['participant_project_title_charmax']));
echo "</td></tr>\n"; ?>
echo "<tr><td>".i18n("Project Number").": </td><td><input type=\"text\" name=\"projectnumber\" size=\"10\" value=\"$projectinfo->projectnumber\" />"; </td>
echo "<tr><td>".i18n("Project Sort").": </td><td><input type=\"text\" name=\"projectsort\" size=\"10\" value=\"$projectinfo->projectsort\" />"; </tr><tr>
echo "<tr><td>".i18n("Age Category").": </td><td>"; <td><?=i18n("Project Number")?>: </td>
echo i18n($agecategories[$projectcategories_id]['category']); <td><input type="text" name="projectnumber" size="10" value="<?=$projectinfo->projectnumber?>" />
echo " (".i18n("Grades %1-%2",array($agecategories[$projectcategories_id]['mingrade'],$agecategories[$projectcategories_id]['maxgrade'])).")"; <input type="button" id="project_regenerate_number" value="<?=i18n("Re-Generate Project Number")?>" />
echo "</td></tr>"; </td>
echo "<tr><td>".i18n("Division").": </td><td>"; </tr><tr>
<td><?=i18n("Project Sort")?>: </td>
//###### Feature Specific - filtering divisions by category <td><input type="text" name="projectsort" size="10" value="<?=$projectinfo->projectsort?>" /></td>
</tr><tr>
<td><?=i18n("Age Category")?>: </td>
<td><?=i18n($agecategories[$projectcategories_id]['category'])?> (<?=i18n("Grades %1-%2",array($agecategories[$projectcategories_id]['mingrade'],$agecategories[$projectcategories_id]['maxgrade']))?>)</td>
</tr><tr>
<td><?=i18n("Division")?>: </td>
<td>
<?
//###### Feature Specific - filtering divisions by category
if($config['filterdivisionbycategory']=="yes"){ if($config['filterdivisionbycategory']=="yes"){
$q=mysql_query("SELECT projectdivisions.* FROM projectdivisions,projectcategoriesdivisions_link WHERE projectdivisions.id=projectdivisions_id AND projectcategories_id=".$projectcategories_id." AND projectdivisions.year='".$config['FAIRYEAR']."' AND projectcategoriesdivisions_link.year='".$config['FAIRYEAR']."' ORDER BY division"); $q=mysql_query("SELECT projectdivisions.* FROM projectdivisions,projectcategoriesdivisions_link WHERE projectdivisions.id=projectdivisions_id AND projectcategories_id=".$projectcategories_id." AND projectdivisions.year='".$config['FAIRYEAR']."' AND projectcategoriesdivisions_link.year='".$config['FAIRYEAR']."' ORDER BY division");
echo mysql_error(); echo mysql_error();
//### //###
}else } else
$q=mysql_query("SELECT * FROM projectdivisions WHERE year='".$config['FAIRYEAR']."' ORDER BY division"); $q=mysql_query("SELECT * FROM projectdivisions WHERE year='".$config['FAIRYEAR']."' ORDER BY division");
echo "<select name=\"projectdivisions_id\">"; echo "<select name=\"projectdivisions_id\">";
echo "<option value=\"\">".i18n("Select a division")."</option>\n"; echo "<option value=\"\">".i18n("Select a division")."</option>\n";
while($r=mysql_fetch_object($q)) while($r=mysql_fetch_object($q)) {
{
if($r->id == $projectinfo->projectdivisions_id) $sel="selected=\"selected\""; else $sel=""; if($r->id == $projectinfo->projectdivisions_id) $sel="selected=\"selected\""; else $sel="";
echo "<option $sel value=\"$r->id\">".htmlspecialchars(i18n($r->division))."</option>\n"; echo "<option $sel value=\"$r->id\">".htmlspecialchars(i18n($r->division))."</option>\n";
} }
echo "</select>".REQUIREDFIELD; echo "</select>".REQUIREDFIELD;
if($config['usedivisionselector']=="yes")
{ if($config['usedivisionselector']=="yes") {
?> ?>
<script language="javascript" type="text/javascript"> <script language="javascript" type="text/javascript">
@ -222,6 +233,7 @@ if($projectinfo)
</script> </script>
<? <?
} }
echo "<br />"; echo "<br />";
echo i18n("WARNING! If you change the division you must manually change the project number too! It will NOT be assigned a new number automatically"); echo i18n("WARNING! If you change the division you must manually change the project number too! It will NOT be assigned a new number automatically");
echo "</td></tr>"; echo "</td></tr>";
@ -234,8 +246,7 @@ if($projectinfo)
else else
$currentlang=$_SESSION['lang']; $currentlang=$_SESSION['lang'];
foreach($config['languages'] AS $key=>$val) foreach($config['languages'] AS $key=>$val) {
{
if($currentlang==$key) $selected="selected=\"selected\""; else $selected=""; if($currentlang==$key) $selected="selected=\"selected\""; else $selected="";
echo "<option $selected value=\"$key\">$val</option>"; echo "<option $selected value=\"$key\">$val</option>";
@ -246,13 +257,10 @@ if($projectinfo)
echo "<tr><td>".i18n("Requirements").": </td><td>"; echo "<tr><td>".i18n("Requirements").": </td><td>";
echo "<table>"; echo "<table>";
if($config['participant_project_table']=="no") if($config['participant_project_table']=="no") {
{
//if we arent asking them if they want a table or not, then we set it to 'yes' assuming everyone will get a table //if we arent asking them if they want a table or not, then we set it to 'yes' assuming everyone will get a table
echo " <input type=\"hidden\" name=\"req_table\" value=\"yes\" />"; echo " <input type=\"hidden\" name=\"req_table\" value=\"yes\" />";
} } else {
else
{
echo "<tr>"; echo "<tr>";
echo " <td>".i18n("Table").REQUIREDFIELD."</td>"; echo " <td>".i18n("Table").REQUIREDFIELD."</td>";
if($projectinfo->req_table=="yes") $check="checked=\"checked\""; else $check=""; if($projectinfo->req_table=="yes") $check="checked=\"checked\""; else $check="";
@ -302,28 +310,11 @@ if($projectinfo)
echo i18n("%1 words maximum",array($config['participant_project_summary_wordmax'])); echo i18n("%1 words maximum",array($config['participant_project_summary_wordmax']));
echo "</div>"; echo "</div>";
echo "</td></tr>"; ?>
</td></tr>
echo "</table>"; </table>
echo "<input type=\"submit\" value=\"".i18n("Save Project Information")."\" />\n"; <input type="button" id="project_save" value="<?=i18n("Save Project Information")?>" />
echo "</form>"; </form>
<?
}
echo ("<br /><br /><h3>".i18n("Other Actions")."</h3><br />");
echo "<form name=\"projectform\" method=\"post\" action=\"project_editor.php\">\n";
echo "<input type=\"hidden\" name=\"action\" value=\"genprojnum\">\n";
echo "<input type=\"hidden\" name=\"id\" value=\"$projectinfo->id\">\n";
echo "<input type=\"hidden\" name=\"registration_id\" value=\"$registration_id\">\n";
echo "<input type=\"submit\" value=\"".i18n("Re-Generate Project Number")."\" />\n";
echo "</form>";
}
else
{
echo error(i18n("Invalid project to edit"));
}
send_footer();
?> ?>