3 namespace Rapsys\AirBundle\Repository
; 
   5 use Symfony\Component\Translation\TranslatorInterface
; 
   6 use Doctrine\DBAL\Types\Type
; 
   7 use Doctrine\ORM\Query\ResultSetMapping
; 
   8 use Doctrine\ORM\Query
; 
  13 class SessionRepository 
extends \Doctrine\ORM\EntityRepository 
{ 
  14         ///Set accuweather max number of daily pages 
  15         const ACCUWEATHER_DAILY 
= 12; 
  17         ///Set accuweather max number of hourly pages 
  18         const ACCUWEATHER_HOURLY 
= 3; 
  20         ///Set user with bad behaviour delay 
  23         ///Set user with good behaviour delay 
  27         //TODO: document utf-8 codes ? 
  30                 'Morning' => 'π
', #0001f305 
  31                 'Afternoon' => 'βοΈ', #2600 
  32                 'Evening' => 'π', #0001f307 
  33                 'After' => 'β¨', #2728 
  35                 'Cleary' => 'β', #2600 
  36                 'Sunny' => 'β
', #26c5 
  37                 'Cloudy' => 'β', #2601 
  38                 'Winty' => 'βοΈ', #2744 
  39                 'Rainy' => 'π', #0001f302 
  40                 'Stormy' => 'β' #2614 
  44          * Find session by location, slot and date 
  46          * @param $location The location 
  47          * @param $slot The slot 
  48          * @param $date The datetime 
  50         public function findOneByLocationSlotDate($location, $slot, $date) { 
  52                 return $this->getEntityManager() 
  53                         ->createQuery('SELECT s FROM RapsysAirBundle:Session s WHERE (s.location = :location AND s.slot = :slot AND s.date = :date)') 
  54                         ->setParameter('location', $location) 
  55                         ->setParameter('slot', $slot) 
  56                         ->setParameter('date', $date) 
  61          * Find sessions by date period 
  63          * @param $period The date period 
  65         public function findAllByDatePeriod($period) { 
  67                 return $this->getEntityManager() 
  68                         ->createQuery('SELECT s FROM RapsysAirBundle:Session s WHERE s.date BETWEEN :begin AND :end') 
  69                         ->setParameter('begin', $period->getStartDate()) 
  70                         ->setParameter('end', $period->getEndDate()) 
  75          * Find sessions by location and date period 
  77          * @param $location The location 
  78          * @param $period The date period 
  80         public function findAllByLocationDatePeriod($location, $period) { 
  82                 return $this->getEntityManager() 
  83                         ->createQuery('SELECT s FROM RapsysAirBundle:Session s WHERE (s.location = :location AND s.date BETWEEN :begin AND :end)') 
  84                         ->setParameter('location', $location) 
  85                         ->setParameter('begin', $period->getStartDate()) 
  86                         ->setParameter('end', $period->getEndDate()) 
  91          * Find one session by location and user id within last month 
  93          * @param $location The location id 
  94          * @param $user The user id 
  96         public function findOneWithinLastMonthByLocationUser($location, $user) { 
  98                 $em = $this->getEntityManager(); 
 101                 $qs = $em->getConfiguration()->getQuoteStrategy(); 
 102                 $dp = $em->getConnection()->getDatabasePlatform(); 
 104                 //Get quoted table names 
 105                 //XXX: this allow to make this code table name independent 
 107                         'RapsysAirBundle:Session' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Session'), $dp), 
 108                         'RapsysAirBundle:Application' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Application'), $dp), 
 114                 //XXX: give the gooddelay to guest just in case 
 117 FROM RapsysAirBundle:Session s 
 118 JOIN RapsysAirBundle:Application a ON (a.id = s.application_id AND a.user_id = :uid AND (a.canceled IS NULL OR TIMESTAMPDIFF(DAY, a.canceled, ADDTIME(s.date, s.begin)) < 1)) 
 119 WHERE s.location_id = :lid AND s.date >= DATE_ADD(DATE_SUB(NOW(), INTERVAL 1 MONTH), INTERVAL :gooddelay DAY) 
 122                 //Replace bundle entity name by table name 
 123                 $req = str_replace(array_keys($tables), array_values($tables), $req); 
 125                 //Get result set mapping instance 
 126                 $rsm = new ResultSetMapping(); 
 129                 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php 
 130                 $rsm->addScalarResult('id', 'id', 'integer') 
 131                         ->addIndexByScalar('id'); 
 135                         ->createNativeQuery($req, $rsm) 
 136                         ->setParameter('lid', $location) 
 137                         ->setParameter('uid', $user) 
 138                         ->setParameter('gooddelay', self
::GOOD_DELAY
) 
 139                         ->getOneOrNullResult(); 
 143          * Fetch session by id 
 145          * @param $id The session id 
 146          * @return array The session data 
 148         public function fetchOneById($id) { 
 150                 $em = $this->getEntityManager(); 
 153                 $qs = $em->getConfiguration()->getQuoteStrategy(); 
 154                 $dp = $em->getConnection()->getDatabasePlatform(); 
 156                 //Get quoted table names 
 157                 //XXX: this allow to make this code table name independent 
 159                         'RapsysAirBundle:GroupUser' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('groups'), $em->getClassMetadata('RapsysAirBundle:User'), $dp), 
 160                         'RapsysAirBundle:Session' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Session'), $dp), 
 161                         'RapsysAirBundle:Application' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Application'), $dp), 
 162                         'RapsysAirBundle:Group' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Group'), $dp), 
 163                         'RapsysAirBundle:Location' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Location'), $dp), 
 164                         'RapsysAirBundle:Slot' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Slot'), $dp), 
 165                         'RapsysAirBundle:User' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:User'), $dp), 
 171                 //TODO: compute scores ? 
 172                 //TODO: compute delivery date ? (J-3/J-4 ?) 
 178         ADDDATE(ADDTIME(s.date, s.begin), INTERVAL IF(s.slot_id = 4, 1, 0) DAY) AS start, 
 180         ADDDATE(ADDTIME(ADDTIME(s.date, s.begin), s.length), INTERVAL IF(s.slot_id = 4, 1, 0) DAY) AS stop, 
 192         s.location_id AS l_id, 
 195         l.address AS l_address, 
 196         l.zipcode AS l_zipcode, 
 198         l.latitude AS l_latitude, 
 199         l.longitude AS l_longitude, 
 202         s.application_id AS a_id, 
 204         au.pseudonym AS au_pseudonym, 
 205         GROUP_CONCAT(sa.id ORDER BY sa.user_id SEPARATOR "\\n") AS sa_id, 
 206         GROUP_CONCAT(IFNULL(sa.score, 'NULL') ORDER BY sa.user_id SEPARATOR "\\n") AS sa_score, 
 207         GROUP_CONCAT(sa.created ORDER BY sa.user_id SEPARATOR "\\n") AS sa_created, 
 208         GROUP_CONCAT(sa.updated ORDER BY sa.user_id SEPARATOR "\\n") AS sa_updated, 
 209         GROUP_CONCAT(IFNULL(sa.canceled, 'NULL') ORDER BY sa.user_id SEPARATOR "\\n") AS sa_canceled, 
 210         GROUP_CONCAT(sa.user_id ORDER BY sa.user_id SEPARATOR "\\n") AS sau_id, 
 211         GROUP_CONCAT(sau.pseudonym ORDER BY sa.user_id SEPARATOR "\\n") AS sau_pseudonym 
 212 FROM RapsysAirBundle:Session AS s 
 213 JOIN RapsysAirBundle:Location AS l ON (l.id = s.location_id) 
 214 JOIN RapsysAirBundle:Slot AS t ON (t.id = s.slot_id) 
 215 LEFT JOIN RapsysAirBundle:Application AS a ON (a.id = s.application_id) 
 216 LEFT JOIN RapsysAirBundle:User AS au ON (au.id = a.user_id) 
 217 LEFT JOIN RapsysAirBundle:Application AS sa ON (sa.session_id = s.id) 
 218 LEFT JOIN RapsysAirBundle:User AS sau ON (sau.id = sa.user_id) 
 224                 //Replace bundle entity name by table name 
 225                 $req = str_replace(array_keys($tables), array_values($tables), $req); 
 227                 //Get result set mapping instance 
 228                 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php 
 229                 $rsm = new ResultSetMapping(); 
 232                 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php 
 233                 $rsm->addScalarResult('id', 'id', 'integer') 
 234                         ->addScalarResult('date', 'date', 'date') 
 235                         ->addScalarResult('begin', 'begin', 'time') 
 236                         ->addScalarResult('start', 'start', 'datetime') 
 237                         ->addScalarResult('length', 'length', 'time') 
 238                         ->addScalarResult('stop', 'stop', 'datetime') 
 239                         ->addScalarResult('rainfall', 'rainfall', 'float') 
 240                         ->addScalarResult('rainrisk', 'rainrisk', 'float') 
 241                         ->addScalarResult('realfeel', 'realfeel', 'float') 
 242                         ->addScalarResult('realfeelmin', 'realfeelmin', 'float') 
 243                         ->addScalarResult('realfeelmax', 'realfeelmax', 'float') 
 244                         ->addScalarResult('temperature', 'temperature', 'float') 
 245                         ->addScalarResult('temperaturemin', 'temperaturemin', 'float') 
 246                         ->addScalarResult('temperaturemax', 'temperaturemax', 'float') 
 247                         ->addScalarResult('locked', 'locked', 'datetime') 
 248                         ->addScalarResult('created', 'created', 'datetime') 
 249                         ->addScalarResult('updated', 'updated', 'datetime') 
 250                         ->addScalarResult('l_id', 'l_id', 'integer') 
 251                         ->addScalarResult('l_short', 'l_short', 'string') 
 252                         ->addScalarResult('l_title', 'l_title', 'string') 
 253                         ->addScalarResult('l_address', 'l_address', 'string') 
 254                         ->addScalarResult('l_zipcode', 'l_zipcode', 'string') 
 255                         ->addScalarResult('l_city', 'l_city', 'string') 
 256                         ->addScalarResult('l_latitude', 'l_latitude', 'float') 
 257                         ->addScalarResult('l_longitude', 'l_longitude', 'float') 
 258                         ->addScalarResult('t_id', 't_id', 'integer') 
 259                         ->addScalarResult('t_title', 't_title', 'string') 
 260                         ->addScalarResult('a_id', 'a_id', 'integer') 
 261                         ->addScalarResult('au_id', 'au_id', 'integer') 
 262                         ->addScalarResult('au_pseudonym', 'au_pseudonym', 'string') 
 263                         //XXX: is a string because of \n separator 
 264                         ->addScalarResult('sa_id', 'sa_id', 'string') 
 265                         //XXX: is a string because of \n separator 
 266                         ->addScalarResult('sa_score', 'sa_score', 'string') 
 267                         //XXX: is a string because of \n separator 
 268                         ->addScalarResult('sa_created', 'sa_created', 'string') 
 269                         //XXX: is a string because of \n separator 
 270                         ->addScalarResult('sa_updated', 'sa_updated', 'string') 
 271                         //XXX: is a string because of \n separator 
 272                         ->addScalarResult('sa_canceled', 'sa_canceled', 'string') 
 273                         //XXX: is a string because of \n separator 
 274                         ->addScalarResult('sau_id', 'sau_id', 'string') 
 275                         //XXX: is a string because of \n separator 
 276                         ->addScalarResult('sau_pseudonym', 'sau_pseudonym', 'string') 
 277                         ->addIndexByScalar('id'); 
 281                         ->createNativeQuery($req, $rsm) 
 282                         ->setParameter('sid', $id) 
 283                         ->getOneOrNullResult(); 
 287          * Fetch sessions calendar with translated location by date period 
 289          * @param $translator The TranslatorInterface instance 
 290          * @param $period The date period 
 291          * @param $locationId The location id 
 292          * @param $sessionId The session id 
 293          * @param $granted The session is granted 
 295         public function fetchCalendarByDatePeriod(TranslatorInterface 
$translator, $period, $locationId = null, $sessionId = null, $granted = false) { 
 297                 $em = $this->getEntityManager(); 
 300                 $qs = $em->getConfiguration()->getQuoteStrategy(); 
 301                 $dp = $em->getConnection()->getDatabasePlatform(); 
 303                 //Get quoted table names 
 304                 //XXX: this allow to make this code table name independent 
 306                         'RapsysAirBundle:GroupUser' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('groups'), $em->getClassMetadata('RapsysAirBundle:User'), $dp), 
 307                         'RapsysAirBundle:Session' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Session'), $dp), 
 308                         'RapsysAirBundle:Application' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Application'), $dp), 
 309                         'RapsysAirBundle:Group' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Group'), $dp), 
 310                         'RapsysAirBundle:Location' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Location'), $dp), 
 311                         'RapsysAirBundle:Slot' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Slot'), $dp), 
 312                         'RapsysAirBundle:User' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:User'), $dp), 
 318                 //TODO: change as_u_* in sau_*, a_u_* in au_*, etc, see request up 
 319                 $req = 'SELECT s.id, s.date, s.rainrisk, s.rainfall, s.realfeel, s.temperature, s.location_id AS l_id, l.short AS l_short, l.title AS l_title, s.slot_id AS t_id, t.title AS t_title, s.application_id AS a_id, a.user_id AS a_u_id, au.pseudonym AS a_u_pseudonym, GROUP_CONCAT(sa.user_id ORDER BY sa.user_id SEPARATOR "\\n") AS as_u_id, GROUP_CONCAT(sau.pseudonym ORDER BY sa.user_id SEPARATOR "\\n") AS as_u_pseudonym 
 320                         FROM RapsysAirBundle:Session AS s 
 321                         JOIN RapsysAirBundle:Location AS l ON (l.id = s.location_id) 
 322                         JOIN RapsysAirBundle:Slot AS t ON (t.id = s.slot_id) 
 323                         '.($granted?'':'LEFT ').'JOIN RapsysAirBundle:Application AS a ON (a.id = s.application_id) 
 324                         '.($granted?'':'LEFT ').'JOIN RapsysAirBundle:User AS au ON (au.id = a.user_id) 
 325                         LEFT JOIN RapsysAirBundle:Application AS sa ON (sa.session_id = s.id) 
 326                         LEFT JOIN RapsysAirBundle:User AS sau ON (sau.id = sa.user_id) 
 327                         WHERE s.date BETWEEN :begin AND :end 
 328                         '.($locationId?'AND s.location_id = :lid':'').' 
 332                 //Replace bundle entity name by table name 
 333                 $req = str_replace(array_keys($tables), array_values($tables), $req); 
 335                 //Get result set mapping instance 
 336                 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php 
 337                 $rsm = new ResultSetMapping(); 
 340                 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php 
 341                 //addScalarResult($sqlColName, $resColName, $type = 'string'); 
 342                 $rsm->addScalarResult('id', 'id', 'integer') 
 343                         ->addScalarResult('date', 'date', 'date') 
 344                         ->addScalarResult('rainrisk', 'rainrisk', 'float') 
 345                         ->addScalarResult('rainfall', 'rainfall', 'float') 
 346                         ->addScalarResult('realfeel', 'realfeel', 'float') 
 347                         ->addScalarResult('temperature', 'temperature', 'float') 
 348                         ->addScalarResult('t_id', 't_id', 'integer') 
 349                         ->addScalarResult('t_title', 't_title', 'string') 
 350                         ->addScalarResult('l_id', 'l_id', 'integer') 
 351                         ->addScalarResult('l_short', 'l_short', 'string') 
 352                         ->addScalarResult('l_title', 'l_title', 'string') 
 353                         ->addScalarResult('a_id', 'a_id', 'integer') 
 354                         ->addScalarResult('a_u_id', 'a_u_id', 'integer') 
 355                         ->addScalarResult('a_u_pseudonym', 'a_u_pseudonym', 'string') 
 356                         //XXX: is a string because of \n separator 
 357                         ->addScalarResult('as_u_id', 'as_u_id', 'string') 
 358                         //XXX: is a string because of \n separator 
 359                         ->addScalarResult('as_u_pseudonym', 'as_u_pseudonym', 'string') 
 360                         ->addIndexByScalar('id'); 
 364                         ->createNativeQuery($req, $rsm) 
 365                         ->setParameter('begin', $period->getStartDate()) 
 366                         ->setParameter('end', $period->getEndDate()) 
 367                         ->setParameter('lid', $locationId) 
 376                 //Iterate on each day 
 377                 foreach($period as $date) { 
 378                         //Init day in calendar 
 379                         $calendar[$Ymd = $date->format('Ymd')] = [ 
 380                                 'title' => $date->format('d'), 
 385                         //Detect month change 
 386                         if ($month != $date->format('m')) { 
 387                                 $month = $date->format('m'); 
 388                                 //Append month for first day of month 
 389                                 //XXX: except if today to avoid double add 
 390                                 if ($date->format('U') != strtotime('today')) { 
 391                                         $calendar[$Ymd]['title'] .= '/'.$month; 
 395                         if ($date->format('U') == ($today = strtotime('today'))) { 
 396                                 $calendar[$Ymd]['title'] .= '/'.$month; 
 397                                 $calendar[$Ymd]['current'] = true; 
 398                                 $calendar[$Ymd]['class'][] = 'current'; 
 400                         //Disable passed days 
 401                         if ($date->format('U') < $today) { 
 402                                 $calendar[$Ymd]['disabled'] = true; 
 403                                 $calendar[$Ymd]['class'][] = 'disabled'; 
 405                         //Set next month days 
 406                         if ($date->format('m') > date('m')) { 
 407                                 $calendar[$Ymd]['next'] = true; 
 408                                 $calendar[$Ymd]['class'][] = 'next'; 
 411                         //Iterate on each session to find the one of the day 
 412                         foreach($res as $session) { 
 413                                 if (($sessionYmd = $session['date']->format('Ymd')) == $Ymd) { 
 414                                         //Count number of application 
 415                                         $count = count(explode("\n", $session['as_u_id'])); 
 419                                         if (!empty($session['a_id'])) { 
 420                                                 $applications = [ $session['a_u_id'] => $session['a_u_pseudonym'] ]; 
 421                                                 $class[] = 'granted'; 
 422                                         } elseif ($count == 0) { 
 423                                                 $class[] = 'orphaned'; 
 424                                         } elseif ($count > 1) { 
 425                                                 $class[] = 'disputed'; 
 427                                                 $class[] = 'pending'; 
 430                                         if ($sessionId == $session['id']) { 
 431                                                 $class[] = 'highlight'; 
 435                                         //XXX: realfeel may be null, temperature should not 
 436                                         $temperature = $session['realfeel'] !== null ? $session['realfeel'] : $session['temperature']; 
 439                                         //XXX: rainfall may be null 
 440                                         if ($session['rainrisk'] > 0.50 || $session['rainfall'] > 2) { 
 441                                                 $weather = self
::GLYPHS
['Stormy']; 
 442                                         } elseif ($session['rainrisk'] > 0.40 || $session['rainfall'] > 1) { 
 443                                                 $weather = self
::GLYPHS
['Rainy']; 
 444                                         } elseif ($temperature > 24) { 
 445                                                 $weather = self
::GLYPHS
['Cleary']; 
 446                                         } elseif ($temperature > 17) { 
 447                                                 $weather = self
::GLYPHS
['Sunny']; 
 448                                         } elseif ($temperature > 10) { 
 449                                                 $weather = self
::GLYPHS
['Cloudy']; 
 450                                         } elseif ($temperature !== null) { 
 451                                                 $weather = self
::GLYPHS
['Winty']; 
 459                                         //Check if realfeel is available 
 460                                         if ($session['realfeel'] !== null) { 
 461                                                 $weathertitle[] = $session['realfeel'].'Β°R'; 
 464                                         //Check if temperature is available 
 465                                         if ($session['temperature'] !== null) { 
 466                                                 $weathertitle[] = $session['temperature'].'Β°C'; 
 469                                         //Check if rainrisk is available 
 470                                         if ($session['rainrisk'] !== null) { 
 471                                                 $weathertitle[] = ($session['rainrisk']*100).'%'; 
 474                                         //Check if rainfall is available 
 475                                         if ($session['rainfall'] !== null) { 
 476                                                 $weathertitle[] = $session['rainfall'].'mm'; 
 481                                                 0 => $translator->trans($session['t_title']).' '.$translator->trans('at '.$session['l_title']).$translator->trans(':') 
 484                                         //Fetch pseudonyms from session applications 
 485                                         $applications +
= array_combine(explode("\n", $session['as_u_id']), array_map(function ($v) {return '- '.$v
;}, explode("\n", $session['as_u_pseudonym']))); 
 487                                         //Check that session is not granted 
 488                                         if (empty($session['a_id'])) { 
 489                                                 //With location id and unique application 
 490                                                 if ($locationId && $count == 1) { 
 491                                                         //Set unique application pseudonym as title 
 492                                                         $title = $session['as_u_pseudonym']; 
 493                                                 //Without location id or multiple application 
 495                                                         //Set location title with optional count 
 496                                                         $title = $translator->trans($session['l_title']).($count > 1 ? ' ['.$count.']':''); 
 500                                                 //Replace granted application 
 501                                                 $applications[$session['a_u_id']] = '* '.$session['a_u_pseudonym']; 
 502                                                 //Set pseudonym with optional location title and count 
 503                                                 $title = $session['a_u_pseudonym'].($locationId?'':' '.$translator->trans('at '.$session['l_short'])).($count > 1 ? ' ['.$count.']':''); 
 507                                         $calendar[$Ymd]['sessions'][$session['t_id'].sprintf('%02d', $session['l_id'])] = [ 
 508                                                 'id' => $session['id'], 
 511                                                 'slot' => self
::GLYPHS
[$session['t_title']], 
 512                                                 'slottitle' => $translator->trans($session['t_title']), 
 513                                                 'weather' => $weather, 
 514                                                 'weathertitle' => implode(' ', $weathertitle), 
 515                                                 'applications' => $applications 
 521                         ksort($calendar[$Ymd]['sessions']); 
 529          * Fetch sessions calendar with translated location by date period and user 
 531          * @param $translator The TranslatorInterface instance 
 532          * @param $period The date period 
 533          * @param $userId The user id 
 534          * @param $sessionId The session id 
 536         public function fetchUserCalendarByDatePeriod(TranslatorInterface 
$translator, $period, $userId = null, $sessionId = null) { 
 538                 $em = $this->getEntityManager(); 
 541                 $qs = $em->getConfiguration()->getQuoteStrategy(); 
 542                 $dp = $em->getConnection()->getDatabasePlatform(); 
 544                 //Get quoted table names 
 545                 //XXX: this allow to make this code table name independent 
 547                         'RapsysAirBundle:GroupUser' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('groups'), $em->getClassMetadata('RapsysAirBundle:User'), $dp), 
 548                         'RapsysAirBundle:Session' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Session'), $dp), 
 549                         'RapsysAirBundle:Application' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Application'), $dp), 
 550                         'RapsysAirBundle:Group' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Group'), $dp), 
 551                         'RapsysAirBundle:Location' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Location'), $dp), 
 552                         'RapsysAirBundle:Slot' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Slot'), $dp), 
 553                         'RapsysAirBundle:User' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:User'), $dp), 
 559                 $req = 'SELECT s.id, s.date, s.rainrisk, s.rainfall, s.realfeel, s.temperature, s.location_id AS l_id, l.short AS l_short, l.title AS l_title, s.slot_id AS t_id, t.title AS t_title, s.application_id AS a_id, a.user_id AS a_u_id, au.pseudonym AS a_u_pseudonym, GROUP_CONCAT(sa.user_id ORDER BY sa.user_id SEPARATOR "\\n") AS as_u_id, GROUP_CONCAT(CONCAT("- ", sau.pseudonym) ORDER BY sa.user_id SEPARATOR "\\n") AS as_u_pseudonym 
 560                         FROM RapsysAirBundle:Session AS s 
 561                         JOIN RapsysAirBundle:Location AS l ON (l.id = s.location_id) 
 562                         JOIN RapsysAirBundle:Slot AS t ON (t.id = s.slot_id) 
 563                         '.($userId?'JOIN RapsysAirBundle:Application AS sua ON (sua.session_id = s.id)':'').' 
 564                         LEFT JOIN RapsysAirBundle:Application AS a ON (a.id = s.application_id) 
 565                         LEFT JOIN RapsysAirBundle:User AS au ON (au.id = a.user_id) 
 566                         LEFT JOIN RapsysAirBundle:Application AS sa ON (sa.session_id = s.id) 
 567                         LEFT JOIN RapsysAirBundle:User AS sau ON (sau.id = sa.user_id) 
 568                         WHERE s.date BETWEEN :begin AND :end 
 569                         '.($userId?'AND sua.user_id = :uid':'').' 
 573                 //Replace bundle entity name by table name 
 574                 $req = str_replace(array_keys($tables), array_values($tables), $req); 
 576                 //Get result set mapping instance 
 577                 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php 
 578                 $rsm = new ResultSetMapping(); 
 581                 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php 
 582                 //addScalarResult($sqlColName, $resColName, $type = 'string'); 
 583                 $rsm->addScalarResult('id', 'id', 'integer') 
 584                         ->addScalarResult('date', 'date', 'date') 
 585                         ->addScalarResult('rainrisk', 'rainrisk', 'float') 
 586                         ->addScalarResult('rainfall', 'rainfall', 'float') 
 587                         ->addScalarResult('realfeel', 'realfeel', 'float') 
 588                         ->addScalarResult('temperature', 'temperature', 'float') 
 589                         ->addScalarResult('t_id', 't_id', 'integer') 
 590                         ->addScalarResult('t_title', 't_title', 'string') 
 591                         ->addScalarResult('l_id', 'l_id', 'integer') 
 592                         ->addScalarResult('l_short', 'l_short', 'string') 
 593                         ->addScalarResult('l_title', 'l_title', 'string') 
 594                         ->addScalarResult('a_id', 'a_id', 'integer') 
 595                         ->addScalarResult('a_u_id', 'a_u_id', 'integer') 
 596                         ->addScalarResult('a_u_pseudonym', 'a_u_pseudonym', 'string') 
 597                         //XXX: is a string because of \n separator 
 598                         ->addScalarResult('as_u_id', 'as_u_id', 'string') 
 599                         //XXX: is a string because of \n separator 
 600                         ->addScalarResult('as_u_pseudonym', 'as_u_pseudonym', 'string') 
 601                         ->addIndexByScalar('id'); 
 605                         ->createNativeQuery($req, $rsm) 
 606                         ->setParameter('begin', $period->getStartDate()) 
 607                         ->setParameter('end', $period->getEndDate()) 
 608                         ->setParameter('uid', $userId) 
 617                 //Iterate on each day 
 618                 foreach($period as $date) { 
 619                         //Init day in calendar 
 620                         $calendar[$Ymd = $date->format('Ymd')] = [ 
 621                                 'title' => $date->format('d'), 
 626                         //Detect month change 
 627                         if ($month != $date->format('m')) { 
 628                                 $month = $date->format('m'); 
 629                                 //Append month for first day of month 
 630                                 //XXX: except if today to avoid double add 
 631                                 if ($date->format('U') != strtotime('today')) { 
 632                                         $calendar[$Ymd]['title'] .= '/'.$month; 
 636                         if ($date->format('U') == ($today = strtotime('today'))) { 
 637                                 $calendar[$Ymd]['title'] .= '/'.$month; 
 638                                 $calendar[$Ymd]['current'] = true; 
 639                                 $calendar[$Ymd]['class'][] = 'current'; 
 641                         //Disable passed days 
 642                         if ($date->format('U') < $today) { 
 643                                 $calendar[$Ymd]['disabled'] = true; 
 644                                 $calendar[$Ymd]['class'][] = 'disabled'; 
 646                         //Set next month days 
 647                         if ($date->format('m') > date('m')) { 
 648                                 $calendar[$Ymd]['next'] = true; 
 649                                 $calendar[$Ymd]['class'][] = 'next'; 
 652                         //Iterate on each session to find the one of the day 
 653                         foreach($res as $session) { 
 654                                 if (($sessionYmd = $session['date']->format('Ymd')) == $Ymd) { 
 655                                         //Count number of application 
 656                                         $count = count(explode("\n", $session['as_u_id'])); 
 660                                         if (!empty($session['a_id'])) { 
 661                                                 $applications = [ $session['a_u_id'] => $session['a_u_pseudonym'] ]; 
 662                                                 if ($session['a_u_id'] == $userId) { 
 663                                                         $class[] = 'granted'; 
 665                                                         $class[] = 'disputed'; 
 667                                         } elseif ($count == 0) { 
 668                                                 $class[] = 'orphaned'; 
 669                                         } elseif ($count > 1) { 
 670                                                 $class[] = 'disputed'; 
 672                                                 $class[] = 'pending'; 
 675                                         if ($sessionId == $session['id']) { 
 676                                                 $class[] = 'highlight'; 
 680                                         //XXX: realfeel may be null, temperature should not 
 681                                         $temperature = $session['realfeel'] !== null ? $session['realfeel'] : $session['temperature']; 
 684                                         //XXX: rainfall may be null 
 685                                         if ($session['rainrisk'] > 0.50 || $session['rainfall'] > 2) { 
 686                                                 $weather = self
::GLYPHS
['Stormy']; 
 687                                         } elseif ($session['rainrisk'] > 0.40 || $session['rainfall'] > 1) { 
 688                                                 $weather = self
::GLYPHS
['Rainy']; 
 689                                         } elseif ($temperature > 24) { 
 690                                                 $weather = self
::GLYPHS
['Cleary']; 
 691                                         } elseif ($temperature > 17) { 
 692                                                 $weather = self
::GLYPHS
['Sunny']; 
 693                                         } elseif ($temperature > 10) { 
 694                                                 $weather = self
::GLYPHS
['Cloudy']; 
 695                                         } elseif ($temperature !== null) { 
 696                                                 $weather = self
::GLYPHS
['Winty']; 
 704                                         //Check if realfeel is available 
 705                                         if ($session['realfeel'] !== null) { 
 706                                                 $weathertitle[] = $session['realfeel'].'Β°R'; 
 709                                         //Check if temperature is available 
 710                                         if ($session['temperature'] !== null) { 
 711                                                 $weathertitle[] = $session['temperature'].'Β°C'; 
 714                                         //Check if rainrisk is available 
 715                                         if ($session['rainrisk'] !== null) { 
 716                                                 $weathertitle[] = ($session['rainrisk']*100).'%'; 
 719                                         //Check if rainfall is available 
 720                                         if ($session['rainfall'] !== null) { 
 721                                                 $weathertitle[] = $session['rainfall'].'mm'; 
 726                                                 0 => $translator->trans($session['t_title']).' '.$translator->trans('at '.$session['l_title']).$translator->trans(':') 
 729                                         //Check that session is not granted 
 730                                         if (empty($session['a_id'])) { 
 731                                                 //Fetch pseudonyms from session applications 
 732                                                 $applications +
= array_combine(explode("\n", $session['as_u_id']), explode("\n", $session['as_u_pseudonym'])); 
 735                                                 //Fetch pseudonyms from session applications 
 736                                                 $applications +
= array_combine(explode("\n", $session['as_u_id']), explode("\n", $session['as_u_pseudonym'])); 
 737                                                 //Replace granted application 
 738                                                 $applications[$session['a_u_id']] = '* '.$session['a_u_pseudonym']; 
 742                                         $title = $translator->trans($session['l_title']).($count > 1 ? ' ['.$count.']':''); 
 745                                         $calendar[$Ymd]['sessions'][$session['t_id'].sprintf('%02d', $session['l_id'])] = [ 
 746                                                 'id' => $session['id'], 
 749                                                 'slot' => self
::GLYPHS
[$session['t_title']], 
 750                                                 'slottitle' => $translator->trans($session['t_title']), 
 751                                                 'weather' => $weather, 
 752                                                 'weathertitle' => implode(' ', $weathertitle), 
 753                                                 'applications' => $applications 
 759                         ksort($calendar[$Ymd]['sessions']); 
 767          * Find every session pending application 
 769          * @return array<Session> The sessions to update 
 771         public function findAllPendingApplication() { 
 773                 $em = $this->getEntityManager(); 
 776                 $qs = $em->getConfiguration()->getQuoteStrategy(); 
 777                 $dp = $em->getConnection()->getDatabasePlatform(); 
 779                 //Get quoted table names 
 780                 //XXX: this allow to make this code table name independent 
 782                         'RapsysAirBundle:Session' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Session'), $dp), 
 787                 //Select all sessions without application attributed with diff(start, now) <= if(diff(start, created) <= GOOD_DELAY day, diff(start, created)*3/4, GOOD_DELAY day) 
 788                 //XXX: consider only unfinished sessions with stop > now 
 789                 //XXX: consider as grace time first quarter (1/4) between creation and start time when below gooddelay 
 790                 //XXX: we may remove ADDDATE(start, INTERVAL IF(s.slot_id = 4, 1, 0) DAY) used for after specificity 
 793 FROM RapsysAirBundle:Session AS s 
 795         s.application_id IS NULL AND 
 796         ADDDATE(ADDTIME(ADDTIME(s.date, s.begin), s.length), INTERVAL IF(s.slot_id = 4, 1, 0) DAY) > NOW() AND 
 797         TIMEDIFF(ADDDATE(ADDTIME(s.date, s.begin), INTERVAL IF(s.slot_id = 4, 1, 0) DAY), NOW()) <= IF(DATEDIFF(ADDDATE(ADDTIME(s.date, s.begin), INTERVAL IF(s.slot_id = 4, 1, 0) DAY), s.created) <= :gooddelay, SEC_TO_TIME(TIME_TO_SEC(TIMEDIFF(ADDDATE(ADDTIME(s.date, s.begin), INTERVAL IF(s.slot_id = 4, 1, 0) DAY), s.created))*3/4), SEC_TO_TIME(:gooddelay*24*3600)) 
 798 ORDER BY ADDDATE(ADDTIME(s.date, s.begin), INTERVAL IF(s.slot_id = 4, 1, 0) DAY) ASC, s.created ASC 
 801                 //Replace bundle entity name by table name 
 802                 $req = str_replace(array_keys($tables), array_values($tables), $req); 
 804                 //Get result set mapping instance 
 805                 $rsm = new ResultSetMapping(); 
 809                         ->addEntityResult('RapsysAirBundle:Session', 's') 
 810                         ->addFieldResult('s', 'id', 'id') 
 811                         ->addIndexBy('s', 'id'); 
 815                         ->createNativeQuery($req, $rsm) 
 816                         ->setParameter('limit', PHP_INT_MAX
) 
 817                         ->setParameter('gooddelay', self
::GOOD_DELAY
) 
 822          * Find all session pending hourly weather 
 824          * @return array<Session> The sessions to update 
 826         public function findAllPendingHourlyWeather() { 
 828                 $em = $this->getEntityManager(); 
 831                 $qs = $em->getConfiguration()->getQuoteStrategy(); 
 832                 $dp = $em->getConnection()->getDatabasePlatform(); 
 834                 //Get quoted table names 
 835                 //XXX: this allow to make this code table name independent 
 837                         'RapsysAirBundle:Session' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Session'), $dp), 
 838                         'RapsysAirBundle:Slot' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Slot'), $dp), 
 839                         'RapsysAirBundle:Location' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Location'), $dp), 
 844                 //Select all sessions starting and stopping in the next 3 days 
 845                 //XXX: select session starting after now and stopping before date(now)+3d as accuweather only provide hourly data for the next 3 days (INTERVAL 3 DAY) 
 846                 //XXX: we may remove ADDDATE(start, INTERVAL IF(s.slot_id = 4, 1, 0) DAY) used for after specificity 
 848 SELECT s.id, s.slot_id, s.location_id, s.date, s.begin, s.length, s.rainfall, s.rainrisk, s.realfeel, s.realfeelmin, s.realfeelmax, s.temperature, s.temperaturemin, s.temperaturemax, l.zipcode, o.title 
 849 FROM RapsysAirBundle:Session AS s 
 850 JOIN RapsysAirBundle:Slot AS o ON (o.id = s.slot_id) 
 851 JOIN RapsysAirBundle:Location AS l ON (l.id = s.location_id) 
 852 WHERE ADDDATE(ADDTIME(s.date, s.begin), INTERVAL IF(s.slot_id = 4, 1, 0) DAY) >= NOW() AND ADDDATE(ADDTIME(ADDTIME(s.date, s.begin), s.length), INTERVAL IF(s.slot_id = 4, 1, 0) DAY) < DATE(ADDDATE(NOW(), INTERVAL :accuhourly DAY)) 
 855                 //Replace bundle entity name by table name 
 856                 $req = str_replace(array_keys($tables), array_values($tables), $req); 
 858                 //Get result set mapping instance 
 859                 $rsm = new ResultSetMapping(); 
 863                         ->addEntityResult('RapsysAirBundle:Session', 's') 
 864                         ->addFieldResult('s', 'id', 'id') 
 865                         ->addFieldResult('s', 'date', 'date') 
 866                         ->addFieldResult('s', 'begin', 'begin') 
 867                         ->addFieldResult('s', 'length', 'length') 
 868                         ->addFieldResult('s', 'rainfall', 'rainfall') 
 869                         ->addFieldResult('s', 'rainrisk', 'rainrisk') 
 870                         ->addFieldResult('s', 'realfeel', 'realfeel') 
 871                         ->addFieldResult('s', 'realfeelmin', 'realfeelmin') 
 872                         ->addFieldResult('s', 'realfeelmax', 'realfeelmax') 
 873                         ->addFieldResult('s', 'temperature', 'temperature') 
 874                         ->addFieldResult('s', 'temperaturemin', 'temperaturemin') 
 875                         ->addFieldResult('s', 'temperaturemax', 'temperaturemax') 
 876                         ->addJoinedEntityResult('RapsysAirBundle:Slot', 'o', 's', 'slot') 
 877                         ->addFieldResult('o', 'slot_id', 'id') 
 878                         ->addFieldResult('o', 'title', 'title') 
 879                         ->addJoinedEntityResult('RapsysAirBundle:Location', 'l', 's', 'location') 
 880                         ->addFieldResult('l', 'location_id', 'id') 
 881                         ->addFieldResult('l', 'zipcode', 'zipcode') 
 882                         ->addIndexBy('s', 'id'); 
 886                         ->createNativeQuery($req, $rsm) 
 887                         ->setParameter('accuhourly', self
::ACCUWEATHER_HOURLY
) 
 892          * Find all session pending daily weather 
 894          * @return array<Session> The sessions to update 
 896         public function findAllPendingDailyWeather() { 
 898                 $em = $this->getEntityManager(); 
 901                 $qs = $em->getConfiguration()->getQuoteStrategy(); 
 902                 $dp = $em->getConnection()->getDatabasePlatform(); 
 904                 //Get quoted table names 
 905                 //XXX: this allow to make this code table name independent 
 907                         'RapsysAirBundle:Session' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Session'), $dp), 
 908                         'RapsysAirBundle:Slot' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Slot'), $dp), 
 909                         'RapsysAirBundle:Location' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Location'), $dp), 
 914                 //Select all sessions stopping after next 3 days 
 915                 //XXX: select session stopping after or equal date(now)+3d as accuweather only provide hourly data for the next 3 days (INTERVAL 3 DAY) 
 916                 //XXX: we may remove ADDDATE(start, INTERVAL IF(s.slot_id = 4, 1, 0) DAY) used for after specificity 
 918 SELECT s.id, s.slot_id, s.location_id, s.date, s.begin, s.length, s.rainfall, s.rainrisk, s.realfeel, s.realfeelmin, s.realfeelmax, s.temperature, s.temperaturemin, s.temperaturemax, l.zipcode, o.title 
 919 FROM RapsysAirBundle:Session AS s 
 920 JOIN RapsysAirBundle:Slot AS o ON (o.id = s.slot_id) 
 921 JOIN RapsysAirBundle:Location AS l ON (l.id = s.location_id) 
 922 WHERE ADDDATE(ADDTIME(ADDTIME(s.date, s.begin), s.length), INTERVAL IF(s.slot_id = 4, 1, 0) DAY) >= DATE(ADDDATE(NOW(), INTERVAL :accuhourly DAY)) AND ADDDATE(ADDTIME(ADDTIME(s.date, s.begin), s.length), INTERVAL IF(s.slot_id = 4, 1, 0) DAY) < DATE(ADDDATE(NOW(), INTERVAL :accudaily DAY)) 
 925                 //Replace bundle entity name by table name 
 926                 $req = str_replace(array_keys($tables), array_values($tables), $req); 
 928                 //Get result set mapping instance 
 929                 $rsm = new ResultSetMapping(); 
 933                         ->addEntityResult('RapsysAirBundle:Session', 's') 
 934                         ->addFieldResult('s', 'id', 'id') 
 935                         ->addFieldResult('s', 'date', 'date') 
 936                         ->addFieldResult('s', 'begin', 'begin') 
 937                         ->addFieldResult('s', 'length', 'length') 
 938                         ->addFieldResult('s', 'rainfall', 'rainfall') 
 939                         ->addFieldResult('s', 'rainrisk', 'rainrisk') 
 940                         ->addFieldResult('s', 'realfeel', 'realfeel') 
 941                         ->addFieldResult('s', 'realfeelmin', 'realfeelmin') 
 942                         ->addFieldResult('s', 'realfeelmax', 'realfeelmax') 
 943                         ->addFieldResult('s', 'temperature', 'temperature') 
 944                         ->addFieldResult('s', 'temperaturemin', 'temperaturemin') 
 945                         ->addFieldResult('s', 'temperaturemax', 'temperaturemax') 
 946                         ->addJoinedEntityResult('RapsysAirBundle:Slot', 'o', 's', 'slot') 
 947                         ->addFieldResult('o', 'slot_id', 'id') 
 948                         ->addFieldResult('o', 'title', 'title') 
 949                         ->addJoinedEntityResult('RapsysAirBundle:Location', 'l', 's', 'location') 
 950                         ->addFieldResult('l', 'location_id', 'id') 
 951                         ->addFieldResult('l', 'zipcode', 'zipcode') 
 952                         ->addIndexBy('s', 'id'); 
 956                         ->createNativeQuery($req, $rsm) 
 957                         ->setParameter('accudaily', self
::ACCUWEATHER_DAILY
) 
 958                         ->setParameter('accuhourly', self
::ACCUWEATHER_HOURLY
) 
 963          * Fetch session best application by session id 
 965          * @param int $id The session id 
 966          * @return Application|null The application or null 
 968         public function findBestApplicationById($id) { 
 970                 $em = $this->getEntityManager(); 
 973                 $qs = $em->getConfiguration()->getQuoteStrategy(); 
 974                 $dp = $em->getConnection()->getDatabasePlatform(); 
 976                 //Get quoted table names 
 977                 //XXX: this allow to make this code table name independent 
 979                         'RapsysAirBundle:Application' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Application'), $dp), 
 980                         'RapsysAirBundle:Session' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Session'), $dp), 
 986                  * Query session applications ranked by score, created and user_id 
 988                  * @xxx User with bad behaviour application are excluded until remaining <= baddelay: 
 989                  * - with (count(premium = 1)+1)/(count(premium = 0)+1) >= 1 
 990                  * - with count(sessions) <= 5 
 991                  * - with average(temperature) >= average(temperature other) + 10 
 993                  * @xxx Magic happen on this line: 
 994                  * WHERE IF(d.pnp_rate >= 1, d.remaining <= SEC_TO_TIME(:baddelay), 1) AND IF(d.s_count <= 5, d.remaining <= SEC_TO_TIME(:baddelay), 1) AND IF(d.tr_rate >= (d.otr_rate + 10), d.remaining <= SEC_TO_TIME(:baddelay), 1) 
 996                  * @todo Limit score on last year only ??? 
 997                  * AND DATEDIFF(s.date, NOW()) <= 365 
 999                  * TODO: we may add ADDDATE(start, INTERVAL IF(s.slot_id = 4, 1, 0) DAY) used for after specificity 
1002 SELECT d.id, d.score 
1013                 AVG(IF(a4.id IS NOT NULL, s4.temperature/(1+s4.rainfall), NULL)) AS otr_rate, 
1023                         COUNT(a3.id) AS s_count, 
1024                         AVG(IF(a3.id IS NOT NULL AND s3.temperature IS NOT NULL AND s3.rainfall IS NOT NULL, s3.temperature/(1+s3.rainfall), NULL)) AS tr_rate, 
1025                         (SUM(IF(a3.id IS NOT NULL AND s3.premium = 1, 1, 0))+1)/(SUM(IF(a3.id IS NOT NULL AND s3.premium = 0, 1, 0))+1) AS pnp_rate 
1031                                 SUM(IF(a2.id IS NOT NULL, 1/ABS(DATEDIFF(s.date, s2.date)), 0)) AS score, 
1032                                 TIMEDIFF(ADDTIME(s.date, s.begin), NOW()) AS remaining, 
1035                         JOIN applications AS a ON (a.session_id = s.id AND a.canceled IS NULL) 
1036                         LEFT JOIN sessions AS s2 ON (s2.id != s.id AND s2.location_id = s.location_id AND s2.slot_id = s.slot_id AND s2.application_id IS NOT NULL) 
1037                         LEFT JOIN applications AS a2 ON (a2.id = s2.application_id AND a2.user_id = a.user_id AND (a2.canceled IS NULL OR TIMESTAMPDIFF(DAY, a2.canceled, ADDTIME(s2.date, s2.begin)) < 1)) 
1043                 LEFT JOIN sessions AS s3 ON (s3.id != b.session_id AND s3.application_id IS NOT NULL) 
1044                 LEFT JOIN applications AS a3 ON (a3.id = s3.application_id AND a3.user_id = b.user_id AND (a3.canceled IS NULL OR TIMESTAMPDIFF(DAY, a3.canceled, ADDTIME(s3.date, s3.begin)) < 1)) 
1049         LEFT JOIN sessions AS s4 ON (s4.id != c.session_id AND s4.application_id IS NOT NULL AND s4.temperature IS NOT NULL AND s4.rainfall IS NOT NULL) 
1050         LEFT JOIN applications AS a4 ON (a4.id = s4.application_id AND a4.user_id != c.user_id AND (a4.canceled IS NULL OR TIMESTAMPDIFF(DAY, a4.canceled, ADDTIME(s4.date, s4.begin)) < 1)) 
1055 WHERE IF(d.pnp_rate >= 1, d.remaining <= SEC_TO_TIME(:baddelay), 1) AND IF(d.s_count <= 5, d.remaining <= SEC_TO_TIME(:baddelay), 1) AND IF(d.tr_rate >= (d.otr_rate + 10), d.remaining <= SEC_TO_TIME(:baddelay), 1) 
1056 ORDER BY d.score ASC, d.created ASC, d.user_id ASC 
1059                 //Replace bundle entity name by table name 
1060                 $req = str_replace(array_keys($tables), array_values($tables), $req); 
1062                 //Set update request 
1063                 $upreq = 'UPDATE RapsysAirBundle:Application SET score = :score, updated = NOW() WHERE id = :id'; 
1065                 //Replace bundle entity name by table name 
1066                 $upreq = str_replace(array_keys($tables), array_values($tables), $upreq); 
1068                 //Get result set mapping instance 
1069                 $rsm = new ResultSetMapping(); 
1071                 //Declare all fields 
1073                         ->addEntityResult('RapsysAirBundle:Application', 'a') 
1074                         ->addFieldResult('a', 'id', 'id') 
1075                         ->addFieldResult('a', 'score', 'score') 
1076                         ->addIndexBy('a', 'id'); 
1079                 //XXX: setting limit in subqueries is required to prevent mariadb optimisation 
1081                         ->createNativeQuery($req, $rsm) 
1082                         ->setParameter('sid', $id) 
1083                         ->setParameter('baddelay', self
::BAD_DELAY
*24*3600) 
1084                         ->setParameter('limit', PHP_INT_MAX
) 
1085                         //XXX: removed, we update score before returning best candidate 
1086                         //->getOneOrNullResult(Query::HYDRATE_SINGLE_SCALAR); 
1093                 foreach($applications as $application) { 
1094                         //Check if we already saved best candidate 
1095                         if ($ret === null) { 
1096                                 //Return first application 
1097                                 $ret = $application; 
1100                         //Update application updated field 
1101                         //XXX: updated field is not modified for user with bad behaviour as application is not retrieved until delay is reached 
1102                         $em->getConnection()->executeUpdate($upreq, ['id' => $application->getId(), 'score' => $application->getScore()], ['id' => Type
::INTEGER, 'score' => Type
::FLOAT]); 
1105                 //Return best ranked application