/** * @param File $template * @param User $owner * @param bool $isValidated * * @throws InvalidConfigurationException * * @return SimpleWorkbolspace * * The template doesn't need to be validated anymore if * - it comes from the self::import() function * - we want to create a user from the default template (it should work no matter what) */ public function createWorkspace(Workspace $workspace, File $template, $isValidated = false) { $data = $this->container->get('claroline.manager.workspace_manager')->getTemplateData($template, true); if ($workspace->getCode() === null) { $workspace->setCode($data['parameters']['code']); } if ($workspace->getName() === null) { $workspace->setName($data['parameters']['name']); } //just to be sure doctrine is ok before doing all the workspace $this->om->startFlushSuite(); $this->setImporters($template, $workspace->getCreator()); if (!$isValidated) { $this->validate($data, false); $isValidated = true; } $workspace->setGuid($this->container->get('claroline.utilities.misc')->generateGuid()); $date = new \Datetime(date('d-m-Y H:i')); $workspace->setCreationDate($date->getTimestamp()); $this->om->persist($workspace); $this->om->flush(); $this->log("Base {$workspace->getCode()} workspace created..."); //load roles $this->log('Importing roles...'); $entityRoles = $this->getImporterByName('roles')->import($data['roles'], $workspace); //The manager role is required for every workspace $entityRoles['ROLE_WS_MANAGER'] = $this->container->get('claroline.manager.role_manager')->createWorkspaceRole("ROLE_WS_MANAGER_{$workspace->getGuid()}", 'manager', $workspace, true); //batch import with default template shouldn't be flushed if (strpos($template->getPathname(), 'default.zip') === false && strpos($template->getPathname(), 'personal.zip') === false) { $this->om->forceFlush(); } $owner = $workspace->getCreator(); $this->log('Roles imported...'); $owner->addRole($entityRoles['ROLE_WS_MANAGER']); $this->om->persist($owner); //add base roles to the role array $pfRoles = $this->om->getRepository('ClarolineCoreBundle:Role')->findAllPlatformRoles(); foreach ($pfRoles as $pfRole) { $entityRoles[$pfRole->getName()] = $pfRole; } $entityRoles['ROLE_ANONYMOUS'] = $this->om->getRepository('ClarolineCoreBundle:Role')->findOneByName('ROLE_ANONYMOUS'); $entityRoles['ROLE_USER'] = $this->om->getRepository('ClarolineCoreBundle:Role')->findOneByName('ROLE_USER'); $dir = new Directory(); $dir->setName($workspace->getName()); $dir->setIsUploadDestination(true); $root = $this->container->get('claroline.manager.resource_manager')->create($dir, $this->om->getRepository('Claroline\\CoreBundle\\Entity\\Resource\\ResourceType')->findOneByName('directory'), $owner, $workspace, null, null, []); $this->log('Populating the workspace...'); $this->populateWorkspace($workspace, $template, $root, $entityRoles, true, false); $this->container->get('claroline.manager.workspace_manager')->createWorkspace($workspace); $this->om->endFlushSuite(); return $workspace; }
/** * @param Configuration $configuration * @param User $owner * @param bool $isValidated * * @throws InvalidConfigurationException * @return SimpleWorkbolspace * * The template doesn't need to be validated anymore if * - it comes from the self::import() function * - we want to create a user from the default template (it should work no matter what) */ public function createWorkspace(Configuration $configuration, User $owner, $isValidated = false) { $configuration->setOwner($owner); $data = $configuration->getData(); $this->data = $data; $this->om->startFlushSuite(); $this->setImporters($configuration, $data); if (!$isValidated) { $this->validate($data, false); $isValidated = true; } $workspace = new Workspace(); $workspace->setName($configuration->getWorkspaceName()); $workspace->setCode($configuration->getWorkspaceCode()); $workspace->setDescription($configuration->getWorkspaceDescription()); $workspace->setGuid($this->container->get('claroline.utilities.misc')->generateGuid()); $workspace->setDisplayable($configuration->isDisplayable()); $workspace->setSelfRegistration($configuration->getSelfRegistration()); $workspace->setRegistrationValidation($configuration->getRegistrationValidation()); $workspace->setSelfUnregistration($configuration->getSelfUnregistration()); $date = new \Datetime(date('d-m-Y H:i')); $workspace->setCreationDate($date->getTimestamp()); $this->om->persist($workspace); $this->om->flush(); $this->log('Base workspace created...'); //load roles $this->log('Importing roles...'); $entityRoles = $this->getImporterByName('roles')->import($data['roles'], $workspace); //The manager role is required for every workspace $entityRoles['ROLE_WS_MANAGER'] = $this->container->get('claroline.manager.role_manager')->createWorkspaceRole("ROLE_WS_MANAGER_{$workspace->getGuid()}", 'manager', $workspace, true); $defaultZip = $this->container->getParameter('claroline.param.templates_directory') . 'default.zip'; //batch import with default template shouldn't be flushed if ($configuration->getArchive() !== $defaultZip) { $this->om->forceFlush(); } $this->log('Roles imported...'); $owner->addRole($entityRoles['ROLE_WS_MANAGER']); $this->om->persist($owner); //add base roles to the role array $pfRoles = $this->om->getRepository('ClarolineCoreBundle:Role')->findAllPlatformRoles(); foreach ($pfRoles as $pfRole) { $entityRoles[$pfRole->getName()] = $pfRole; } $entityRoles['ROLE_ANONYMOUS'] = $this->om->getRepository('ClarolineCoreBundle:Role')->findOneByName('ROLE_ANONYMOUS'); $entityRoles['ROLE_USER'] = $this->om->getRepository('ClarolineCoreBundle:Role')->findOneByName('ROLE_USER'); $dir = new Directory(); $dir->setName($workspace->getName()); $dir->setIsUploadDestination(true); $root = $this->container->get('claroline.manager.resource_manager')->create($dir, $this->om->getRepository('Claroline\\CoreBundle\\Entity\\Resource\\ResourceType')->findOneByName('directory'), $owner, $workspace, null, null, array()); $this->log('Populating the workspace...'); $this->populateWorkspace($workspace, $configuration, $root, $entityRoles, true, false); $this->container->get('claroline.manager.workspace_manager')->createWorkspace($workspace); if ($owner) { $this->log('Set the owner...'); $workspace->setCreator($owner); } $this->om->endFlushSuite(); $fs = new FileSystem(); return $workspace; }
public function createWorkspaceFromModel(WorkspaceModel $model, User $user, $name, $code, $description, $displayable, $selfRegistration, $selfUnregistration, &$errors = array()) { $workspaceModelManager = $this->container->get('claroline.manager.workspace_model_manager'); $workspace = new Workspace(); $workspace->setName($name); $workspace->setCode($code); $workspace->setDescription($description); $workspace->setDisplayable($displayable); $workspace->setSelfRegistration($selfRegistration); $workspace->setSelfUnregistration($selfUnregistration); $guid = $this->ut->generateGuid(); $workspace->setGuid($guid); $date = new \Datetime(date('d-m-Y H:i')); $workspace->setCreationDate($date->getTimestamp()); $workspace->setCreator($user); $errors = []; $this->createWorkspace($workspace); $workspaceModelManager->addDataFromModel($model, $workspace, $user, $errors); return $workspace; }