You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.4 KiB
105 lines
3.4 KiB
<?php
|
|
|
|
namespace Entity\Repository;
|
|
|
|
use Doctrine\ORM\EntityRepository;
|
|
|
|
/**
|
|
* ItemPropertyRepository
|
|
*
|
|
* This class was generated by the Doctrine ORM. Add your own custom
|
|
* repository methods below.
|
|
*/
|
|
class ItemPropertyRepository extends EntityRepository
|
|
{
|
|
/**
|
|
*
|
|
* @param $tool
|
|
* @param $itemId
|
|
* @param \Entity\EntityCourse $course
|
|
* @param int $sessionId
|
|
* @param int $groupId
|
|
*
|
|
* @return \Doctrine\ORM\QueryBuilder
|
|
*/
|
|
public function getUsersSubscribedToItem($tool, $itemId, \Entity\EntityCourse $course, $sessionId = 0, $groupId = 0)
|
|
{
|
|
/*$items = $this->findBy(array(
|
|
'course' => $course,
|
|
'idSession' => $sessionId,
|
|
'toGroupId' => $groupId
|
|
));
|
|
return $items;*/
|
|
|
|
$qb = $this->createQueryBuilder('i')
|
|
->select('i');
|
|
|
|
$wherePart = $qb->expr()->andx();
|
|
|
|
//Selecting courses for users
|
|
$qb->innerJoin('i.user', 'u');
|
|
|
|
$wherePart->add($qb->expr()->eq('i.tool', $qb->expr()->literal($tool)));
|
|
$wherePart->add($qb->expr()->eq('i.lasteditType', $qb->expr()->literal('LearnpathSubscription')));
|
|
$wherePart->add($qb->expr()->eq('i.ref', $itemId));
|
|
$wherePart->add($qb->expr()->eq('i.cId', $course->getId()));
|
|
$wherePart->add($qb->expr()->eq('i.idSession', $sessionId));
|
|
$wherePart->add($qb->expr()->eq('i.toGroupId', $groupId));
|
|
|
|
$qb->where($wherePart);
|
|
$q = $qb->getQuery();
|
|
return $q->execute();
|
|
}
|
|
|
|
public function SubscribedUsersToItem($tool, \Entity\EntityCourse $course, $sessionId, $lpId, $newUserList = array()) {
|
|
$em = $this->getEntityManager();
|
|
|
|
$user = $em->getRepository('Entity\EntityUser');
|
|
$courseRepo = $em->getRepository('Entity\EntityCourse');
|
|
|
|
$usersSubscribedToItem = $this->getUsersSubscribedToItem($tool, $lpId, $course, $sessionId);
|
|
$users = $courseRepo->getSubscribedStudents($course);
|
|
|
|
$alreadyAddedUsers = array();
|
|
if ($usersSubscribedToItem) {
|
|
foreach ($usersSubscribedToItem as $itemProperty) {
|
|
$alreadyAddedUsers[] = $itemProperty->getToUserId();
|
|
}
|
|
}
|
|
|
|
$usersToDelete = $alreadyAddedUsers;
|
|
|
|
if (!empty($newUserList)) {
|
|
$usersToDelete = array_diff($alreadyAddedUsers, $newUserList);
|
|
}
|
|
|
|
if ($usersToDelete) {
|
|
foreach ($usersToDelete as $userToDelete) {
|
|
$item = $this->findOneBy(array(
|
|
'tool' => $tool,
|
|
'idSession' => $sessionId,
|
|
'ref' => $lpId,
|
|
'toUserId' => $userToDelete
|
|
));
|
|
if ($item) {
|
|
$em->remove($item);
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach ($newUserList as $userId) {
|
|
$userObj = $user->find($userId);
|
|
if (!in_array($userId, $alreadyAddedUsers)) {
|
|
$item = new \Entity\EntityCItemProperty($userObj, $course);
|
|
$item->setTool($tool);
|
|
$item->setRef($lpId);
|
|
$item->setIdSession($sessionId);
|
|
$item->setToGroupId(0);
|
|
$item->setLasteditType('LearnpathSubscription');
|
|
$item->setVisibility('1');
|
|
$em->persist($item); //$em is an instance of EntityManager
|
|
}
|
|
}
|
|
$em->flush();
|
|
}
|
|
} |