<?
include_once '/home/krista02/vladsoft.tech/misoft/engine/config.php'; /// подключение к бд

	#Делаем выборку и обрабатываем один любой конкурс где дата завершения меньше за реальную дату, тоесть конкурс завершен
	$chKonkurs = mysql_query( "SELECT * FROM konkurs WHERE date_end < '". time() ."' LIMIT 1" );
    if ( mysql_num_rows( $chKonkurs ) > 0 ) {
        $chKonkursRow = mysql_fetch_array( $chKonkurs );
		
		#Определяем количиство призовых мест
		$i_limit = 3;
		if( $chKonkursRow['priz4'] > 0 ) { $i_limit = 4; }
		if( $chKonkursRow['priz5'] > 0 ) { $i_limit = 5; }
		if( $chKonkursRow['priz6'] > 0 ) { $i_limit = 6; }
		if( $chKonkursRow['priz7'] > 0 ) { $i_limit = 7; }
		if( $chKonkursRow['priz8'] > 0 ) { $i_limit = 8; }
		if( $chKonkursRow['priz9'] > 0 ) { $i_limit = 9; }
		if( $chKonkursRow['priz10'] > 0 ) { $i_limit = 10; }
        
        #Делаем выборку информации о победителях
		$chKonkursUser = mysql_query( "SELECT iduser, summ FROM konkurs_temp WHERE summ >= '" . $chKonkursRow['min_type'] . "' and id_konkurs = '" . $chKonkursRow['id'] . "' and type = '" . $chKonkursRow['type'] . "' ORDER BY summ DESC LIMIT " . $i_limit ."" );
        
        #Создаем цыкл с победителями и сумой их баллов
		while ( $chKonkursUserRow = mysql_fetch_array( $chKonkursUser ) ) {
            $array[] = $chKonkursUserRow['iduser'];
            $arraySumm[] = $chKonkursUserRow['summ'];
        }
        
		#Первое место
        if ( empty( $array[0] ) ) {
            $array[0] = '';
            $priz1 = 0;
        } else {
            if ( $chKonkursRow['allsumm'] ) {
                $priz1 = $arraySumm[0] * $chKonkursRow['priz1'];
            } else {
                $priz1 = $chKonkursRow['priz1'];
            }
            
            $prizType = 'money';
            
            mysql_query( "UPDATE user SET $prizType = $prizType + $priz1, earnings_competition = earnings_competition + $priz1 WHERE id = '" . $array[0] . "'" );
        }
		
		#Второе место
        if ( empty( $array[1] ) ) {
            $array[1] = '';
            $priz2 = 0;
        } else {
            if ( $chKonkursRow['allsumm'] ) {
                $priz2 = $arraySumm[1] * $chKonkursRow['priz2'];
            } else {
                $priz2 = $chKonkursRow['priz2'];
            }
            
            $prizType = 'money';
            
            mysql_query( "UPDATE user SET $prizType = $prizType + $priz2, earnings_competition = earnings_competition + $priz2 WHERE id = '" . $array[1] . "'" );
        }
		
		#Третье место
        if ( empty( $array[2] ) ) {
            $array[2] = '';
            $priz3 = 0;
        } else {
            if ( $chKonkursRow['allsumm'] ) {
                $priz3 = $arraySumm[2] * $chKonkursRow['priz3'];
            } else {
                $priz3 = $chKonkursRow['priz3'];
            }
            
            $prizType = 'money';
            
            mysql_query( "UPDATE user SET $prizType = $prizType + $priz3, earnings_competition = earnings_competition + $priz3 WHERE id = '" . $array[2] . "'" );
        }
		
		#Читвертое призовое место (опционально)
        if ( empty( $array[3] ) ) {
            $array[3] = '';
            $priz4 = 0;
        } else {
            if ( $chKonkursRow['allsumm'] ) {
                $priz4 = $arraySumm[3] * $chKonkursRow['priz4'];
            } else {
                $priz4 = $chKonkursRow['priz4'];
            }
            
            $prizType = 'money';
            
            mysql_query( "UPDATE user SET $prizType = $prizType + $priz4, earnings_competition = earnings_competition + $priz4 WHERE id = '" . $array[3] . "'" );
        }
		
		#Пятое призовое место (опционально)
        if ( empty( $array[4] ) ) {
            $array[4] = '';
            $priz5 = 0;
        } else {
            if ( $chKonkursRow['allsumm'] ) {
                $priz5 = $arraySumm[4] * $chKonkursRow['priz5'];
            } else {
                $priz5 = $chKonkursRow['priz5'];
            }
            
            $prizType = 'money';
            
            mysql_query( "UPDATE user SET $prizType = $prizType + $priz5, earnings_competition = earnings_competition + $priz5 WHERE id = '" . $array[4] . "'" );
        }
		
		#Шестое призовое место (опционально)
        if ( empty( $array[5] ) ) {
            $array[5] = '';
            $priz6 = 0;
        } else {
            if ( $chKonkursRow['allsumm'] ) {
                $priz6 = $arraySumm[5] * $chKonkursRow['priz6'];
            } else {
                $priz6 = $chKonkursRow['priz6'];
            }
            
            $prizType = 'money';
            
            mysql_query( "UPDATE user SET $prizType = $prizType + $priz6, earnings_competition = earnings_competition + $priz6 WHERE id = '" . $array[5] . "'" );
        }
		
		#Седьмое призовое место (опционально)
        if ( empty( $array[6] ) ) {
            $array[6] = '';
            $priz7 = 0;
        } else {
            if ( $chKonkursRow['allsumm'] ) {
                $priz7 = $arraySumm[6] * $chKonkursRow['priz7'];
            } else {
                $priz7 = $chKonkursRow['priz7'];
            }
            
            $prizType = 'money';
            
            mysql_query( "UPDATE user SET $prizType = $prizType + $priz7, earnings_competition = earnings_competition + $priz7 WHERE id = '" . $array[6] . "'" );
        }
		
		#Восьмое призовое место (опционально)
        if ( empty( $array[7] ) ) {
            $array[7] = '';
            $priz8 = 0;
        } else {
            if ( $chKonkursRow['allsumm'] ) {
                $priz8 = $arraySumm[7] * $chKonkursRow['priz8'];
            } else {
                $priz8 = $chKonkursRow['priz8'];
            }
            
            $prizType = 'money';
            
            mysql_query( "UPDATE user SET $prizType = $prizType + $priz8, earnings_competition = earnings_competition + $priz8 WHERE id = '" . $array[7] . "'" );
        }
		
		#Девятое призовое место (опционально)
        if ( empty( $array[8] ) ) {
            $array[8] = '';
            $priz9 = 0;
        } else {
            if ( $chKonkursRow['allsumm'] ) {
                $priz9 = $arraySumm[8] * $chKonkursRow['priz9'];
            } else {
                $priz9 = $chKonkursRow['priz9'];
            }
            
            $prizType = 'money';
            
            mysql_query( "UPDATE user SET $prizType = $prizType + $priz9, earnings_competition = earnings_competition + $priz9 WHERE id = '" . $array[8] . "'" );
        }
		
		#Десятое призовое место (опционально)
        if ( empty( $array[9] ) ) {
            $array[9] = '';
            $priz10 = 0;
        } else {
            if ( $chKonkursRow['allsumm'] ) {
                $priz10 = $arraySumm[9] * $chKonkursRow['priz10'];
            } else {
                $priz10 = $chKonkursRow['priz10'];
            }
            
            $prizType = 'money';
            
            mysql_query( "UPDATE user SET $prizType = $prizType + $priz10, earnings_competition = earnings_competition + $priz10 WHERE id = '" . $array[9] . "'" );
        }
        
		#Создаем запись завершеного конкурса
		mysql_query( "INSERT INTO konkurs_end SET 
			id_konkurs = '" . $chKonkursRow['id'] . "', 
			type = '" . $chKonkursRow['type'] . "', 
			date_start = '" . $chKonkursRow['date_start'] . "', 
			date_end = '" . $chKonkursRow['date_end'] . "', 
			
			iduser1 = '" . $array[0] . "', 
			iduser2 = '" . $array[1] . "', 
			iduser3 = '" . $array[2] . "', 
			iduser4 = '" . $array[3] . "', 
			iduser5 = '" . $array[4] . "', 
			iduser6 = '" . $array[5] . "', 
			iduser7 = '" . $array[6] . "', 
			iduser8 = '" . $array[7] . "', 
			iduser9 = '" . $array[8] . "', 
			iduser10 = '" . $array[9] . "', 
			
			priz1 = '" . $priz1 . "', 
			priz2 = '" . $priz2 . "', 
			priz3 = '" . $priz3 . "', 
			priz4 = '" . $priz4 . "', 
			priz5 = '" . $priz5 . "', 
			priz6 = '" . $priz6 . "', 
			priz7 = '" . $priz7 . "', 
			priz8 = '" . $priz8 . "', 
			priz9 = '" . $priz9 . "', 
			priz10 = '" . $priz10 . "', 
			
			priz_type = '" . $chKonkursRow['priz_type'] . "',
			number_of_prizes = '" . $i_limit . "'
		" );
        
		#Удаляем конкурс и участником
        mysql_query( "DELETE FROM konkurs WHERE id = '" . $chKonkursRow['id'] . "'" );
        mysql_query( "DELETE FROM konkurs_temp WHERE id_konkurs = '" . $chKonkursRow['id'] . "'" );
        
        
    } 


?>