Make the roll() function able to determine all fields to roll, and

properly handle fields that could be NULL.  Use the roll() to roll
awards and prizes, both had potential NULL fields that were rolling to
'0', causing downloaded awards/prizes to be unrecognized after rollover.

This also means we can add fields and rollover will continue to work properly.
This commit is contained in:
dave 2010-04-16 09:43:26 +00:00
parent afc353bdb9
commit 29db8ac071

View File

@ -54,18 +54,52 @@
</script>
<?
function roll($currentfairyear, $newfairyear, $table, $fields)
function roll($currentfairyear, $newfairyear, $table, $where='', $replace=array())
{
$q=mysql_query("SELECT * FROM $table WHERE year='$currentfairyear'");
/* Field Type Null Key Default Extra
* id int(10) unsigned NO PRI NULL auto_increment
* sponsors_id int(10) unsigned NO MUL 0
* award_source_fairs_id int(10) unsigned YES NULL
*/
/* Get field list for this table */
$q = mysql_query("SHOW COLUMNS IN `$table`");
while(($c = mysql_fetch_assoc($q))) {
$col[$c['Field']] = $c;
}
/* Record fields we care about */
$fields = array();
$keys = array_keys($col);
foreach($keys as $k) {
/* Skip id field */
if($col[$k]['Extra'] == 'auto_increment') continue;
/* Skip year field */
if($k == 'year') continue;
$fields[] = $k;
}
if($where == '') $where='1';
/* Get data */
$q=mysql_query("SELECT * FROM $table WHERE year='$currentfairyear' AND $where");
echo mysql_error();
$names = '`'.join('`,`', $fields).'`';
/* Process data */
while($r=mysql_fetch_assoc($q)) {
$vals = '';
foreach($fields as $f) {
$vals .= ",'".mysql_real_escape_string($r[$f])."'";
if(array_key_exists($f, $replace))
$vals .= ",'".mysql_real_escape_string($replace[$f])."'";
else if($col[$f]['Null'] == 'YES' && $r[$f] == NULL)
$vals .= ',NULL';
else
$vals .= ",'".mysql_real_escape_string($r[$f])."'";
}
mysql_query("INSERT INTO $table(`year`,$names) VALUES ('$newfairyear'$vals)");
mysql_query("INSERT INTO `$table`(`year`,$names) VALUES ('$newfairyear'$vals)");
echo "INSERT INTO `$table`(`year`,$names) VALUES ('$newfairyear'$vals)<br/>";
echo mysql_error();
}
}
@ -75,6 +109,9 @@
$newfairyear=intval($_POST['nextfairyear']);
$currentfairyear=intval($config['FAIRYEAR']);
$cy = $currentfairyear;
$ny = $newfairyear;
if($newfairyear<$currentfairyear)
echo error(i18n("You cannot roll backwards in years!"));
else if($newfairyear==$currentfairyear)
@ -172,83 +209,23 @@
echo i18n("Rolling awards")."<br />";
//awards
$q=mysql_query("SELECT * FROM award_awards WHERE year='$currentfairyear'");
echo mysql_error();
while($r=mysql_fetch_object($q))
{
/* award_source_fairs_id could be NULL, we want to keep it NULL if it is */
if($r->award_sourcefairs_id == null)
$award_source_fairs_id="NULL";
else
$award_source_fairs_id="'".mysql_real_escape_string($r->award_source_fairs_id)."'";
mysql_query("INSERT INTO award_awards (sponsors_id,award_types_id,name,criteria,description,
presenter,`order`,year,excludefromac,cwsfaward,self_nominate,schedule_judges,external_identifier,
external_additional_materials,external_register_winners,
external_postback,award_source_fairs_id) VALUES (
'".mysql_real_escape_string($r->sponsors_id)."',
'".mysql_real_escape_string($r->award_types_id)."',
'".mysql_real_escape_string($r->name)."',
'".mysql_real_escape_string($r->criteria)."',
'".mysql_real_escape_string($r->description)."',
'".mysql_real_escape_string($r->presenter)."',
'".mysql_real_escape_string($r->order)."',
'".mysql_real_escape_string($newfairyear)."',
'".mysql_real_escape_string($r->excludefromac)."',
'".mysql_real_escape_string($r->cwsfaward)."',
'".mysql_real_escape_string($r->self_nominate)."',
'".mysql_real_escape_string($r->schedule_judges)."',
'".mysql_real_escape_string($r->external_identifier)."',
'".mysql_real_escape_string($r->external_additional_materials)."',
'".mysql_real_escape_string($r->external_register_winners)."',
'".mysql_real_escape_string($r->external_postback)."',
$award_source_fairs_id)");
while($r=mysql_fetch_object($q)) {
/* Roll the one award */
roll($cy, $ny, 'award_awards', "id='{$r->id}'");
$award_awards_id=mysql_insert_id();
$q2=mysql_query("SELECT * FROM award_awards_projectcategories WHERE year='$currentfairyear' AND award_awards_id='$r->id'");
echo mysql_error();
while($r2=mysql_fetch_object($q2))
{
mysql_query("INSERT INTO award_awards_projectcategories (award_awards_id,projectcategories_id,year) VALUES (
'".mysql_real_escape_string($award_awards_id)."',
'".mysql_real_escape_string($r2->projectcategories_id)."',
'".mysql_real_escape_string($newfairyear)."')");
}
$q2=mysql_query("SELECT * FROM award_awards_projectdivisions WHERE year='$currentfairyear' AND award_awards_id='$r->id'");
echo mysql_error();
while($r2=mysql_fetch_object($q2))
{
mysql_query("INSERT INTO award_awards_projectdivisions (award_awards_id,projectdivisions_id,year) VALUES (
'".mysql_real_escape_string($award_awards_id)."',
'".mysql_real_escape_string($r2->projectdivisions_id)."',
'".mysql_real_escape_string($newfairyear)."')");
}
roll($cy, $ny, 'award_awards_projectcategories', "award_awards_id='{$r->id}'",
array('award_awards_id' => $award_awards_id));
roll($cy, $ny, 'award_awards_projectdivisions', "award_awards_id='{$r->id}'",
array('award_awards_id' => $award_awards_id));
echo i18n("&nbsp; Rolling award prizes")."<br />";
$q2=mysql_query("SELECT * FROM award_prizes WHERE year='$currentfairyear' AND award_awards_id='$r->id'");
echo mysql_error();
while($r2=mysql_fetch_object($q2))
{
mysql_query("INSERT INTO award_prizes (award_awards_id,cash,scholarship,`value`,prize,number,`order`,year,excludefromac,trophystudentkeeper,trophystudentreturn,trophyschoolkeeper,trophyschoolreturn,external_identifier) VALUES (
'".mysql_real_escape_string($award_awards_id)."',
'".mysql_real_escape_string($r2->cash)."',
'".mysql_real_escape_string($r2->scholarship)."',
'".mysql_real_escape_string($r2->value)."',
'".mysql_real_escape_string($r2->prize)."',
'".mysql_real_escape_string($r2->number)."',
'".mysql_real_escape_string($r2->order)."',
'".mysql_real_escape_string($newfairyear)."',
'".mysql_real_escape_string($r2->excludefromac)."',
'".mysql_real_escape_string($r2->trophystudentkeeper)."',
'".mysql_real_escape_string($r2->trophystudentreturn)."',
'".mysql_real_escape_string($r2->trophyschoolkeeper)."',
'".mysql_real_escape_string($r2->trophyschoolreturn)."',
'".mysql_real_escape_string($r2->external_identifier)."'
)");
}
roll($cy, $ny, 'award_prizes', "award_awards_id='{$r->id}'",
array('award_awards_id' => $award_awards_id));
}
echo i18n("Rolling award types")."<br />";
@ -308,15 +285,13 @@
'".mysql_real_escape_string($r->required)."',
'".mysql_real_escape_string($r->ord)."')");
echo i18n("Rolling registration fee items")."<br />";
//regfee items
roll($currentfairyear, $newfairyear, 'regfee_items',
array('name','description','cost','per'));
echo i18n("Rolling registration fee items")."<br />";
roll($cy, $ny, 'regfee_items');
//volunteer positions
echo i18n('Rolling volunteer positions')."<br />";
roll($currentfairyear, $newfairyear, 'volunteer_positions',
array('name','desc','meet_place','start','end'));
roll($cy, $ny, 'volunteer_positions');
//timeslots and rounds
echo i18n('Rolling judging timeslots and rounds')."<br />";