View source
<?php
namespace Drupal\opigno_learning_path\Controller;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\SettingsCommand;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Form\FormState;
use Drupal\group\Entity\Group;
use Drupal\opigno_learning_path\Entity\LPManagedContent;
use Drupal\opigno_learning_path\Entity\LPManagedLink;
use Drupal\opigno_learning_path\LearningPathContentTypesManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\file\Entity\File;
class LearningPathManagerController extends ControllerBase {
private $content_types_manager;
public function __construct(LearningPathContentTypesManager $content_types_manager) {
$this->content_types_manager = $content_types_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('opigno_learning_path.content_types.manager'));
}
public function index(Group $group, Request $request) {
$tempstore = \Drupal::service('tempstore.private')
->get('opigno_group_manager');
return [
'#theme' => 'opigno_learning_path_manager',
'#attached' => [
'library' => [
'opigno_group_manager/manage_app',
],
],
'#base_path' => $request
->getBasePath(),
'#base_href' => $request
->getPathInfo(),
'#learning_path_id' => $group
->id(),
'#user_has_info_card' => $tempstore
->get('hide_info_card') ? FALSE : TRUE,
];
}
public function getItemForm(Group $group, $type = NULL, $item = 0) {
$content_type = $this->content_types_manager
->createInstance($type);
$form = $content_type
->getFormObject($item);
$form_build = \Drupal::formBuilder()
->getForm($form, [
'learning_path_info' => [
'learning_path_id' => $group
->id(),
'lp_content_type' => $type,
],
]);
return $form_build;
}
public static function ajaxFormEntityCallback(&$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
if ($form_state
->hasAnyErrors()) {
$form['#attributes']['class'][] = 'lp-content-item-errors';
return $form;
}
$entity = $form_state
->getBuildInfo()['callback_object']
->getEntity();
$media = $entity
->get('field_course_media_image')->entity;
$file = isset($media) ? File::load($media
->get('field_media_image')->getValue[0]['target_id']) : NULL;
$item = [];
$item['cid'] = $entity
->id();
$item['contentType'] = \Drupal::routeMatch()
->getParameter('type');
$item['entityId'] = $entity
->id();
$item['entityBundle'] = \Drupal::routeMatch()
->getParameter('type');
$item['title'] = $entity
->get('label')
->getString();
$item['imageUrl'] = $file ? file_create_url($file
->getFileUri()) : '';
$response
->addCommand(new SettingsCommand([
'formValues' => $item,
'messages' => \Drupal::messenger()
->all(),
], TRUE));
\Drupal::messenger()
->deleteAll();
return $response;
}
public function ajaxFormEntityFormSubmit($form, FormState &$form_state) {
$build_info = $form_state
->getBuildInfo();
foreach ($build_info['args'] as $arg_key => $arg_value) {
if ($arg_key === 'learning_path_info') {
$lp_id = $arg_value['learning_path_id'];
$lp_content_type_id = $arg_value['lp_content_type'];
break;
}
}
if (!isset($lp_id) || !isset($lp_content_type_id)) {
return;
}
$entity = $build_info['callback_object']
->getEntity();
$input = $form_state
->getUserInput();
$clean_keys = $form_state
->getCleanValueKeys();
$clean_keys[] = 'ajax_page_state';
foreach ($input as $key => $item) {
if (!in_array($key, $clean_keys) && substr($key, 0, 1) !== '_') {
unset($input[$key]);
}
}
$input['entity'] = $entity;
$form_state
->setUserInput($input);
$form_state
->setRebuild();
$form_state
->setStorage([]);
}
public function updateItemMandatory(Group $group, Request $request) {
$datas = json_decode($request
->getContent());
if (empty($datas->cid) || isset($datas->isMandatory) === FALSE) {
return new JsonResponse(NULL, Response::HTTP_BAD_REQUEST);
}
$cid = $datas->cid;
$mandatory = $datas->isMandatory;
$content = LPManagedContent::load($cid);
$content
->setMandatory($mandatory);
$content
->save();
return new JsonResponse(NULL, Response::HTTP_OK);
}
public function updateItemMinScore(Group $group, Request $request) {
$datas = json_decode($request
->getContent());
if (empty($datas->cid)) {
return new JsonResponse(NULL, Response::HTTP_BAD_REQUEST);
}
$cid = $datas->cid;
$success_score_min = empty($datas->successScoreMin) ? 0 : $datas->successScoreMin;
$content = LPManagedContent::load($cid);
$content
->setSuccessScoreMin($success_score_min);
$content
->save();
return new JsonResponse(NULL, Response::HTTP_OK);
}
public function getItems(Group $group) {
$entities = [];
$managed_contents = LPManagedContent::loadByProperties([
'learning_path_id' => $group
->id(),
]);
foreach ($managed_contents as $managed_content) {
$content_type_id = $managed_content
->getLearningPathContentTypeId();
$content_type = $this->content_types_manager
->createInstance($content_type_id);
$lp_content = $content_type
->getContent($managed_content
->getEntityId());
$manager_array = $lp_content
->toManagerArray($managed_content);
$entities[] = $manager_array;
}
return new JsonResponse($entities, Response::HTTP_OK);
}
public function getPositions(Group $group) {
$entityPositions = [];
try {
$managed_contents = LPManagedContent::loadByProperties([
'learning_path_id' => $group
->id(),
]);
} catch (InvalidPluginDefinitionException $e) {
return new JsonResponse(NULL, Response::HTTP_INTERNAL_SERVER_ERROR);
}
foreach ($managed_contents as $managed_content) {
$entityPositions[] = [
'cid' => $managed_content
->id(),
'col' => $managed_content
->getCoordinateX(),
'row' => $managed_content
->getCoordinateY(),
];
}
return new JsonResponse($entityPositions, Response::HTTP_OK);
}
public function setPositions(Request $request) {
$datas = json_decode($request
->getContent());
if (empty($datas->mainItemPositions)) {
return new JsonResponse(NULL, Response::HTTP_BAD_REQUEST);
}
$content_positions = $datas->mainItemPositions;
foreach ($content_positions as $content_position) {
$content = LPManagedContent::load($content_position->cid);
$content
->setCoordinateX($content_position->col);
$content
->setCoordinateY($content_position->row);
$content
->save();
}
return new JsonResponse(NULL, Response::HTTP_OK);
}
public function addItem(Group $group, Request $request) {
$datas = json_decode($request
->getContent());
if (empty($datas->entityId) || empty($datas->contentType)) {
return new JsonResponse(NULL, Response::HTTP_BAD_REQUEST);
}
$entityId = $datas->entityId;
$contentType = $datas->contentType;
$parentCid = empty($datas->parentCid) ? NULL : $datas->parentCid;
$new_content = LPManagedContent::createWithValues($group
->id(), $contentType, $entityId);
$new_content
->save();
if (!empty($parentCid)) {
LPManagedLink::createWithValues($group
->id(), $parentCid, $new_content
->id())
->save();
}
if ($datas->contentType == 'ContentTypeCourse') {
$added_entity = \Drupal::entityTypeManager()
->getStorage('group')
->load($datas->entityId);
$group
->addContent($added_entity, 'subgroup:' . $added_entity
->bundle());
}
return new JsonResponse([
'cid' => $new_content
->id(),
], Response::HTTP_OK);
}
public function removeItem(Request $request) {
$datas = json_decode($request
->getContent());
if (empty($datas->cid)) {
return new JsonResponse(NULL, Response::HTTP_BAD_REQUEST);
}
$cid = $datas->cid;
$lp_content_entity = LPManagedContent::load($cid);
$learning_path_plugin = $lp_content_entity
->getLearningPathContentType();
if ($learning_path_plugin
->getPluginId() == 'ContentTypeCourse') {
$lp_group = \Drupal::entityTypeManager()
->getStorage('group')
->load($lp_content_entity
->get('learning_path_id')->entity
->id());
$group_contents = $lp_group
->getContentByEntityId('subgroup:opigno_course', $lp_content_entity
->get('entity_id')->value);
if (!empty($group_contents)) {
foreach ($group_contents as $group_content) {
$group_content
->delete();
}
}
}
$lp_content_entity
->delete();
return new JsonResponse(NULL, Response::HTTP_OK);
}
public function addLink(Group $group, Request $request) {
$datas = json_decode($request
->getContent());
if (empty($datas->parentCid) || empty($datas->childCid)) {
return new JsonResponse(NULL, Response::HTTP_BAD_REQUEST);
}
$parentCid = $datas->parentCid;
$childCid = $datas->childCid;
$new_link = LPManagedLink::createWithValues($group
->id(), $parentCid, $childCid, 0);
$new_link
->save();
return new JsonResponse(NULL, Response::HTTP_OK);
}
public function updateLink(Group $group, Request $request) {
$datas = json_decode($request
->getContent());
if (empty($datas->parentCid) || empty($datas->childCid) || isset($datas->requiredScore) === FALSE) {
return new JsonResponse(NULL, Response::HTTP_BAD_REQUEST);
}
$parentCid = $datas->parentCid;
$childCid = $datas->childCid;
$requiredScore = $datas->requiredScore;
try {
$links = LPManagedLink::loadByProperties([
'learning_path_id' => $group
->id(),
'parent_content_id' => $parentCid,
'child_content_id' => $childCid,
]);
} catch (InvalidPluginDefinitionException $e) {
return new JsonResponse(NULL, Response::HTTP_INTERNAL_SERVER_ERROR);
}
if (empty($links)) {
$new_link = LPManagedLink::createWithValues($group
->id(), $parentCid, $childCid, $requiredScore);
$new_link
->save();
return new JsonResponse(NULL, Response::HTTP_OK);
}
foreach ($links as $link) {
$link
->setRequiredScore($requiredScore);
$link
->save();
}
return new JsonResponse(NULL, Response::HTTP_OK);
}
public function removeLink(Group $group, Request $request) {
$datas = json_decode($request
->getContent());
if (empty($datas->parentCid) || empty($datas->childCid)) {
return new JsonResponse(NULL, Response::HTTP_BAD_REQUEST);
}
$parentCid = $datas->parentCid;
$childCid = $datas->childCid;
try {
$links = LPManagedLink::loadByProperties([
'learning_path_id' => $group
->id(),
'parent_content_id' => $parentCid,
'child_content_id' => $childCid,
]);
} catch (InvalidPluginDefinitionException $e) {
return new JsonResponse(NULL, Response::HTTP_INTERNAL_SERVER_ERROR);
}
foreach ($links as $link) {
$link
->delete();
}
return new JsonResponse(NULL, Response::HTTP_OK);
}
public function getAvailableItems($mainItem = NULL) {
$available_contents = [];
$content_types_definitions = $this->content_types_manager
->getDefinitions();
foreach ($content_types_definitions as $content_type_id => $content_type_definition) {
$content_type = $this->content_types_manager
->createInstance($content_type_id);
$content_type_contents = $content_type
->getAvailableContents();
foreach ($content_type_contents as $content_type_content) {
$available_contents[] = $content_type_content
->toManagerArray();
}
}
return new JsonResponse($available_contents, Response::HTTP_OK);
}
public function getItemTypes($mainItem = NULL, $json_output = TRUE) {
$available_types = [];
$content_types_definitions = $this->content_types_manager
->getDefinitions();
foreach ($content_types_definitions as $content_type_definition) {
$available_types[] = [
'bundle' => $content_type_definition['id'],
'contentType' => $content_type_definition['id'],
'name' => $content_type_definition['readable_name'],
];
}
if ($json_output) {
return new JsonResponse($available_types, Response::HTTP_OK);
}
else {
return $available_types;
}
}
public function createItem(Request $request, $type = NULL) {
}
public function updateItem(Request $request) {
}
public function publish(Group $group) {
if ($group->field_learning_path_published->value == 1) {
}
else {
$group
->set('field_learning_path_published', 1);
$group
->save();
}
return $this
->redirect('entity.group.canonical', [
'group' => $group
->id(),
]);
}
public function unpublish(Group $group) {
if ($group->field_learning_path_published->value == 0) {
}
else {
$group
->set('field_learning_path_published', 0);
$group
->save();
}
return $this
->redirect('entity.group.canonical', [
'group' => $group
->id(),
]);
}
}