Skip to content

Instantly share code, notes, and snippets.

@jschroed91
Last active August 29, 2015 14:13
Show Gist options
  • Save jschroed91/b40be1752e7bea2d897d to your computer and use it in GitHub Desktop.
Save jschroed91/b40be1752e7bea2d897d to your computer and use it in GitHub Desktop.
/**
* Returns an array of eligible Parted Proposals
* Eligible means having the same proponent info as a given potential child
*
* @param AbstractCollaborationProposal
* @return AbstractCollaborationProposal[]
*/
public function findEligiblePartedProposals(AbstractCollaborationProposal $childProposal)
{
/* @var $qb QueryBuilder */
$qb = $this->managerRegistry
->getManagerForClass('ProposalBundle:AbstractCollaborationProposal')
->createQueryBuilder();
$qb->select('p')
->from('ProposalBundle:AbstractCollaborationProposal', 'p')
->join('p.parentProposalMetadata','parentMetadata');
$roleNames = array(Role::PROPONENT, Role::CO_PROPONENT);
$collaboratorCount = 0;
foreach ($childProposal->getCollaborators() as $index => $collaborator) {
if (in_array($collaborator->getRole()->getName(), $roleNames)) {
$collaboratorCount++;
$alias = "c$index";
$roleKey = $alias . "_role";
$userKey = $alias . "_user";
$qb->join('p.collaborators', $alias, 'WITH', $qb->expr()->andX(
$qb->expr()->eq("$alias.role", ":$roleKey"),
$qb->expr()->eq("$alias.user", ":$userKey")
))
->setParameter($roleKey, $collaborator->getRole())
->setParameter($userKey, $collaborator->getUser());
}
}
$qb->leftJoin('ProposalBundle:Role', 'r', 'WITH', $qb->expr()->in('r.name', ':roleNames'))
->leftJoin('p.collaborators', 'collaborator', 'WITH', $qb->expr()->eq('collaborator.role', 'r'))
->groupBy('p.id')
->having($qb->expr()->eq('COUNT(collaborator)', ':collaboratorCount'))
->setParameter('collaboratorCount', $collaboratorCount)
->setParameter('roleNames', $roleNames);
return $qb->getQuery()->getResult();
}
public function testFindEligiblePartedProposals()
{
$this->logIn();
$user = $this->getMockUser(MockUsers::DEFAULT_USER);
$doctorUser = $this->getMockUser(MockUsers::DOCTOR_USER);
$governmentalUser = $this->getMockUser(MockUsers::GOVERNMENTAL_USER);
$partedProposal = $this->createProposalEntity();
$childProposal = $this->createProposalEntity();
$childProposal2 = $this->createProposalEntity();
$this->partParentManager->createPartedProposal($partedProposal);
$this->createAndAddCollaborator($doctorUser, Role::CO_PROPONENT, $partedProposal);
$this->createAndAddCollaborator($doctorUser, Role::CO_PROPONENT, $childProposal);
$this->createAndAddCollaborator($doctorUser, Role::CO_PROPONENT, $childProposal2);
$this->createAndAddCollaborator($governmentalUser, Role::CO_PROPONENT, $childProposal2);
static::$entityManager->flush();
static::$entityManager->clear();
$this->assertInstanceOf(
'\Caxy\ProposalBundle\Entity\PartedProposalMetadata',
$partedProposal->getParentProposalMetadata()
);
\Doctrine\Common\Util\Debug::dump(count($this->partParentManager->findEligiblePartedProposals($childProposal)));
\Doctrine\Common\Util\Debug::dump(count($this->partParentManager->findEligiblePartedProposals($childProposal2)));
}
protected function createAndAddCollaborator($user, $roleName, $proposal)
{
$collab = $this->collaboratorManager->createCollaborator($user, $roleName, CollaboratorStatus::ACCEPTED);
$collab->setProposal($proposal);
$proposal->addCollaborator($collab);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment