Exemple #1
0
 /**
  * @param ResourceRights $right
  * @return $this
  */
 public function addRight(ResourceRights $right)
 {
     $right->setResourceLink($this);
     $this->rights[] = $right;
     return $this;
 }
 /**
  * @param Request $request
  *
  * @return RedirectResponse|Response
  */
 public function createAction(Request $request)
 {
     /** @var AbstractResource $resource */
     $resource = $this->createNew();
     $form = $this->getForm($resource);
     $this->denyAccessUnlessGranted(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER);
     if ($form->handleRequest($request)->isValid()) {
         $sharedType = $form->get('shared')->getData();
         $shareList = array();
         switch ($sharedType) {
             case 'this_course':
                 // Default Chamilo behaviour:
                 // Teachers can edit and students can see
                 $shareList = array(array('sharing' => 'course', 'mask' => ResourceNodeVoter::getReaderMask(), 'role' => ResourceNodeVoter::ROLE_CURRENT_COURSE_STUDENT, 'search' => $this->getCourse()->getId()), array('sharing' => 'course', 'mask' => ResourceNodeVoter::getEditorMask(), 'role' => ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER, 'search' => $this->getCourse()->getId()));
                 break;
             case 'shared':
                 $shareList = $form->get('rights')->getData();
                 break;
             case 'only_me':
                 $shareList = array(array('sharing' => 'user', 'only_me' => true));
                 break;
         }
         /** @var NotebookRepository $repository */
         $repository = $this->getRepository();
         $resourceNode = $repository->addResourceNode($resource, $this->getUser());
         // Loops all sharing options
         foreach ($shareList as $share) {
             $idList = array();
             if (isset($share['search'])) {
                 $idList = explode(',', $share['search']);
             }
             $resourceRight = new ResourceRights();
             $resourceRight->setMask($share['mask']);
             $resourceRight->setRole($share['role']);
             // Build links
             switch ($share['sharing']) {
                 case 'everyone':
                     $repository->addResourceToEveryone($resourceNode, $resourceRight);
                     break;
                 case 'course':
                     $repository->addResourceToCourse($resourceNode, $this->getCourse(), $resourceRight);
                     break;
                 case 'session':
                     $repository->addResourceToSession($resourceNode, $this->getCourse(), $this->getSession(), $resourceRight);
                     break;
                 case 'user':
                     // Only for me
                     if (isset($rights['only_me'])) {
                         $repository->addResourceOnlyToMe($resourceNode);
                     } else {
                         // To other users
                         $repository->addResourceToUserList($resourceNode, $idList);
                     }
                     break;
                 case 'group':
                     // @todo
                     break;
             }
         }
         $resource->setResourceNode($resourceNode);
         $resource = $this->domainManager->create($resource);
         if ($this->config->isApiRequest()) {
             return $this->handleView($this->view($resource));
         }
         if (null === $resource) {
             return $this->redirectHandler->redirectToIndex();
         }
         return $this->redirectHandler->redirectTo($resource);
     }
     if ($this->config->isApiRequest()) {
         return $this->handleView($this->view($form));
     }
     $view = $this->view()->setTemplate($this->config->getTemplate('create.html'))->setData(array($this->config->getResourceName() => $resource, 'form' => $form->createView()));
     return $this->handleView($view);
 }