public function share(array $shareRequest, User $user)
    {
        $errors = $this->validateShareRequest($shareRequest);
        if (count($errors) > 0) {
            throw new InvalidDataException('Share request is not valid', $errors);
        }

        $adminRights = isset($shareRequest['adminRights']) && $shareRequest['adminRights'];

        /** @var ItemRepository $questionRepo */
        $questionRepo = $this->om->getRepository('UJMExoBundle:Item\Item');
        // Loaded questions (we load it to be sure it exist)
        $questions = $questionRepo->findByUuids($shareRequest['questions']);

        /** @var UserRepository $userRepo */
        $userRepo = $this->om->getRepository('ClarolineCoreBundle:User');
        // Loaded users (we load it to be sure it exist)
        $users = $userRepo->findByIds($shareRequest['users']);

        // Share each question with each user
        foreach ($questions as $question) {
            if ($this->itemManager->canEdit($question, $user)) {
                $sharedWith = $this->om
                    ->getRepository('UJMExoBundle:Item\Shared')
                    ->findBy(['question' => $question]);

                foreach ($users as $user) {
                    $shared = $this->getSharedForUser($user, $sharedWith);
                    if (empty($shared)) {
                        $shared = new Shared();
                        $shared->setQuestion($question);
                        $shared->setUser($user);
                    }

                    $shared->setAdminRights($adminRights);
                    $this->om->persist($shared);
                }
            }
        }

        $this->om->flush();
    }
public function share(array $shareRequest, User $user)
    {
        $errors = $this->validateShareRequest($shareRequest);
        if (count($errors) > 0) {
            throw new InvalidDataException('Share request is not valid', $errors);
        }

        $adminRights = isset($shareRequest['adminRights']) && $shareRequest['adminRights'];

        /** @var ItemRepository $questionRepo */
        $questionRepo = $this->om->getRepository('UJMExoBundle:Item\Item');
        // Loaded questions (we load it to be sure it exist)
        $questions = $questionRepo->findByUuids($shareRequest['questions']);

        /** @var UserRepository $userRepo */
        $userRepo = $this->om->getRepository('ClarolineCoreBundle:User');
        // Loaded users (we load it to be sure it exist)
        $users = $userRepo->findByIds($shareRequest['users']);

        // Share each question with each user
        foreach ($questions as $question) {
            if ($this->itemManager->canEdit($question, $user)) {
                $sharedWith = $this->om
                    ->getRepository('UJMExoBundle:Item\Shared')
                    ->findBy(['question' => $question]);

                foreach ($users as $user) {
                    $shared = $this->getSharedForUser($user, $sharedWith);
                    if (empty($shared)) {
                        $shared = new Shared();
                        $shared->setQuestion($question);
                        $shared->setUser($user);
                    }

                    $shared->setAdminRights($adminRights);
                    $this->om->persist($shared);
                }
            }
        }

        $this->om->flush();
    }
public function share ( array $ shareRequest , User $ user ) { $ errors = $ this -> validateShareRequest ( $ shareRequest ) ; if ( count ( $ errors ) > 0 ) { throw new InvalidDataException ( 'Share request is not valid' , $ errors ) ; } $ adminRights = isset ( $ shareRequest [ 'adminRights' ] ) && $ shareRequest [ 'adminRights' ] ; /** @var ItemRepository $questionRepo */ $ questionRepo = $ this -> om -> getRepository ( 'UJMExoBundle:Item\Item' ) ; // Loaded questions (we load it to be sure it exist) $ questions = $ questionRepo -> findByUuids ( $ shareRequest [ 'questions' ] ) ; /** @var UserRepository $userRepo */ $ userRepo = $ this -> om -> getRepository ( 'ClarolineCoreBundle:User' ) ; // Loaded users (we load it to be sure it exist) $ users = $ userRepo -> findByIds ( $ shareRequest [ 'users' ] ) ; // Share each question with each user foreach ( $ questions as $ question ) { if ( $ this -> itemManager -> canEdit ( $ question , $ user ) ) { $ sharedWith = $ this -> om -> getRepository ( 'UJMExoBundle:Item\Shared' ) -> findBy ( [ 'question' => $ question ] ) ; foreach ( $ users as $ user ) { $ shared = $ this -> getSharedForUser ( $ user , $ sharedWith ) ; if ( empty ( $ shared ) ) { $ shared = new Shared ( ) ; $ shared -> setQuestion ( $ question ) ; $ shared -> setUser ( $ user ) ; } $ shared -> setAdminRights ( $ adminRights ) ; $ this -> om -> persist ( $ shared ) ; } } } $ this -> om -> flush ( ) ; }
Shares a list of question to users.

@param array $shareRequest - an object containing the questions and users to link
@param User  $user

@throws InvalidDataException
Shares a list of question to users .