diff --git a/admin/project_editor.php b/admin/project_editor.php
index 926bfc1..6db675c 100644
--- a/admin/project_editor.php
+++ b/admin/project_editor.php
@@ -42,10 +42,17 @@
if($_POST['action']=="genprojnum") {
- mysql_query("UPDATE projects SET projectnumber=NULL WHERE id='{$_POST['id']}'");
- $pn = generateProjectNumber($registration_id);
+ $id = intval($_POST['id']);
+ mysql_query("UPDATE projects SET projectnumber=NULL,projectsort=NULL,
+ projectnumber_seq='0',projectsort_seq='0'
+ WHERE id='$id'");
+ echo mysql_error();
+ list($pn,$ps,$pns,$pss) = generateProjectNumber($registration_id);
// print("Generated Project Number [$pn]");
- mysql_query("UPDATE projects SET projectnumber='$pn' WHERE id='{$_POST['id']}'");
+ mysql_query("UPDATE projects SET projectnumber='$pn',projectsort='$ps',
+ projectnumber_seq='$pns',projectsort_seq='$pss'
+ WHERE id='$id'");
+ echo mysql_error();
}
if($_POST['action']=="save")
diff --git a/admin/registration_receivedforms.php b/admin/registration_receivedforms.php
index 53d0024..a249c0b 100644
--- a/admin/registration_receivedforms.php
+++ b/admin/registration_receivedforms.php
@@ -211,8 +211,10 @@ echo mysql_Error();
if($projectnum == null)
{
- $projectnumber = generateProjectNumber($reg_id);
- mysql_query("UPDATE projects SET projectnumber='$projectnumber' WHERE registrations_id='$reg_id' AND year='{$config['FAIRYEAR']}'");
+ list($projectnumber,$ps,$pns,$pss) = generateProjectNumber($reg_id);
+ mysql_query("UPDATE projects SET projectnumber='$projectnumber',
+ projectsort='$ps',projectnumber_seq='$pns',projectsort_seq='$pss'
+ WHERE registrations_id='$reg_id' AND year='{$config['FAIRYEAR']}'");
echo happy(i18n("Assigned Project Number: %1",array($projectnumber)));
}
else
diff --git a/admin/reports_editor.php b/admin/reports_editor.php
index 591dfe2..30b34b6 100644
--- a/admin/reports_editor.php
+++ b/admin/reports_editor.php
@@ -307,7 +307,7 @@ function reportReload()
//only go through the columns if there are columns to go through
if(count($report['col'])) {
foreach($report['col'] as $o=>$d) {
- echo "
Column ".($x + 1).": | ";
+ echo "
Column ".($x + 1).": | ";
echo "";
if(intval($x) != intval($o)) {
echo ("WARNING, out of order!");
@@ -318,7 +318,7 @@ function reportReload()
}
}
for(;$x<$n_columns;$x++) {
- echo " |
Column ".($x + 1).": | ";
+ echo "
Column ".($x + 1).": | ";
echo "";
field_selector("col[$x][field]", "col$x", '');
echo " |
";
diff --git a/admin/reports_students.inc.php b/admin/reports_students.inc.php
index ca5dcbc..dc9c6bc 100644
--- a/admin/reports_students.inc.php
+++ b/admin/reports_students.inc.php
@@ -31,7 +31,8 @@ $report_students_fields = array(
'name' => 'Project Number',
'header' => '#',
'width' => 0.5,
- 'table' => 'CAST(projects.projectnumber AS UNSIGNED)' ),
+ 'table' => 'projects.projectnumber',
+ 'table_sort' => 'projects.projectsort'),
'last_name' => array(
'name' => 'Student -- Last Name',
@@ -489,14 +490,13 @@ $report_students_fields = array(
'header' => '',
'width' => 0.1,
'table' => "CONCAT(' ')"),
-
- 'gvrsf_tn' => array (
+/* 'gvrsf_tn' => array (
'name' => 'GVRSF Project Number (Table Number)',
'header' => '#',
'width' => 0.6,
'table' => "CAST(projects.projectnumber AS UNSIGNED) AS GVRSFTBL, CONCAT(projectcategories.category_shortform, ' ', projects.projectnumber, ' ', projectdivisions.division_shortform)",
'table_sort' => 'GVRSFTBL'),
-
+*/
'easyparse_allnames' => array(
'name' => "Easy Parse -- All Student Names (REQUIRES MYSQL 5.0) ",
'header' => 'Student(s)',
diff --git a/db/db.code.version.txt b/db/db.code.version.txt
index 7fe4e49..cd5b025 100644
--- a/db/db.code.version.txt
+++ b/db/db.code.version.txt
@@ -1 +1 @@
-91
+92
diff --git a/projects.inc.php b/projects.inc.php
index e440b74..15546dd 100644
--- a/projects.inc.php
+++ b/projects.inc.php
@@ -47,7 +47,7 @@ function getProjectsEligibleForAward($award_id)
AND projects.projectnumber is not null
AND projects.year='".$config['FAIRYEAR']."'
ORDER BY
- projectnumber
+ projectsort
");
$projects=array();
while($prjr=mysql_fetch_object($prjq))
@@ -206,7 +206,7 @@ function getProjectsNominatedForSpecialAward($award_id)
AND projects.projectnumber is not null
AND projects.year='".$config['FAIRYEAR']."'
ORDER BY
- projectnumber
+ projectsort
");
$projects=array();
while($prjr=mysql_fetch_object($prjq))
@@ -251,7 +251,7 @@ function getSpecialAwardsNominatedByRegistrationID($id)
AND projects.projectnumber is not null
AND projects.year='".$config['FAIRYEAR']."'
ORDER BY
- projectnumber
+ projectsort
");
$projects=array();
while($prjr=mysql_fetch_object($prjq))
diff --git a/register_participants.inc.php b/register_participants.inc.php
index 1de351a..e6fae17 100644
--- a/register_participants.inc.php
+++ b/register_participants.inc.php
@@ -319,49 +319,106 @@ function generateProjectNumber($registration_id)
echo mysql_error();
$r=mysql_fetch_object($q);
- $projectnumber=$config['project_num_format'];
+ $p=array('number'=>array(), 'sort'=>array() );
+ $p['number']['str'] = $config['project_num_format'];
+ $p['sort']['str'] = trim($config['project_sort_format']);
- //first replace the division and category
- $projectnumber=str_replace('D',$r->projectdivisions_id,$projectnumber);
- $projectnumber=str_replace('C',$r->projectcategories_id,$projectnumber);
- $projectnumber=str_replace('d',$r->division_shortform,$projectnumber);
- $projectnumber=str_replace('c',$r->category_shortform,$projectnumber);
+ if($p['sort']['str'] == '') $p['sort']['str'] = $p['number']['str'];
- //now change the N to a % so we can use it as a wildcard
- $querynum=str_replace('N','%',$projectnumber);
- $searchq=mysql_query("SELECT projectnumber FROM projects WHERE year='".$config['FAIRYEAR']."' AND projectnumber LIKE '$querynum'");
- if(mysql_num_rows($searchq))
- {
- //first, put them all in an array
- $proj_nums=array();
- while($searchr=mysql_fetch_object($searchq))
- {
- $proj_nums[]=$searchr->projectnumber;
+ /* Replace each letter with {letter}, so that we can do additional
+ * replacements below, without risking subsituting in a letter that may
+ * get replaced. */
+ foreach(array('number','sort') as $x) {
+ $p[$x]['str']=ereg_replace('[CcDd]', '{\\0}', $p[$x]['str']);
+ $p[$x]['str']=ereg_replace('(N|X)([0-9])?', '{\\0}', $p[$x]['str']);
+ }
+
+ /* Do some replacements that we don' thave to do anything fancy with,
+ * and setup some variables for future queries */
+ foreach(array('number','sort') as $x) {
+ $p[$x]['str']=str_replace('{D}',$r->projectdivisions_id,$p[$x]['str']);
+ $p[$x]['str']=str_replace('{C}',$r->projectcategories_id,$p[$x]['str']);
+ $p[$x]['str']=str_replace('{d}',$r->division_shortform,$p[$x]['str']);
+ $p[$x]['str']=str_replace('{c}',$r->category_shortform,$p[$x]['str']);
+ $p[$x]['n_used'] = array();
+ $p[$x]['x_used'] = array();
+ }
+
+ /* Build a total list of projects for finding a global number, and
+ * while constructing the list, build a list for the division/cat
+ * sequence number */
+ $q = mysql_query("SELECT projectnumber_seq,projectsort_seq,
+ projectdivisions_id,projectcategories_id
+ FROM projects
+ WHERE year='{$config['FAIRYEAR']}'
+ AND projectnumber_seq!='0'
+ AND projectnumber IS NOT NULL");
+ echo mysql_error();
+ while($i = mysql_fetch_object($q)) {
+ if( ($r->projectdivisions_id == $i->projectdivisions_id)
+ &&($r->projectcategories_id == $i->projectcategories_id) ) {
+ $p['number']['n_used'][] = $i->projectnumber_seq;
+ $p['sort']['n_used'][] = $i->projectsort_seq;
}
- //we will eventually find a good number, so lets loop forever until we find a good one
- $testnum=1;
- $Nnum=1;
- $ok=false;
- do
- {
- $Nnum=sprintf("%02d",$testnum);
- $test_projectnumber=str_replace('N',$Nnum,$projectnumber);
- if(!in_array($test_projectnumber,$proj_nums))
- $ok=true;
- $testnum++;
- }while(!$ok);
-
-
+ $p['number']['x_used'][] = $i->projectnumber_seq;
+ $p['sort']['x_used'][] = $i->projectsort_seq;
}
- else
- {
- $Nnum="01";
+
+ /* We only support one N or X to keep things simple, find which
+ * one we need and how much to pad it */
+ foreach(array('number','sort') as $x) {
+ if(ereg("(N|X)([0-9])?", $p[$x]['str'], $regs)) {
+ $p[$x]['seq_type'] = $regs[1];
+ if($regs[2] != '')
+ $p[$x]['seq_pad'] = $regs[2];
+ else
+ $p[$x]['seq_pad'] = ($regs[1] == 'N') ? 2 : 3;
+
+ if($regs[1] == 'N')
+ $p[$x]['used'] = $p[$x]['n_used'];
+ else
+ $p[$x]['used'] = $p[$x]['x_used'];
+ } else {
+ /* FIXME: maybe we should error here? Not having an N
+ * or an X in the projectnumber or projectsort is a bad
+ * thing */
+ $p[$x]['seq_type'] = '';
+ $p[$x]['seq_pad'] = 0;
+ $p[$x]['used'] = array();
+ }
}
- $projectnumber=str_replace('N',$Nnum,$projectnumber);
+ /* Find the lowest unused number. FIXME: this could be a config
+ * option, we could search for the lowest unused number (if projects
+ * get deleted), or we could just go +1 beyond the highest */
+ foreach(array('number','sort') as $x) {
+ if($p[$x]['seq_type'] == '') continue;
+ $n = 0;
+ while(1) {
+ $n++;
+ if(in_array($n, $p[$x]['used'])) continue;
- return $projectnumber;
+ $r = sprintf("%'0{$p[$x]['seq_pad']}d", $n);
+ $str = ereg_replace("{(N|X)([0-9])?}", $r, $p[$x]['str']);
+ $p[$x]['str'] = $str;
+ $p[$x]['n'] = $n;
+ break;
+ }
+
+ /* If we're using the same number type for sorting, then we, in
+ * theory, know what that number is, so we can go ahead and
+ * blindly use it */
+ if($p['number']['seq_type'] == $p['sort']['seq_type']) {
+ $r = sprintf("%'0{$p['sort']['seq_pad']}d", $n);
+ $p['sort']['str'] = ereg_replace("{(N|X)([0-9])?}", $r, $p['sort']['str']);
+ $p['sort']['n'] = $n;
+ break;
+ }
+ }
+
+ return array($p['number']['str'], $p['sort']['str'],
+ $p['number']['n'], $p['sort']['n']);
}
function computeRegistrationFee($regid)