Esempio n. 1
0
 /**
  * Randomly assigns winner status to lottery submissions in tg_lottery.
  * @return integer Number of spots filled
  */
 public function chooseWinners()
 {
     $currentGame = GameFactory::getCurrent();
     if ($currentGame->getLotteryStarted()) {
         throw new \Exception('Lottery previously run');
     }
     $currentGame->setLotteryStarted(true);
     \phpws2\ResourceFactory::saveResource($currentGame);
     $total_spots = $this->totalAvailableSpots();
     $won_spots = $this->getTotalWinners($currentGame->getId());
     $spots_left = $total_spots - $won_spots;
     if ($spots_left === 0) {
         return $total_spots;
     }
     $db = \Database::getDB();
     $tbl = $db->addTable('tg_lottery');
     $tbl->addField('id');
     $tbl->addField('student_id');
     $tbl->addFieldConditional('winner', 0);
     $tbl->addFieldConditional('game_id', $currentGame->getId());
     $tbl->randomOrder();
     for ($i = 0; $i < $spots_left; $i++) {
         $row = $db->selectOneRow();
         if ($row['id']) {
             $this->flagWinner($row['id']);
             Student::incrementWins($row['student_id']);
         } else {
             break;
         }
     }
     return $i;
 }