Example #1
0
        echo $form->hiddenField($matchGames[$i], "[{$i}]GROUP");
        echo $form->hiddenField($matchGames[$i], "[{$i}]NAME", array('value' => $i + 1));
    }
    echo "</p>";
    echo "</td>";
    if ($matchGames[$i]->STATUS < 3) {
        echo "<td>";
        Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');
        $matchGames[$i]->TIME = $matchGames[$i]->TIME == NULL ? NULL : date("d/m/Y H:i:s", strtotime($matchGames[$i]->TIME));
        $this->widget('CJuiDateTimePicker', array('model' => $matchGames[$i], 'attribute' => "[{$i}]TIME", 'language' => 'es', 'mode' => 'datetime', 'options' => array("dateFormat" => 'dd/mm/yy', 'timeFormat' => 'hh:mm', 'minDate' => $model->START_DATE, 'beforeShowDay' => 'js:function(date){
                                          var day = date.getDay();
                                          return [' . $stringADays . ', ""];
                                        }'), 'htmlOptions' => array('class' => 'input-medium', 'placeholder' => "Fecha del encuentro")));
        echo "</td>";
        echo "<td>";
        echo $form->dropDownList($matchGames[$i], "[{$i}]PLAY_GROUND_ID", CHtml::listData(PlayGround::model()->findAll(), 'ID', 'NAME'), array('prompt' => 'SELECCIONE'));
        echo "</td>";
        echo "<td>";
        echo $form->dropDownList($matchGames[$i], "[{$i}]ID_REFEREE", CHtml::listData(Referee::model()->findAll(), 'ID', 'NAME'), array('class' => 'input-medium', 'prompt' => 'SELECCIONE'));
        echo "</td>";
    } else {
        echo '<td valign="bottom"><input  type="text" value="' . $matchGames[$i]->TIME . '" readonly="readonly" class="input-medium" /></td>';
        echo '<td valign="baseline"><input  type="text" value="' . $matchGames[$i]->pLAYGROUND->NAME . '" readonly="readonly" class="input-medium" /></td>';
        echo '<td valign="middle"><input  type="text" value="' . $matchGames[$i]->rEFEREE->NAME . '" readonly="readonly" class="input-medium" /></td>';
    }
    if ($matchGames[$i]->STATUS < 2) {
        $readyToPublish = false;
    }
    ?>

 /**
  * Returns suggested Schedules for specific round
  * @param integer $tournamentId
  * @param integer $roundId
  */
 public function actionGetSuggestedSchedules($tournamentId, $group)
 {
     if (Yii::app()->request->isAjaxRequest) {
         $i = 0;
         $model = $this->loadModel($tournamentId);
         $matchs = MatchGame::model()->findAllByAttributes(array('TOURNAMENT_ID' => $tournamentId, 'GROUP' => $group));
         $foundSchedules = array();
         $grounds = PlayGround::model()->findAllByAttributes(array('ACTIVE' => '1'));
         $days_of_game = array();
         $schedules_of_game = array();
         //find another solution for this
         $day_of_week = array(1 => 'Sun', 2 => 'Mon', 3 => 'Tue', 4 => 'Wed', 5 => 'Thu', 6 => 'Fri', 7 => 'Sat');
         $schedules_of_game = explode(",", $model->SCHEDULE_CONFIG);
         $days_of_game = explode(",", $model->SCHEDULE_D);
         $nowFormat = date('Y-m-d H:i:s');
         $time_of_match = $model->MATCH_LONG_TIME * $model->MATCH_TIMES;
         $suggested = false;
         $now = new DateTime();
         $found = false;
         $j = 0;
         $a_end_time = array();
         $a_start_time = array();
         $a_start_time_1 = array();
         $all_limit = false;
         while (!$found && $i < 100) {
             foreach ($days_of_game as $day) {
                 $now->modify('next ' . $day_of_week[$day]);
                 foreach ($schedules_of_game as $schedule) {
                     /*Do validation in order to prevent index exception*/
                     $start_time = new DateTime($now->format("Y-m-d") . ' ' . $model->aSchedule[$schedule]['from']);
                     $limit_time = new DateTime($now->format("Y-m-d") . ' ' . $model->aSchedule[$schedule]['to']);
                     foreach ($grounds as $tmpGround) {
                         $a_start_time[$tmpGround->ID] = $start_time;
                     }
                     while (!$all_limit && !$found) {
                         foreach ($grounds as $playGround) {
                             //$a_start_time[$playGround->ID] = $a_start_time[$playGround->ID];
                             $a_end_time[$playGround->ID] = new DateTime($a_start_time[$playGround->ID]->format('Y-m-d H:i'));
                             $a_end_time[$playGround->ID]->modify('+' . $time_of_match . ' minutes');
                             $a_start_time_1[$playGround->ID] = new DateTime($a_start_time[$playGround->ID]->format('Y-m-d H:i'));
                             $a_start_time_1[$playGround->ID]->modify('+' . '1' . ' minutes');
                             $command = Yii::app()->db->createCommand();
                             $command->select('tt.ID, tm.TIME as start_match, DATE_ADD(tm.TIME,INTERVAL  (tt.MATCH_LONG_TIME * 2) MINUTE) as end_match');
                             $command->from('tbl_match_game tm');
                             $command->join('tbl_tournament tt', 'tm.TOURNAMENT_ID = tt.ID');
                             $command->where('tm.PLAY_GROUND_ID = :play_id AND tt.ACTIVE = :tournamentStatus  AND (tm.TIME  BETWEEN :start_time AND :end_time OR DATE_ADD(tm.TIME, INTERVAL(tt.MATCH_LONG_TIME * 2) MINUTE) BETWEEN :start_time_1 AND :end_time)', array(':play_id' => $playGround->ID, ':tournamentStatus' => 1, ':start_time' => $a_start_time[$playGround->ID]->format('Y-m-d H:i'), ':end_time' => $a_end_time[$playGround->ID]->format('Y-m-d H:i'), ':start_time_1' => $a_start_time_1[$playGround->ID]->format('Y-m-d H:i')));
                             $command->order('end_match desc');
                             $list = $command->queryAll();
                             if (sizeof($list)) {
                                 $a_start_time[$playGround->ID] = new DateTime($list[0]['end_match']);
                             } else {
                                 if ($a_end_time[$playGround->ID] <= $limit_time) {
                                     $foundSchedules[][$playGround->ID] = $a_start_time[$playGround->ID]->format('d/m/Y H:i');
                                 }
                                 $a_start_time[$playGround->ID] = new DateTime($a_end_time[$playGround->ID]->format('Y-m-d H:i'));
                                 $found = sizeof($foundSchedules) == sizeof($matchs) * sizeof($grounds);
                                 if ($found) {
                                     break 4;
                                 }
                             }
                         }
                         //foreach
                         $i = 0;
                         foreach ($a_end_time as $end_time_1) {
                             if ($end_time_1 <= $limit_time) {
                                 break 1;
                             }
                             $i++;
                         }
                         $all_limit = $i == sizeof($a_end_time);
                         $j++;
                     }
                     //while
                 }
             }
         }
         usort($foundSchedules, array($this, "sortFunction"));
         $referees = Referee::model()->findAllByAttributes(array('ACTIVE' => '1'));
         $finalSchedules = array();
         $auxSchedules = array();
         $k = 1;
         foreach ($foundSchedules as $foundSchedule) {
             foreach ($foundSchedule as $keySchedule => $valueSchedule) {
                 foreach ($referees as $referee) {
                     $criteria = new CDbCriteria();
                     $v_startTime = DateTime::createFromFormat('d/m/Y H:i', $valueSchedule);
                     $v_endTime = DateTime::createFromFormat('d/m/Y H:i', $valueSchedule);
                     $v_endTime->modify('+' . $time_of_match . ' minutes');
                     //$criteria->addBetweenCondition('TIME', $v_startTime->format('Y-m-d H:i'), $v_endTime->format('Y-m-d H:i'), 'AND');
                     $modelMatch = MatchGame::model()->find(array('condition' => 'ID_REFEREE=:idReferee AND TIME >= :startDate AND TIME <= :endDate', 'params' => array(':idReferee' => $referee->ID, ':startDate' => $v_startTime->format('Y-m-d H:i'), ':endDate' => $v_endTime->format('Y-m-d H:i'))));
                     if ($modelMatch === null) {
                         $in_array = array_search(array('TIME' => $valueSchedule, 'REFEREE' => $referee->ID), $auxSchedules);
                         if (!$in_array) {
                             $finalSchedules[$k]['PLAY_GROUND'] = $keySchedule;
                             $finalSchedules[$k]['TIME'] = $valueSchedule;
                             $finalSchedules[$k]['REFEREE'] = $referee->ID;
                             $auxSchedules[$k]['TIME'] = $valueSchedule;
                             $auxSchedules[$k]['REFEREE'] = $referee->ID;
                             $k++;
                         } else {
                         }
                     }
                     /*$command = Yii::app()->db->createCommand();
                     		$command->select('tt.ID, tm.TIME as start_match, DATE_ADD(tm.TIME,INTERVAL  (tt.MATCH_LONG_TIME * 2) MINUTE) as end_match');
                     		$command->from('tbl_match_game tm');
                     		$command->join('tbl_tournament tt', 'tm.TOURNAMENT_ID = tt.ID');
                     		$command->where('tm.PLAY_GROUND_ID = :play_id AND tt.ACTIVE = :tournamentStatus  AND (tm.TIME  BETWEEN :start_time AND :end_time OR DATE_ADD(tm.TIME, INTERVAL(tt.MATCH_LONG_TIME * 2) MINUTE) BETWEEN :start_time_1 AND :end_time)',
                     				array(':play_id'=>$playGround->ID,':tournamentStatus'=>1,':start_time'=>$a_start_time[$playGround->ID]->format('Y-m-d H:i'),':end_time'=>$a_end_time[$playGround->ID]->format('Y-m-d H:i'), ':start_time_1'=>$a_start_time_1[$playGround->ID]->format('Y-m-d H:i'),));
                     		$command->order('end_match desc');
                     		$list = $command->queryAll();*/
                 }
             }
         }
         echo CJSON::encode($finalSchedules);
         Yii::app()->end();
     }
 }
 /**
  * Returns the data model based on the primary key given in the GET variable.
  * If the data model is not found, an HTTP exception will be raised.
  * @param integer $id the ID of the model to be loaded
  * @return PlayGround the loaded model
  * @throws CHttpException
  */
 public function loadModel($id)
 {
     $model = PlayGround::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }