Advertisement
Guest User

Untitled

a guest
Jan 24th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.17 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Cursometr\Facts\CourseStructure;
  4.  
  5. use App\Cursometr\Facts\UsingQuery;
  6. use Tinderbox\ClickhouseBuilder\Integrations\Laravel\Builder;
  7. use Tinderbox\ClickhouseBuilder\Query\Identifier;
  8.  
  9. class CourseStructureLearnersTotalFact extends AbstractCourseStructureFact implements UsingQuery
  10. {
  11.     protected $format = 'integer';
  12.     protected $defaultValue = 0;
  13.    
  14.     public static function getName(): string
  15.     {
  16.         return 'learners-total';
  17.     }
  18.  
  19.     public function getQuery(\Closure $queryCallback = null): Builder
  20.     {
  21.         $learnersQuery = $this->getCleanQuery()
  22.             ->table('learners')
  23.             ->arrayJoin('entitiesAttempts')
  24.             ->select('learnerId', 'entitiesAttempts.entityIri as entityIri')
  25.             ->applyWherePeriod('entitiesAttempts.startedAt', $this->getPeriod())
  26.             ->final();
  27.  
  28.         if (!is_null($queryCallback)) {
  29.             $learnersQuery = $queryCallback($learnersQuery, $this);
  30.         }
  31.  
  32.         $attemptsQuery = $this->getCleanQuery()
  33.             ->from($learnersQuery)
  34.             ->select('learnerId', 'entityIri as childEntityIri')
  35.             ->where('learnerId', '>', 0);
  36.  
  37.         /*
  38.          * Запрос на распаковывание таблицы с IRI
  39.          */
  40.         $longTableQuery = $this->getCleanQuery()
  41.                                ->table(new Identifier($this->getTempTableName()))
  42.                                ->select('entityIri', 'childEntityIri')
  43.                                ->arrayJoin('childs as childEntityIri');
  44.  
  45.         $summaryJoinQuery = $this->getCleanQuery()
  46.                              ->from($longTableQuery)
  47.                              ->select('entityIri', 'learnerId')
  48.                              ->allLeftJoin($attemptsQuery, ['childEntityIri']);
  49.  
  50.         $finalQuery = $this->getCleanQuery()
  51.             ->from($summaryJoinQuery)
  52.             ->select(raw('uniq(learnerId) as '.static::getIdentifier()), 'entityIri')
  53.             ->where('learnerId', '>', 0)
  54.             ->groupBy('entityIri');
  55.  
  56.         return $finalQuery;
  57.     }
  58.  
  59.     public function getJoinColumns(): array
  60.     {
  61.         return ['entityIri'];
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement