<?

function db_update_131_pre()
{
	global $config;
	$year = $config['FAIRYEAR'];
    //since there's only ever been award sponsors in the system, we can 
    //add a sponsorship entry with a value of the total sum of the prizes given
    //for each sponsor

    $q=$pdo->prepare("SELECT * FROM sponsors");
   $q->execute();
    while($r=$q->fetch(PDO::FETCH_OBJ)) {
        $total=0;
        $awardq=$pdo->prepare("SELECT * FROM award_awards WHERE sponsors_id='$r->id' AND year='$year'");
        $awardq->execute();
        while($awardr=$awardq->fetch(PDO::FETCH_OBJ)) {
            $prizeq=$pdo->prepare("SELECT cash,scholarship,value,number FROM award_prizes WHERE award_awards_id='$awardr->id'");
            $prizeq->execute();
            while($prizer=$prizeq->fetch(PDO::FETCH_OBJ)) {
                //some people never set the value for some reason, i dunno why.. 
                $realvalue=max($prizer->cash+$prizer->scholarship,$prizer->value);
                $totalvalue=$realvalue*$prizer->number;
                $total+=$totalvalue;
            }
        }
        echo "Creating sponsorship for ID: $r->id value: $total\n";
        $pdo->prepare("INSERT INTO sponsorships (sponsors_id,fundraising_type,value,status,probability,year) VALUES (
            '$r->id',
            'sfawards',
            '$total',
            'pending',
            '25',
            '$year')");
        $pdo->execute(0;)
        $stmt = $pdo->prepare("INSERT INTO sponsors_logs (sponsors_id,dt,users_id,log) VALUES ('$r->id',NOW(),0,'Automatically created sponsorship from existing sponsor. type=award, value=\$$total, status=pending, probability=25%')");
        $stmt->execute();
    }
	
}

?>
