Skip to content

Commit

Permalink
Bind existing training course to a workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfyWin committed Sep 23, 2024
1 parent e5e884c commit 11fa9d5
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 6 deletions.
59 changes: 59 additions & 0 deletions src/plugin/cursus/Controller/CourseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,28 @@ public function listArchivedAction(Request $request): JsonResponse
));
}

/**
* @Route("/list/existing", name="list_existing", methods={"GET"})
*/
public function listExistingAction(Request $request): JsonResponse
{
$this->checkPermission('IS_AUTHENTICATED_FULLY', null, [], true);

$filters = array_merge($request->query->all(), [
'hiddenFilters' => $this->getDefaultHiddenFilters(),
]);

$list = $this->crud->list(Course::class, $filters, $this->getOptions()['list']);

$list['data'] = array_filter($list['data'], function (array $course) {
return is_null($course['workspace']);
});

sort($list['data']);

return new JsonResponse($list);
}

/**
* @Route("/archive", name="archive", methods={"POST"})
*/
Expand Down Expand Up @@ -244,6 +266,43 @@ public function copyAction(Request $request): JsonResponse
}, $processed));
}

/**
* @Route("/bind", name="bind_workspace", methods={"PATCH"})
*/
public function bindCourseToWorkspaceAction(Request $request): JsonResponse
{
$processed = [];

$this->om->startFlushSuite();

$data = $this->decodeRequest($request);

$workspaceData = $data['workspace'] ?? null;
$workspace = null;

if ($workspaceData) {
$workspace = $this->om->getRepository(Workspace::class)->findOneBy(['uuid' => $data['workspace']['id']]);
}

/** @var Course[] $courses */
$courses = $this->om->getRepository(Course::class)->findBy([
'uuid' => $data['ids'],
]);

foreach ($courses as $course) {
if ($this->authorization->isGranted('ADMINISTRATE', $course)) {
$course->setWorkspace($workspace);
$processed[] = $course;
}
}

$this->om->endFlushSuite();

return new JsonResponse(array_map(function (Course $course) {
return $this->serializer->serialize($course);
}, $processed));
}

/**
* @Route("/{slug}/open", name="open", methods={"GET"})
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ const EmptyCourse = (props) =>
</p>
<CreationType {...props} />
</ContentSizing>

</ToolPage>

EmptyCourse.propTypes = {
Expand Down
21 changes: 19 additions & 2 deletions src/plugin/cursus/Resources/modules/course/components/type.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,25 @@ const CreationType = (props) => {
description: trans('create_mode_existing_desc', {}, 'cursus'),
displayed: props.contextType === 'workspace',
action: {
type: CALLBACK_BUTTON,
callback: () => true
type: MODAL_BUTTON,
modal: [MODAL_TRAINING_COURSES, {
url: ['apiv2_cursus_course_list_existing'],
selectAction: (selected) => ({
type: ASYNC_BUTTON,
label: trans('bind', {}, 'actions'),
request: {
url: url(['apiv2_cursus_course_bind_workspace']),
request: {
method: 'PATCH',
body: JSON.stringify({
ids: selected.map(course => course.id),
workspace: props.contextId
})
},
success: () => history.push(props.path)
}
})
}]
},
group: trans('create_mode_group_existing', {}, 'cursus')
}
Expand Down
2 changes: 2 additions & 0 deletions src/plugin/cursus/Resources/translations/actions.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"archive_training_confirm_title": "{1} Archive a training | [2,Inf[ Archive multiple trainings",
"archive_training_confirm_message": "{1} Are you sure you want to archive this training? | [2,Inf[ Are you sure you want to archive these %count% trainings?",

"bind": "Bind",

"restore_training": "Restore training",
"restore_training_help": "Restore the training in the list of active trainings.",

Expand Down
2 changes: 2 additions & 0 deletions src/plugin/cursus/Resources/translations/actions.fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"archive_training_confirm_title": "{1} Archivage d'une formation | [2,Inf[ Archivage de plusieurs formations",
"archive_training_confirm_message": "{1} Êtes-vous sûr de vouloir archiver cette formation ? | [2,Inf[ Êtes-vous sûr de vouloir archiver ces %count% formations ?",

"bind": "Associer",

"restore_training": "Restaurer la formation",
"restore_training_help": "Permet de restaurer la formation dans la liste des formations actives.",

Expand Down
6 changes: 3 additions & 3 deletions src/plugin/cursus/Serializer/CourseSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ public function serialize(Course $course, array $options = []): array
'users' => $course->getMaxUsers(),
],
'tags' => $this->serializeTags($course),
'workspace' => $course->getWorkspace() ?
$this->workspaceSerializer->serialize($course->getWorkspace(), [SerializerInterface::SERIALIZE_MINIMAL]) :
null,
];

if (!in_array(SerializerInterface::SERIALIZE_TRANSFER, $options)) {
Expand Down Expand Up @@ -164,9 +167,6 @@ public function serialize(Course $course, array $options = []): array
return $this->panelFacetSerializer->serialize($panelFacet);
}, $course->getPanelFacets()->toArray()),
],
'workspace' => $course->getWorkspace() ?
$this->workspaceSerializer->serialize($course->getWorkspace(), [SerializerInterface::SERIALIZE_MINIMAL]) :
null,
'organizations' => array_map(function (Organization $organization) {
return $this->orgaSerializer->serialize($organization, [SerializerInterface::SERIALIZE_MINIMAL]);
}, $course->getOrganizations()->toArray()),
Expand Down

0 comments on commit 11fa9d5

Please sign in to comment.