Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 34 |
| GenericSurveyQuery | |
0.00% |
0 / 1 |
|
0.00% |
0 / 3 |
20.00 | |
0.00% |
0 / 32 |
| active | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 17 |
|||
| userSelector | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 3 |
|||
| availableForUser | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 12 |
|||
| <?php | |
| namespace common\models; | |
| use Yii; | |
| /** | |
| * Generic implementation of interface | |
| */ | |
| trait GenericSurveyQuery { | |
| private $subQueryJoined = false; | |
| /** | |
| * Select only active surveys right now | |
| */ | |
| public function active() | |
| { | |
| $this->andWhere( | |
| ['AND', | |
| ['OR', | |
| 'available_from <= :time', | |
| 'available_from IS NULL', | |
| ], | |
| ['OR', | |
| 'available_to >= :time', | |
| 'available_to IS NULL', | |
| ], | |
| ], [ | |
| ':time' => date('Y-m-d H:i:s'), | |
| ] | |
| ); | |
| return $this; | |
| } | |
| protected $subQuery; | |
| protected $foreignColumn; | |
| protected function userSelector($user_id) | |
| { | |
| return ['user_id' => $user_id]; | |
| } | |
| /** | |
| * Select only available surveys for user | |
| */ | |
| public function availableForUser($user_id) | |
| { | |
| $this->subQuery->select([$this->foreignColumn]); | |
| $this->subQuery->andWhere($this->userSelector($user_id)); | |
| if(!$this->subQueryJoined) | |
| { | |
| $class = $this->modelClass; | |
| $tableName = $class::tableName(); | |
| $this->andWhere(['NOT IN', $tableName . '.[[id]]', $this->subQuery]); | |
| $this->subQueryJoined = true; | |
| } | |
| return $this; | |
| } | |
| } |