/** * @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; }
/** * Import a workspace list from a csv data. * * @param array $workspaces */ public function importWorkspaces(array $workspaces, $logger = null, $update = false) { $i = 0; $workspaceModelManager = $this->container->get('claroline.manager.workspace_model_manager'); foreach ($workspaces as $workspace) { ++$i; $this->om->startFlushSuite(); $endDate = null; $model = null; $name = $workspace[0]; $code = $workspace[1]; $isVisible = $workspace[2]; $selfRegistration = $workspace[3]; $registrationValidation = $workspace[4]; $selfUnregistration = $workspace[5]; if (isset($workspace[6]) && trim($workspace[6]) !== '') { $user = $this->om->getRepository('ClarolineCoreBundle:User')->findOneByUsername($workspace[6]); } else { $user = $this->container->get('security.context')->getToken()->getUser(); } if (isset($workspace[7])) { $model = $this->om->getRepository('ClarolineCoreBundle:Model\\WorkspaceModel')->findOneByName($workspace[7]); } if (isset($workspace[8])) { $endDate = new \DateTime(); $endDate->setTimestamp($workspace[8]); } if ($update) { $workspace = $this->getOneByCode($code); if (!$workspace) { //if the workspace doesn't exists, just keep going... continue; } if ($logger) { $logger('Updating ' . $code . ' (' . $i . '/' . count($workspaces) . ') ...'); } } else { $workspace = new Workspace(); } $workspace->setName($name); $workspace->setCode($code); $workspace->setDisplayable($isVisible); $workspace->setSelfRegistration($selfRegistration); $workspace->setSelfUnregistration($selfUnregistration); $workspace->setRegistrationValidation($registrationValidation); $workspace->setCreator($user); if ($endDate) { $workspace->setEndDate($endDate); } if (!$update) { if ($logger) { $logger('Creating ' . $code . ' (' . $i . '/' . count($workspaces) . ') ...'); } if ($model) { $guid = $this->ut->generateGuid(); $this->createWorkspace($workspace); $workspace->setGuid($guid); $date = new \Datetime(date('d-m-Y H:i')); $workspace->setCreationDate($date->getTimestamp()); $workspaceModelManager->addDataFromModel($model, $workspace, $user); } else { $template = new File($this->container->getParameter('claroline.param.default_template')); $this->container->get('claroline.manager.transfer_manager')->createWorkspace($workspace, $template, true); } } else { if ($model) { $workspaceModelManager->updateDataFromModel($model, $workspace); } } $this->om->persist($workspace); $logger('UOW: ' . $this->om->getUnitOfWork()->size()); if ($i % 100 === 0) { $this->om->forceFlush(); $user = $this->om->getRepository('ClarolineCoreBundle:User')->find($user->getId()); $this->om->merge($user); $this->om->refresh($user); } } $logger('Final flush...'); $this->om->endFlushSuite(); }
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; }
public function generateWorkspace(Course $course, CourseSession $session) { $user = $this->tokenStorage->getToken()->getUser(); $model = $course->getWorkspaceModel(); $description = $course->getDescription(); $displayable = false; $selfRegistration = false; $selfUnregistration = false; $registrationValidation = false; $name = $course->getTitle() . ' [' . $session->getName() . ']'; $code = $this->generateWorkspaceCode($course->getCode()); if (is_null($model)) { $template = new File($this->defaultTemplate); $workspace = new Workspace(); $workspace->setCreator($user); $workspace->setName($name); $workspace->setCode($code); $workspace->setDisplayable($displayable); $workspace->setSelfRegistration($selfRegistration); $workspace->setSelfUnregistration($selfUnregistration); $workspace->setRegistrationValidation($registrationValidation); $workspace->setDescription($description); $workspace = $this->workspaceManager->create($workspace, $template); } else { $workspace = $this->workspaceManager->createWorkspaceFromModel($model, $user, $name, $code, $description, $displayable, $selfRegistration, $selfUnregistration); } $workspace->setWorkspaceType(0); $startDate = $session->getStartDate(); $endDate = $session->getEndDate(); if (!is_null($startDate)) { $workspace->setStartDate($startDate); } if (!is_null($endDate)) { $workspace->setEndDate($endDate); } $this->workspaceManager->editWorkspace($workspace); return $workspace; }