Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 56
MeetingHelper
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 7
72.00
0.00% covered (danger)
0.00%
0 / 56
 findAvailableParticipants
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 14
 canMeet
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 5
 getEventSpans
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 7
 getMeetParticipantsHost
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 6
 getMeetParticipantsGuest
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 6
 getMeetingCountQuery
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 9
 getEventSpanCountQuery
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 9
<?php
namespace common\components;
use Yii;
use common\models\Participant;
use common\models\EventSpan;
use common\models\Meeting;
use yii\db\Query;
class MeetingHelper
{
    /**
     * @param integer $participant
     * @return Query
     */
    public static function findAvailableParticipants($participant)
    {
        $eventSpans = static::getEventSpans($participant);
        $meetParticipantsHost = static::getMeetParticipantsHost($participant);
        $meetParticipantsGuest = static::getMeetParticipantsGuest($participant);
        $participants = Participant::find()
            ->from(['participant' => Participant::tableName()])
            ->innerJoin(['ees' => '{{%participant_event_span}}'], 'ees.participant_id = participant.id')
            ->where(['<>', 'participant.id', $participant])
            ->andWhere(['=', 'participant.is_support_only', false])
            ->andWhere(['IN', 'ees.event_span_id', $eventSpans])
            ->andWhere(['NOT IN', 'participant.id', $meetParticipantsHost])
            ->andWhere(['NOT IN', 'participant.id', $meetParticipantsGuest]);
        return $participants;
    }
    /**
     * @param integer $participant1
     * @param integer $participant2
     * @return boolean
     */
    public static function canMeet($participant1, $participant2)
    {
        // If they already have meeting
        $meetingsQuery = static::getMeetingCountQuery($participant1, $participant2);
        // Get my & partner event spans
        $eventSpansQuery = static::getEventSpanCountQuery($participant1, $participant2);
        return ($meetingsQuery->count() == 0) && ($eventSpansQuery->count() > 0);
    }
    /**
     * @param integer $participant
     * @return Query
     */
    protected static function getEventSpans($participant)
    {
        return (new Query)
            ->select('ees.event_span_id')
            ->from(['e' => Participant::tableName()])
            ->innerJoin(['ees' => '{{%participant_event_span}}'], 'ees.participant_id = e.id')
            ->where(['e.id' => $participant]);
    }
    /**
     * @param integer $participant
     * @return Query
     */
    protected static function getMeetParticipantsHost($participant)
    {
        return (new Query)
            ->select('m.guest_participant_id')
            ->from(['m' => Meeting::tableName()])
            ->where(['m.host_participant_id' => $participant]);
    }
    /**
     * @param integer $participant
     * @return Query
     */
    protected static function getMeetParticipantsGuest($participant)
    {
        return (new Query)
            ->select('m.host_participant_id')
            ->from(['m' => Meeting::tableName()])
            ->where(['m.guest_participant_id' => $participant]);
    }
    /**
     * @param integer $participant1
     * @param integer $participant2
     * @return Query
     */
    protected static function getMeetingCountQuery($participant1, $participant2)
    {
        return Meeting::find()
            ->where([
                'or', 
                ['and', 'host_participant_id = :eid1', 'guest_participant_id = :eid2'],
                ['and', 'host_participant_id = :eid2', 'guest_participant_id = :eid1']
            ])
            ->params([':eid1' => $participant1, ':eid2' => $participant2]);
    }
    /**
     * @param integer $participant1
     * @param integer $participant2
     * @return Query
     */
    protected static function getEventSpanCountQuery($participant1, $participant2)
    {
        return (new Query())
            ->select('COUNT(*) AS participant_count')
            ->from('{{%participant_event_span}}')
            ->where(['or', 'participant_id = :eid1', 'participant_id = :eid2'])
            ->groupBy('event_span_id')
            ->having(['participant_count' => 2])
            ->params([':eid1' => $participant1, ':eid2' => $participant2]);
    }
}